Permalink
Browse files

add admin stuff

  • Loading branch information...
2 parents a31ba4c + 7b7ce5e commit 31bc08f6c50ae854c0f1eb87a31a06f6ff7d7e2e Colin Roache committed May 4, 2010
View
@@ -1,7 +1,11 @@
import tornado.database as database
+from uuid import uuid4
import json
+def getFreshUUID():
+ return uuid4().hex
+
def getConnection():
return database.Connection("/tmp/mysql.sock", user="root", database="dpgpjs")
@@ -35,13 +39,32 @@ def updateProblem(problem_id,name,comments,start_population,max_population,tourn
c=getConnection()
return c.execute("UPDATE problems SET name = \"%s\", comments = \"%s\", start_population = \"%s\", max_population = \"%s\", tournament_size = \"%s\", crossover_probability = \"%s\", mutation_probability = \"%s\", clone_probability = \"%s\" WHERE id = %s LIMIT 1" % (name, comments, start_population,max_population,tournament_size,crossover_probability,mutation_probability, clone_probability, problem_id))
+
+def getProgramsForProblem(problem_id,num_programs):
+ c=getConnection()
+
+ sql = "SELECT * FROM programs WHERE problem = %s ORDER BY fitness,RAND() DESC LIMIT 0,%s" % (problem_id,num_programs)
+
+ results = c.query(sql)
+
+ return [result['program_string'] for result in results]
+
def getFitnessCases(problem_id):
c=getConnection()
return [case for case in c.query("SELECT * FROM fitness_cases WHERE problem_id = %s" % problem_id) ]
-def storeWorkerResults(problem_id,program,program_fitness):
- pass
+def storeWorkerResults(problem_id,program,program_fitness,client_id):
+ c=getConnection()
+
+ print problem_id
+ print program
+ print program_fitness
+ print client_id
+
+ sql = "INSERT INTO programs (`problem`,`program_string`,`fitness`,`client_id`) VALUES (%s,\'%s\',%s,\'%s\')" % (problem_id,program.replace('%','%%'),program_fitness,client_id)
+
+ c.execute(sql)
def importProblemJSON(json_txt):
try:
@@ -59,6 +82,4 @@ def importProblemJSON(json_txt):
for case in fitness_cases[0]:
initializer = "( %s x INTEGER.DEFINE )" % case['x1'] #for now, assume int stack and only 1 x value.
- c.execute("INSERT INTO fitness_cases (`problem_id`,`initializer`, `ans`) VALUES (%s, \'%s\', \'%s\')" % (rowid, initializer, case['ans']))
-
- """" { "name": "Symbolic Regression Test", "comments" : "y=2x", "evaluated_stack" : "int", "start_population" : 20, "max_population" : 75, "tournament_size" : 5, "crossover_probability" : 10, "mutation_probability" : 80, "clone_probability" : 10, "fitness_cases" : [ [ { "x1" : 2, "ans" : 4 }, { "x1" : 3, "ans" : 6 }, { "x1" : 4, "ans" : 8 }, { "x1" : -1, "ans" : -2 }, { "x1" : -2, "ans" : -4 }, { "x1" : -3, "ans" : -6 } ] ] }"""
+ c.execute("INSERT INTO fitness_cases (`problem_id`,`initializer`, `ans`) VALUES (%s, \'%s\', \'%s\')" % (rowid, initializer, case['ans']))
View
@@ -10,39 +10,37 @@
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("templates/main.html", problems=db.getProblems())
-
-class UploadHandler(tornado.web.RequestHandler):
- def get(self):
- self.render("templates/upload.html")
-
-class UploadFormHandler(tornado.web.RequestHandler):
- # TODO: after git merge, switch upload to admin
- def get(self):
- self.render("templates/error.html")
-
- def post(self):
- if db.importProblemJSON(self.get_argument('config_json')) == False:
- self.write ('Invalid JSON.')
- else:
- self.redirect("/")
class WorkerHandler(tornado.web.RequestHandler):
def get(self,worker_id):
- self.render("templates/worker.html", worker_id = worker_id)
+ self.render("templates/worker.html", worker_id = worker_id, client_id = db.getFreshUUID())
class WorkerJSHandler(tornado.web.RequestHandler):
- def get(self,worker_id):
- self.render("templates/gp_worker.js", fitness_cases=db.getFitnessCases(worker_id))
+ def get(self,problem_id):
+ self.render("templates/gp_worker.js", fitness_cases=db.getFitnessCases(problem_id))
class ResultsUploadHandler(tornado.web.RequestHandler):
def post(self):
- # uploadedData = json.loads(self.ge)
- print self.request.arguments
+ uploadedData = json.loads(self.request.body)
+ # print uploadedData
+ db.storeWorkerResults(uploadedData['problem_id'],uploadedData['program']['code'],uploadedData['program']['fitness'],uploadedData['client_id'])
+
+class RequestProgramsHandler(tornado.web.RequestHandler):
+ def get(self,problem_id,num_programs):
+ self.write(str(db.getProgramsForProblem(problem_id,num_programs)))
+ # self.write('hi?')
class AdminHandler(tornado.web.RequestHandler):
def get(self):
self.render("templates/admin/index.html",problems=db.getProblems())
-
+class AdminUploadHandler(tornado.web.RequestHandler):
+ def get(self):
+ self.render("templates/admin/upload.html")
+ def post(self):
+ if db.importProblemJSON(self.get_argument('config_json')) == False:
+ self.write ('Invalid JSON.')
+ else:
+ self.redirect("/")
class AdminEditHandler(tornado.web.RequestHandler):
def get(self,problem_id):
self.render("templates/admin/edit.html",problem=db.getProblem(problem_id))
@@ -63,12 +61,12 @@ def post(self,problem_id):
application = tornado.web.Application([
(r"/", MainHandler),
- (r"/upload/", UploadHandler),
- (r"/new_problem", UploadFormHandler),
(r"/gp_worker([0-9]+)\.js", WorkerJSHandler),
(r"/worker([0-9]+)", WorkerHandler),
(r"/uploadresults", ResultsUploadHandler),
+ (r"/requestprograms([0-9]+)\&num_programs\=([0-9]+)", RequestProgramsHandler),
(r"/admin", AdminHandler),
+ (r"/admin/upload/", AdminUploadHandler),
(r"/admin/edit([0-9]+)", AdminEditHandler)
], **settings)
@@ -12,7 +12,7 @@ if (!runInWorker) {
function updateStatus() {};
}
-function startGPBrain(worker_id)
+function startGPBrain(problem_id,client_id)
{
// testGP();
// start();
@@ -25,8 +25,10 @@ function startGPBrain(worker_id)
return;
}
else if (a.data['msgtype'] == 'uploadProgram'){
- a.data['msg']['id']=worker_id;
+ a.data['msg']['problem_id']=problem_id;
+ a.data['msg']['client_id']=client_id;
$.ajax({'url':'uploadresults', 'type':'POST', 'data':JSON.stringify(a.data['msg'])});
+ console.log(JSON.stringify(a.data['msg']));
console.log("about to upload program!");
console.log(a.data['msg']);
return;
@@ -54,7 +56,7 @@ function startGPBrain(worker_id)
// var worker = JsWorker.createWorkerFromUrl("gp_worker.js", onMessage);
if (runInWorker) {
- var worker = new Worker('gp_worker' + worker_id +'.js');
+ var worker = new Worker('gp_worker' + problem_id +'.js');
worker.onmessage = onMessage;
worker.onerror = function (err) {alert ('Error! ' + err);};
View
@@ -108,8 +108,12 @@ PushGP.prototype.doGeneration = function() {
}
else
this.generationsSinceLastImprovement++;
- if(this.generationsSinceLastImprovement > this.reportBestAfter)
+ if(this.generationsSinceLastImprovement > this.reportBestAfter) {
+
+ workerDebug("first: " + this.pushPrograms[0]['code']);
+ workerDebug("last: " + this.pushPrograms[this.pushPrograms.length-1]['code']);
reportUp({'msgtype' : 'uploadProgram', 'msg' : {'program':this.pushPrograms[0], 'generationCount':this.generations}});
+ }
};
PushGP.prototype.fitnessEvaluate = function(arrayOfPrograms) {
@@ -8,6 +8,10 @@
</head>
+<div style="padding:5px">
+ <a href="/admin/upload/">Upload</a> a new problem.
+</div>
+
<body>
{% for problem in problems %}
<div style="border-style:solid;border-width:2px;padding:3px">
@@ -5,7 +5,7 @@
<h1>Use this page to upload a new problem.</h1>
<div>
- <form action="/new_problem" method="POST">
+ <form action="/admin/upload/" method="POST">
<textarea style="width:80%;height:30%" name="config_json"></textarea>
<div><input type="submit" value="Create"></div>
@@ -5,9 +5,6 @@
<h1>Distributed Push.</h1>
-<div style="padding:5px">
- <a href="upload/">Upload</a> a new problem.
-</div>
<div style="padding:10px">
{% for problem in problems %}
@@ -13,12 +13,12 @@
<script src="static/gp_brain.js"></script>
<script>
-
+
$(document).ready (
function()
{
$('#gpChart').hide();
- startGPBrain({{ worker_id }});
+ startGPBrain({{ worker_id }},"{{ client_id }}");
});

0 comments on commit 31bc08f

Please sign in to comment.