Here's what I'd like to do as an exercise to improve my Python and to play with the idea of genetic programming in basic form.

From within a script, I'd like to spawn a number (1000?) of additional scripts with randomly generated but grammatically sane Python related to a very simple math problem. I'd like the main script to then test the output of these generated scripts and delete the scripts if they give an error or deliver a significantly wrong result. The scripts that come closest to the correct result get, say, a 100 new variations each, until correct results are reached.

What can I do without overwhelming either myself or my PC?

5 Years
Discussion Span
Last Post by pyTony

Usually you must limit operations very much, to simlify things.
see: http://www.genetic-programming.com/gppreparatory.html

Using that page, I've decided:

1. Function Set - division, multiplication, equality operator
2. Terminal Set - input variables for distance (2.3 miles) and duration (40 minutes), output variable for result
3. Fitness Measure - convert a distance and duration to average speed (3.45 mph)
4. Control Parameters - initial population of 100, then 10 for each of 10 most successful
5. Termination - first individual reaches correct result

How would one actually generate and evaluate scripts from within a script?


For anyone stumbling on this, here's the code of what I ended up doing. I don't actually do anything evolutionary in this code. I spawn bots, but they don't spawn additional bots, as the bots could at no point stumble upon a better way of reaching the result. It's as basic as it gets:

from random import choice
import os

#Generate the solution's Function Set

fun = ['/', '*']

#Generate the solution's Terminal Set

var = [2.3, 40, 60]

#Generate individuals

for i in range (1,11):
    bots = 'bot%s.py' % i
    f = open(bots, 'w')
    f.write('result=' + str(choice(var)) + choice(fun) + str(choice(var)) + choice(fun) + str(choice(var)) + '\n')
        filename = 'bot' + str(i)
        exec('from ' + filename + ' import *')
        print result
        if result == 3.45:
            print 'bot' + str(i) + ' wins the genetic race'
    except Exception, e:
        print e
        os.remove(filename + '.py')
        if os.path.exists(filename + '.pyc'):
            os.remove(filename + '.pyc')
        print filename + ' has failed and died'

You are correct your function is just random way to find a formula allready in fixed form, but you have it too complicated also. No need to put the string to file before testing, no zero value and no minus, so division by zero not possible:

from random import choice

#Generate the solution's Function Set
fun = ['/', '*']

#Generate the solution's Terminal Set
d = {'x':2.3, 'y':40, 'z':60}
var = d.keys()
correct = 3.45
eps = 1E-6

#Generate random formula of two operators out of two and three variables out of three possibilities
while True:
    f = choice(var) + choice(fun) + choice(var) + choice(fun) + choice(var)
    result = eval(f, d)
    print('result = %s = %f' % (f, result))
    # fitness
    if abs(result - correct) < eps:
        print('Result correct, formula number %i: %s, won the race' % (i, f))
        i += 1

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.