Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • pillisju/metrosim
1 result
Show changes
Commits on Source (2)
......@@ -61,18 +61,19 @@ func simHandler(action string) http.HandlerFunc {
}
case "launch":
log.Println("[API] Launch request")
//TODELETElog.Println("[API] Launch request")
if simulation != nil {
log.Println("[API] running")
//TODELETElog.Println("[API] running")
if !simulation.IsRunning() {
log.Println("[API] running")
//TODELETElog.Println("[API] running")
go simulation.Run()
}
msg, _ := json.Marshal(simulation.Print())
//TODELETEfmt.Printf("%d\n", len(msg))
fmt.Fprintf(w, "%s", msg)
}
case "stop":
// TODO
// TODOD
}
}
}
......
......@@ -5,9 +5,6 @@ package simulation
*/
import (
"fmt"
"log"
//"fmt"
//"log"
......@@ -77,7 +74,7 @@ func (ag *Agent) ID() AgentID {
func (ag *Agent) Start() {
log.Printf("%s starting...\n", ag.id)
//TODELETElog.Printf("%s starting...\n", ag.id)
go ag.listenForRequests()
// si c'est un controlleur on lance le timer de durée de vie
......@@ -313,7 +310,7 @@ func (ag *Agent) ShiftAgent() bool {
// Prise en compte de la vitesse de déplacement
time.Sleep(ag.vitesse * time.Millisecond)
fmt.Printf("J'ai bougé")
//TODELETEfmt.Printf("J'ai bougé")
return true
}
}
......@@ -363,18 +360,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("[MoveAgent, %s] You have to move %s for the %d time \n", ag.id, blockingAgentID, i)
//TODELETEfmt.Printf("[MoveAgent, %s] You have to move %s for the %d time \n", ag.id, blockingAgentID, i)
reqToBlockingAgent = req.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[ag.id] //Attend la réponse
if repFromBlockingAgent.Decision() == Done { //BlockingAgent lui a répondu Done, il s'est donc poussé
fmt.Printf("okay i will move agent %s \n", ag.id)
//TODELETEfmt.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)
//TODELETEfmt.Printf("i can't move agent %s \n", ag.id)
return false //il ne peut pas bouger, il s'arrête
}
}
......@@ -430,10 +427,10 @@ func (ag *Agent) listenForRequests() {
for {
if ag.request == nil {
req := <-ag.env.agentsChan[ag.id]
fmt.Printf("[listenForRequests] Request received by :%s , decision : %d \n", ag.id, req.Decision())
//TODELETEfmt.Printf("[listenForRequests] Request received by :%s , decision : %d \n", ag.id, req.Decision())
ag.request = &req
if ag.request.Decision() == Expel {
fmt.Println("[listenForRequests] Expel received by :", ag.id)
//TODELETEfmt.Println("[listenForRequests] Expel received by :", ag.id)
}
if ag.request != nil && (ag.request.Decision() == Disappear || ag.request.Decision() == EnterMetro) {
return
......
......@@ -48,7 +48,8 @@ func (c *Controleur) Deliberate(ag *Agent) {
// Vérifier si la valeur de faceCase ne correspond pas au motif
matchedCont, err1 := regexp.MatchString(regexCont, c.faceCase)
matchedFraud, err := regexp.MatchString(regexFraudeur, c.faceCase)
//fmt.Println("faceCase : ", c.faceCase)
//
//.0fmt.Println("faceCase : ", c.faceCase)
//fmt.Println("matchedAgt : ", matchedAgt)
if err != nil {
......@@ -102,9 +103,9 @@ func (c *Controleur) Act(ag *Agent) {
case Disappear:
ag.env.RemoveAgent(ag)
case Expel: //Expel
case Expel: //Expel
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")
//TODELETEfmt.Print("L'agent ", agt_face_id, " a été expulsé \n")
ag.env.controlledAgents[agt_face_id] = true // l'agent qui se trouve devant le controleur est controlé
ag.env.agentsChan[agt_face_id] <- *req.NewRequest(ag.env.agentsChan[ag.id], ag.decision) // envoie la decision du controleur à l'agent qui se trouve devant lui
//time.Sleep(500 * time.Millisecond)
......
package simulation
import (
"fmt"
alg "metrosim/internal/algorithms"
req "metrosim/internal/request"
"sync"
......@@ -49,7 +48,7 @@ func NewEnvironment(ags []Agent, carte [50][50]string, metros []Metro, newAgtCh
gates := make([]alg.Coord, 0)
for _, metro := range metros {
fmt.Println(metro.way.gates)
//TODELETEfmt.Println(metro.way.gates)
for _, gate := range metro.way.gates {
gates = append(gates, gate)
}
......@@ -151,11 +150,11 @@ func (env *Environment) RemoveAgent(agt *Agent) {
for i := borneInfRow; i < borneSupRow; i++ {
for j := borneInfCol; j < borneSupCol; j++ {
if len(env.station[i][j])>1{
if len(env.station[i][j]) > 1 {
env.station[i][j] = agt.isOn[alg.Coord{i, j}]
}
alg.RemoveCoord(alg.Coord{i, j}, agt.isOn)
}
}
}
......@@ -177,8 +176,6 @@ func (env *Environment) writeAgent(agt *Agent) {
}
func (env *Environment) getNbAgentsAround(pos alg.Coord) int {
//pos est la position de la porte
way := env.getWay(pos)
......@@ -253,7 +250,6 @@ func (env *Environment) Station() [50][50]string {
return env.station
}
func (env *Environment) getNearGateFromGate(gate Gate) []alg.Coord {
/*
* Renvoie les portes proches de la porte passée en paramètre
......@@ -268,4 +264,3 @@ func (env *Environment) getNearGateFromGate(gate Gate) []alg.Coord {
nearGates = append(nearGates, gate.Position)
return nearGates
}
......@@ -2,7 +2,6 @@ package simulation
import (
"fmt"
"log"
"math/rand"
alg "metrosim/internal/algorithms"
req "metrosim/internal/request"
......@@ -38,7 +37,7 @@ func NewMetro(freq time.Duration, stopT time.Duration, capacity, freeS int, way
func (metro *Metro) Start() {
// Début de la simulation du métro
log.Printf("Metro starting...\n")
//TODELETElog.Printf("Metro starting...\n")
refTime := time.Now()
//var step int
// affichage des portes au départ
......@@ -86,12 +85,12 @@ func (metro *Metro) pickUpGate(gate *alg.Coord, endTime time.Time, force bool) {
agent := metro.findAgent(AgentID(gate_cell))
if agent != nil && (((!force && agent.width*agent.height <= metro.freeSpace) && alg.EqualCoord(&agent.destination, gate)) || force) {
fmt.Println("agent entering metro : ", agent.id, "at gate ", gate)
//TODELETEfmt.Println("agent entering metro : ", agent.id, "at gate ", gate)
metro.way.env.agentsChan[agent.id] <- *req.NewRequest(metro.comChannel, EnterMetro)
select {
case <-metro.comChannel:
metro.freeSpace = metro.freeSpace - agent.width*agent.height
fmt.Println("agent entered metro : ", agent.id, "at gate ", gate)
//TODELETEfmt.Println("agent entered metro : ", agent.id, "at gate ", gate)
case <-time.After(2 * time.Second):
// Si l'agent prend trop de temps à répondre, on le supprime "manuellement"
if metro.findAgent(agent.id) != nil {
......
......@@ -5,7 +5,6 @@ package simulation
*/
import (
"fmt"
"math/rand"
alg "metrosim/internal/algorithms"
req "metrosim/internal/request"
......@@ -41,14 +40,14 @@ func (mr *MobiliteReduite) Deliberate(ag *Agent) {
ag.decision = Expel
return
case Disappear:
fmt.Println("[Deliberate]", ag.id, "Disappear cond 1 (requete)")
//TODELETEfmt.Println("[Deliberate]", ag.id, "Disappear cond 1 (requete)")
ag.decision = Disappear
return
case Wait:
ag.decision = Wait
return
case EnterMetro:
fmt.Println("[MobiliteReduite, Deliberate] EnterMetro")
//TODELETEfmt.Println("[MobiliteReduite, Deliberate] EnterMetro")
ag.decision = EnterMetro
return
case YouHaveToMove:
......@@ -100,7 +99,7 @@ func (mr *MobiliteReduite) Act(ag *Agent) {
//mr.MoveMR(ag)
ag.MoveAgent()
case EnterMetro:
fmt.Printf("[MobiliteReduite, Act %s] EnterMetro \n", ag.id)
//TODELETEfmt.Printf("[MobiliteReduite, Act %s] EnterMetro \n", ag.id)
ag.env.RemoveAgent(ag)
mr.req.Demandeur() <- *req.NewRequest(ag.env.agentsChan[ag.id], ACK)
}
......
......@@ -223,15 +223,15 @@ func (simu *Simulation) Print() [][]string {
element := simu.env.station[i][j]
if len(element) > 1 {
result[i][j] = element[:1] // Stocker le premier caractère si la longueur est supérieure à 1
fmt.Print(result[i][j] + " ")
//fmt.Print(result[i][j] + " ")
} else {
result[i][j] = element
fmt.Print(result[i][j] + " ")
//fmt.Print(result[i][j] + " ")
}
}
fmt.Println()
//TODELETEfmt.Println()
}
fmt.Println()
//TODELETEfmt.Println()
time.Sleep(200 * time.Millisecond) // 1 fps !
return result
}
......@@ -246,7 +246,7 @@ func (simu *Simulation) ActivateFlow() {
for {
ag := &Agent{}
ag = nil
fmt.Println(probability)
//TODELETEfmt.Println(probability)
switch {
case probability < 0.1: // 10% de probabilité
ag = NewAgent("Normal"+fmt.Sprint(simu.env.agentCount), &simu.env, make(chan int), 200, true, &UsagerNormal{}, simu.env.entries[rand.Intn(len(simu.env.entries))], simu.env.gates[rand.Intn(len(simu.env.gates))], 1, 1)
......
package simulation
import (
"fmt"
"math/rand"
alg "metrosim/internal/algorithms"
req "metrosim/internal/request"
......@@ -22,7 +21,7 @@ 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
fmt.Printf("Requete recue par l'agent lambda %s : %d \n ", ag.id, ag.request.Decision())
//TODELETEfmt.Printf("Requete recue par l'agent lambda %s : %d \n ", ag.id, ag.request.Decision())
ul.requete = ag.request
default:
......@@ -45,7 +44,7 @@ func (ul *UsagerLambda) Deliberate(ag *Agent) {
ag.decision = Disappear
return
case EnterMetro:
fmt.Println("[UsagerLambda, Deliberate] EnterMetro %s", ag.id)
//TODELETEfmt.Println("[UsagerLambda, Deliberate] EnterMetro %s", ag.id)
ag.decision = EnterMetro
return
case Wait:
......@@ -90,12 +89,12 @@ func (ul *UsagerLambda) Act(ag *Agent) {
case Expel:
//fmt.Println("[AgentLambda, Act] Expel")
ag.destination = ag.findNearestExit()
fmt.Printf("[UsagerLambda, Act] position de l'agent %s = (%d , %d) \n", ag.id, ag.position[0], ag.position[1])
fmt.Printf("[UsagerLambda, Act] destination de l'agent %s = (%d , %d) \n", ag.id, ag.destination[0], ag.destination[1])
//TODELETEfmt.Printf("[UsagerLambda, Act] position de l'agent %s = (%d , %d) \n", ag.id, ag.position[0], ag.position[1])
//TODELETEfmt.Printf("[UsagerLambda, Act] destination de l'agent %s = (%d , %d) \n", ag.id, ag.destination[0], ag.destination[1])
ag.env.controlledAgents[ag.id] = true
ag.path = make([]alg.Node, 0)
ag.MoveAgent()
fmt.Printf("[UsagerLambda, Act] J'ai bougé %s , ma position = (%d , %d)\n", ag.id, ag.position[0], ag.position[1])
//TODELETEfmt.Printf("[UsagerLambda, Act] J'ai bougé %s , ma position = (%d , %d)\n", ag.id, ag.position[0], ag.position[1])
case TryToMove:
//fmt.Printf("Je suis %s est-ce que j'ai bougé? %t \n", ag.id, movement)
if ag.ShiftAgent() {
......
......@@ -7,7 +7,6 @@ package simulation
*/
import (
"fmt"
"math"
"math/rand"
alg "metrosim/internal/algorithms"
......@@ -58,11 +57,11 @@ func (un *UsagerNormal) Deliberate(ag *Agent) {
ag.decision = Move
return
case YouHaveToMove:
fmt.Println("[AgentNormal, Deliberate] J'essaye de bouger ", ag.id)
//TODELETEfmt.Println("[AgentNormal, Deliberate] J'essaye de bouger ", ag.id)
movement := ag.MoveAgent()
//fmt.Printf("Je suis agent %s Resultat du mouvement de la personne %t \n", ag.id, movement)
if movement {
fmt.Println("[AgentNormal, Deliberate] J'ai bougé ", ag.id)
//TODELETEfmt.Println("[AgentNormal, Deliberate] J'ai bougé ", ag.id)
ag.decision = Done
} else {
ag.decision = Noop
......@@ -92,14 +91,14 @@ func (un *UsagerNormal) Act(ag *Agent) {
//fmt.Printf("[UsagerLambda, Act] agent %s est disparu \n",ag.id)
ag.env.RemoveAgent(ag)
case EnterMetro:
fmt.Printf("[UsagerNormal, Act] agent %s entre dans le Metro \n", ag.id)
//TODELETEfmt.Printf("[UsagerNormal, Act] agent %s entre dans le Metro \n", ag.id)
ag.env.RemoveAgent(ag)
//fmt.Printf("Demandeur d'entrer le metro : %s \n",un.req.Demandeur())
un.req.Demandeur() <- *req.NewRequest(ag.env.agentsChan[ag.id], ACK)
case Expel:
//fmt.Println("[AgentLambda, Act] Expel")
ag.destination = ag.findNearestExit()
fmt.Printf("[UsagerNormal, Act] destination de l'agent %s = %s \n", ag.id, ag.destination)
//TODELETEfmt.Printf("[UsagerNormal, Act] destination de l'agent %s = %s \n", ag.id, ag.destination)
ag.env.controlledAgents[ag.id] = true
ag.path = make([]alg.Node, 0)
ag.MoveAgent()
......@@ -113,7 +112,7 @@ func (un *UsagerNormal) Act(ag *Agent) {
un.req.Demandeur() <- *req.NewRequest(ag.env.agentsChan[ag.id], Done)
case TryToMove:
movement := ag.MoveAgent()
fmt.Printf("Je suis %s est-ce que j'ai bougé? %t \n", ag.id, movement)
//TODELETEfmt.Printf("Je suis %s est-ce que j'ai bougé? %t \n", ag.id, movement)
if movement {
un.req.Demandeur() <- *req.NewRequest(ag.env.agentsChan[ag.id], Done)
} else {
......@@ -130,7 +129,7 @@ func (un *UsagerNormal) SetUpDestination(ag *Agent) {
choix_voie := rand.Intn(len(ag.env.metros)) // choix de la voie de métro aléatoire
dest_porte := (un.findBestGate(ag, ag.env.metros[choix_voie].way.gates))
ag.destination = dest_porte
fmt.Println("[UsagerNormal, setUpDestination] destination de l'agent ", ag.id, " = ", ag.destination, " son position = ", ag.position)
//TODELETEfmt.Println("[UsagerNormal, setUpDestination] destination de l'agent ", ag.id, " = ", ag.destination, " son position = ", ag.position)
}
func (un *UsagerNormal) findBestGate(ag *Agent, gates []alg.Coord) alg.Coord {
......@@ -156,7 +155,7 @@ func (un *UsagerNormal) findBestGate(ag *Agent, gates []alg.Coord) alg.Coord {
//fmt.Println("[findBestGate] agent Position : ",ag.position)
//fmt.Println("[findBestGate] gates non normalisé : ",gatesDistances)
normalizedGates, _, _ := normalizeGates(gatesDistances)
fmt.Println("[findBestGate, %s] gates normalisé : ",ag.id ,normalizedGates)
//TODELETEfmt.Println("[findBestGate, %s] gates normalisé : ",ag.id ,normalizedGates)
bestGates := gates_with_lowest_score(normalizedGates)
bestGate := bestGates[0]
......