diff --git a/cmd/simu/main.go b/cmd/simu/main.go index f963e03be40af2a537924923ad7f76bad953b94a..1d8c7e3d43dd24cf295430f6786c3c93fdade5a2 100644 --- a/cmd/simu/main.go +++ b/cmd/simu/main.go @@ -6,7 +6,7 @@ import ( ) func main() { - s := simulation.NewSimulation(10, -1, 600*time.Second) + s := simulation.NewSimulation(4, -1, 600*time.Second) //go simulation.StartAPI(s) s.Run() } diff --git a/internal/simulation/agent.go b/internal/simulation/agent.go index 259787324257e9ade9aa62fd7bf6b9fb08aaf650..44da201f27bbf4176e973569615f79a9a9f03d5d 100644 --- a/internal/simulation/agent.go +++ b/internal/simulation/agent.go @@ -9,6 +9,7 @@ package simulation */ import ( + "fmt" "log" "math/rand" alg "metrosim/internal/algorithms" @@ -257,15 +258,18 @@ func (ag *Agent) MoveAgent() bool { for !accept && i < 3 { //Demande à l'agent qui bloque de se pousser (réitère trois fois s'il lui dit pas possible) i += 1 + fmt.Printf("You have to move %s for the %d time \n", blockingAgentID, i) reqToBlockingAgent = NewRequest(ag.env.agentsChan[ag.id], YouHaveToMove) //Création "Hello, je suis ag.id, move." ag.env.agentsChan[blockingAgentID] <- *reqToBlockingAgent //Envoi requête repFromBlockingAgent = <-ag.env.agentsChan[blockingAgentID] //Attend la réponse if repFromBlockingAgent.decision == 5 { //BlockingAgent lui a répondu Done, il s'est donc poussé + fmt.Printf("okay i will move agent %s \n", ag.id) accept = true } } if !accept { + fmt.Printf("i can't move agent %s \n", ag.id) return false //il ne peut pas bouger, il s'arrête } } diff --git a/internal/simulation/simu.go b/internal/simulation/simu.go index 6eb6f1daf5c1279d78367dc2512897ab67289c7e..7891576838f7383548c3ecd2ebcd9c1d0233c038 100644 --- a/internal/simulation/simu.go +++ b/internal/simulation/simu.go @@ -99,15 +99,12 @@ func NewSimulation(agentCount int, maxStep int, maxDuration time.Duration) (simu // création des agents et des channels for i := 0; i < agentCount; i++ { - // création de l'agent syncChan := make(chan int) - //ag := NewAgent(id, &simu.env, syncChan, time.Duration(time.Second), 0, true, Coord{0, 8 + i%2}, Coord{0, 8 + i%2}, &UsagerLambda{}, Coord{0, 8 + i%2}, Coord{12 - 4*(i%2), 18 - 15*(i%2)}) - //ag := NewAgent(id, &simu.env, syncChan, 1000, 0, true, &UsagerLambda{}, Coord{18, 4}, Coord{0, 8}, 2, 1) - + id := fmt.Sprintf("Agent%d", i) ag := &Agent{} - if i%2 == 0 { //Type Agent + /*if i%2 == 0 { //Type Agent id := fmt.Sprintf("Agent%d", i) //NewAgent(id string, env *Environment, syncChan chan int, vitesse time.Duration, force int, politesse bool, behavior Behavior, departure, destination Coord, width, height int) ag = NewAgent(id, &simu.env, syncChan, 200, 0, false, &UsagerLambda{}, Coord{18, 4}, Coord{13, 4}, 1, 1) @@ -116,9 +113,13 @@ func NewSimulation(agentCount int, maxStep int, maxDuration time.Duration) (simu id := fmt.Sprintf("Agent%d", i) ag = NewAgent(id, &simu.env, syncChan, 200, 0, true, &UsagerLambda{}, Coord{1, 8}, Coord{8, 5}, 1, 1) //ag = NewAgent(id, &simu.env, syncChan, 1000, 0, true, &Controleur{}, Coord{18, 12}, Coord{18, 4}, 1, 1) - } + }*/ - //ag := NewAgent(id, &simu.env, syncChan, 1000, 0, true, &UsagerLambda{}, Coord{19, 12}, Coord{0, 8}, 2, 1) + if i != 2 { + ag = NewAgent(id, &simu.env, syncChan, 200, 0, false, &UsagerLambda{}, Coord{18, 4}, Coord{13, 4}, 1, 1) + } else { + ag = NewAgent(id, &simu.env, syncChan, 200, 0, true, &UsagerLambda{}, Coord{18, 4}, Coord{13, 4}, 1, 1) + } // ajout de l'agent à la simulation simu.agents = append(simu.agents, *ag) diff --git a/internal/simulation/usagerLambda.go b/internal/simulation/usagerLambda.go index 5ff3c48c1ce8556e994b968153546188824c730e..23bb6ed9ede1443609a0b95274032b7aa1c70087 100644 --- a/internal/simulation/usagerLambda.go +++ b/internal/simulation/usagerLambda.go @@ -15,7 +15,7 @@ type UsagerLambda struct { func (ul *UsagerLambda) Percept(ag *Agent) { switch { case ag.request != nil: //verifier si l'agent est communiqué par un autre agent, par exemple un controleur lui a demandé de s'arreter - print("Requete recue par l'agent lambda : ", ag.request.decision, "\n") + print("Requete recue par %d : %d", ag.id, ag.request.decision, "\n") ul.req = *ag.request default: ag.stuck = ag.isStuck() @@ -38,11 +38,13 @@ func (ul *UsagerLambda) Deliberate(ag *Agent) { } else if ul.req.decision == Wait { ag.decision = Wait } else if ul.req.decision == YouHaveToMove { + fmt.Println("J'essaye de bouger") movement := ag.MoveAgent() + fmt.Printf("Je suis agent %s Resultat du mouvement de la personne %t \n", ag.id, movement) if movement { - ag.decision = 5 + ag.decision = Done } else { - ag.decision = 0 + ag.decision = Noop } } else { ag.decision = Move