Added monitors and statistics, also made a module with some

specific python stuff in __init__.py
This commit is contained in:
maartenkeijzer 2003-01-10 15:41:17 +00:00
commit ea2e369542
19 changed files with 503 additions and 47 deletions

View file

@ -8,10 +8,20 @@ print 'done'
from copy import copy
class MinimFit(float):
def __cmp__(self, other):
if other == None: # I seem to be getting None's, don't know why
return 1
return float.__cmp__(other, self)
class EvalFunc(eoEvalFunc):
def __call__(self, eo):
eo.fitness = reduce(lambda x,y: x+y, eo.genome, 0)
class MinEvalFunc(eoEvalFunc):
def __call__(self, eo):
f = reduce(lambda x,y: x+y, eo.genome, 0 )
eo.fitness = MinimFit(f)
class Init(eoInit):
def __init__(self, genome_length = 10):
@ -68,7 +78,7 @@ if __name__ == '__main__':
print
print
pop = Pop(1, init)
pop = eoPop(1, init)
pop[0] = eo;

View file

@ -2,7 +2,8 @@
for i in *.py
do
python $i
python $i > /dev/null
done

View file

@ -10,10 +10,10 @@ class TestSGA(unittest.TestCase):
def runtest(self, breed):
pop = Pop(50, init)
pop = eoPop(50, init)
for indy in pop: evaluate(indy)
newpop = Pop();
newpop = eoPop();
breed(pop,newpop)
@ -33,5 +33,9 @@ class TestSGA(unittest.TestCase):
self.runtest(breed)
def suite():
return unittest.makeSuite(TestSGA,'test')
if __name__=='__main__':
unittest.main()

View file

@ -1,7 +1,115 @@
from maxone import *
from math import exp
import unittest
class TestSGA(unittest.TestCase):
class MyInit(eoInit):
def __call__(self, eo):
eo.genome = [rng().normal(), rng().normal(), rng().normal()];
class MyMutate(eoMonOp):
def __call__(self, eo):
std = 0.05
eo.genome = copy(eo.genome)
eo.genome[0] += rng().normal() * std
eo.genome[1] += rng().normal() * std
eo.genome[2] += rng().normal() * std
return 1
class AnEval(eoEvalFunc):
def __init__(self):
eoEvalFunc.__init__(self)
setObjectivesSize(2);
setObjectivesValue(0,1);
setObjectivesValue(1,1);
def __call__(self, eo):
x = abs(eo.genome[0])
y = abs(eo.genome[1])
z = abs(eo.genome[2])
eo.fitness = [ x / (x+y+z), y /(x+y+z) ]
import Gnuplot
g = Gnuplot.Gnuplot()
g.reset()
def do_plot(pop):
l1 = []
l2 = []
for indy in pop:
l1.append(indy.fitness[0])
l2.append(indy.fitness[1])
d = Gnuplot.Data(l1,l2, with = 'points')
d2 = Gnuplot.Data([0,1],[1,0], with='lines')
g.plot(d,d2)
class NSGA_II(eoAlgo):
def __init__(self, ngens):
self.cont = eoGenContinue(ngens);
self.selectOne = eoDetTournamentSelect(2);
self.evaluate = AnEval()
self.mutate = MyMutate()
self.init = MyInit()
self.seq = eoProportionalOp()
self.seq.add(self.mutate, 1.0)
self.perf2worth = eoNDSorting_II()
def __call__(self, pop):
sz = len(pop)
i = 0
while self.cont(pop):
newpop = eoPop()
populator = eoSelectivePopulator(pop, newpop, self.selectOne);
while len(newpop) < sz:
self.seq(populator)
for indy in newpop:
self.evaluate(indy)
pop.push_back(indy)
self.perf2worth(pop)
self.perf2worth.sort_pop(pop)
#print pop[0].fitness, pop[0].genome
pop.resize(sz)
#worth = self.perf2worth.getValue()
#print worth[0], worth[sz-1]
i += 1
if i%100 == 0:
pass #do_plot(pop)
worths = self.perf2worth.getValue()
w0 = int(worths[0]-0.001)
for i in range(len(pop)):
if worths[i] <= w0:
break;
print pop[i].genome
print pop[i].fitness
class TestNSGA_II(unittest.TestCase):
def testIndividuals(self):
setObjectivesSize(2);
@ -26,12 +134,11 @@ class TestSGA(unittest.TestCase):
self.failUnlessEqual(dominates(eo2, eo2), 0)
def testNDSorting(self):
setObjectivesSize(2);
setObjectivesValue(0,-1)
setObjectivesValue(1,-1);
pop = Pop()
pop = eoPop()
pop.resize(6)
pop[5].fitness = [0.15,0.87]
@ -50,10 +157,20 @@ class TestSGA(unittest.TestCase):
print indy.fitness
worths = srt.value()
worths = srt.getValue()
print worths
print type(worths)
def testNSGA_II(self):
init = MyInit();
evaluate = AnEval();
pop = eoPop(25, init)
for indy in pop: evaluate(indy)
nsga = NSGA_II(50)
nsga(pop)
if __name__=='__main__':
unittest.main()

View file

@ -48,7 +48,7 @@ class TestPickling(unittest.TestCase):
def testPop(self):
pop = Pop(40, init)
pop = eoPop(40, init)
for indy in pop:
evaluate(indy)

View file

@ -22,7 +22,7 @@ class Xover(Crossover):
class TestPopulator(unittest.TestCase):
def make_pop(self):
pop = Pop(20, init)
pop = eoPop(20, init)
for indy in pop: evaluate(indy)
return pop
@ -57,7 +57,7 @@ class TestPopulator(unittest.TestCase):
seq.add(xover, 0.8)
pop = self.make_pop();
offspring = Pop()
offspring = eoPop()
sel = eoDetTournamentSelect(2)

View file

@ -4,7 +4,7 @@ import unittest
class TestReduce(unittest.TestCase):
def run_test(self, ReduceClass, Arg = None):
pop = Pop(10, init)
pop = eoPop(10, init)
for indy in pop: evaluate(indy)
if Arg:

View file

@ -9,7 +9,7 @@ class TestSGA(unittest.TestCase):
def __init__(self, a):
unittest.TestCase.__init__(self, a)
self.pop = Pop(4, Init())
self.pop = eoPop(4, Init())
for i in range(len(self.pop)):
self.pop[i].fitness = i;

View file

@ -3,23 +3,42 @@ import unittest
class TestSGA(unittest.TestCase):
def test(self):
evaluate = EvalFunc()
def dotestSGA(self, evaluate):
init = Init(20)
mutate = Mutate()
xover = Crossover()
pop = Pop(50, init)
pop = eoPop(50, init)
for indy in pop: evaluate(indy)
select = eoDetTournamentSelect(3);
cont = eoGenContinue(20);
cont1 = eoGenContinue(20);
cont = eoCheckPoint(cont1)
mon = eoGnuplot1DMonitor()
avg = eoAverageStat()
bst = eoBestFitnessStat()
mon.add(avg)
mon.add(bst)
# add it to the checkpoint
cont.add(avg)
#cont.add(mon)
cont.add(bst)
sga = eoSGA(select, xover, 0.6, mutate, 0.4, evaluate, cont);
sga(pop)
print pop.best()
def testSGA_Max(self):
evaluate = EvalFunc()
self.dotestSGA(evaluate)
def testSGA_Min(self):
evaluate = MinEvalFunc()
self.dotestSGA(evaluate)
if __name__=='__main__':
unittest.main()