Permalink
Browse files

...

  • Loading branch information...
psawaya committed May 4, 2010
1 parent 9a333b2 commit 7b7ce5ec28d42f7de75b48fa8e8a18d64558eea0
Showing with 50 additions and 14 deletions.
  1. +24 −2 Server/db.py
  2. +14 −6 Server/dpgp.py
  3. +5 −3 Server/static/gp_brain.js
  4. +5 −1 Server/static/pushgp.js
  5. +2 −2 Server/templates/worker.html
View
@@ -1,22 +1,44 @@
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")
def getProblems():
c=getConnection()
return [problem for problem in c.query("SELECT * FROM problems")]
+
+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:
View
@@ -27,15 +27,22 @@ def post(self):
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?')
settings = {"static_path": os.path.join(os.path.dirname(__file__), "static") }
@@ -45,7 +52,8 @@ def post(self):
(r"/new_problem", UploadFormHandler),
(r"/gp_worker([0-9]+)\.js", WorkerJSHandler),
(r"/worker([0-9]+)", WorkerHandler),
- (r"/uploadresults", ResultsUploadHandler)
+ (r"/uploadresults", ResultsUploadHandler),
+ (r"/requestprograms([0-9]+)\&num_programs\=([0-9]+)", RequestProgramsHandler)
], **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) {
@@ -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 7b7ce5e

Please sign in to comment.