diff --git a/HTML/index.html b/HTML/index.html deleted file mode 100644 index ae4bbe08bd737dc351320b1934400b21e9cc6c77..0000000000000000000000000000000000000000 --- a/HTML/index.html +++ /dev/null @@ -1,17 +0,0 @@ -<!DOCTYPE html> -<html> - <head> - <title>π en direct</title> - <meta charset="utf-8" /> - <link rel="stylesheet" type="text/css" href="style.css"> - <script src="pi.js"></script> - </head> - <body> - <section> - <h1> π </h1> - <p> - π = <span id="pi"></pspan> - </p> - </section> - </body> -</html> diff --git a/HTML/server.go b/api/HTML/server.go similarity index 100% rename from HTML/server.go rename to api/HTML/server.go diff --git a/api.go b/api/api.go similarity index 78% rename from api.go rename to api/api.go index 2de0db589a045fcb4b47fa92c78478164aa58866..f5247e1e362b0bd370c6796ded43b06ca049d318 100644 --- a/api.go +++ b/api/api.go @@ -1,4 +1,4 @@ -package simulation +package api import ( "encoding/json" @@ -6,13 +6,14 @@ import ( "log" "net/http" "time" + sim "metrosim/internal/simulation" ) -func StartAPI(sim *Simulation) { +func StartAPI(sim *sim.Simulation) { mux := http.NewServeMux() pi := func(w http.ResponseWriter, r *http.Request) { - msg, _ := json.Marshal(sim.env.PI()) + msg, _ := json.Marshal(sim.Env().PI()) fmt.Fprintf(w, "%s", msg) } diff --git a/cmd/simu/main.go b/cmd/simu/main.go index 93deaced0df1ec6e0bb057368e113d3b181228b7..f7d520dfa8148306eaee88f31c1b470d6c80ee3b 100644 --- a/cmd/simu/main.go +++ b/cmd/simu/main.go @@ -1,7 +1,7 @@ package main import ( - simulation "metrosim" + simulation "metrosim/internal/simulation" "time" ) diff --git a/astar.go b/internal/algorithms/astar.go similarity index 91% rename from astar.go rename to internal/algorithms/astar.go index 459b998a44992783fada047d3a5c0046809e0d2b..bd99bd0389f37d30f593a9f5857eff7195f07122 100644 --- a/astar.go +++ b/internal/algorithms/astar.go @@ -1,4 +1,4 @@ -package simulation +package algorithms import ( "container/heap" @@ -13,6 +13,18 @@ type Node struct { row, col, cost, heuristic int } +func NewNode(row, col, cost, heuristic int) *Node{ + return &Node{row, col ,cost , heuristic} +} + +func (nd *Node) Row() int{ + return nd.row +} + +func (nd *Node) Col() int{ + return nd.col +} + type PriorityQueue []*Node func (pq PriorityQueue) Len() int { return len(pq) } @@ -38,7 +50,7 @@ func (pq *PriorityQueue) Pop() interface{} { return item } -func findPath(matrix [20][20]string, start, end Node, forbidenCell Node) []Node { +func FindPath(matrix [20][20]string, start, end Node, forbidenCell Node) []Node { pq := make(PriorityQueue, 0) heap.Init(&pq) diff --git a/agent.go b/internal/simulation/agent.go similarity index 74% rename from agent.go rename to internal/simulation/agent.go index 6a19989b3c6f903f01d17d313265d12626b4d1a3..c0c4040ba10bb3cb073f4b350cb26b48a350ac74 100644 --- a/agent.go +++ b/internal/simulation/agent.go @@ -3,8 +3,10 @@ package simulation import ( //"fmt" + //"fmt" "log" "math/rand" + alg "metrosim/internal/algorithms" "time" ) @@ -43,33 +45,6 @@ type Behavior interface { Act(*Agent) } -type UsagerLambda struct{} - -func (ul *UsagerLambda) Percept(ag *Agent) { - ag.stuck = ag.isStuck() - if ag.stuck { - return - } - -} - -func (ul *UsagerLambda) Deliberate(ag *Agent) { - if ag.stuck { - ag.decision = Wait - } else { - ag.decision = Move - } -} - -func (ul *UsagerLambda) Act(ag *Agent) { - if ag.decision == Move { - ag.MoveAgent() - } else if ag.decision == Wait { - n := rand.Intn(2) // temps d'attente aléatoire - time.Sleep(time.Duration(n) * time.Second) - } - -} func NewAgent(id string, env *Environment, syncChan chan int, vitesse time.Duration, force int, politesse bool, UpCoord Coord, DownCoord Coord, behavior Behavior, departure, destination Coord) *Agent { return &Agent{AgentID(id), vitesse, force, politesse, UpCoord, DownCoord, departure, destination, behavior, env, syncChan, Noop, env.station[UpCoord[0]][UpCoord[1]], false} @@ -100,14 +75,14 @@ func (ag *Agent) Act(env *Environment) { } } -func IsMovementSafe(path []Node, env *Environment) bool { +func IsMovementSafe(path []alg.Node, env *Environment) bool { // Détermine si le movement est faisable - return len(path) > 0 && (env.station[path[0].row][path[0].col] == "B" || env.station[path[0].row][path[0].col] == "_") + return len(path) > 0 && (env.station[path[0].Row()][path[0].Col()] == "B" || env.station[path[0].Row()][path[0].Col()] == "_") } -func IsAgentBlocking(path []Node, env *Environment) bool { +func IsAgentBlocking(path []alg.Node, env *Environment) bool { // Détermine si un agent se trouve sur la case à visiter - return len(path) > 0 && env.station[path[0].row][path[0].col] == "A" + return len(path) > 0 && env.station[path[0].Row()][path[0].Col()] == "A" } func (ag *Agent) isStuck() bool { @@ -138,27 +113,26 @@ func (ag *Agent) MoveAgent() { // Il faudrait faire en sorte que les agents bougent et laisse passer les autres // ============ Initialisation des noeuds de départ ====================== - start := Node{ag.coordBasOccupation[0], ag.coordBasOccupation[1], 0, 0} - end := Node{ag.destination[0], ag.destination[1], 0, 0} + start := *alg.NewNode(ag.coordBasOccupation[0], ag.coordBasOccupation[1], 0, 0) + end := *alg.NewNode(ag.destination[0], ag.destination[1], 0, 0) // ================== Tentative de calcul du chemin ======================= - path := findPath(ag.env.station, start, end, Node{}) - + path := alg.FindPath(ag.env.station, start, end, *alg.NewNode(-1,-1,0,0)) + // ================== Etude de faisabilité ======================= if IsAgentBlocking(path, 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 = findPath(ag.env.station, start, end, path[0]) + path = alg.FindPath(ag.env.station, start, end, path[0]) time.Sleep(time.Second) } if IsMovementSafe(path, ag.env) { ag.env.station[ag.coordBasOccupation[0]][ag.coordBasOccupation[1]] = ag.isOn - ag.isOn = ag.env.station[path[0].row][path[0].col] - ag.coordBasOccupation[0] = path[0].row - ag.coordBasOccupation[1] = path[0].col + ag.isOn = ag.env.station[path[0].Row()][path[0].Col()] + ag.coordBasOccupation[0] = path[0].Row() + ag.coordBasOccupation[1] = path[0].Col() ag.env.station[ag.coordBasOccupation[0]][ag.coordBasOccupation[1]] = "A" - // ============ Prise en compte de la vitesse de déplacement ====================== time.Sleep(ag.vitesse) } diff --git a/env.go b/internal/simulation/env.go similarity index 100% rename from env.go rename to internal/simulation/env.go diff --git a/simu.go b/internal/simulation/simu.go similarity index 99% rename from simu.go rename to internal/simulation/simu.go index 6756148f41f6a1449820a382ed27d8e916555051..81371282b7dcf9e5ef1553c46a284db11218d65b 100644 --- a/simu.go +++ b/internal/simulation/simu.go @@ -75,6 +75,10 @@ type Simulation struct { syncChans sync.Map } +func (sim *Simulation) Env() (*Environment){ + return &sim.env +} + func NewSimulation(agentCount int, maxStep int, maxDuration time.Duration) (simu *Simulation) { simu = &Simulation{} simu.agents = make([]Agent, 0, agentCount) diff --git a/internal/simulation/usagerLambda.go b/internal/simulation/usagerLambda.go new file mode 100644 index 0000000000000000000000000000000000000000..1f977f18c14bf8f2708fc52e447086145261f472 --- /dev/null +++ b/internal/simulation/usagerLambda.go @@ -0,0 +1,34 @@ +package simulation + +import( + "math/rand" + "time" +) + +type UsagerLambda struct{} + +func (ul *UsagerLambda) Percept(ag *Agent) { + ag.stuck = ag.isStuck() + if ag.stuck { + return + } + +} + +func (ul *UsagerLambda) Deliberate(ag *Agent) { + if ag.stuck { + ag.decision = Wait + } else { + ag.decision = Move + } +} + +func (ul *UsagerLambda) Act(ag *Agent) { + if ag.decision == Move { + ag.MoveAgent() + } else if ag.decision == Wait { + n := rand.Intn(2) // temps d'attente aléatoire + time.Sleep(time.Duration(n) * time.Second) + } + +} \ No newline at end of file