diff --git a/cmd/simu/main.go b/cmd/simu/main.go index c29a1f66f5a6b3a3bf7c3dee0a49389e68be6697..c298cf2bfb099acbc2cec11fb165e1ad79a66d61 100644 --- a/cmd/simu/main.go +++ b/cmd/simu/main.go @@ -6,7 +6,7 @@ import ( ) func main() { - s := simulation.NewSimulation(1, -1, 600*time.Second) + s := simulation.NewSimulation(2, -1, 600*time.Second) //go simulation.StartAPI(s) s.Run() } diff --git a/internal/simulation/agent.go b/internal/simulation/agent.go index f372c1e358a06cfc4b6bedc5f07d6c325ee370bc..9e3817bc2185b56ff5b944e9c364b5373cfd2592 100644 --- a/internal/simulation/agent.go +++ b/internal/simulation/agent.go @@ -83,7 +83,7 @@ func NewAgent(id string, env *Environment, syncChan chan int, vitesse time.Durat // visitedPanneaux[panneau] = false // } // visiting := alg.NewNode(destination[0], destination[1], 0, HeuristicWithObstacles(departure, destination, env), 0, 0) - return &Agent{AgentID(id), vitesse, force, politesse, departure, departure, destination, behavior, env, syncChan, Noop, isOn, false, width, height, 0, make([]alg.Node, 0), nil} + return &Agent{AgentID(id), vitesse, force, politesse, departure, departure, destination, behavior, env, syncChan, Noop, isOn, false, width, height, 3, make([]alg.Node, 0), nil} } func (ag *Agent) ID() AgentID { @@ -291,7 +291,7 @@ func writeAgent(matrix *[20][20]string, agt *Agent) { for i := borneInfRow; i < borneSupRow; i++ { for j := borneInfCol; j < borneSupCol; j++ { - matrix[i][j] = "A"//string(agt.id)[0:1] + matrix[i][j] = string(agt.id) } } diff --git a/internal/simulation/controleur.go b/internal/simulation/controleur.go index 57217dd901ad263be2c3800d326af345c2f723ba..65ce3b266861ba46d866528c14e1f73434ef4021 100644 --- a/internal/simulation/controleur.go +++ b/internal/simulation/controleur.go @@ -44,13 +44,15 @@ func (c *Controleur) Deliberate(ag *Agent) { // Vérifier si la valeur de faceCase ne correspond pas au motif matchedAgt, err1 := regexp.MatchString(regexAgent, c.faceCase) matchedFraud, err2 := regexp.MatchString(regexFraudeur, c.faceCase) - + fmt.Println("faceCase : ", c.faceCase) + fmt.Println("matchedAgt : ", matchedAgt) if err1 != nil || err2 != nil { fmt.Println("Erreur lors de l'analyse de la regex :", err1, err2) return } else { if matchedAgt { - ag.decision = Wait // arreter l'agent devant lui + fmt.Println("L'agent ", c.faceCase, " a été arrêté") + ag.decision = Expel // arreter l'agent devant lui } else if matchedFraud { ag.decision = Expel // virer l'agent devant lui } else{ @@ -72,6 +74,7 @@ func (c *Controleur) Act(ag *Agent) { time.Sleep(time.Duration(n) * time.Second) } else { agt_face_id := AgentID(c.faceCase) //id de l'agent qui se trouve devant le controleur + fmt.Print("L'agent ", agt_face_id, " a été expulsé\n") ag.env.agentsChan[agt_face_id] <- *NewRequest(ag.id, ag.decision) // envoie la decision du controleur à l'agent qui se trouve devant lui } } diff --git a/internal/simulation/simu.go b/internal/simulation/simu.go index 904e72622e3ece1b204d212129cb05107e43580d..b166f8bad934d4f5b96336a4852cf3703ede7f25 100644 --- a/internal/simulation/simu.go +++ b/internal/simulation/simu.go @@ -93,24 +93,27 @@ 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 - id := fmt.Sprintf("Agent #%d", i) + 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) - /* + ag := &Agent{} - if i%2==0{ + 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, 1000, 0, true, &UsagerLambda{}, Coord{18, 4}, Coord{0, 8}, 2, 1) - }else{ - ag = NewAgent(id, &simu.env, syncChan, 1000, 0, true, &UsagerLambda{}, Coord{1, 8}, Coord{8, 5}, 1, 1) + ag = NewAgent(id, &simu.env, syncChan, 1000, 0, true, &UsagerLambda{}, Coord{18, 4}, Coord{0, 8}, 1, 1) + }else{ // Type Controleur + id := fmt.Sprintf("Controleur%d", i) + //ag = NewAgent(id, &simu.env, syncChan, 1000, 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) + + //ag := NewAgent(id, &simu.env, syncChan, 1000, 0, true, &UsagerLambda{}, Coord{19, 12}, Coord{0, 8}, 2, 1) // ajout de l'agent à la simulation simu.agents = append(simu.agents, *ag) @@ -172,7 +175,8 @@ func (simu *Simulation) Run() { log.Printf("Fin de la simulation [step: %d, in: %d, out: %d, π: %f]", simu.step, simu.env.PI()) } -func (simu *Simulation) Print() { + +func (simu *Simulation) Print_v0() { for { for i := 0; i < 20; i++ { fmt.Println(simu.env.station[i]) @@ -181,10 +185,31 @@ func (simu *Simulation) Print() { fmt.Println() //fmt.Println("============================================================") //time.Sleep(time.Second / 4) // 60 fps ! - time.Sleep(5000 * time.Millisecond) // 1 fps ! + time.Sleep(500 * time.Millisecond) // 1 fps ! //fmt.Print("\033[H\033[2J") // effacement du terminal } } +func (simu *Simulation) Print() { + for { + for i := 0; i < 20; i++ { + for j := 0; j < 20; j++ { + element := simu.env.station[i][j] + if len(element) > 1 { + fmt.Print(string(element[0]) + " ") // Afficher le premier caractère si la longueur est supérieure à 1 + } else { + fmt.Print(element+" ") + } + } + fmt.Println() + } + fmt.Println() + //fmt.Println("============================================================") + //time.Sleep(time.Second / 4) // 60 fps ! + time.Sleep(500 * time.Millisecond) // 1 fps ! + //fmt.Print("\033[H\033[2J") // effacement du terminal + } +} + func (simu *Simulation) Log() { // Not implemented diff --git a/internal/simulation/usagerLambda.go b/internal/simulation/usagerLambda.go index 58be2d8d4fda05293e57eeb58d3f717eef277ae6..e845f115f66f729ae41ffbc86cafdc1ebe873db2 100644 --- a/internal/simulation/usagerLambda.go +++ b/internal/simulation/usagerLambda.go @@ -9,24 +9,23 @@ import ( type UsagerLambda struct{ req Request } -/* + func (ul *UsagerLambda) Percept(ag *Agent) { // récupérer le channel de l'agent lambda chan_agt := ag.env.GetAgentChan(ag.id) select { case req := <-chan_agt : //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 : ", req.decision, "\n") ul.req = req case <- time.After(time.Second): ag.stuck = ag.isStuck() - } - // - ag.stuck = ag.isStuck() - if ag.stuck { - return + if ag.stuck { + return + } } - // + } @@ -38,6 +37,8 @@ func (ul *UsagerLambda) Deliberate(ag *Agent) { ag.decision = Wait } else if ul.req.decision == Expel{ // cette condition est inutile car l'usager lambda ne peut pas etre expulsé ag.decision = Expel + }else { + ag.decision = Move } } @@ -49,59 +50,8 @@ func (ul *UsagerLambda) Act(ag *Agent) { time.Sleep(time.Duration(n) * time.Second) } else if ag.decision == Disapear { RemoveAgent(&ag.env.station, ag) - } else { + } else { //age.decision == Expel ag.destination = ag.departure ag.MoveAgent() } } - - -/* - * Fonction qui permet de trouver la sortie la plus proche - -func (ul *UsagerLambda) findNearestExit(env *Environment) Coord{ - station := env.station - for i := 0; i < len(station); i++ { - for j := 0; j < len(station[i]); j++ { - if station[i][j] == "X" { - return Coord{i,j} - } - } - } - return Coord{0,0} -} -*/ - - -func (ul *UsagerLambda) Percept(ag *Agent) { - ag.stuck = ag.isStuck() - if ag.stuck { - return - - } - -} - -func (ul *UsagerLambda) Deliberate(ag *Agent) { - if ag.position == ag.destination && (ag.isOn[ag.position] == "W" || ag.isOn[ag.position] == "S") { - fmt.Println(ag.id, "disapear") - ag.decision = Disapear - } else if ag.stuck { - ag.decision = Wait - } else { - ag.decision = Move - } -} - -func (ul *UsagerLambda) Act(ag *Agent) { - if ag.decision == Move { - ag.MoveAgent() - } else if ag.decision == Wait { - n := rand.Intn(2) // temps d'attente aléatoire - time.Sleep(time.Duration(n) * time.Second) - } else if ag.decision == Disapear { - RemoveAgent(&ag.env.station, ag) - } - -} -