Skip to content
Snippets Groups Projects
Commit 08e13ecb authored by julienpillis's avatar julienpillis
Browse files

suppression calcul chemin à chaque action

parent 47975acd
No related branches found
No related tags found
2 merge requests!7Main,!6Signalisation
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
) )
func main() { func main() {
s := simulation.NewSimulation(1, -1, 600*time.Second) s := simulation.NewSimulation(2, -1, 600*time.Second)
//go simulation.StartAPI(s) //go simulation.StartAPI(s)
s.Run() s.Run()
} }
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
"math/rand" "math/rand"
alg "metrosim/internal/algorithms" alg "metrosim/internal/algorithms"
"time" "time"
"fmt" //"fmt"
) )
type Action int64 type Action int64
...@@ -46,6 +46,7 @@ type Agent struct { ...@@ -46,6 +46,7 @@ type Agent struct {
width int width int
height int height int
orientation int orientation int
path []alg.Node
} }
type Behavior interface { type Behavior interface {
...@@ -57,7 +58,7 @@ type Behavior interface { ...@@ -57,7 +58,7 @@ type Behavior interface {
func NewAgent(id string, env *Environment, syncChan chan int, vitesse time.Duration, force int, politesse bool, behavior Behavior, departure, destination Coord, width, height int) *Agent { func NewAgent(id string, env *Environment, syncChan chan int, vitesse time.Duration, force int, politesse bool, behavior Behavior, departure, destination Coord, width, height int) *Agent {
isOn := make(map[Coord]string) isOn := make(map[Coord]string)
saveCells(&env.station, isOn, departure, width, height, 0) saveCells(&env.station, isOn, departure, width, height, 0)
return &Agent{AgentID(id), vitesse, force, politesse, departure, departure, destination, behavior, env, syncChan, Noop, isOn, false, width, height, 0} return &Agent{AgentID(id), vitesse, force, politesse, departure, departure, destination, behavior, env, syncChan, Noop, isOn, false, width, height, 0, make([]alg.Node, 0)}
} }
func (ag *Agent) ID() AgentID { func (ag *Agent) ID() AgentID {
...@@ -179,23 +180,32 @@ func (ag *Agent) MoveAgent() { ...@@ -179,23 +180,32 @@ func (ag *Agent) MoveAgent() {
start := *alg.NewNode(ag.position[0], ag.position[1], 0, 0, ag.width, ag.height) start := *alg.NewNode(ag.position[0], ag.position[1], 0, 0, ag.width, ag.height)
end := *alg.NewNode(ag.destination[0], ag.destination[1], 0, 0, ag.width, ag.height) end := *alg.NewNode(ag.destination[0], ag.destination[1], 0, 0, ag.width, ag.height)
// ================== Tentative de calcul du chemin ======================= // ================== Tentative de calcul du chemin =======================
timea := time.Now() if len(ag.path) == 0 {
path := alg.FindPath(ag.env.station, start, end, *alg.NewNode(-1, -1, 0, 0, 0, 0)) // Recherche d'un chemin si inexistant
fmt.Println(time.Since(timea)) path := alg.FindPath(ag.env.station, start, end, *alg.NewNode(-1, -1, 0, 0, 0, 0))
ag.path = path
}
// ================== Etude de faisabilité ======================= // ================== Etude de faisabilité =======================
// fmt.Println(ag.position,path[0]) // fmt.Println(ag.position,path[0])
if IsAgentBlocking(path, ag, ag.env) { if IsAgentBlocking(ag.path, ag, ag.env) {
// Si un agent bloque notre déplacement, on attend un temps aléatoire, et reconstruit un chemin en évitant la position // Si un agent bloque notre déplacement, on attend un temps aléatoire, et reconstruit un chemin en évitant la position
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond) time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
path = alg.FindPath(ag.env.station, start, end, path[0]) path := alg.FindPath(ag.env.station, start, end, ag.path[0])
time.Sleep(time.Second) time.Sleep(time.Second)
ag.path = path
} }
if IsMovementSafe(path, ag, ag.env) { if IsMovementSafe(ag.path, ag, ag.env) {
removeAgent(&ag.env.station, ag) removeAgent(&ag.env.station, ag)
rotateAgent(ag, path[0].Or()) rotateAgent(ag, ag.path[0].Or())
//ag.env.station[ag.coordBasOccupation[0]][ag.coordBasOccupation[1]] = ag.isOn //ag.env.station[ag.coordBasOccupation[0]][ag.coordBasOccupation[1]] = ag.isOn
ag.position[0] = path[0].Row() ag.position[0] = ag.path[0].Row()
ag.position[1] = path[0].Col() ag.position[1] = ag.path[0].Col()
if len(ag.path) > 1 {
ag.path = ag.path[1:]
} else {
ag.path = nil
}
saveCells(&ag.env.station, ag.isOn, ag.position, ag.width, ag.height, ag.orientation) saveCells(&ag.env.station, ag.isOn, ag.position, ag.width, ag.height, ag.orientation)
//ag.env.station[ag.coordBasOccupation[0]][ag.coordBasOccupation[1]] = "A" //ag.env.station[ag.coordBasOccupation[0]][ag.coordBasOccupation[1]] = "A"
writeAgent(&ag.env.station, ag) writeAgent(&ag.env.station, ag)
......
...@@ -11,8 +11,12 @@ type Environment struct { ...@@ -11,8 +11,12 @@ type Environment struct {
agentCount int agentCount int
station [20][20]string station [20][20]string
agentsChan map[AgentID]chan AgentID agentsChan map[AgentID]chan AgentID
//zones map[ZoneID][]Coord // Zones de la station
//panneaux map[ZoneID][]Coord // Les panneaux de la station, permettant d'aller vers la zone
} }
type ZoneID int
func NewEnvironment(ags []Agent, carte [20][20]string, agentsCh map[AgentID]chan AgentID) (env *Environment) { func NewEnvironment(ags []Agent, carte [20][20]string, agentsCh map[AgentID]chan AgentID) (env *Environment) {
return &Environment{ags: ags, agentCount: len(ags), station: carte, agentsChan: agentsCh} return &Environment{ags: ags, agentCount: len(ags), station: carte, agentsChan: agentsCh}
} }
......
...@@ -97,7 +97,7 @@ func NewSimulation(agentCount int, maxStep int, maxDuration time.Duration) (simu ...@@ -97,7 +97,7 @@ func NewSimulation(agentCount int, maxStep int, maxDuration time.Duration) (simu
syncChan := make(chan int) 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, 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{2, 8}, Coord{13, 15}, 2, 1) ag := NewAgent(id, &simu.env, syncChan, 1000, 0, true, &UsagerLambda{}, Coord{2, 8}, Coord{13, 14}, 2, 1)
//ag := NewAgent(id, &simu.env, syncChan, 1000, 0, true, &UsagerLambda{}, Coord{5, 8}, Coord{0, 0}, 2, 1) //ag := NewAgent(id, &simu.env, syncChan, 1000, 0, true, &UsagerLambda{}, Coord{5, 8}, Coord{0, 0}, 2, 1)
// ajout de l'agent à la simulation // ajout de l'agent à la simulation
......
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