diff --git a/cmd/simu/main.go b/cmd/simu/main.go index c29a1f66f5a6b3a3bf7c3dee0a49389e68be6697..c298cf2bfb099acbc2cec11fb165e1ad79a66d61 100644 --- a/cmd/simu/main.go +++ b/cmd/simu/main.go @@ -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() } diff --git a/internal/simulation/agent.go b/internal/simulation/agent.go index 5013677e8e57621c63d3b33403a019d355b3450f..22c6f8f089cbae83eea400c4a309ecf63dde1b75 100644 --- a/internal/simulation/agent.go +++ b/internal/simulation/agent.go @@ -14,7 +14,7 @@ import ( "math/rand" alg "metrosim/internal/algorithms" "time" - "fmt" + //"fmt" ) type Action int64 @@ -46,6 +46,7 @@ type Agent struct { width int height int orientation int + path []alg.Node } 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 { isOn := make(map[Coord]string) 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 { @@ -179,23 +180,32 @@ func (ag *Agent) MoveAgent() { 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) // ================== Tentative de calcul du chemin ======================= - timea := time.Now() - path := alg.FindPath(ag.env.station, start, end, *alg.NewNode(-1, -1, 0, 0, 0, 0)) - fmt.Println(time.Since(timea)) + if len(ag.path) == 0 { + // Recherche d'un chemin si inexistant + path := alg.FindPath(ag.env.station, start, end, *alg.NewNode(-1, -1, 0, 0, 0, 0)) + ag.path = path + } + // ================== Etude de faisabilité ======================= // 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 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) + ag.path = path } - if IsMovementSafe(path, ag, ag.env) { + if IsMovementSafe(ag.path, ag, ag.env) { 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.position[0] = path[0].Row() - ag.position[1] = path[0].Col() + ag.position[0] = ag.path[0].Row() + 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) //ag.env.station[ag.coordBasOccupation[0]][ag.coordBasOccupation[1]] = "A" writeAgent(&ag.env.station, ag) diff --git a/internal/simulation/env.go b/internal/simulation/env.go index 107b44e75fd80e8d4fb2d8c8880ba34a6b58b5c6..b00aa1911e255d30bba868bf386c9e6e3f233750 100644 --- a/internal/simulation/env.go +++ b/internal/simulation/env.go @@ -11,8 +11,12 @@ type Environment struct { agentCount int station [20][20]string 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) { return &Environment{ags: ags, agentCount: len(ags), station: carte, agentsChan: agentsCh} } diff --git a/internal/simulation/simu.go b/internal/simulation/simu.go index 52a019e6ec63a23de4071078ec59b94bbbd12010..18ad5af9466cd9a4fb1f1c172963a08dce2d9988 100644 --- a/internal/simulation/simu.go +++ b/internal/simulation/simu.go @@ -97,7 +97,7 @@ func NewSimulation(agentCount int, maxStep int, maxDuration time.Duration) (simu 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{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) // ajout de l'agent à la simulation