-
Notifications
You must be signed in to change notification settings - Fork 18
/
env.py
312 lines (272 loc) · 11.5 KB
/
env.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from gym import core, spaces
from gym.utils import seeding
import numpy as np
import math
import sys
if sys.version_info[0] >= 3:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk
from .tilemap import TileMap
from .corecontrol import MicropolisControl
else:
import gtk
from tilemap import TileMap
from corecontrol import MicropolisControl
import time
import torch
class MicropolisEnv(core.Env):
def __init__(self, MAP_X=20, MAP_Y=20, PADDING=0):
self.SHOW_GUI=False
self.start_time = time.time()
self.print_map = False
self.num_episode = 0
self.max_step = 1000
self.max_static = 0
self.player_step = False
self.last_reward = 0
#self.setMapSize((MAP_X, MAP_Y), PADDING)
def seed(self, seed=None):
self.np_random, seed1 = seeding.np_random(seed)
# Derive a random seed. This gets passed as a uint, but gets
# checked as an int elsewhere, so we need to keep it below
# 2**31.
seed2 = seeding.hash_seed(seed1 + 1) % 2**31
np.random.seed(seed)
return [seed1, seed2]
def setMapSize(self, size, max_step=None, rank=None, print_map=False, PADDING=0, static_builds=True,
parallel_gui=False, render_gui=False,
empty_start=False, noreward=False):
if max_step is not None:
self.max_step = max_step
self.empty_start = empty_start
if type(size) == int:
self.MAP_X = size
self.MAP_Y = size
else:
self.MAP_X = size[0]
self.MAP_Y = size[1]
self.obs_width = self.MAP_X + PADDING * 2
self.micro = MicropolisControl(self.MAP_X, self.MAP_Y, PADDING, parallel_gui=parallel_gui, rank=rank)
self.static_builds = True
self.win1 = self.micro.win1
self.micro.SHOW_GUI=self.SHOW_GUI
self.num_step = 0
self.minFunds = 5000
self.initFunds = 10000000
self.num_tools = self.micro.num_tools
self.num_zones = self.micro.num_zones
# res, com, ind pop, demand
self.num_scalars = 6
self.num_density_maps = 3
num_user_features = 1 # static builds
# traffic, power, density
self.num_obs_channels = self.micro.map.num_features + self.num_scalars + self.num_density_maps + num_user_features
#ac_low = np.zeros((3))
#ac_high = np.array([self.num_tools - 1, self.MAP_X - 1, self.MAP_Y - 1])
#self.action_space = spaces.Box(low=ac_low, high=ac_high, dtype=int)
self.action_space = spaces.Discrete(self.num_tools * self.MAP_X * self.MAP_Y)
self.last_state = None
self.metadata = {'runtime.vectorized': True}
low_obs = np.full((self.num_obs_channels, self.MAP_X, self.MAP_Y), fill_value=-1)
high_obs = np.full((self.num_obs_channels, self.MAP_X, self.MAP_Y), fill_value=1)
# TODO: can/should we use Tuples of MultiBinaries instead, for greater efficiency?
self.observation_space = spaces.Box(low=low_obs, high=high_obs, dtype = float)
self.state = None
self.intsToActions = {}
self.actionsToInts = np.zeros((self.num_tools, self.MAP_X, self.MAP_Y))
self.mapIntsToActions()
self.last_pop = 0
self.last_num_roads = 0
# self.past_actions = np.full((self.num_tools, self.MAP_X, self.MAP_Y), False)
self.print_map = print_map
self.render_gui = render_gui
self.no_reward = noreward
self.mayor_rating = 50
self.last_mayor_rating = self.mayor_rating
self.last_priority_road_net_size = 0
def mapIntsToActionsChunk(self):
''' Unrolls the action vector into spatial chunks (does this matter empirically?).'''
w0 = 20
w1 = 10
i = 0
for j0 in range(self.MAP_X // w0):
for k0 in range(self.MAP_Y // w0):
for j1 in range(w0 // w1):
for k1 in range(w0 // w1):
for z in range(self.num_tools):
for x in range(j0 * w0 + j1*w1,
j0 * w0 + (j1+1)*w1):
for y in range(k0 * w0 + k1*w1,
k0 * w0 + (k1+1)*w1):
self.intsToActions[i] = [z, x, y]
i += 1
def mapIntsToActions(self):
''' Unrolls the action vector in the same order as the pytorch model
on its forward pass.'''
chunk_width = 1
i = 0
for z in range(self.num_tools):
for x in range(self.MAP_X):
for y in range(self.MAP_Y):
self.intsToActions[i] = [z, x, y]
self.actionsToInts[z, x, y] = i
i += 1
def randomStep(self):
self.step(self.action_space.sample())
def close(self):
self.micro.close()
def randomStaticStart(self):
num_static = 100
lst_epi = 500
# num_static = math.ceil(((lst_epi - self.num_episode) / lst_epi) * num_static)
# num_static = max(0, max_static)
self.micro.setFunds(10000000)
if num_static > 0:
num_static = self.np_random.randint(0, num_static + 1)
for i in range(num_static):
if i % 2 == 0:
static_build = True
else:
static_build = False
self.step(self.action_space.sample(), static_build=True)
def randomStart(self):
r = self.np_random.randint(0, 100)
self.micro.setFunds(10000000)
for i in range(r):
self.step(self.action_space.sample())
# i = np.random.randint(0, (self.obs_width * self.obs_width / 3))
# a = (np.random.randint(0, self.num_tools, i), np.random.randint(0, self.obs_width, i), np.random.randint(0, self.obs_width, i))
# for j in range(i):
# self.micro.takeSetupAction((a[0][j], a[1][j], a[2][j]))
def reset(self):
if True:
#if self.render_gui:
if False:
self.micro.clearBotBuilds()
else:
self.micro.clearMap()
else:
self.micro.newMap()
self.num_step = 0
#self.randomStaticStart()
self.micro.engine.simTick()
self.micro.setFunds(self.initFunds)
#curr_funds = self.micro.getFunds()
curr_pop = self.getPop()
self.state = self.getState()
self.last_pop=0
self.micro.num_roads = 0
self.last_num_roads = 0
#self.past_actions.fill(False)
self.num_episode += 1
self.curr_reward = 0
self.last_reward = 0
return self.state
# def getRoadPenalty(self):
#
# class roadPenalty(torch.nn.module):
# def __init__(self):
# super(roadPenalty, self).__init__()
# self.
def getState(self):
resPop, comPop, indPop = self.micro.getResPop(), self.micro.getComPop(), self.micro.getIndPop()
resDemand, comDemand, indDemand = self.micro.engine.getDemands()
scalars = [resPop, comPop, indPop, resDemand, comDemand, indDemand]
return self.observation(scalars)
def observation(self, scalars):
state = self.micro.map.getMapState()
density_maps = self.micro.getDensityMaps()
road_networks = self.micro.map.road_networks
if self.render_gui:
#print(road_networks, self.micro.map.road_net_sizes)
pass
scalar_layers = np.zeros((len(scalars), self.MAP_X, self.MAP_Y))
for si in range(len(scalars)):
scalar_layers[si].fill(scalars[si])
state = np.concatenate((state, density_maps, scalar_layers), 0)
if self.static_builds:
state = np.concatenate((state, self.micro.map.static_builds), 0)
return state
def getPop(self):
resPop, comPop, indPop = (1/4) * self.micro.getResPop(), \
self.micro.getComPop(), \
self.micro.getIndPop()
curr_pop = resPop + \
comPop + \
indPop
return curr_pop
def getPopReward(self):
resPop, comPop, indPop = (1/4) * self.micro.getResPop(), self.micro.getComPop(), self.micro.getIndPop()
curr_pop = resPop + comPop + indPop
zone_variety = 0
if resPop > 0:
zone_variety += 1
if comPop > 0:
zone_variety += 1
if indPop > 0:
zone_variety += 1
#zone_bonus = (zone_variety - 1) * curr_pop
#curr_pop += zone_bonus
#curr_pop = np.log(resPop + 1) + np.log(comPop + 1) + np.log(indPop + 1)
return curr_pop
def step(self, a, static_build=False):
if self.player_step:
if self.player_step == a:
static_build=False
self.player_step = None
a = self.intsToActions[a]
self.micro.takeAction(a, static_build)
reward = 0
self.curr_pop = self.getPopReward()
self.curr_mayor_rating = self.getRating()
self.state = self.getState()
if not self.no_reward:
reward = self.curr_pop #+ (self.micro.total_traffic / 100)
if reward > 0 and self.micro.map.num_roads > 0: # to avoid one-road minima in early training
max_net = 0
for n in self.micro.map.road_net_sizes.values():
if n > max_net:
max_net = n
#reward += (max_net / self.micro.map.num_roads) * min(100, reward) #the avg reward when roads are introduced to boost res
self.curr_reward = reward - self.last_reward
self.last_reward = reward
#reward += (self.curr_mayor_rating - self.last_mayor_rating)
self.last_mayor_rating = self.curr_mayor_rating
self.last_pop = self.curr_pop
curr_funds = self.micro.getFunds()
bankrupt = curr_funds < self.minFunds
terminal = bankrupt or self.num_step >= self.max_step
if True and self.print_map:
if static_build:
print('STATIC BUILD')
self.printMap()
if self.render_gui:
self.micro.render()
infos = {}
if self.micro.player_builds:
b = self.micro.player_builds[0]
a = self.actionsToInts[b]
infos['player_move'] = int(a)
self.micro.player_builds = self.micro.player_builds[1:]
self.player_step = a
self.num_step += 1
return (self.state, self.curr_reward, terminal, infos)
def getRating(self):
return self.micro.engine.cityYes
def printMap(self, static_builds=True):
if static_builds:
static_map = self.micro.map.static_builds
else:
static_map = None
np.set_printoptions(threshold=np.inf)
zone_map = self.micro.map.zoneMap[-1]
zone_map = np.array_repr(zone_map).replace(', ',' ').replace('],\n', ']\n').replace(',\n', ',').replace(', ', ' ').replace(' ',' ').replace(' ',' ')
print('{}\npopulation: {}, traffic: {}, episode: {}, step: {}, reward: {}\n {}'.format(zone_map, self.curr_pop, self.micro.total_traffic, self.num_episode, self.num_step, self.curr_reward, static_map))
#print(self.micro.map.centers)
def render(self, mode='human'):
self.micro.render()
def test(self):
env = MicropolisEnv()
for i in range(5000):
env.step(env.action_space.sample())