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

add_find_functions

parent cb096c18
No related branches found
No related tags found
1 merge request!14Merge v2
......@@ -6,7 +6,7 @@ import (
)
func main() {
s := simulation.NewSimulation(6, -1, 600*time.Second)
s := simulation.NewSimulation(20, -1, 600*time.Second)
//go simulation.StartAPI(s)
s.Run()
}
......@@ -181,10 +181,10 @@ func Heuristic(row, col int, end Node) int {
// On introduit de l'aléatoire pour ajouter de la diversité dans la construction des chemins
// On évite d'avoir tout le temps le même chemin pour un même point de départ et d'arrivé
//return abs(row-end.row) + abs(col-end.col) + rand.Intn(3)
return abs(row-end.row) + abs(col-end.col) + rand.Intn(10)
return Abs(row-end.row) + Abs(col-end.col) + rand.Intn(10)
}
func abs(x int) int {
func Abs(x int) int {
if x < 0 {
return -x
}
......
......@@ -364,3 +364,36 @@ func calculateBounds(position Coord, width, height, orientation int) (infRow, su
}
return borneInfRow, borneSupRow, borneInfCol, borneSupCol
}
func (ag *Agent) findNearestGate(gates [] Coord) (Coord) {
// Recherche de la porte la plus proche
nearest := Coord{0, 0}
min := 1000000
for _, gate := range gates {
dist := alg.Abs(ag.position[0]-gate[0]) + alg.Abs(ag.position[1]-gate[1])
if dist < min {
min = dist
nearest = gate
}
}
return nearest
}
func (ag *Agent) findNearestExit() (Coord){
// Recherche de la sortie la plus proche
nearest := Coord{0, 0}
min := 1000000
for i := 0; i < 20; i++ {
for j := 0; j < 20; j++ {
if ag.env.station[i][j] == "S" {
dist := alg.Abs(ag.position[0]-i) + alg.Abs(ag.position[1]-j)
if dist < min {
min = dist
nearest = Coord{i, j}
}
}
}
}
return nearest
}
\ No newline at end of file
package simulation
import (
//"fmt"
"fmt"
"math/rand"
"time"
alg "metrosim/internal/algorithms"
)
type UsagerLambda struct {
req Request
req *Request
}
func (ul *UsagerLambda) Percept(ag *Agent) {
......@@ -19,7 +19,7 @@ func (ul *UsagerLambda) Percept(ag *Agent) {
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
//fmt.Println("[AgentLambda, Percept] Requete recue par l'agent lambda : ", req.decision)
ul.req = req
ul.req = &req
case <- time.After(time.Second):
ag.stuck = ag.isStuck()
if ag.stuck {
......@@ -32,12 +32,16 @@ func (ul *UsagerLambda) Percept(ag *Agent) {
func (ul *UsagerLambda) Deliberate(ag *Agent) {
//fmt.Println("[AgentLambda Deliberate] decision :", ul.req.decision)
if ul.req.decision == Stop{
ag.decision = Wait
} else if ul.req.decision == Expel{ // cette condition est inutile car l'usager lambda ne peut pas etre expulsé , elle est nécessaire pour les agents fraudeurs
ag.decision = Expel
}else if ag.position == ag.destination && (ag.isOn[ag.position] == "W" || ag.isOn[ag.position] == "S") {
if (ul.req != nil ) {
if ul.req.decision == Stop{
ag.decision = Wait
ul.req = nil //demande traitée
} else { // sinon alors la requete est de type "Viré" cette condition est inutile car l'usager lambda ne peut pas etre expulsé , elle est nécessaire pour les agents fraudeurs
fmt.Println("[AgentLambda, Deliberate] Expel")
ag.decision = Expel
ul.req = nil //demande traitée
}
}else if ag.position == ag.destination && (ag.isOn[ag.position] == "W" || ag.isOn[ag.position] == "S") {
//fmt.Println(ag.id, "disapear")
ag.decision = Disapear
} else {
......@@ -55,8 +59,9 @@ func (ul *UsagerLambda) Act(ag *Agent) {
} else if ag.decision == Disapear {
RemoveAgent(&ag.env.station, ag)
} else { //age.decision == Expel
//fmt.Println("[AgentLambda, Act] Expel")
ag.destination = ag.departure
fmt.Println("[AgentLambda, Act] Expel")
ag.destination = ag.findNearestExit()
fmt.Println("[AgentLambda, Act] destination = ",ag.destination)
ag.env.controlledAgents[ag.id] = true
ag.path = make([]alg.Node,0)
ag.MoveAgent()
......
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