Skip to content
Snippets Groups Projects
Commit f3fa8e1d authored by Jean-Christophe Routier's avatar Jean-Christophe Routier
Browse files

add initial context

parent d87b24d5
Branches
No related tags found
No related merge requests found
......@@ -26,6 +26,6 @@ object RobeColouringExample {
val constraintFrodoGandalf = new Constraint(frodo, aragorn, costFrodoGandalf)
val pb = new DCOP(Set(gandalf, aragorn, frodo), Set(constraintGandalfAragorn, constraintGandalfFrodo, constraintFrodoGandalf))
val assignment : Context= new Context()
val initialContext : Context= new Context()
.fix(Map(gandalf -> black, aragorn -> black, frodo -> black))
}
......@@ -20,7 +20,7 @@ import scala.language.postfixOps
* @param algorithm which determines the agent behaviour
* @param system of Actors
*/
class DecentralizedSolver(pb: DCOP, val algorithm: Algorithm, val system: ActorSystem) extends Solver(pb) {
class DecentralizedSolver(pb: DCOP, val algorithm: Algorithm, val initialContext: Context, val system: ActorSystem) extends Solver(pb) {
var metric : Metric = collection.mutable.Map[String,Double]()
......@@ -32,7 +32,7 @@ class DecentralizedSolver(pb: DCOP, val algorithm: Algorithm, val system: ActorS
// Launch a new supervisor
DecentralizedSolver.id += 1
val supervisor : ActorRef =
system.actorOf(Props(classOf[Supervisor], pb, algorithm),
system.actorOf(Props(classOf[Supervisor], pb, algorithm, initialContext),
name = "supervisor"+DecentralizedSolver.id)
/**
......
......@@ -19,7 +19,8 @@ import akka.actor.{FSM, Stash}
*/
class AgentBehaviour(variable: Variable,
neighbours : Set[Variable],
constraints: Set[Constraint])
constraints: Set[Constraint],
initialContext : Context)
extends VariableAgent(variable, neighbours, constraints)
with FSM[MGM2State, MGM2Mind]
with Stash {
......@@ -27,7 +28,7 @@ class AgentBehaviour(variable: Variable,
/**
* Initially the agent has no task in the bundle
*/
startWith(Init, new MGM2Mind())
startWith(Init, new MGM2Mind(initialContext))
/**
* Reject some offers
......
......@@ -78,7 +78,7 @@ class MGM2Mind(val context : Context = new Context(),
variable.domain.foreach { value =>
val potentialContext: Context = context.fix(variable, value)
val potentialCost = potentialContext.cost(constraints)
val delta = currentCost - potentialCost
val delta = potentialCost - currentCost
if (delta ~> bestDelta) {
val move = new UnilateralMove(potentialContext, delta)
bestDelta = delta
......
......@@ -12,7 +12,7 @@ import akka.actor.{Actor, ActorRef, FSM, Props, Stash, PoisonPill}
* @param algorithm which determines the agent behaviour
*
* */
class Supervisor(val pb : DCOP, val algorithm: Algorithm) extends Actor with Stash
class Supervisor(val pb : DCOP, val algorithm: Algorithm, val initialContext: Context) extends Actor with Stash
with FSM[SupervisorState, SupervisorStatus] {
var debug = false
......@@ -39,7 +39,7 @@ class Supervisor(val pb : DCOP, val algorithm: Algorithm) extends Actor with Sta
if (debug) println(s"Supervisor> variable=$variable constraints=$constraintsOfVariable")
val actor = algorithm match {
case MGM2 =>
context.actorOf(Props(classOf[AgentBehaviour], variable, neighbours, constraintsOfVariable), variable.name.toString)
context.actorOf(Props(classOf[AgentBehaviour], variable, neighbours, constraintsOfVariable, initialContext), variable.name.toString)
case other => throw new RuntimeException(s"Supervisor ($stateName): error the algorithm $other is not yet implemented")
}
directory.add(variable, actor)// add it to the directory
......@@ -249,10 +249,12 @@ class Supervisor(val pb : DCOP, val algorithm: Algorithm) extends Actor with Sta
object Supervisor {
var counter = 0
val NB_ROUNDS = 5
val NB_ROUNDS = 10
def incrementCounter() : Int = {
counter = counter + 1
counter
}
}
......@@ -18,14 +18,14 @@ object Main extends App {
if (! pb.isSound) throw new RuntimeException("Pb is not sound")
println(pb)
println(s"First assignment: $assignment")
if (! pb.isSound(assignment))
println(s"First assignment: $initialContext")
if (! pb.isSound(initialContext))
throw new RuntimeException("this assignment is not sound")
if (! pb.isFull(assignment))
if (! pb.isFull(initialContext))
throw new RuntimeException("this assignment is not full")
println("with objective: " + pb.objective(assignment))
println("with objective: " + pb.objective(initialContext))
val solver = new DecentralizedSolver(pb, MGM2, system)
val solver = new DecentralizedSolver(pb, MGM2, initialContext, system)
solver.trace = true
solver.debug = false
val outcome = solver.run()
......
......@@ -16,8 +16,8 @@ class ObjectiveRobeColouringExampleSpec extends AnyFlatSpec {
"The objective when all the robes are black" should " be 8.0" in {
println(pb)
println(assignment)
assert(pb.objective(assignment) ~= 8.00)
println(initialContext)
assert(pb.objective(initialContext) ~= 8.00)
}
"The objective when all the robes are white" should " be 12.0" in {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment