Skip to content
Snippets Groups Projects
Commit 9aa8827f authored by Jana Eltayeb El Rafei's avatar Jana Eltayeb El Rafei
Browse files

ajout_controlleur

parent 52bcc2ee
No related branches found
No related tags found
1 merge request!8Controleur
......@@ -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()
}
......@@ -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)
}
}
......
......@@ -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
}
}
......@@ -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
......
......@@ -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)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment