Permalink
Browse files

uploading and downloading work, ready for demo

  • Loading branch information...
psawaya committed May 4, 2010
1 parent 31bc08f commit fa0815d29c5bea608631abf28ff4fd97f2914523
View
@@ -9,7 +9,6 @@ def getFreshUUID():
def getConnection():
return database.Connection("/tmp/mysql.sock", user="root", database="dpgpjs")
def getProblems():
c=getConnection()
problems = [problem for problem in c.query("SELECT * FROM problems")]
@@ -23,6 +22,13 @@ def getProblems():
problem.bestProgram = "none"
return problems
def getGPParams(problem_id):
c=getConnection()
problem = c.query("SELECT * FROM problems WHERE id = %s" % problem_id)[0]
return problem
def getProblem(problem_id):
c=getConnection()
problem = c.query("SELECT * FROM problems WHERE id = %s" % problem_id)[0]
@@ -35,10 +41,10 @@ def getProblem(problem_id):
problem.bestProgram = "none"
return problem
def updateProblem(problem_id,name,comments,start_population,max_population,tournament_size,crossover_probability,mutation_probability,clone_probability):
def updateProblem(problem_id,name,comments,allowed,start_population,max_population,tournament_size,crossover_probability,mutation_probability,clone_probability):
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))
return c.execute("UPDATE problems SET name = \"%s\", comments = \"%s\", allowed = \"%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, allowed, start_population,max_population,tournament_size,crossover_probability,mutation_probability, clone_probability, problem_id))
def getProgramsForProblem(problem_id,num_programs):
c=getConnection()
@@ -47,7 +53,7 @@ def getProgramsForProblem(problem_id,num_programs):
results = c.query(sql)
return [result['program_string'] for result in results]
return '[' + ",".join (["\"%s\"" % result['program_string'] for result in results]) + ']'
def getFitnessCases(problem_id):
c=getConnection()
@@ -76,9 +82,17 @@ def importProblemJSON(json_txt):
fitness_cases = json_obj['fitness_cases']
json_obj['allowed'] = '[' + ",".join (['"%s"' % str(string) for string in json_obj['allowed']]) + ']'
del json_obj['fitness_cases']
rowid = c.execute ("INSERT INTO problems (%s) VALUES (%s)" % (",".join(["`%s`" % x for x in json_obj.keys()]), ",".join([ ["'%s'" % x,str(x)][type(x) != str and type(x) != unicode] for x in json_obj.values()]) ) )
sql = "INSERT INTO problems (%s) VALUES (%s)" % (",".join(["`%s`" % x for x in json_obj.keys()]), ",".join([ ["'%s'" % x,str(x)][type(x) != str and type(x) != unicode] for x in json_obj.values()]) )
sql = sql.replace('%','%%')
print "sql = %s" % sql
rowid = c.execute (sql)
for case in fitness_cases[0]:
initializer = "( %s x INTEGER.DEFINE )" % case['x1'] #for now, assume int stack and only 1 x value.
View
@@ -17,7 +17,7 @@ def get(self,worker_id):
class WorkerJSHandler(tornado.web.RequestHandler):
def get(self,problem_id):
self.render("templates/gp_worker.js", fitness_cases=db.getFitnessCases(problem_id))
self.render("templates/gp_worker.js", fitness_cases=db.getFitnessCases(problem_id), gp_params=db.getGPParams(problem_id))
class ResultsUploadHandler(tornado.web.RequestHandler):
def post(self):
@@ -28,6 +28,7 @@ def post(self):
class RequestProgramsHandler(tornado.web.RequestHandler):
def get(self,problem_id,num_programs):
self.write(str(db.getProgramsForProblem(problem_id,num_programs)))
print "gave programs to client"
# self.write('hi?')
class AdminHandler(tornado.web.RequestHandler):
@@ -47,13 +48,14 @@ def get(self,problem_id):
def post(self,problem_id):
name = self.get_argument('name')
comments = self.get_argument('comments')
allowed_commands = self.get_argument('allowed_commands')
start_population = self.get_argument('start_population')
max_population = self.get_argument('max_population')
tournament_size = self.get_argument('tournament_size')
crossover_probability = self.get_argument('crossover_probability')
mutation_probability = self.get_argument('mutation_probability')
clone_probability = self.get_argument('clone_probability')
db.updateProblem(problem_id,name,comments,start_population,max_population,tournament_size,crossover_probability,mutation_probability,clone_probability)
db.updateProblem(problem_id,name,comments,allowed_commands,start_population,max_population,tournament_size,crossover_probability,mutation_probability,clone_probability)
self.render("templates/admin/edit.html", problem=db.getProblem(problem_id))
View
@@ -8,6 +8,8 @@ var timeStarted = undefined;
var generationFitnesses = [];
var worker = undefined;
if (!runInWorker) {
function updateStatus() {};
}
@@ -46,6 +48,18 @@ function startGPBrain(problem_id,client_id)
}
dumpGPData(a.data['programsStr'],a.data['bestFitness']);
worker.postMessage({'msgtype' : "heartbeat"});
}
else if(a.data['msgtype'] == 'downloadPrograms') {
$.ajax({url:'requestprograms'+problem_id+'&num_programs='+a.data['msg']['numPrograms'],
type:'GET',
dataType:'json',
success:function(json){
console.log("from server:"+json);
// console.log(JSON.parse(text));
worker.postMessage({'msgtype' : "addPrograms", "msg":json});
}});
}
}
@@ -56,11 +70,11 @@ function startGPBrain(problem_id,client_id)
// var worker = JsWorker.createWorkerFromUrl("gp_worker.js", onMessage);
if (runInWorker) {
var worker = new Worker('gp_worker' + problem_id +'.js');
worker = new Worker('gp_worker' + problem_id +'.js');
worker.onmessage = onMessage;
worker.onerror = function (err) {alert ('Error! ' + err);};
worker.postMessage(100000);
worker.postMessage("start");
// worker.postMessage("start");
}
else
@@ -1,10 +1,10 @@
//FIXME: push.js seems to have bug on INTEGER.FROMBOOLEAN
var pushScriptFuncs = [/*"FLOAT.+","FLOAT.-","FLOAT./","FLOAT.*","FLOAT.%","FLOAT.>","FLOAT.<","FLOAT.=","FLOAT.COS","FLOAT.DEFINE","FLOAT.DUP","FLOAT.FLUSH" *//*,"FLOAT.FROMBOOLEAN"*//*,"FLOAT.FROMINTEGER","FLOAT.MAX","FLOAT.MIN","FLOAT.POP","FLOAT.RAND","FLOAT.ROT","FLOAT.SHOVE","FLOAT.SIN","FLOAT.STACKDEPTH","FLOAT.SWAP","FLOAT.TAN","FLOAT.YANK","FLOAT.YANKDUP",*/"INTEGER.+","INTEGER.-","INTEGER./","INTEGER.*","INTEGER.%","INTEGER.>","INTEGER.<","INTEGER.=",/*"INTEGER.DEFINE"*/"INTEGER.DUP","INTEGER.FLUSH",/*,"INTEGER.FROMBOOLEAN",*/"INTEGER.FROMFLOAT","INTEGER.MAX","INTEGER.MIN","INTEGER.POP",/*"INTEGER.RAND",*/"INTEGER.ROT","INTEGER.SHOVE","INTEGER.STACKDEPTH","INTEGER.SWAP","INTEGER.YANK","INTEGER.YANKDUP",
/*var pushScriptFuncs = [/*"FLOAT.+","FLOAT.-","FLOAT./","FLOAT.*","FLOAT.%","FLOAT.>","FLOAT.<","FLOAT.=","FLOAT.COS","FLOAT.DEFINE","FLOAT.DUP","FLOAT.FLUSH" *//*,"FLOAT.FROMBOOLEAN"*//*,"FLOAT.FROMINTEGER","FLOAT.MAX","FLOAT.MIN","FLOAT.POP","FLOAT.RAND","FLOAT.ROT","FLOAT.SHOVE","FLOAT.SIN","FLOAT.STACKDEPTH","FLOAT.SWAP","FLOAT.TAN","FLOAT.YANK","FLOAT.YANKDUP","INTEGER.+","INTEGER.-","INTEGER./","INTEGER.*","INTEGER.%","INTEGER.>","INTEGER.<","INTEGER.=",/*"INTEGER.DEFINE""INTEGER.DUP","INTEGER.FLUSH",/*,"INTEGER.FROMBOOLEAN","INTEGER.FROMFLOAT","INTEGER.MAX","INTEGER.MIN","INTEGER.POP",/*"INTEGER.RAND","INTEGER.ROT","INTEGER.SHOVE","INTEGER.STACKDEPTH","INTEGER.SWAP","INTEGER.YANK","INTEGER.YANKDUP",
"CODE.QUOTE", "CODE.IF", "CODE.DO"
/*,"BOOLEAN.=","BOOLEAN.AND","BOOLEAN.DEFINE","BOOLEAN.DUP","BOOLEAN.FLUSH","BOOLEAN.FROMFLOAT","BOOLEAN.FROMINTEGER","BOOLEAN.NOT","BOOLEAN.OR","BOOLEAN.POP","BOOLEAN.RAND","BOOLEAN.ROT","BOOLEAN.SHOVE","BOOLEAN.STACKDEPTH","BOOLEAN.SWAP","BOOLEAN.YANK","BOOLEAN.YANKDUP","CODE.=","CODE.ATOM","CODE.CAR","CODE.CDR","CODE.CONS","CODE.CONTAINS","CODE.DEFINE","CODE.DO","CODE.DO*","CODE.DO*COUNT","CODE.DO*RANGE","CODE.DO*TIMES","CODE.DUP","CODE.FLUSH","CODE.IF","CODE.LENGTH","CODE.LIST","CODE.NOOP","CODE.NTH","CODE.NULL","CODE.SHOVE","CODE.POP","CODE.QUOTE","CODE.ROT","CODE.SHOVE","CODE.STACKDEPTH","CODE.SWAP","CODE.YANK","CODE.YANKDUP","EXEC.=","EXEC.DEFINE","EXEC.DO*COUNT","EXEC.DO*RANGE","EXEC.DO*TIMES","EXEC.DUP","EXEC.FLUSH","EXEC.IF","EXEC.K","EXEC.POP","EXEC.ROT","EXEC.S","EXEC.SHOVE","EXEC.STACKDEPTH","EXEC.SWAP","EXEC.Y","EXEC.YANK","EXEC.YANKDUP"/*,"NAME.=","NAME.DUP","NAME.FLUSH","NAME.POP","NAME.RAND","NAME.ROT","NAME.SHOVE","NAME.STACKDEPTH","NAME.SWAP","NAME.YANK","NAME.YANKDUP"*/];
/*,"BOOLEAN.=","BOOLEAN.AND","BOOLEAN.DEFINE","BOOLEAN.DUP","BOOLEAN.FLUSH","BOOLEAN.FROMFLOAT","BOOLEAN.FROMINTEGER","BOOLEAN.NOT","BOOLEAN.OR","BOOLEAN.POP","BOOLEAN.RAND","BOOLEAN.ROT","BOOLEAN.SHOVE","BOOLEAN.STACKDEPTH","BOOLEAN.SWAP","BOOLEAN.YANK","BOOLEAN.YANKDUP","CODE.=","CODE.ATOM","CODE.CAR","CODE.CDR","CODE.CONS","CODE.CONTAINS","CODE.DEFINE","CODE.DO","CODE.DO*","CODE.DO*COUNT","CODE.DO*RANGE","CODE.DO*TIMES","CODE.DUP","CODE.FLUSH","CODE.IF","CODE.LENGTH","CODE.LIST","CODE.NOOP","CODE.NTH","CODE.NULL","CODE.SHOVE","CODE.POP","CODE.QUOTE","CODE.ROT","CODE.SHOVE","CODE.STACKDEPTH","CODE.SWAP","CODE.YANK","CODE.YANKDUP","EXEC.=","EXEC.DEFINE","EXEC.DO*COUNT","EXEC.DO*RANGE","EXEC.DO*TIMES","EXEC.DUP","EXEC.FLUSH","EXEC.IF","EXEC.K","EXEC.POP","EXEC.ROT","EXEC.S","EXEC.SHOVE","EXEC.STACKDEPTH","EXEC.SWAP","EXEC.Y","EXEC.YANK","EXEC.YANKDUP"/*,"NAME.=","NAME.DUP","NAME.FLUSH","NAME.POP","NAME.RAND","NAME.ROT","NAME.SHOVE","NAME.STACKDEPTH","NAME.SWAP","NAME.YANK","NAME.YANKDUP"];*/
function testGP() {
startGP(function(){});
@@ -1,11 +1,13 @@
var pushGP;
var worker = null;
GPWorker = function () {
this.gens = 0;
};
GPWorker.prototype.startGPWorker = function(numGens) {
GPWorker.prototype.startGPWorker = function(msg) {
// var numGens = message.data;
@@ -37,24 +39,41 @@ GPWorker.prototype.startGPWorker = function(numGens) {
return;
}
pushGP = new PushGP();
pushGP = new PushGP(gp_params);
pushGP.startGP(cases);/*[[0,1],[2,5],[3,100]]);*/
for (this.gens = 0; this.gens<numGens; this.gens++) {
var str = pushGP.getProgramsHTMLString();
postMessage({msgtype:"programStr", programsStr : str, bestFitness : pushGP.currentBestFitness});
// for (this.gens = 0; this.gens<numGens; this.gens++) {
pushGP.doGeneration();
doPushGen();
// postMessage("did one!") ;
}
// }
};
onmessage = function(message) {
var worker = new GPWorker();
worker.startGPWorker(message.data);
if (worker == undefined) {
worker = new GPWorker();
worker.startGPWorker(message.data);
}
else if (message.data['msgtype'] == "heartbeat")
// workerDebug(pushGP);
{
doPushGen();
}
else if (message.data['msgtype'] == "addPrograms"){
pushGP.addPrograms(message.data['msg']);
}
}
function doPushGen(){
pushGP.doGeneration();
var str = pushGP.getProgramsHTMLString();
postMessage({msgtype:"programStr", programsStr : str, bestFitness : pushGP.currentBestFitness});
}
function reportUp(msg) {
View
@@ -9,23 +9,26 @@ PushGP = function () {
this.testCases = [[0,0]];
this.fitnessFunc = function(a) { return 0; };
this.currentBestFitness = undefined;
this.currentBestFitness = 99999;
this.params = {
'crossoverPercentile' : 2,
'mutationProbability' : 25,
'cloneProbability' : 10,
'startPopulation' : 20,
'tournamentSize' : 10,
'maxPopulation' : 75
'maxPopulation' : 75,
'stagnantGenerations' : 10,
'reportAfter' : 1,
'numProgramsToDownload' : 2
};
this.generationsSinceLastImprovement = 0;
this.lastImprovedFitness = undefined;
this.generationsSinceLastReported = 0;
this.favorSmaller = true;
this.reportBestAfter = 0;
// this.reportBestAfter = 0;
this.generations = 0;
};
@@ -52,6 +55,13 @@ PushGP.prototype.startGP = function (testCases,fitnessFunc,params) {
};
PushGP.prototype.addPrograms = function(arrayOfNewPrograms) {
for (var programX = 0; programX < arrayOfNewPrograms.length; programX++) {
this.pushPrograms.push ({code: arrayOfNewPrograms[programX], fitness : 0});
workerDebug ("adding " + arrayOfNewPrograms[programX]);
}
}
PushGP.prototype.doGeneration = function() {
// for (var programX in this.pushPrograms) {
// //...
@@ -79,7 +89,14 @@ PushGP.prototype.doGeneration = function() {
return programA['fitness'] - programB['fitness'];
});
this.currentBestFitness = this.pushPrograms[0]['fitness'];
if(this.pushPrograms[0]['fitness'] < this.currentBestFitness) {
this.currentBestFitness = this.pushPrograms[0]['fitness'];
this.generationsSinceLastImprovement = 0;
}
else
this.generationsSinceLastImprovement++;
this.doUploadAndDownload();
if (this.pushPrograms.length > this.params.maxPopulation) {
//Cull the most unfit
@@ -101,20 +118,30 @@ PushGP.prototype.doGeneration = function() {
// Upload if necessary
for (var randomX = 0; randomX < 10; randomX++)
this.newPushPrograms.push({code: randomCode(), fitness: 0});
};
if(this.currentBestFitness < this.lastImprovedFitness) {
this.lastImprovedFitness = this.currentBestFitness;
this.generationsSinceLastImprovement = 0;
PushGP.prototype.doUploadAndDownload = function () {
workerDebug('generationsSinceLastImprovement = ' + this.generationsSinceLastImprovement);
workerDebug('this.params.stagnantGenerations = ' + this.params.stagnantGenerations );
if(this.generationsSinceLastImprovement > this.params.stagnantGenerations) {
//download new programs here!
reportUp({'msgtype' : 'downloadPrograms', 'msg' : {'numPrograms': this.params.numProgramsToDownload }});
}
else
this.generationsSinceLastImprovement++;
if(this.generationsSinceLastImprovement > this.reportBestAfter) {
if(this.generationsSinceLastReported == this.params.reportAfter) {
//do an upload here!
this.generationsSinceLastReported = 0;
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}});
}
};
else
this.generationsSinceLastReported++;
}
PushGP.prototype.fitnessEvaluate = function(arrayOfPrograms) {
for (var programX = 0; programX < arrayOfPrograms.length; programX++) {
@@ -15,6 +15,7 @@
<div style="border-style:solid;border-width:2px;padding:3px">
<div>Name: <input type="text" name="name" value="{{ problem.name}}" id="name" /><div>
<div>Comments: <input type="text" name="comments" value="{{ problem.comments }}" id="comments" /></div>
<div>Allowed Commands: <textarea name="allowed" rows="8" cols="40">{{ problem.allowed }}</textarea>
<div>Evaluated Stack: {{ problem.evaluated_stack }}</div>
<div>Start Population: <input type="text" name="start_population" value="{{ problem.start_population }}" id="start_population" /></div>
<div>Max Population: <input type="text" name="max_population" value="{{ problem.max_population }}" id="max_population" /></div>
@@ -7,4 +7,16 @@ var cases = [
{% end %}
];
];
var gp_params = {
'crossoverPercentile' : {{ gp_params.crossover_probability }},
'mutationProbability' : {{ gp_params.mutation_probability }},
'cloneProbability' : {{ gp_params.clone_probability }},
'startPopulation' : {{ gp_params.start_population }},
'tournamentSize' : {{ gp_params.tournament_size }},
'maxPopulation' : {{ gp_params.max_population }},
'stagnantGenerations' : 0,
'numProgramsToDownload' : 2
};
var pushScriptFuncs = {{ gp_params.allowed }};

0 comments on commit fa0815d

Please sign in to comment.