Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
Collective risk game
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package 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
Liyao Zhu
Collective risk game
Commits
2cbe44dc
Commit
2cbe44dc
authored
6 years ago
by
Liyao Zhu
Browse files
Options
Downloads
Patches
Plain Diff
stacked bar of alpha added
simplified select for well-mixed graph
parent
b80060de
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
game.py
+45
-13
45 additions, 13 deletions
game.py
graph.py
+33
-22
33 additions, 22 deletions
graph.py
with
78 additions
and
35 deletions
game.py
+
45
−
13
View file @
2cbe44dc
...
@@ -172,20 +172,19 @@ def rep(repeat, N=100, R=1, K=99, P=0, Actions=[0, 0.2, 0.4, 0.6, 0.8], I=1000,
...
@@ -172,20 +172,19 @@ def rep(repeat, N=100, R=1, K=99, P=0, Actions=[0, 0.2, 0.4, 0.6, 0.8], I=1000,
return
data
return
data
def
averageOfLast
(
data
,
Actions
,
r
=
0
,
lastIterations
=
100
):
def
averageOfLast
(
data
,
Actions
,
r
=
0
,
lastIterations
=
100
):
sum
=
0
sum
=
0
action_counter
=
{}
action_counter
=
{
action
:
0
for
action
in
Actions
}
for
i
in
range
(
-
1
,
-
lastIterations
-
1
,
-
1
):
for
i
in
range
(
-
1
,
-
lastIterations
-
1
,
-
1
):
sum
+=
np
.
sum
(
data
[
i
,
r
]
*
Actions
)
sum
+=
np
.
sum
(
data
[
i
,
r
]
*
Actions
)
for
a
in
Actions
:
for
a
in
range
(
len
(
Actions
))
:
action_counter
[
a
]
+=
data
[
i
,
r
,
a
]
/
lastIterations
action_counter
[
Actions
[
a
]
]
+=
data
[
i
,
r
,
a
]
/
lastIterations
return
(
sum
/
100
,
action_counter
)
return
(
sum
/
100
,
action_counter
)
def
graph_kp3d
(
Actions
,
Klist
=
[
2
],
Plist
=
[
0.2
,
0.5
,
0.8
],
repeat
=
1
):
def
graph_kp3d
(
Actions
,
Klist
=
[
2
,
4
],
Plist
=
[
0.2
,
0.5
,
0.8
],
repeat
=
1
):
K
=
Klist
K
=
Klist
P
=
Plist
P
=
Plist
...
@@ -194,7 +193,8 @@ def graph_kp3d(Actions, Klist=[2], Plist=[0.2, 0.5, 0.8], repeat=1):
...
@@ -194,7 +193,8 @@ def graph_kp3d(Actions, Klist=[2], Plist=[0.2, 0.5, 0.8], repeat=1):
for
k
in
range
(
len
(
K
)):
for
k
in
range
(
len
(
K
)):
for
p
in
range
(
len
(
P
)):
for
p
in
range
(
len
(
P
)):
data
=
rep
(
repeat
,
K
=
K
[
k
],
P
=
P
[
p
])
# Specify other params by adding here or change default of rep
data
=
rep
(
repeat
,
K
=
K
[
k
],
P
=
P
[
p
])
# Specify other params by adding here or change default of rep
meanA
[
k
][
p
]
=
averageOfLast
(
data
,
Actions
,
lastIterations
=
100
)
# Doing the first round only -- for now
meanA
[
k
][
p
]
=
averageOfLast
(
data
,
Actions
,
lastIterations
=
100
)[
0
]
# Doing the first round only -- for now
print
(
"
k, p, mean
"
,
k
,
p
,
meanA
[
k
][
p
])
P
,
K
=
np
.
meshgrid
(
P
,
K
)
P
,
K
=
np
.
meshgrid
(
P
,
K
)
...
@@ -208,6 +208,35 @@ def graph_kp3d(Actions, Klist=[2], Plist=[0.2, 0.5, 0.8], repeat=1):
...
@@ -208,6 +208,35 @@ def graph_kp3d(Actions, Klist=[2], Plist=[0.2, 0.5, 0.8], repeat=1):
plt
.
show
()
plt
.
show
()
def
stackBar_alpha
(
r
,
Actions
,
alphaList
,
repeat
=
1
):
# Plotting the data for round r
A
=
len
(
Actions
)
p
=
[]
count
=
np
.
zeros
((
A
,
len
(
alphaList
)))
# of each action in each iter
ind
=
np
.
arange
(
len
(
alphaList
))
width
=
0.3
for
al
in
range
(
len
(
alphaList
)):
data
=
rep
(
repeat
,
Actions
=
Actions
,
alpha
=
alphaList
[
al
])
action_counter
=
averageOfLast
(
data
,
Actions
,
r
,
100
)[
1
]
for
a
in
range
(
A
):
count
[
a
,
al
]
=
action_counter
[
Actions
[
a
]]
base
=
0
for
a
in
range
(
A
):
p
.
append
(
plt
.
bar
(
ind
,
count
[
a
],
width
,
bottom
=
base
,
color
=
str
(
0.9
-
0.9
*
Actions
[
a
])))
base
+=
count
[
a
]
plt
.
ylabel
(
'
Number of Actions
'
)
plt
.
xlabel
(
'
Alpha, the loss fraction
'
)
plt
.
title
(
'
Average Number of Actions in Round
'
+
str
(
r
+
1
))
plt
.
xticks
(
ind
,
alphaList
)
# plt.yticks(np.arange(0, 81, 10))
plt
.
legend
(
tuple
([
p
[
x
][
0
]
for
x
in
range
(
A
)][::
-
1
]),
tuple
(
Actions
[::
-
1
]),
loc
=
'
lower left
'
)
plt
.
show
()
def
main
():
def
main
():
# Read-in or Define Parameters
# Read-in or Define Parameters
...
@@ -226,19 +255,22 @@ def main():
...
@@ -226,19 +255,22 @@ def main():
Graph1: Number of Actions of Round r (start by 0) by Iteration
Graph1: Number of Actions of Round r (start by 0) by Iteration
"""
"""
# Repeat game and get the averaged data
# Repeat game and get the averaged data
RepeatTimes
=
30
#
RepeatTimes = 30
data
=
rep
(
RepeatTimes
,
N
,
R
,
K
,
P
,
Actions
,
I
,
RF
,
alpha
)
#
data = rep(RepeatTimes, N, R, K, P, Actions, I, RF, alpha)
#
for
r
in
range
(
R
):
#
for r in range(R):
stackPlot
(
data
,
r
,
Actions
,
I
,
"
Fully-Mixed Graph
"
)
#
stackPlot(data, r, Actions, I, "Fully-Mixed Graph")
"""
"""
Graph2: Average contribution by K, P
Graph2: Average contribution by K, P
"""
"""
graph_kp3d
(
Actions
)
# graph_kp3d(Actions)
"""
Graph3: Actions by different alpha value
"""
stackBar_alpha
(
0
,
Actions
,
alphaList
=
[
0
,
0.2
,
0.4
,
0.5
,
0.6
,
0.7
,
0.8
,
0.9
,
1
])
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
main
()
main
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
graph.py
+
33
−
22
View file @
2cbe44dc
...
@@ -65,6 +65,17 @@ class Graph:
...
@@ -65,6 +65,17 @@ class Graph:
select
=
[]
select
=
[]
selectedNodes
=
{
i
:
0
for
i
in
range
(
self
.
N
)}
selectedNodes
=
{
i
:
0
for
i
in
range
(
self
.
N
)}
if
self
.
K
==
self
.
N
-
1
:
#Well-mixed graph
permutation
=
np
.
random
.
permutation
(
self
.
N
)
selectedNodes
=
{
i
:
1
for
i
in
range
(
self
.
N
)}
if
self
.
N
%
2
==
1
:
extraNode
=
np
.
random
.
randint
(
0
,
self
.
N
)
while
extraNode
==
permutation
[
self
.
N
-
1
]:
extraNode
=
np
.
random
.
randint
(
0
,
self
.
N
)
np
.
append
(
permutation
,
extraNode
)
selectedNodes
[
extraNode
]
+=
1
select
=
permutation
.
reshape
((
int
(
len
(
permutation
)
/
2
),
2
))
else
:
while
edges
:
# Loop when edges is not empty
while
edges
:
# Loop when edges is not empty
i
,
j
=
edges
[
np
.
random
.
randint
(
0
,
len
(
edges
))]
i
,
j
=
edges
[
np
.
random
.
randint
(
0
,
len
(
edges
))]
# print("selected nodes:", i, j)
# print("selected nodes:", i, j)
...
...
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