Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MetroSim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julien Pillis
MetroSim
Commits
f31b5038
Commit
f31b5038
authored
1 year ago
by
julienpillis
Browse files
Options
Downloads
Patches
Plain Diff
détails dans la boucle de percep/delib/action
parent
ffb03e07
No related branches found
Branches containing commit
No related tags found
1 merge request
!1
Agents
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
agent.go
+45
-8
45 additions, 8 deletions
agent.go
cmd/simu/main.go
+2
-2
2 additions, 2 deletions
cmd/simu/main.go
simu.go
+40
-12
40 additions, 12 deletions
simu.go
with
87 additions
and
22 deletions
agent.go
+
45
−
8
View file @
f31b5038
...
...
@@ -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
})
}
}
...
...
This diff is collapsed.
Click to expand it.
cmd/simu/main.go
+
2
−
2
View file @
f31b5038
...
...
@@ -6,7 +6,7 @@ import (
)
func
main
()
{
s
:=
simulation
.
NewSimulation
(
3
,
-
1
,
600
*
time
.
Second
)
go
simulation
.
StartAPI
(
s
)
s
:=
simulation
.
NewSimulation
(
3
0
,
-
1
,
600
*
time
.
Second
)
//
go simulation.StartAPI(s)
s
.
Run
()
}
This diff is collapsed.
Click to expand it.
simu.go
+
40
−
12
View file @
f31b5038
...
...
@@ -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"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"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
},
{
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
},
{
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
},
{
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
},
{
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
},
{
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
,
"_"
},
}
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
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment