Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DataScienceSWP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
phwitte
DataScienceSWP
Commits
27c3f258
Commit
27c3f258
authored
7 years ago
by
markr
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
01cd64db
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Final/python files/simplest_simulation.py
+117
-0
117 additions, 0 deletions
Final/python files/simplest_simulation.py
with
117 additions
and
0 deletions
Final/python files/simplest_simulation.py
0 → 100644
+
117
−
0
View file @
27c3f258
import
time
import
random
from
graphics
import
*
# get here: http://mcsp.wartburg.edu/zelle/python/graphics.py
import
matplotlib.pyplot
as
plt
import
numpy
as
np
# imports from our files
from
help_functions
import
*
from
classes
import
Agent
from
check_boundarys
import
checkBoundary
# ------------------------------------------------------------
# global variables
# whether average distance will be ploted at end of simulation
plot
=
True
# window dimensions
winWidth
=
700
winHeight
=
500
window
=
GraphWin
(
"
Window
"
,
winWidth
,
winHeight
)
# max time for one simulation
maxTime
=
4000
# max velocity of the agents
maxV
=
8
# number of agents
agentNum
=
75
# -------------------------------------------------------------
# simplest simulation
# updateVelociy for simplest simulation
def
updateV
(
agent
,
nearestNeighbour
):
# actual math
vx
=
agent
.
x_velocity
+
0.1
*
nearestNeighbour
.
x_velocity
+
random
.
uniform
(
-
3
,
3
)
vy
=
agent
.
y_velocity
+
0.1
*
nearestNeighbour
.
y_velocity
+
random
.
uniform
(
-
3
,
3
)
# check new velocity against max velocity
if
(
abs
(
vx
)
<
maxV
)
:
agent
.
set_xvelocity
(
vx
)
elif
(
vx
<=
-
maxV
):
agent
.
set_xvelocity
(
-
maxV
)
else
:
agent
.
set_xvelocity
(
maxV
)
if
(
abs
(
vy
)
<
maxV
):
agent
.
set_yvelocity
(
vy
)
elif
(
vy
<=
-
maxV
):
agent
.
set_yvelocity
(
-
maxV
)
else
:
agent
.
set_yvelocity
(
maxV
)
return
agent
# update all agents with simplest simulation
def
update_simplest
(
agents
):
for
agent
in
agents
:
nearestNeighbour
=
nearest_neighbour
(
agent
,
agents
)
agent
=
updateV
(
agent
,
nearestNeighbour
)
agent
=
checkBoundary
(
agent
,
winWidth
,
winHeight
)
agent
.
draw_Line
()
return
agents
# -------------------------------------------------------------
# main
def
main
():
agents
=
[]
if
plot
:
# data for plotting
distancedata
=
np
.
array
([])
distancestd
=
np
.
array
([])
numpycount
=
np
.
array
([])
# generate agents
for
i
in
range
(
agentNum
):
agent
=
Agent
(
Point
(
random
.
uniform
(
0
,
winWidth
),
random
.
uniform
(
0
,
winHeight
)),
window
,
maxV
)
# give them a random starting direction
agent
.
set_xvelocity
(
random
.
uniform
(
-
2
,
2
))
agent
.
set_yvelocity
(
random
.
uniform
(
-
2
,
2
))
agent
.
draw_Line
()
agents
.
append
(
agent
)
# main loop
for
i
in
range
(
maxTime
):
if
plot
:
rawdata
=
totalavgdistance
(
agents
,
distancedata
)
#print ("rawdata: "+ str(rawdata))
distancedata
=
np
.
append
(
distancedata
,
np
.
mean
(
rawdata
))
distancestd
=
np
.
append
(
distancestd
,
np
.
std
(
rawdata
))
numpycount
=
np
.
append
(
numpycount
,
i
)
agents
=
update_simplest
(
agents
)
time
.
sleep
(
0.01
)
# show plot of average distance
if
plot
:
plt
.
errorbar
(
numpycount
,
distancedata
,
yerr
=
distancestd
)
plt
.
show
()
window
.
getMouse
()
window
.
close
()
main
()
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