Skip to content
Snippets Groups Projects
Commit 787fefb9 authored by julienpillis's avatar julienpillis
Browse files

v1 intégration panneaux

parent 08e13ecb
No related branches found
No related tags found
2 merge requests!7Main,!6Signalisation
......@@ -6,7 +6,7 @@ import (
)
func main() {
s := simulation.NewSimulation(2, -1, 600*time.Second)
s := simulation.NewSimulation(1, -1, 600*time.Second)
//go simulation.StartAPI(s)
s.Run()
}
......@@ -54,6 +54,8 @@ func (pq *PriorityQueue) Pop() interface{} {
*pq = old[0 : n-1]
return item
}
type ZoneID int
type Coord [2]int
func FindPath(matrix [20][20]string, start, end Node, forbidenCell Node) []Node {
pq := make(PriorityQueue, 0)
......@@ -64,7 +66,7 @@ func FindPath(matrix [20][20]string, start, end Node, forbidenCell Node) []Node
parents := make(map[Node]Node)
closestPoint := start // Initialisation avec le point de départ
closestDistance := heuristic(start.row, start.col, end)
closestDistance := Heuristic(start.row, start.col, end)
foundPath := false
......@@ -72,7 +74,7 @@ func FindPath(matrix [20][20]string, start, end Node, forbidenCell Node) []Node
current := heap.Pop(&pq).(*Node)
// Mise à jour du point le plus proche si le point actuel est plus proche
currentDistance := heuristic(current.row, current.col, end)
currentDistance := Heuristic(current.row, current.col, end)
if currentDistance < closestDistance {
closestPoint = *current
closestDistance = currentDistance
......@@ -116,7 +118,7 @@ func getNeighbors(matrix [20][20]string, current, end Node, forbiddenCell Node)
//fmt.Println("okk")
neighbors := make([]*Node, 0)
// Possible moves: up, down, left, right, rotate (clockwise)
// Possible moves: up, down, left, right
possibleMoves := [][]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
for _, move := range possibleMoves {
......@@ -130,7 +132,7 @@ func getNeighbors(matrix [20][20]string, current, end Node, forbiddenCell Node)
row: newRow,
col: newCol,
cost: current.cost + 1,
heuristic: heuristic(newRow, newCol, end),
heuristic: Heuristic(newRow, newCol, end),
width: current.width,
height: current.height,
orientation: current.orientation,
......@@ -143,7 +145,7 @@ func getNeighbors(matrix [20][20]string, current, end Node, forbiddenCell Node)
return neighbors
}
func heuristic(row, col int, end Node) int {
func Heuristic(row, col int, end Node) int {
// Heuristique simple : distance de Manhattan
return abs(row-end.row) + abs(col-end.col)
}
......
......@@ -6,15 +6,15 @@ package simulation
* // TODO: Gérer les moments où les agents font du quasi-sur place car ils ne peuvent plus bouger
* // TODO: Il arrive encore que certains agents soient bloqués, mais c'est quand il n'y a aucun mouvement possible.
* // Il faudrait faire en sorte que les agents bougent et laisse passer les autres
*
* // TODO: vérifier map playground, destination en (0,0)
*/
import (
//"fmt"
"log"
"math/rand"
alg "metrosim/internal/algorithms"
"time"
//"fmt"
)
type Action int64
......@@ -30,23 +30,24 @@ type Coord [2]int
type AgentID string
type Agent struct {
id AgentID
vitesse time.Duration
force int
politesse bool
position Coord // Coordonnées de référence, width et height on compte width et height à partir de cette position
departure Coord
destination Coord
behavior Behavior
env *Environment
syncChan chan int
decision int
isOn map[Coord]string // Contenu de la case sur laquelle il se trouve
stuck bool
width int
height int
orientation int
path []alg.Node
id AgentID
vitesse time.Duration
force int
politesse bool
position Coord // Coordonnées de référence, width et height on compte width et height à partir de cette position
departure Coord
destination Coord
behavior Behavior
env *Environment
syncChan chan int
decision int
isOn map[Coord]string // Contenu de la case sur laquelle il se trouve
stuck bool
width int
height int
orientation int
path []alg.Node
visitedPanneaux map[Coord]bool
}
type Behavior interface {
......@@ -58,7 +59,11 @@ 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, make([]alg.Node, 0)}
visitedPanneaux := make(map[Coord]bool, len(env.panneaux[env.zones[destination]]))
for _, panneau := range env.panneaux[env.zones[destination]] {
visitedPanneaux[panneau] = false
}
return &Agent{AgentID(id), vitesse, force, politesse, departure, departure, destination, behavior, env, syncChan, Noop, isOn, false, width, height, 0, make([]alg.Node, 0), visitedPanneaux}
}
func (ag *Agent) ID() AgentID {
......@@ -177,18 +182,18 @@ func (ag *Agent) isStuck() bool {
func (ag *Agent) MoveAgent() {
// ============ Initialisation des noeuds de départ ======================
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 =======================
if len(ag.path) == 0 {
start, end := ag.generatePathExtremities()
// 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(ag.path, ag, ag.env) {
start, end := ag.generatePathExtremities()
// 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, ag.path[0])
......@@ -217,6 +222,13 @@ func (ag *Agent) MoveAgent() {
}
func (ag *Agent) generatePathExtremities() (alg.Node, alg.Node) {
start := *alg.NewNode(ag.position[0], ag.position[1], 0, 0, ag.width, ag.height)
destination := ag.findDestination()
end := *alg.NewNode(destination[0], destination[1], 0, 0, ag.width, ag.height)
return start, end
}
func removeAgent(matrix *[20][20]string, agt *Agent) {
// Supprime l'agent de la matrice
......@@ -259,16 +271,40 @@ func saveCells(matrix *[20][20]string, savedCells map[Coord]string, position Coo
func removeCoord(to_remove Coord, mapping map[Coord]string) {
// Suppression d'une clé dans une map
for coord, _ := range mapping {
if coord[0] == to_remove[0] && coord[1] == to_remove[1] {
if equalCoord(&coord, &to_remove) {
delete(mapping, coord)
}
}
}
func equalCoord(coord1, coord2 *Coord) bool {
return coord1[0] == coord2[0] && coord1[1] == coord2[1]
}
func rotateAgent(agt *Agent, orientation int) {
agt.orientation = orientation
}
func (ag *Agent) findDestination() Coord {
destinationZone := ag.env.zones[ag.destination]
if destinationZone != ag.env.zones[ag.position] {
// Si on n'est pas dans la zone de la destination , on va s'orienter par un panneau
//estimDistPos := alg.Heuristic(ag.position[0], ag.position[1], *alg.NewNode(ag.destination[0], ag.destination[1], 0, 0, 0, 0))
for _, panneau := range ag.env.panneaux[destinationZone] {
// On se rapproche du panneau menant à la zone
//estimDistPan := alg.Heuristic(panneau[0], panneau[1], *alg.NewNode(ag.destination[0], ag.destination[1], 0, 0, 0, 0))
if !ag.visitedPanneaux[panneau] {
//TODO:revoir la mise à jour, peut-être à faire lorsqu'on se situe au niveau de panneau, pas avant
//TODO:trouver une meilleure heuristique
ag.visitedPanneaux[panneau] = true
return panneau
}
}
}
// Sinon, on tente d'aller directement à la destination
return ag.destination
}
func calculateBounds(position Coord, width, height, orientation int) (infRow, supRow, infCol, supCol int) {
borneInfRow := 0
borneSupRow := 0
......
......@@ -11,14 +11,14 @@ 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
zones map[Coord]ZoneID // 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}
func NewEnvironment(ags []Agent, carte [20][20]string, agentsCh map[AgentID]chan AgentID, zones map[Coord]ZoneID, panneaux map[ZoneID][]Coord) (env *Environment) {
return &Environment{ags: ags, agentCount: len(ags), station: carte, agentsChan: agentsCh, zones: zones, panneaux: panneaux}
}
func (env *Environment) AddAgent(agt Agent) {
......
......@@ -42,6 +42,57 @@ var carte [20][20]string = [20][20]string{
{"X", "X", "X", "X", "S", "S", "X", "X", "X", "X", "X", "X", "E", "E", "X", "X", "X", "X", "X", "X"},
}
var zonesCarte map[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, 9}: 1,
{0, 10}: 2, {0, 11}: 2, {0, 12}: 2, {0, 13}: 2, {0, 14}: 2, {0, 15}: 2, {0, 16}: 2, {0, 17}: 2, {0, 18}: 2, {0, 19}: 2,
{1, 10}: 2, {1, 11}: 2, {1, 12}: 2, {1, 13}: 2, {1, 14}: 2, {1, 15}: 2, {1, 16}: 2, {1, 17}: 2, {1, 18}: 2, {1, 19}: 2,
{2, 10}: 2, {2, 11}: 2, {2, 12}: 2, {2, 13}: 2, {2, 14}: 2, {2, 15}: 2, {2, 16}: 2, {2, 17}: 2, {2, 18}: 2, {2, 19}: 2,
{3, 10}: 2, {3, 11}: 2, {3, 12}: 2, {3, 13}: 2, {3, 14}: 2, {3, 15}: 2, {3, 16}: 2, {3, 17}: 2, {3, 18}: 2, {3, 19}: 2,
{4, 10}: 2, {4, 11}: 2, {4, 12}: 2, {4, 13}: 2, {4, 14}: 2, {4, 15}: 2, {4, 16}: 2, {4, 17}: 2, {4, 18}: 2, {4, 19}: 2,
{5, 10}: 2, {5, 11}: 2, {5, 12}: 2, {5, 13}: 2, {5, 14}: 2, {5, 15}: 2, {5, 16}: 2, {5, 17}: 2, {5, 18}: 2, {5, 19}: 2,
{6, 10}: 2, {6, 11}: 2, {6, 12}: 2, {6, 13}: 2, {6, 14}: 2, {6, 15}: 2, {6, 16}: 2, {6, 17}: 2, {6, 18}: 2, {6, 19}: 2,
{7, 10}: 2, {7, 11}: 2, {7, 12}: 2, {7, 13}: 2, {7, 14}: 2, {7, 15}: 2, {7, 16}: 2, {7, 17}: 2, {7, 18}: 2, {7, 19}: 2,
{8, 10}: 2, {8, 11}: 2, {8, 12}: 2, {8, 13}: 2, {8, 14}: 2, {8, 15}: 2, {8, 16}: 2, {8, 17}: 2, {8, 18}: 2, {8, 19}: 2,
{9, 10}: 2, {9, 11}: 2, {9, 12}: 2, {9, 13}: 2, {9, 14}: 2, {9, 15}: 2, {9, 16}: 2, {9, 17}: 2, {9, 18}: 2, {9, 19}: 2,
{10, 0}: 3, {10, 1}: 3, {10, 2}: 3, {10, 3}: 3, {10, 4}: 3, {10, 5}: 3, {10, 6}: 3, {10, 7}: 3, {10, 8}: 3, {10, 9}: 3,
{11, 0}: 3, {11, 1}: 3, {11, 2}: 3, {11, 3}: 3, {11, 4}: 3, {11, 5}: 3, {11, 6}: 3, {11, 7}: 3, {11, 8}: 3, {11, 9}: 3,
{12, 0}: 3, {12, 1}: 3, {12, 2}: 3, {12, 3}: 3, {12, 4}: 3, {12, 5}: 3, {12, 6}: 3, {12, 7}: 3, {12, 8}: 3, {12, 9}: 3,
{13, 0}: 3, {13, 1}: 3, {13, 2}: 3, {13, 3}: 3, {13, 4}: 3, {13, 5}: 3, {13, 6}: 3, {13, 7}: 3, {13, 8}: 3, {13, 9}: 3,
{14, 0}: 3, {14, 1}: 3, {14, 2}: 3, {14, 3}: 3, {14, 4}: 3, {14, 5}: 3, {14, 6}: 3, {14, 7}: 3, {14, 8}: 3, {14, 9}: 3,
{15, 0}: 3, {15, 1}: 3, {15, 2}: 3, {15, 3}: 3, {15, 4}: 3, {15, 5}: 3, {15, 6}: 3, {15, 7}: 3, {15, 8}: 3, {15, 9}: 3,
{16, 0}: 3, {16, 1}: 3, {16, 2}: 3, {16, 3}: 3, {16, 4}: 3, {16, 5}: 3, {16, 6}: 3, {16, 7}: 3, {16, 8}: 3, {16, 9}: 3,
{17, 0}: 3, {17, 1}: 3, {17, 2}: 3, {17, 3}: 3, {17, 4}: 3, {17, 5}: 3, {17, 6}: 3, {17, 7}: 3, {17, 8}: 3, {17, 9}: 3,
{18, 0}: 3, {18, 1}: 3, {18, 2}: 3, {18, 3}: 3, {18, 4}: 3, {18, 5}: 3, {18, 6}: 3, {18, 7}: 3, {18, 8}: 3, {18, 9}: 3,
{19, 0}: 3, {19, 1}: 3, {19, 2}: 3, {19, 3}: 3, {19, 4}: 3, {19, 5}: 3, {19, 6}: 3, {19, 7}: 3, {19, 8}: 3, {19, 9}: 3,
{10, 10}: 4, {10, 11}: 4, {10, 12}: 4, {10, 13}: 4, {10, 14}: 4, {10, 15}: 4, {10, 16}: 4, {10, 17}: 4, {10, 18}: 4, {10, 19}: 4,
{11, 10}: 4, {11, 11}: 4, {11, 12}: 4, {11, 13}: 4, {11, 14}: 4, {11, 15}: 4, {11, 16}: 4, {11, 17}: 4, {11, 18}: 4, {11, 19}: 4,
{12, 10}: 4, {12, 11}: 4, {12, 12}: 4, {12, 13}: 4, {12, 14}: 4, {12, 15}: 4, {12, 16}: 4, {12, 17}: 4, {12, 18}: 4, {12, 19}: 4,
{13, 10}: 4, {13, 11}: 4, {13, 12}: 4, {13, 13}: 4, {13, 14}: 4, {13, 15}: 4, {13, 16}: 4, {13, 17}: 4, {13, 18}: 4, {13, 19}: 4,
{14, 10}: 4, {14, 11}: 4, {14, 12}: 4, {14, 13}: 4, {14, 14}: 4, {14, 15}: 4, {14, 16}: 4, {14, 17}: 4, {14, 18}: 4, {14, 19}: 4,
{15, 10}: 4, {15, 11}: 4, {15, 12}: 4, {15, 13}: 4, {15, 14}: 4, {15, 15}: 4, {15, 16}: 4, {15, 17}: 4, {15, 18}: 4, {15, 19}: 4,
{16, 10}: 4, {16, 11}: 4, {16, 12}: 4, {16, 13}: 4, {16, 14}: 4, {16, 15}: 4, {16, 16}: 4, {16, 17}: 4, {16, 18}: 4, {16, 19}: 4,
{17, 10}: 4, {17, 11}: 4, {17, 12}: 4, {17, 13}: 4, {17, 14}: 4, {17, 15}: 4, {17, 16}: 4, {17, 17}: 4, {17, 18}: 4, {17, 19}: 4,
{18, 10}: 4, {18, 11}: 4, {18, 12}: 4, {18, 13}: 4, {18, 14}: 4, {18, 15}: 4, {18, 16}: 4, {18, 17}: 4, {18, 18}: 4, {18, 19}: 4,
{19, 10}: 4, {19, 11}: 4, {19, 12}: 4, {19, 13}: 4, {19, 14}: 4, {19, 15}: 4, {19, 16}: 4, {19, 17}: 4, {19, 18}: 4, {19, 19}: 4,
}
var panneauxCarte map[ZoneID][]Coord = map[ZoneID][]Coord{
// Placement des panneaux d'orientation
1: {Coord{7, 11}, Coord{14, 11}, Coord{10, 19}},
2: {Coord{14, 11}, Coord{10, 19}},
3: {Coord{7, 11}, Coord{10, 19}, Coord{14, 11}},
4: {Coord{7, 11}, Coord{10, 19}},
}
var playground [20][20]string = [20][20]string{
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
......@@ -87,7 +138,7 @@ func NewSimulation(agentCount int, maxStep int, maxDuration time.Duration) (simu
// Communication entre agents
mapChan := make(map[AgentID]chan AgentID)
simu.env = *NewEnvironment([]Agent{}, carte, mapChan)
simu.env = *NewEnvironment([]Agent{}, carte, mapChan, zonesCarte, panneauxCarte)
//simu.env = *NewEnvironment([]Agent{}, playground, mapChan)
// création des agents et des channels
......@@ -97,7 +148,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, 14}, 2, 1)
ag := NewAgent(id, &simu.env, syncChan, 1000, 0, true, &UsagerLambda{}, Coord{2, 8}, Coord{13, 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
......
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