From f3fa8e1dbe33957afd5aa1f9a92917171648a80b Mon Sep 17 00:00:00 2001 From: Jean-Christophe <jean-christophe.routier@univ-lille.fr> Date: Thu, 2 Jul 2020 17:51:58 +0200 Subject: [PATCH] add initial context --- .../org/scadcop/example/RobeColouringExample.scala | 2 +- .../solver/decentralized/DecentralizedSolver.scala | 4 ++-- .../solver/decentralized/mgm2/AgentBehaviour.scala | 5 +++-- .../scadcop/solver/decentralized/mgm2/MGM2Mind.scala | 2 +- .../solver/decentralized/supervisor/Supervisor.scala | 8 +++++--- src/main/scala/org/scadcop/util/Main.scala | 10 +++++----- .../problem/ObjectiveRobeColouringExampleSpec.scala | 4 ++-- 7 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main/scala/org/scadcop/example/RobeColouringExample.scala b/src/main/scala/org/scadcop/example/RobeColouringExample.scala index 67c4075..56f51e8 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 771c9c6..c4240c1 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 763281d..53ba91c 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 b7dafea..28c62e7 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 51e59e7..2c221d1 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 580926a..4da83ad 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 95d684a..bc28934 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 { -- GitLab