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
markr
DataScienceSWP
Commits
d6c8cb99
Commit
d6c8cb99
authored
8 years ago
by
phwitte
Browse files
Options
Downloads
Patches
Plain Diff
Update couzin.py
parent
1f140779
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
couzin.py
+63
-31
63 additions, 31 deletions
couzin.py
with
63 additions
and
31 deletions
couzin.py
+
63
−
31
View file @
d6c8cb99
...
@@ -32,6 +32,21 @@ def absvec(a, b):
...
@@ -32,6 +32,21 @@ def absvec(a, b):
if
m
==
0
:
m
=
0.001
if
m
==
0
:
m
=
0.001
return
m
return
m
def
calc_angle
(
x1
,
y1
,
x2
,
y2
):
skalar
=
x1
*
x2
+
y1
*
y2
abs1
=
absvec
(
x1
,
y1
)
abs2
=
absvec
(
x2
,
y2
)
erg
=
skalar
/
(
abs1
*
abs2
)
if
erg
>
1
:
#print erg
erg
=
1
elif
erg
<
-
1
:
#print erg
erg
=-
1
return
math
.
degrees
(
math
.
acos
(
erg
))
def
neigbour_in_zones
(
a
,
aas
,
zor_r
,
zoo_r
,
zoa_r
):
def
neigbour_in_zones
(
a
,
aas
,
zor_r
,
zoo_r
,
zoa_r
):
zor
=
[]
zor
=
[]
zoo
=
[]
zoo
=
[]
...
@@ -41,13 +56,15 @@ def neigbour_in_zones(a, aas, zor_r, zoo_r, zoa_r):
...
@@ -41,13 +56,15 @@ def neigbour_in_zones(a, aas, zor_r, zoo_r, zoa_r):
if
(
a
==
agent
):
if
(
a
==
agent
):
True
True
else
:
else
:
dis
=
distance
(
a
[
0
].
getX
()
,
a
[
0
].
get
Y
(),
agent
[
0
].
get
X
()
,
agent
[
0
].
getY
())
dis
=
absvec
(
agent
[
0
].
getX
()
-
a
[
0
].
get
X
()
,
agent
[
0
].
get
Y
()
-
a
[
0
].
getY
()
)
if
dis
<=
zor_r
:
if
dis
<=
zor_r
:
zor
.
append
(
agent
)
zor
.
append
(
agent
)
elif
dis
<=
zoo_r
:
elif
dis
<=
zoo_r
:
zoo
.
append
(
agent
)
zoo
.
append
(
agent
)
elif
dis
<=
zoa_r
:
elif
dis
<=
zoa_r
:
zoa
.
append
(
agent
)
zoa
.
append
(
agent
)
#print len(zoo)+len(zor)+len(zoa)
return
[
zor
,
zoo
,
zoa
]
return
[
zor
,
zoo
,
zoa
]
...
@@ -136,19 +153,8 @@ def updateV_couzin(a, matrix, maxV):
...
@@ -136,19 +153,8 @@ def updateV_couzin(a, matrix, maxV):
dx
=
a
[
1
]
dx
=
a
[
1
]
dy
=
a
[
2
]
dy
=
a
[
2
]
if
abs
(
dx
)
<
maxV
:
dx
+=
random
.
uniform
(
-
1
,
1
)
True
dy
+=
random
.
uniform
(
-
1
,
1
)
elif
dx
<=
-
maxV
:
dx
=
-
maxV
else
:
dx
=
maxV
if
abs
(
dy
)
<
maxV
:
True
elif
dy
<=
-
maxV
:
dy
=
-
maxV
else
:
dy
=
maxV
return
[
dx
,
dy
]
return
[
dx
,
dy
]
...
@@ -176,16 +182,22 @@ def main():
...
@@ -176,16 +182,22 @@ def main():
winWidth
=
500
winWidth
=
500
winHeight
=
500
winHeight
=
500
zor_r
=
5
0
zor_r
=
2
0
zoo_r
=
100
zoo_r
=
100
zoa_r
=
20
0
zoa_r
=
1
20
window
=
GraphWin
(
"
Window
"
,
winWidth
,
winHeight
)
window
=
GraphWin
(
"
Window
"
,
winWidth
,
winHeight
)
maxTime
=
2
0
maxTime
=
400
0
maxV
=
8
maxV
=
8
agentNum
=
20
speed
=
8
agents2
=
[[
0
for
x
in
range
(
5
)]
for
y
in
range
(
agentNum
)]
maxTurn
=
80
radTurn
=
math
.
radians
(
maxTurn
)
negRadTurn
=
math
.
radians
(
360
-
maxTurn
)
agentNum
=
50
agents
=
[[
0
for
x
in
range
(
5
)]
for
y
in
range
(
agentNum
)]
#directions = [[0 for x in range(2)] for y in range(agentNum)]
#directions = [[0 for x in range(2)] for y in range(agentNum)]
#generate point
#generate point
# 0 Point
# 0 Point
...
@@ -193,7 +205,8 @@ def main():
...
@@ -193,7 +205,8 @@ def main():
# 2 YVelocity
# 2 YVelocity
# 3 Line
# 3 Line
# 4 temp. VelocityPoint
# 4 temp. VelocityPoint
for
agent
in
agents2
:
for
agent
in
agents
:
agent
[
0
]
=
Point
(
random
.
uniform
(
0
,
winWidth
),
random
.
uniform
(
0
,
winHeight
))
agent
[
0
]
=
Point
(
random
.
uniform
(
0
,
winWidth
),
random
.
uniform
(
0
,
winHeight
))
agent
[
1
]
=
random
.
uniform
(
-
2
,
2
)
agent
[
1
]
=
random
.
uniform
(
-
2
,
2
)
...
@@ -202,11 +215,16 @@ def main():
...
@@ -202,11 +215,16 @@ def main():
agent
[
0
].
draw
(
window
)
agent
[
0
].
draw
(
window
)
agent
[
3
]
=
Line
(
agent
[
0
],
Point
(
agent
[
0
].
getX
()
+
agent
[
1
],
agent
[
0
].
getY
()
+
agent
[
2
]))
agent
[
3
]
=
Line
(
agent
[
0
],
Point
(
agent
[
0
].
getX
()
+
agent
[
1
],
agent
[
0
].
getY
()
+
agent
[
2
]))
agent
[
3
].
setArrow
(
"
last
"
)
agent
[
3
].
setArrow
(
"
last
"
)
#
agent[3].draw(window)
agent
[
3
].
draw
(
window
)
agentA
=
[
Point
(
200
,
200
)
,
0
,
0
,
None
,[
0
,
0
]]
"""
agentA = [Point(200, 200) , 0, 0,None,[0,0]]
agentB = [Point(205, 200) , 0, 0,None,[0,0]]
agentB = [Point(205, 200) , 0, 0,None,[0,0]]
agents
=
[
agentA
,
agentB
]
agentC = [Point(210, 200) , 0, 0,None,[0,0]]
agentA[0].draw(window)
agentB[0].draw(window)
agentC[0].draw(window)
"""
#agents = [agentA, agentB, agentC]
#update points
#update points
for
i
in
range
(
maxTime
):
for
i
in
range
(
maxTime
):
# Velocity update
# Velocity update
...
@@ -214,20 +232,34 @@ def main():
...
@@ -214,20 +232,34 @@ def main():
neigh_matrix
=
neigbour_in_zones
(
agent
,
agents
,
zor_r
,
zoo_r
,
zoa_r
)
neigh_matrix
=
neigbour_in_zones
(
agent
,
agents
,
zor_r
,
zoo_r
,
zoa_r
)
agent
[
4
]
=
updateV_couzin
(
agent
,
neigh_matrix
,
maxV
)
agent
[
4
]
=
updateV_couzin
(
agent
,
neigh_matrix
,
maxV
)
print
(
i
++
"
zor:
"
++
len
(
neigh_matrix
[
0
]))
#print str(i) + " zor: " + str(len(neigh_matrix[0]))
#print str(i) + " zoo: " + str(len(neigh_matrix[1]))
#print str(i) + " zoa: " + str(len(neigh_matrix[2]))
# move, draw
# move, draw
for
agent
in
agents
:
for
agent
in
agents
:
alpha
=
calc_angle
(
agent
[
1
],
agent
[
2
],
agent
[
4
][
0
],
agent
[
4
][
1
])
if
alpha
<
maxTurn
or
alpha
>
360
-
maxTurn
:
agent
[
1
]
=
agent
[
4
][
0
]
agent
[
1
]
=
agent
[
4
][
0
]
agent
[
2
]
=
agent
[
4
][
1
]
agent
[
2
]
=
agent
[
4
][
1
]
elif
alpha
<
180
:
agent
[
1
]
=
agent
[
1
]
*
math
.
cos
(
radTurn
)
-
agent
[
2
]
*
math
.
sin
(
radTurn
)
agent
[
2
]
=
agent
[
1
]
*
math
.
sin
(
radTurn
)
+
agent
[
2
]
*
math
.
cos
(
radTurn
)
else
:
agent
[
1
]
=
agent
[
1
]
*
math
.
cos
(
negRadTurn
)
-
agent
[
2
]
*
math
.
sin
(
negRadTurn
)
agent
[
2
]
=
agent
[
1
]
*
math
.
sin
(
negRadTurn
)
+
agent
[
2
]
*
math
.
cos
(
negRadTurn
)
agent
[
1
]
=
1
/
absvec
(
agent
[
1
],
agent
[
2
])
*
agent
[
1
]
*
speed
agent
[
2
]
=
1
/
absvec
(
agent
[
1
],
agent
[
2
])
*
agent
[
2
]
*
speed
agent
=
checkBoundary
(
agent
,
winWidth
,
winHeight
)
agent
=
checkBoundary
(
agent
,
winWidth
,
winHeight
)
#
agent[3].undraw()
agent
[
3
].
undraw
()
#
agent[3] = Line(agent[0], Point(agent[0].getX() + agent[1], agent[0].getY() + agent[2]))
agent
[
3
]
=
Line
(
agent
[
0
],
Point
(
agent
[
0
].
getX
()
+
agent
[
1
],
agent
[
0
].
getY
()
+
agent
[
2
]))
#
agent[3].setArrow("last")
agent
[
3
].
setArrow
(
"
last
"
)
#
agent[3].draw(window)
agent
[
3
].
draw
(
window
)
time
.
sleep
(
0.01
)
time
.
sleep
(
0.01
)
window
.
getMouse
()
window
.
getMouse
()
...
...
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