-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcogwatch.py
175 lines (123 loc) · 3.37 KB
/
cogwatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import random
from cog import Cog
class Populous():
Population = []
def max(cog, comm):
return max([c for c in Populous.Population if c != cog], key=lambda c: c.i[comm])
pass ## return cog with max of this commodity that ISNT the same cog that asked
class Farmer(Cog):
def __init__(self):
Cog.__init__(self)
self.name = "farmer "+self.name
self.employer = None
def live_day(self):
if self.employer != None:
relevant_i = self.employer.i
else:
relevant_i = self.i
if relevant_i["salt"] < relevant_i["cybrous"]:
comm = "salt"
else:
comm = "cybrous"
v = self.mine(comm)
if self.employer != None:
print(v, comm)
self.trade(self.employer, (v, comm, 3, "dollars"))
Cog.live_day(self)
class Bouj(Cog):
def __init__(self):
Cog.__init__(self)
self.name = "mr "+self.name
def live_day(self):
## buy from richest
for comm in ["salt", "cybrous"]:
cog_max = max(Populous.Population, key=lambda c: c.i[comm])
amount = 6
price_per_unit = 1
terms = (price_per_unit * amount, "dollars", amount, comm)
self.trade(cog_max, terms)
## sell to poorest
for comm in ["salt", "cybrous"]:
cog_min = min(Populous.Population, key=lambda c: c.i[comm])
amount = 2
price_per_unit = 5
terms = (amount, comm, price_per_unit * amount, "dollars")
self.trade(cog_max, terms)
Cog.live_day(self)
class Bossman(Cog):
def __init__(self):
Cog.__init__(self)
self.name = "boss "+self.name
self.workers = []
def hire(self, cog):
self.workers.append(cog)
cog.employer = self
cog.name += "*"
def live_day(self):
for comm in ["salt", "cybrous"]:
for i in range(10):
if self.i[comm] <= 2: break
cog_min = min(Populous.Population, key=lambda c: c.i[comm])
amount = 1
price_per_unit = 5
terms = (amount, comm, price_per_unit * amount, "dollars")
self.trade(cog_min, terms)
Cog.live_day(self)
class Church(Cog):
def __init__(self):
Cog.__init__(self)
self.name = "Church"
def live_day(self):
if self.dead: return
self.i["faith"] = 1000
for cog in Populous.Population:
if cog.i["dollars"] >= 10:
self.trade(cog, (4, "faith", int(cog.i["dollars"]*0.1), "dollars"))
for comm in ["salt","cybrous"]:
if cog.i[comm] <= 9:
self.trade(cog, (8, comm, 0, "dollars"))
poorest_cog = min([cog for cog in Populous.Population], key=lambda x: x.i["dollars"])
self.trade(poorest_cog, (20, "dollars", 0, "dollars"))
self.i["dollars"] -= 1
if self.i["dollars"] <= 0: self.die()
def episode(debug=False):
Populous.Population = []
pop = Populous.Population
if random.random() >= 0.5:
fiu = Farmer()
fiu.name = "farmer fiu"
else:
fiu = Bouj()
fiu.name = "mr fiu"
pop.append(fiu)
for i in range(15):
pop.append(Farmer())
for i in range(5):
pop.append(Bouj())
b = Bossman()
pop.append(b)
b.hire(pop[1])
church = Church()
pop.append(church)
for i in range(30):
if debug:
print()
for c in pop:
print(c, c.i)
print()
for cog in pop:
cog.live_day()
if all([cog.dead for cog in pop]): break
return len([cog for cog in pop if not cog.dead])
print(episode(True))
# bn = []
# bs = 0
# iters = 1000
# for i in range(iters):
# print(bs, i/iters, end="\r")
# s, n = episode()
# if s > bs:
# bs = s
# bn = n
# # print(episode(True))
# print(bs, bn)