This repository was archived by the owner on Mar 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
307 lines (270 loc) · 10.1 KB
/
Copy pathcontroller.py
File metadata and controls
307 lines (270 loc) · 10.1 KB
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
from fastapi import File, UploadFile, HTTPException, Depends
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
from fastapi import APIRouter
from models import *
from runner import Runner
from gpRunner import GpRunner
from mlRunner import MLRunner
from validator import validateRunAlgoRequest
from config import ParamsList
import uuid
import os
import pickle
apiRouter = APIRouter(prefix="/api")
backend_url = os.getenv("BASE_URL", "http://localhost:8000") + "/api"
paramsList = ParamsList()
@apiRouter.get(
"/test",
summary="API Healthcheck",
description="Returns a simple message to test the API Health.",
)
async def root():
return JSONResponse(
status_code=200,
content=jsonable_encoder({"message": "Hello World"}),
)
@apiRouter.get(
"/validParams",
summary="Endpoint to fetch valid parameters",
description="Returns the valid parameters for the algorithm.",
)
async def algorithm():
return JSONResponse(
status_code=200,
content=jsonable_encoder(
{
"message": "Successfully fetched valid params",
"algorithms": paramsList.algorithm,
"individual": paramsList.individual,
"populationFunction": paramsList.populationFunction,
"evaluationFunction": paramsList.evaluationFunction,
"crossoverFunction": paramsList.crossoverFunction,
"mutationFunction": paramsList.mutationFunction,
"selectionFunction": paramsList.selectionFunction,
}
),
)
@apiRouter.post(
"/runAlgo",
dependencies=[Depends(validateRunAlgoRequest)],
summary="Endpoint to run the algorithm",
description="Accepts the parameters required to run the algorithm and returns the results.",
)
async def runAlgo(runAlgoModel: RunAlgoModel):
runner = Runner(id=str(uuid.uuid4()))
runner.create(
individual=runAlgoModel.individual,
populationFunction=runAlgoModel.populationFunction,
evaluationFunction=runAlgoModel.evaluationFunction,
weights=runAlgoModel.weights,
individualSize=runAlgoModel.individualSize,
indpb=runAlgoModel.indpb,
randomRange=runAlgoModel.randomRange,
crossoverFunction=runAlgoModel.crossoverFunction,
mutationFunction=runAlgoModel.mutationFunction,
selectionFunction=runAlgoModel.selectionFunction,
tournamentSize=3,
)
log, hof = runner.run(
algorithm=runAlgoModel.algorithm,
populationSize=runAlgoModel.populationSize,
generations=runAlgoModel.generations,
cxpb=runAlgoModel.cxpb,
mutpb=runAlgoModel.mutpb,
mu=runAlgoModel.mu,
lambda_=runAlgoModel.lambda_,
N=runAlgoModel.individualSize,
hofSize=runAlgoModel.hofSize,
)
print("Best individual is: %s\nwith fitness: %s" % (hof[0], hof[0].fitness))
runner.code.write("\tprint(f'Best individual is: {hof[0]}\\nwith fitness: {hof[0].fitness}')")
runner.code.write("\n\n")
hofSerializable = [
{
"individual": list(ind),
"fitness": ind.fitness.values if ind.fitness else None,
}
for ind in hof
]
runner.createPlots(log)
runner.code.write("\n\n")
runner.code.write("if __name__ == '__main__':\n")
runner.code.write("\tmain()")
runner.code.close()
gen, avg, min_, max_ = log.select("gen", "avg", "min", "max")
return JSONResponse(
status_code=200,
content=jsonable_encoder(
{
"message": "Run Algorithm",
"runId": runner.id,
"data": {
"generation": gen,
"average": avg,
"minimum": min_,
"maximum": max_,
},
"plots": {
"fitnessPlot": f"{backend_url}/plots/{runner.id}/fitness_plot.png",
"mutationCrossoverEffectPlot": f"{backend_url}/plots/{runner.id}/mutation_crossover_effect.png",
},
"code": f"{backend_url}/code/{runner.id}.py",
"population": f"{backend_url}/population/{runner.id}/population.pkl",
"hallOfFame": hofSerializable,
}
),
)
@apiRouter.post(
"/runGpAlgo",
summary="Endpoint to run GP algorithm",
description="Accepts the parameters required to run the algorithm and returns the results.",
)
async def runGpAlgo(runGpAlgoModel: RunGpAlgoModel):
runner = GpRunner(id=str(uuid.uuid4()))
runner.addPrimitives(runGpAlgoModel.operators)
runner.addEphemeralConstant()
runner.renameArguments(arg_names=runGpAlgoModel.argNames)
runner.create(
# individualType=runGpAlgoModel.individualType,
expr=runGpAlgoModel.expr,
min_=runGpAlgoModel.min_,
max_=runGpAlgoModel.max_,
realFunction=runGpAlgoModel.realFunction,
individualFunction=runGpAlgoModel.individualFunction,
populationFunction=runGpAlgoModel.populationFunction,
selectionFunction=runGpAlgoModel.selectionFunction,
tournamentSize=runGpAlgoModel.tournamentSize,
expr_mut=runGpAlgoModel.expr_mut,
crossoverFunction=runGpAlgoModel.crossoverFunction,
terminalProb=runGpAlgoModel.terminalProb,
mutationFunction=runGpAlgoModel.mutationFunction,
mutationMode=runGpAlgoModel.mutationMode,
mateHeight=runGpAlgoModel.mateHeight,
mutHeight=runGpAlgoModel.mutHeight,
weights=runGpAlgoModel.weights,
expr_mut_min=runGpAlgoModel.expr_mut_min,
expr_mut_max=runGpAlgoModel.expr_mut_max,
)
exitCode = runner.run(
algorithm=runGpAlgoModel.algorithm,
populationSize=runGpAlgoModel.populationSize,
generations=runGpAlgoModel.generations,
cxpb=runGpAlgoModel.cxpb,
mutpb=runGpAlgoModel.mutpb,
mu=runGpAlgoModel.mu,
lambda_=runGpAlgoModel.lambda_,
N=runGpAlgoModel.individualSize,
hofSize=runGpAlgoModel.hofSize,
)
if exitCode != 0:
return JSONResponse(
status_code=500,
content=jsonable_encoder(
{
"message": "Failed to run the algorithm. Please check the logs."
}
),
)
return JSONResponse(
status_code=200,
content=jsonable_encoder(
{
"message": "Run Algorithm",
"runId": runner.id,
"bestFitness": f"{backend_url}/gp/{runner.id}/best.txt",
"code": f"{backend_url}/gp/{runner.id}/code.py",
"logs": f"{backend_url}/gp/{runner.id}/logbook.txt",
"plots": {
"treePlot": f"{backend_url}/gp/{runner.id}/graph.png",
},
# "population": f"{backend_url}/population/{runner.id}/population.pkl",
}
),
)
# NOTE TO DEVELOPERS : USE SWAGGER UI TO test this endpoint. {BASE_URL}/docs
@apiRouter.post(
"/unpickleFile/",
response_model=UnpickleFileModel,
summary="Unpickle File and Return Data",
description="Accepts a pickle file upload, unpickles it, and returns the data as JSON.",
)
async def upload_file(
file: UploadFile = File(..., description="Upload a pickled (.pkl) file")
):
try:
contents = await file.read()
data = pickle.loads(contents)
return {"data": data}
except pickle.UnpicklingError:
raise HTTPException(
status_code=400, detail="Failed to unpickle file. Invalid file format."
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@apiRouter.post(
"/runMlAlgo",
response_model=MlModel,
summary="Run ML Algo to optimize hyperparameters with Genetic Algorithm",
description="Accepts the parameters required to run the algorithm and returns the results.",
)
async def runMlAlgo(mlModel: MlModel):
try:
runner = MLRunner(id=str(uuid.uuid4()),
sep=mlModel.sep,
mlImportCodeString=mlModel.mlImportCodeString,
evalFunctionCodeString=mlModel.mlEvalFunctionCodeString,)
runner.create(
indpb=mlModel.indpb,
crossoverFunction=mlModel.crossoverFunction,
mutationFunction=mlModel.mutationFunction,
selectionFunction=mlModel.selectionFunction,
tournamentSize=mlModel.tournamentSize,
)
exitCode = runner.run(
algorithm=mlModel.algorithm,
googleDriveUrl=mlModel.googleDriveUrl,
targetColumnName=mlModel.targetColumnName,
weights=mlModel.weights,
populationSize=mlModel.populationSize,
generations=mlModel.generations,
cxpb=mlModel.cxpb,
mutpb=mlModel.mutpb,
mu=mlModel.mu,
lambda_=mlModel.lambda_,
hofSize=mlModel.hofSize,
)
if exitCode == 0:
return JSONResponse(
status_code=200,
content=jsonable_encoder(
{
"message": "Run Algorithm",
"runId": runner.id,
"code": f"{backend_url}/ml/{runner.id}/code.py",
"best": f"{backend_url}/ml/{runner.id}/best.txt",
"logbook": f"{backend_url}/ml/{runner.id}/logbook.txt",
"plots": {
"fitnessPlot": f"{backend_url}/ml/{runner.id}/fitness_plot.png",
},
}
),
)
else:
return JSONResponse(
status_code=500,
content=jsonable_encoder(
{
"message": "Failed to run the algorithm. Please check the logs."
}
),
)
except Exception as e:
return JSONResponse(
status_code=500,
content=jsonable_encoder(
{
"message": f"Failed to run the algorithm. Please check the logs. Error: {str(e)}"
}
),
)