de class is vrijwel zonder errors
er zit echter n lelijke error in de gebruik van de class
Code: Selecteer alles
import math
import random
ADD = "[ + # # # ]"
SUBTRACT = "[ - # # # ]"
MULTIPLY = "[ * # # # ]"
DIVIDE = "[ / # # # ]"
class CGP( object ) :
def __init__( self ) :
self.genes = []
self.numbermode = 0
self.inputmax = 0
self.invoer = []
def set_inputmax( self , no ) :
a = "abcdefghjk"
i = 0
while i < len( a ) and i <= no - 1 :
self.use( a[ i ] )
i += 1
for i in xrange( len( a ) + 2 ) :
self.invoer.append( 0. )
def set_input( self , no , x ) :
if no < 0 or no > len( self.invoer ) - 1 :
self.invoer[ no ] = x
def run( self , prog ) :
l = "abcdefghijk"
while "]" in prog :
eind = prog.find( "]" )
begin = eind - 1
while prog[ begin ] != "[" :
begin -= 1
deel = prog[ begin : eind + 1 ]
q = deel.split()
func = q[ 1 ]
if q[ 2 ] in l :
a = self.invoer[ l.find( q[ 2 ] ) ]
else :
a = float( q[ 2 ] )
if q[ 3 ] in l :
b = self.invoer[ l.find( q[ 3 ] ) ]
else :
b = float( q[ 3 ] )
if q[ 4 ] in l :
c = self.invoer[ l.find( q[ 4 ] ) ]
else :
c = float( q[ 4 ] )
if func == "+" :
ab = a + b
elif func == "*" :
ab = a * b
elif func == "-" :
ab = a - b
elif func == "/" :
if b == 0.0 :
return "error"
else :
ab = a / b
else :
return "error"
l = prog[ : begin ]
m = `ab`
r = prog[ eind + 1 : ]
prog = l + m + r
return prog
def write( self , hooks ) :
dice = random.randint( 0 , len( self.genes )-1 )
while not "[" in self.genes[ dice ] :
dice = random.randint( 0 , len( self.genes )-1 )
prog = self.genes[ dice ]
tel = 0
while "#" in prog and tel < hooks :
p = prog.find( "#" )
dice = random.randint( 0 , len( self.genes ) - 1 )
m = self.genes[ dice ]
if "#" in m :
tel += 1
l = prog[ : p ]
r = prog[ p + 1 : ]
prog = l + m + r
while "#" in prog :
p = prog.find( "#" )
dice = random.randint( 0 , len( self.genes ) - 1 )
while "[" in self.genes[ dice ] :
dice = random.randint( 0 ,
len( self.genes ) - 1 )
m = self.genes[ dice ]
l = prog[ : p ]
r = prog[ p + 1 : ]
prog = l + m + r
return prog
def mix( self , a , b ) :
la = []
for i in xrange( len( a ) ) :
if a[ i ] == "[" :
la.append( i )
lb = []
for i in xrange( len( b ) ) :
if b[ i ] == "[" :
lb.append( i )
dice = random.randint( 0 , len( la ) - 1 )
begina = la[ dice ]
fl = 1
i = begina + 1
while fl > 0 and i < len( a ) :
if a[ i ] == "[" : fl += 1
if a[ i ] == "]" : fl -= 1
i += 1
einda = i
dice = random.randint( 0 , len( lb ) - 1 )
beginb = lb[ dice ]
fl = 1
i = beginb + 1
while fl > 0 and i < len( b ) :
if b[ i ] == "[" : fl += 1
if b[ i ] == "]" : fl -= 1
i += 1
eindb = i
l = a[ : begina ]
m = b[ beginb : eindb + 1 ]
r = a[ einda : ]
return l + m + r
def mutate( self , prog ) :
a = "abcdefghijk"
lp = prog.split()
dice = random.randint( 0 , len( lp ) - 1 )
atom = lp[ dice ]
while atom in ( "[" , "]" ) :
dice = random.randint( 0 , len( lp ) - 1 )
atom = lp[ dice ]
# atom is number
if atom[ 0 ] in "0123456789" :
f = float( atom )
if self.inputmax > 0 :
if random.randint( 0 , 10 ) == 0 :
lp[ dice ] = a[ random.randint( 0 , len( a ) - 1 ) ]
else :
if self.numbermode == 1 :
if random.randint( 0 , 2 ) == 0 :
f += 2**random.randint( 0 , 32 )
else :
f -= 2**random.randint( 0 , 32 )
else :
if random.randint( 0 , 2 ) == 0 :
f += 2**random.randint( -31 , 32 )
else :
f -= 2**random.randint( -31 , 32 )
lp[ dice ] = str( f )
else :
if self.numbermode == 1 :
if random.randint( 0 , 2 ) == 0 :
f += 2**random.randint( 0 , 32 )
else :
f -= 2**random.randint( 0 , 32 )
else :
if random.randint( 0 , 2 ) == 0 :
f += 2**random.randint( -31 , 32 )
else :
f -= 2**random.randint( -31 , 32 )
lp[ dice ] = str( f )
elif atom in "abcdefghijk" :
if random.randint( 0 , 10 ) > 0 :
lp[ dice ] = a[ random.randint( 0 , len( a ) - 1 )]
else :
if self.numbermode == 1 :
if random.randint( 0 , 2 ) == 0 :
f = 2**random.randint( 0 , 32 )
else :
f = -2**random.randint( 0 , 32 )
else :
if random.randint( 0 , 2 ) == 0 :
f = 2**random.randint( -31 , 32 )
else :
f = -2**random.randint( -31 , 32 )
lp[ dice ] = str( f )
else : # atom is function
d = random.randint( 0 , len( self.genes ) - 1 )
while not "[" in self.genes[ d ] :
d = random.randint( 0 ,
len( self.genes ) - 1 )
l = self.genes[ d ].split()
func = l[ 1 ]
lp[ dice ] = func
prog = ""
for i in xrange( len( lp ) ) :
prog += lp[ i ] + " "
return prog
def use( self , gen ) :
self.genes.append( gen )
def intlist( self ) :
for i in xrange( 32 ) :
self.use( str( 2**i ) )
self.numbermode = 1
def dbllist( self ) :
for i in range( -31 , 31 ) :
self.use( str( 2**i ) )
self,numbermode = 2
gp = CGP()
gp.use( ADD )
gp.use( SUBTRACT )
gp.use( MULTIPLY )
gp.use( DIVIDE )
gp.intlist()
#gp.set_inputmax( 3 )
lprog = []
lfout = []
for i in xrange( 10 ) :
lprog.append( gp.write( 6 ) )
lfout.append( 0. )
for i in range( 20 ) :
for t in xrange( len( lprog ) - 1 ) :
uit = float( gp.run( lprog[ t ] ) )
lfout[ t ] == math.fabs( uit - math.pi )
for h in xrange( len( lprog ) - 1 ) :
for l in range( 0 , h ) :
if lfout[ l ] > lfout[ h ] :
hp = lprog[ l ]
lprog[ l ] = lprog[ h ]
lprog[ h ] = hp
hf = lfout[ l ]
lfout[ l ] = lfout[ h ]
lfout[ h ] = hf
print lprog[ 0 ]
for i in range( 4 , len( lprog ) ) :
a = lprog[ random.randint( 0 , 3 ) ]
b = lprog[ random.randint( 0 , 3 ) ]
lprog[ i ] = gp.mix( a , b )
if random.randint( 0 , 10 ) < 2 :
lprog[ i ] = gp.mutate( lprog[ i ] )