diff --git a/src/main/scala/org/scadcop/example/RobeColouringExample.scala b/src/main/scala/org/scadcop/example/RobeColouringExample.scala index 67c407580509f27279f7c49e9a6ada1a356eb553..56f51e812a652edfec32dc7a9f1bebeb34181d6b 100644 --- a/src/main/scala/org/scadcop/example/RobeColouringExample.scala +++ b/src/main/scala/org/scadcop/example/RobeColouringExample.scala @@ -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)) } diff --git a/src/main/scala/org/scadcop/solver/decentralized/DecentralizedSolver.scala b/src/main/scala/org/scadcop/solver/decentralized/DecentralizedSolver.scala index 771c9c61659b6cfb7c49a619eab7b9bfa7819099..c4240c11aa9e2fca7f6509d400cc320aff8f6eb1 100644 --- a/src/main/scala/org/scadcop/solver/decentralized/DecentralizedSolver.scala +++ b/src/main/scala/org/scadcop/solver/decentralized/DecentralizedSolver.scala @@ -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) /** diff --git a/src/main/scala/org/scadcop/solver/decentralized/mgm2/AgentBehaviour.scala b/src/main/scala/org/scadcop/solver/decentralized/mgm2/AgentBehaviour.scala index 763281d7f0032cb5be0d2af4eb230912186622b2..53ba91cf2c69369b2f914dca7375fcb9c4e680c5 100644 --- a/src/main/scala/org/scadcop/solver/decentralized/mgm2/AgentBehaviour.scala +++ b/src/main/scala/org/scadcop/solver/decentralized/mgm2/AgentBehaviour.scala @@ -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 diff --git a/src/main/scala/org/scadcop/solver/decentralized/mgm2/MGM2Mind.scala b/src/main/scala/org/scadcop/solver/decentralized/mgm2/MGM2Mind.scala index b7dafea5f90546fa5747ac7472931d21313af3cd..28c62e722c4a8f1260809f789480cf2fb30de727 100644 --- a/src/main/scala/org/scadcop/solver/decentralized/mgm2/MGM2Mind.scala +++ b/src/main/scala/org/scadcop/solver/decentralized/mgm2/MGM2Mind.scala @@ -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 diff --git a/src/main/scala/org/scadcop/solver/decentralized/supervisor/Supervisor.scala b/src/main/scala/org/scadcop/solver/decentralized/supervisor/Supervisor.scala index 51e59e79031d96885d800f962761211a682100de..2c221d11bc4c24613c0ca612b60f215915bb95db 100644 --- a/src/main/scala/org/scadcop/solver/decentralized/supervisor/Supervisor.scala +++ b/src/main/scala/org/scadcop/solver/decentralized/supervisor/Supervisor.scala @@ -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 } + + } diff --git a/src/main/scala/org/scadcop/util/Main.scala b/src/main/scala/org/scadcop/util/Main.scala index 580926a713708be2fd3584bdebc345438fc4251d..4da83ad691838f3a1da2919b1affbf1c45cf412f 100644 --- a/src/main/scala/org/scadcop/util/Main.scala +++ b/src/main/scala/org/scadcop/util/Main.scala @@ -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() diff --git a/src/test/scala/org/scadcop/problem/ObjectiveRobeColouringExampleSpec.scala b/src/test/scala/org/scadcop/problem/ObjectiveRobeColouringExampleSpec.scala index 95d684a95056fd9209e7b03f06999673d1d4f559..bc28934da82daba2ee8410a135651e0d1adaa3e7 100644 --- a/src/test/scala/org/scadcop/problem/ObjectiveRobeColouringExampleSpec.scala +++ b/src/test/scala/org/scadcop/problem/ObjectiveRobeColouringExampleSpec.scala @@ -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 {