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)
......@@ -243,6 +243,87 @@ func (ag *Agent) NextCell() string {
return "X"
}
func (agt *Agent) MyNextCellIsSafe() bool {
// Simulation du déplacement
ag := *agt
switch ag.direction {
case 0: //haut
ag.position = alg.Coord{ag.position[0] - 1, ag.position[1]}
case 1: //droite
ag.position = alg.Coord{ag.position[0], ag.position[1] + 1}
case 2: //gauche
ag.position = alg.Coord{ag.position[0] + 1, ag.position[1]}
case 3: //bas
ag.position = alg.Coord{ag.position[0], ag.position[1] - 1}
}
if !(ag.position[1] < 0 || ag.position[0] < 0 || ag.position[0] > len(agt.env.station[0]) || ag.position[1] > len(agt.env.station[1])) {
i := ag.position[0]
j := ag.position[1]
if agt.env.station[i][j] != "B" && agt.env.station[i][j] != "_" && agt.env.station[i][j] != "W" && agt.env.station[i][j] != "S" {
// Si on n'est pas sur une case atteignable, en dehors de la zone qu'occupe l'agent avant déplacement, on est bloqué
//fmt.Println("[IsMovementSafe]case inaccessible :",agt.id)
return false
} else {
return true
}
}
return false
}
func (ag *Agent) ShiftAgent() bool {
//fmt.Printf("ShiftAgent")
storeDirection := ag.direction //enregistrer l'orientation initiale de l'agent
for i := 0; i < 4; i++ {
ag.direction = (storeDirection + i) % 4
safe := ag.MyNextCellIsSafe()
if safe { // Deplacement possible safe=true
switch ag.direction {
case 0: //haut
ag.position = alg.Coord{ag.position[0] - 1, ag.position[1]}
case 1: //droite
ag.position = alg.Coord{ag.position[0], ag.position[1] + 1}
case 2:
ag.position = alg.Coord{ag.position[0] + 1, ag.position[1]}
case 3:
ag.position = alg.Coord{ag.position[0], ag.position[1] - 1}
}
//Début du déplacement
ag.env.Lock()
x := ag.position[0]
y := ag.position[1]
if len(ag.isOn) > 0 {
// Suppression de l'agent
ag.env.station[x][y] = ag.isOn[alg.Coord{x, y}]
alg.RemoveCoord(alg.Coord{x, y}, ag.isOn)
}
// Enregistrement des valeurs précédentes de la matrice
ag.isOn[alg.Coord{x, y}] = ag.env.station[x][y]
//Ecriture agent dans la matrice (déplacement)
ag.env.station[x][y] = string(ag.id)
ag.env.Unlock()
//Fin du déplacement
// Prise en compte de la vitesse de déplacement
time.Sleep(ag.vitesse * time.Millisecond)
fmt.Printf("J'ai bougé")
return true
}
}
//si on peut aller nulle part, on se remet dans notre config initiale
ag.direction = storeDirection
//fmt.Printf("Je me bouge pas")
return false
}
func (ag *Agent) MoveAgent() bool {
//fmt.Printf("[MoveAgent, %s ] direction = %d \n",ag.id, ag.direction)
// ================== Tentative de calcul du chemin =======================
......
......@@ -22,9 +22,9 @@ 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())
fmt.Printf("Requete recue par l'agent lambda %s : %d \n ", ag.id, ag.request.Decision())
ul.requete = ag.request
default:
ag.stuck = ag.isStuck()
if ag.stuck {
......@@ -38,7 +38,7 @@ func (ul *UsagerLambda) Deliberate(ag *Agent) {
if ul.requete != nil {
switch ul.requete.Decision() {
case Expel:
case Expel:
ag.decision = Expel
return
case Disappear:
......@@ -54,6 +54,9 @@ func (ul *UsagerLambda) Deliberate(ag *Agent) {
case Move:
ag.decision = Move
return
case YouHaveToMove:
ag.decision = TryToMove
return
default:
ag.decision = Move
return
......@@ -84,27 +87,26 @@ func (ul *UsagerLambda) Act(ag *Agent) {
ag.env.RemoveAgent(ag)
//fmt.Printf("Demandeur d'entrer le metro : %s \n",ul.requete.Demandeur())
ul.requete.Demandeur() <- *req.NewRequest(ag.env.agentsChan[ag.id], ACK)
case Expel :
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] 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])
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])
case Noop:
//Cas ou un usager impoli demande a un usager de bouger et il refuse
ul.requete.Demandeur() <- *req.NewRequest(ag.env.agentsChan[ag.id], Noop)
// nothing to do
case Done:
//Cas ou un usager impoli demande a un usager de bouger et il le fait
ul.requete.Demandeur() <- *req.NewRequest(ag.env.agentsChan[ag.id], Done)
fmt.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() {
ul.requete.Demandeur() <- *req.NewRequest(ag.env.agentsChan[ag.id], Done)
} else {
ul.requete.Demandeur() <- *req.NewRequest(ag.env.agentsChan[ag.id], Noop)
}
}
//ag.request = nil // la requete est traitée
if ag.request!= nil && ag.decision ==ag.request.Decision(){
if ag.request != nil && ag.decision == ag.request.Decision() {
ag.request = nil
ul.requete = nil
} // la requete est traitée
......@@ -119,4 +121,4 @@ func (ul *UsagerLambda) SetUpDestination(ag *Agent) {
func isControlledAgt(ag *Agent) bool {
return ag.env.controlledAgents[ag.id]
}
\ No newline at end of file
}