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
18354d41
Commit
18354d41
authored
7 years ago
by
markr
Browse files
Options
Downloads
Patches
Plain Diff
simplest simulation, with single upload function
parent
7c92754b
No related branches found
No related tags found
Loading
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
simplest.py
+124
-0
124 additions, 0 deletions
simplest.py
with
124 additions
and
0 deletions
simplest.py
0 → 100644
+
124
−
0
View file @
18354d41
from
graphics
import
*
import
time
import
random
import
math
# -------------------------------------------------------------------
# help functions
# Distance function betwen points xi, yi and xii,yii
def
distance
(
xi
,
xii
,
yi
,
yii
):
sq1
=
(
xi
-
xii
)
*
(
xi
-
xii
)
sq2
=
(
yi
-
yii
)
*
(
yi
-
yii
)
return
math
.
sqrt
(
sq1
+
sq2
)
# -------------------------------------------------------------------
# simplest simulation
def
nearest_neighbour
(
a
,
aas
):
minDis
=
float
(
'
inf
'
)
nn
=
None
for
b
in
aas
:
if
(
a
==
b
):
True
elif
(
nn
==
None
):
nn
=
b
else
:
dis
=
distance
(
a
[
0
].
getX
(),
b
[
0
].
getX
(),
a
[
0
].
getY
(),
b
[
0
].
getY
())
if
(
dis
<
minDis
):
minDis
=
dis
nn
=
b
return
b
# updateVelociy
def
updateV
(
agent
,
nn
,
maxV
):
vx
=
agent
[
1
]
+
0.1
*
nn
[
1
]
+
random
.
uniform
(
-
3
,
3
)
vy
=
agent
[
2
]
+
0.1
*
nn
[
2
]
+
random
.
uniform
(
-
3
,
3
)
if
(
abs
(
vx
)
<
maxV
)
:
agent
[
1
]
=
vx
elif
(
vx
<=
-
maxV
):
agent
[
1
]
=
-
maxV
else
:
agent
[
1
]
=
maxV
if
(
abs
(
vy
)
<
maxV
):
agent
[
2
]
=
vy
elif
(
vy
<=
-
maxV
):
agent
[
2
]
=
-
maxV
else
:
agent
[
2
]
=
maxV
return
agent
# check for window boundaries
def
checkBoundary
(
agent
,
winWidth
,
winHeight
):
point
=
agent
[
0
]
point
.
move
(
agent
[
1
],
agent
[
2
])
x
=
point
.
getX
()
y
=
point
.
getY
()
if
x
>
0
and
y
<
winHeight
and
x
<
winWidth
and
y
>
0
:
agent
[
0
]
=
point
elif
x
<=
0
or
x
>=
winWidth
:
agent
[
1
]
=
agent
[
1
]
*
(
-
1
)
agent
[
0
].
move
(
agent
[
1
],
agent
[
2
])
elif
y
<=
0
or
y
>=
winHeight
:
agent
[
2
]
=
agent
[
2
]
*
(
-
1
)
agent
[
0
].
move
(
agent
[
1
],
agent
[
2
])
return
agent
def
update_simplest
(
agent
,
agents
,
maxV
,
winWidth
,
winHeight
,
window
):
for
agent
in
agents
:
nn
=
nearest_neighbour
(
agent
,
agents
)
agent
=
updateV
(
agent
,
nn
,
maxV
)
agent
=
checkBoundary
(
agent
,
winWidth
,
winHeight
)
agent
[
3
].
undraw
()
agent
[
3
]
=
Line
(
agent
[
0
],
Point
(
agent
[
0
].
getX
()
+
agent
[
1
],
agent
[
0
].
getY
()
+
agent
[
2
]))
agent
[
3
].
setArrow
(
"
last
"
)
agent
[
3
].
draw
(
window
)
return
agents
def
main
():
winWidth
=
1000
winHeight
=
700
window
=
GraphWin
(
"
Window
"
,
winWidth
,
winHeight
)
maxTime
=
4000
maxV
=
8
agentNum
=
75
agents
=
[[
0
for
x
in
range
(
4
)]
for
y
in
range
(
agentNum
)]
#generate point
for
agent
in
agents
:
agent
[
0
]
=
Point
(
random
.
uniform
(
0
,
winWidth
),
random
.
uniform
(
0
,
winHeight
))
agent
[
1
]
=
random
.
uniform
(
-
2
,
2
)
agent
[
2
]
=
random
.
uniform
(
-
2
,
2
)
agent
[
0
].
draw
(
window
)
agent
[
3
]
=
Line
(
agent
[
0
],
Point
(
agent
[
0
].
getX
()
+
agent
[
1
],
agent
[
0
].
getY
()
+
agent
[
2
]))
agent
[
3
].
setArrow
(
"
last
"
)
agent
[
3
].
draw
(
window
)
#update points
for
i
in
range
(
maxTime
):
agents
=
update_simplest
(
agent
,
agents
,
maxV
,
winWidth
,
winHeight
,
window
)
time
.
sleep
(
0.01
)
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