-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgae_lib.py
285 lines (239 loc) · 7.95 KB
/
gae_lib.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
#! /usr/bin/env python
import httplib
import json
import sys
from subprocess import call
from gae_config import gae_config
import numpy as np
PENALTY_VALUE = sys.float_info.max
class Job():
def __init__(self, **entries):
self.jobId = 0
self.iteration = 0
self.vmIp = None
self.params = []
self.result = None
self.finished = False
self.sent = 0
self.__dict__.update(entries)
@staticmethod
def serialize(obj):
return obj.__dict__
def __repr__(self):
return str(self.__dict__)
class VM(dict):
def __init__(self, **entries):
self.ip = ''
self.vmtype = ''
self.dateUpdate = ''
self.__dict__.update(entries)
@staticmethod
def serialize(obj):
return obj.__dict__
def __repr__(self):
return str(self.__dict__)
def pop2Jobs(opt):
jobs = []
i = 0
for params in opt.new_pop:
i += 1
job = Job(jobId=i, params=params.tolist(), iteration=opt.cur_iter+1)
jobs.append(job)
return jobs
def restoreCurrentPop(popHolder, cur_iter, throw=False):
""" GET current working population """
try:
connection = httplib.HTTPConnection(gae_config.getServerURL())
connection.request('GET', '/get/pop/')
result = connection.getresponse()
data = result.read()
if result.status == 200:
decoded = json.loads(data)
print 'Received working population: %d' % len(decoded['pop'])
popHolder.pop = np.array(decoded['pop'])
popHolder.vals = np.array(decoded['vals'])
popHolder.cur_iter = cur_iter
#set best individual
best_ix = np.argmin(popHolder.vals)
popHolder.best_x = popHolder.pop[best_ix, :].copy()
popHolder.best_y = popHolder.vals[best_ix].copy()
return True
else:
raise Exception("ERROR http status = "+str(result.status))
except Exception as ex:
if throw:
raise ex
else:
print ex
finally:
connection.close()
return False
def putPop(opt):
""" Update current working population """
try:
connection = httplib.HTTPConnection(gae_config.getServerURL())
body_content = json.dumps({
'pop': opt.pop.tolist(),
'vals': opt.vals.tolist()
}, indent=2)
headers = {"User-Agent": "python-httplib"}
connection.request('PUT', '/put/pop/', body_content, headers)
result = connection.getresponse()
if result.status == 200:
print 'PUT pop OK - HTTP 200'
return True
else:
print result.status
except Exception as ex:
print ex
finally:
connection.close()
return False
def getJobs(throw=False):
# GET jobs
jobs = []
try:
connection = httplib.HTTPConnection(gae_config.getServerURL())
connection.request('GET', '/get/jobs/')
result = connection.getresponse()
data = result.read()
if result.status == 200:
decoded = json.loads(data)
if decoded.has_key('jobs'):
count_jobs = len(decoded['jobs'])
print 'count jobs: '+str(count_jobs)
for job in decoded['jobs']:
temp = Job(**job)
jobs.append(temp)
# print job
else:
raise Exception("ERROR http status = "+str(result.status))
except Exception as ex:
if throw:
raise ex
else:
print ex
finally:
connection.close()
return jobs
def getNextJob():
# GET single job
job = None
try:
connection = httplib.HTTPConnection(gae_config.getServerURL())
connection.request('GET', '/get/job/')
result = connection.getresponse()
data = result.read()
if result.status == 200:
decoded = json.loads(data)
if decoded.has_key('jobs'):
count_jobs = len(decoded['jobs'])
print 'count jobs: '+str(count_jobs)
for j in decoded['jobs']:
job = Job(**j)
# print job
break
else:
print "ERROR http status = "+str(result.status)
except Exception as ex:
print ex
finally:
connection.close()
return job
def getVMs():
vms = []
try:
connection = httplib.HTTPConnection(gae_config.getServerURL())
connection.request('GET', '/get/vms/')
result = connection.getresponse()
data = result.read()
if result.status == 200:
decoded = json.loads(data)
if decoded.has_key('vms'):
count_vms = len(decoded['vms'])
print 'count vms: '+str(count_vms)
for vm in decoded['vms']:
temp = VM(**vm)
vms.append(temp)
# print vm
else:
print "ERROR http status = "+str(result.status)
except Exception as ex:
print ex
finally:
connection.close()
return vms
def putJobs(jobs):
# HTTP PUT Job's
try:
connection = httplib.HTTPConnection(gae_config.getServerURL())
body_content = json.dumps({ 'jobs': jobs}, indent=2, default=Job.serialize)
headers = {"User-Agent": "python-httplib"}
connection.request('PUT', '/put/jobs/', body_content, headers)
result = connection.getresponse()
if result.status == 200:
print 'PUT jobs OK - HTTP 200'
return True
else:
print result.status
except:
pass
finally:
connection.close()
return False
def putJob(job):
# HTTP PUT Job
try:
connection = httplib.HTTPConnection(gae_config.getServerURL())
body_content = json.dumps({ 'jobs': [job] }, indent=2, default=Job.serialize)
headers = {"User-Agent": "python-httplib"}
connection.request('PUT', '/put/job/', body_content, headers)
result = connection.getresponse()
if result.status == 200:
print 'PUT jobs OK - HTTP 200'
return True
else:
print result.status
except:
pass
finally:
connection.close()
return False
def putVMs(vms):
# HTTP PUT VM's
try:
connection = httplib.HTTPConnection(gae_config.getServerURL())
body_content = json.dumps({ 'vms': vms}, indent=2)
headers = {"User-Agent": "python-httplib"}
connection.request('PUT', '/put/vms/', body_content, headers)
result = connection.getresponse()
if result.status == 200:
print 'PUT vms OK - HTTP 200'
return True
else:
print result.status
except:
pass
finally:
connection.close()
return False
def createVMs(popSize):
return True #TODO
if __name__ == '__main__':
#testing
getJobs()
getVMs()
assert putJobs([
Job(**{'params': np.random.random_sample(2).tolist(), 'finished': False, 'jobId': 1, 'result': None, 'vmIp': None}),
Job(**{'params': np.random.random_sample(2).tolist(), 'finished': False, 'jobId': 2, 'result': None, 'vmIp': None})
])
getJobs()
assert putJobs([
Job(**{'params': np.random.random_sample(2).tolist(), 'finished': False, 'jobId': 1, 'iteration': 1, 'result': None, 'vmIp': None}),
Job(**{'params': np.random.random_sample(2).tolist(), 'finished': False, 'jobId': 2, 'iteration': 1, 'result': None, 'vmIp': None})
])
getJobs()
assert putJob(
Job(**{'params': np.random.random_sample(2).tolist(), 'finished': False, 'jobId': 1, 'result': None, 'vmIp': 'LOCALHOST'})
)
getJobs()