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

détails dans la boucle de percep/delib/action

parent ffb03e07
No related branches found
No related tags found
1 merge request!1Agents
......@@ -2,7 +2,7 @@ package simulation
import (
//"container/heap"
"fmt"
//"fmt"
"log"
"math/rand"
"time"
......@@ -46,6 +46,7 @@ type Behavior interface {
type UsagerLambda struct{}
func (ul *UsagerLambda) Percept(ag *Agent, env *Environment) {
// TODO: Essayer un nouveau chemin quand l'agent est bloqué
}
func (ul *UsagerLambda) Deliberate(ag *Agent) {
......@@ -61,20 +62,22 @@ func (ul *UsagerLambda) Act(ag *Agent, env *Environment) {
if ag.decision == Move {
start := ag.coordBasOccupation
end := ag.destination
path := findPathBFS(ag.env.station, start, end)
if len(path) > 0 && env.station[path[0][0]][path[0][1]] != "A" {
fmt.Println(ag.id, path)
if len(path) > 0 && env.station[path[0][0]][path[0][1]] != "A" { // TODO: Pas ouf les conditions je trouve
ag.env.station[ag.coordBasOccupation[0]][ag.coordBasOccupation[1]] = ag.isOn
ag.isOn = ag.env.station[path[0][0]][path[0][1]]
ag.coordBasOccupation[0] = path[0][0]
ag.coordBasOccupation[1] = path[0][1]
ag.env.station[ag.coordBasOccupation[0]][ag.coordBasOccupation[1]] = "A"
}
vitesseInSeconds := int(ag.vitesse)
//vitesseInSeconds := int(ag.vitesse)
// Multiply the vitesse by time.Second
sleepDuration := time.Duration(vitesseInSeconds) * time.Second
time.Sleep(sleepDuration)
//sleepDuration := time.Duration(vitesseInSeconds) * time.Second
time.Sleep(200 * time.Millisecond)
}
if ag.decision == Wait {
n := rand.Intn(1) // n will be between 0 and 10
time.Sleep(time.Duration(n) * time.Second)
}
}
......@@ -128,6 +131,40 @@ func (ag *Agent) Act(env *Environment) {
*
*
*/
/*
func findPathBFS(matrix [20][20]string, start, end Coord) []Coord {
queue := []Coord{start}
visited := make(map[Coord]bool)
parents := make(map[Coord]Coord)
for len(queue) > 0 {
current := queue[0]
queue = queue[1:]
if current == end {
// Construire le chemin à partir des parents
path := []Coord{current}
for parent, ok := parents[current]; ok; parent, ok = parents[parent] {
path = append([]Coord{parent}, path...)
}
return path[1:]
}
visited[current] = true
neighbors := getNeighborsBFS(matrix, current)
for _, neighbor := range neighbors {
if !visited[neighbor] {
parents[neighbor] = current
queue = append(queue, neighbor)
}
}
}
return nil // Aucun chemin trouvé
}
*/
func findPathBFS(matrix [20][20]string, start, end Coord) []Coord {
queue := []Coord{start}
visited := make(map[Coord]bool)
......@@ -170,7 +207,7 @@ func getNeighborsBFS(matrix [20][20]string, current Coord) []Coord {
newRow, newCol := current[0]+move[0], current[1]+move[1]
// Vérifier si la nouvelle position est valide et non visitée
if newRow >= 0 && newRow < len(matrix) && newCol >= 0 && newCol < len(matrix[0]) && (matrix[newRow][newCol] == "_" || matrix[newRow][newCol] == "B") {
if newRow >= 0 && newRow < len(matrix) && newCol >= 0 && newCol < len(matrix[0]) && (matrix[newRow][newCol] != "Q" && matrix[newRow][newCol] != "X") {
neighbors = append(neighbors, Coord{newRow, newCol})
}
}
......
......@@ -6,7 +6,7 @@ import (
)
func main() {
s := simulation.NewSimulation(3, -1, 600*time.Second)
go simulation.StartAPI(s)
s := simulation.NewSimulation(30, -1, 600*time.Second)
//go simulation.StartAPI(s)
s.Run()
}
......@@ -3,6 +3,7 @@ package simulation
import (
"fmt"
"log"
//"math/rand"
"sync"
"time"
)
......@@ -21,12 +22,13 @@ var carte [20][20]string = [20][20]string{
{"X", "X", "X", "X", "X", "X", "X", "X", "W", "W", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"},
{"X", "X", "X", "X", "X", "X", "X", "X", "_", "_", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"},
{"X", "X", "X", "X", "X", "X", "X", "X", "_", "_", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"},
{"X", "X", "X", "X", "X", "X", "X", "X", "_", "_", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"},
{"X", "X", "X", "X", "X", "X", "X", "X", "_", "_", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"},
{"X", "X", "X", "X", "X", "X", "X", "X", "_", "_", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"},
{"X", "X", "X", "X", "X", "X", "X", "X", "_", "_", "X", "X", "X", "X", "X", "X", "_", "_", "_", "X"},
{"_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"X", "X", "_", "_", "_", "_", "_", "_", "_", "_", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"},
{"X", "X", "_", "X", "X", "X", "X", "X", "_", "_", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"},
{"X", "X", "_", "X", "X", "X", "X", "X", "_", "_", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"},
{"X", "X", "_", "X", "X", "X", "X", "X", "_", "_", "X", "X", "X", "X", "X", "X", "_", "_", "_", "X"},
{"_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "X", "_"},
{"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "B", "B"},
{"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "B", "B"},
{"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "B", "B"},
{"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "B", "B"},
......@@ -39,6 +41,29 @@ 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 playground [20][20]string = [20][20]string{
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "X", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
{"_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"},
}
type Simulation struct {
env Environment
agents []Agent
......@@ -56,14 +81,17 @@ func NewSimulation(agentCount int, maxStep int, maxDuration time.Duration) (simu
simu.maxDuration = maxDuration
simu.env = *NewEnvironment([]Agent{}, carte)
//simu.env = *NewEnvironment([]Agent{}, playground)
// création des agents et des channels
for i := 0; i < agentCount; i++ {
// création de l'agent
id := fmt.Sprintf("Agent #%d", i)
syncChan := make(chan int)
ag := NewAgent(id, &simu.env, syncChan, 1, 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)})
fmt.Println(ag.id,ag.departure,ag.destination)
ag := NewAgent(id, &simu.env, syncChan, 1, 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, 1, 0, true, Coord{4,10}, Coord{4,10}, &UsagerLambda{}, Coord{4,10}, Coord{0, 0})
// ajout de l'agent à la simulation
simu.agents = append(simu.agents, *ag)
......@@ -86,7 +114,7 @@ func (simu *Simulation) Run() {
go simu.Print()
// Démarrage des agents
// Démarrage des agents
var wg sync.WaitGroup
for _, agt := range simu.agents {
wg.Add(1)
......@@ -94,11 +122,11 @@ func (simu *Simulation) Run() {
defer wg.Done()
agent.Start()
}(agt)
}
}
// On sauvegarde la date du début de la simulation
simu.start = time.Now()
// Lancement de l'orchestration de tous les agents
// simu.step += 1 // plus de sens
for _, agt := range simu.agents {
......@@ -124,7 +152,7 @@ func (simu *Simulation) Print() {
for i := 0; i < 20; i++ {
fmt.Println(simu.env.station[i])
}
//time.Sleep(time.Second / 60) // 60 fps !
//time.Sleep(time.Second / 4) // 60 fps !
time.Sleep(time.Second) // 1 fps !
//fmt.Print("\033[H\033[2J") // effacement du terminal
}
......
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