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