Created
June 29, 2011 11:24
-
-
Save ernado/1053666 to your computer and use it in GitHub Desktop.
Profiler for GeneticAlgorythm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import cProfile | |
from random import randint | |
from copy import deepcopy | |
from math import floor | |
import random | |
class Organism: | |
#initiate | |
def __init__(self, alleles, fitness, likelihood): | |
self.alleles = alleles | |
self.fitness = fitness | |
self.likelihood = likelihood | |
self.result = 0 | |
def __unicode__(self): | |
return '%s [%s - %s]' % (self.alleles, self.fitness, self.likelihood) | |
class CDiophantine: | |
def __init__(self, coefficients, result): | |
self.coefficients = coefficients | |
self.result = result | |
maxPopulation = 60 | |
organisms = [] | |
def GetGene (self,i): | |
return self.organisms[i] | |
def OrganismFitness (self,gene): | |
gene.result = 0 | |
for i in range (0, len(self.coefficients)): | |
gene.result += self.coefficients[i]*gene.alleles[i] | |
gene.fitness = abs(gene.result - self.result) | |
return gene.fitness | |
def Fitness (self): | |
for organism in self.organisms: | |
organism.fitness = self.OrganismFitness(organism) | |
if organism.fitness == 0: | |
return organism | |
return None | |
def MultiplyFitness (self): | |
coefficientSum = 0 | |
for organism in self.organisms: | |
coefficientSum += 1/float(organism.fitness) | |
return coefficientSum | |
def GenerateLikelihoods (self): | |
last = 0 | |
multiplyFitness = self.MultiplyFitness() | |
for organism in self.organisms: | |
last = ((1/float(organism.fitness)/multiplyFitness)*100) | |
#print '1/%s/%s*100 - %s' % (organism.fitness, multiplyFitness, last) | |
organism.likelihood = last | |
def Breed (self, parentOne, parentTwo): | |
crossover = randint (1,len(self.coefficients)-1) | |
child = deepcopy(parentOne) | |
initial = 0 | |
final = len(parentOne.alleles) - 1 | |
if randint (1,100) < 50: | |
father = parentOne | |
mother = parentTwo | |
else: | |
father = parentTwo | |
mother = parentOne | |
child.alleles = mother.alleles[:crossover] + father.alleles[crossover:] | |
if randint (1,100) < 5: | |
child.alleles[randint(0,len(child.alleles)-1)] = randint (0,self.result) | |
return child | |
def CreateNewOrganisms (self): | |
#generating new population | |
tempPopulation = [] | |
for _ in self.organisms: | |
iterations = 0 | |
father = deepcopy(self.organisms[0]) | |
mother = deepcopy(self.organisms[1]) | |
while father.alleles == mother.alleles: | |
father = self.WeightedChoice() | |
mother = self.WeightedChoice() | |
iterations+=1 | |
if iterations > 35: | |
break | |
kid = self.Breed(father,mother) | |
tempPopulation.append(kid) | |
self.organisms = tempPopulation | |
def WeightedChoice (self): | |
list = [] | |
for organism in self.organisms: | |
list.append((organism.likelihood,organism)) | |
list = sorted((random.random() * x[0], x[1]) for x in list) | |
return list[-1][1] | |
def AverageFitness (self): | |
sum = 0 | |
for organism in self.organisms: | |
sum += organism.fitness | |
return float(sum)/len(self.organisms) | |
def AverageLikelihoods (self): | |
sum = 0 | |
for organism in self.organisms: | |
sum += organism.likelihood | |
return sum/len(self.organisms) | |
def Solve (self): | |
solution = None | |
for i in range(0,self.maxPopulation): | |
alleles = [] | |
# | |
for j in range(0, len(self.coefficients)): | |
alleles.append(randint(0, self.result)) | |
self.organisms.append(Organism(alleles,0,0)) | |
solution = self.Fitness() | |
if solution: | |
return solution.alleles | |
iterations = 0 | |
while not solution and iterations <3000: | |
self.GenerateLikelihoods() | |
self.CreateNewOrganisms() | |
solution = self.Fitness() | |
if solution: | |
print 'SOLUTION FOUND IN %s ITERATIONS' % iterations | |
return solution.alleles | |
iterations += 1 | |
return -1 | |
if __name__ == "__main__": | |
diophantine = CDiophantine ([1,2,3,4],141) | |
cProfile.run('diophantine.Solve()') | |
#print diophantine.Solve() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Profiler for Solve | |
#maxPopulation = 60 | |
#diophantine = CDiophantine ([1,2,3,4],141) | |
D:\Python27\python.exe C:/Users/Ernado/PycharmProjects/GeneticAlgorythm/main.py | |
SOLUTION FOUND IN 43 ITERATIONS | |
12292081 function calls (12189121 primitive calls) in 7.381 seconds | |
Ordered by: standard name | |
ncalls tottime percall cumtime percall filename:lineno(function) | |
1 0.000 0.000 7.381 7.381 <string>:1(<module>) | |
110880/7920 0.262 0.000 0.613 0.000 copy.py:145(deepcopy) | |
87120 0.014 0.000 0.014 0.000 copy.py:198(_deepcopy_atomic) | |
7920 0.031 0.000 0.190 0.000 copy.py:226(_deepcopy_list) | |
7920 0.040 0.000 0.479 0.000 copy.py:253(_deepcopy_dict) | |
110880 0.103 0.000 0.136 0.000 copy.py:267(_keep_alive) | |
7920 0.029 0.000 0.578 0.000 copy.py:283(_deepcopy_inst) | |
60 0.000 0.000 0.000 0.000 main.py:10(__init__) | |
1 0.001 0.001 7.381 7.381 main.py:110(Solve) | |
2686 0.008 0.000 0.010 0.000 main.py:28(OrganismFitness) | |
45 0.002 0.000 0.011 0.000 main.py:35(Fitness) | |
44 0.001 0.000 0.001 0.000 main.py:43(MultiplyFitness) | |
44 0.001 0.000 0.003 0.000 main.py:49(GenerateLikelihoods) | |
2640 0.016 0.000 0.257 0.000 main.py:57(Breed) | |
44 0.168 0.004 7.366 0.167 main.py:73(CreateNewOrganisms) | |
61502 1.403 0.000 6.541 0.000 main.py:90(WeightedChoice) | |
3751622 1.913 0.000 2.371 0.000 main.py:94(<genexpr>) | |
8380 0.019 0.000 0.020 0.000 random.py:173(randrange) | |
8380 0.007 0.000 0.027 0.000 random.py:237(randint) | |
2686 0.000 0.000 0.000 0.000 {abs} | |
31680 0.034 0.000 0.034 0.000 {hasattr} | |
253440 0.031 0.000 0.031 0.000 {id} | |
8136 0.001 0.000 0.001 0.000 {len} | |
3827700 0.488 0.000 0.488 0.000 {method 'append' of 'list' objects} | |
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} | |
221760 0.039 0.000 0.039 0.000 {method 'get' of 'dict' objects} | |
7920 0.002 0.000 0.002 0.000 {method 'iteritems' of 'dict' objects} | |
3698500 0.459 0.000 0.459 0.000 {method 'random' of '_random.Random' objects} | |
7920 0.005 0.000 0.005 0.000 {method 'update' of 'dict' objects} | |
2747 0.001 0.000 0.001 0.000 {range} | |
61502 2.302 0.000 4.674 0.000 {sorted} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#maxPopulation = 120 | |
SOLUTION FOUND IN 105 ITERATIONS | |
198845337 function calls (198349257 primitive calls) in 123.613 seconds | |
Ordered by: standard name | |
ncalls tottime percall cumtime percall filename:lineno(function) | |
1 0.000 0.000 123.613 123.613 <string>:1(<module>) | |
534240/38160 1.295 0.000 3.085 0.000 copy.py:145(deepcopy) | |
419040 0.066 0.000 0.066 0.000 copy.py:198(_deepcopy_atomic) | |
38160 0.156 0.000 0.964 0.000 copy.py:226(_deepcopy_list) | |
38160 0.204 0.000 2.392 0.000 copy.py:253(_deepcopy_dict) | |
533520 0.524 0.000 0.689 0.000 copy.py:267(_keep_alive) | |
38160 0.155 0.000 2.905 0.000 copy.py:283(_deepcopy_inst) | |
120 0.000 0.000 0.000 0.000 main.py:10(__init__) | |
1 0.001 0.001 123.613 123.613 main.py:110(Solve) | |
12797 0.037 0.000 0.045 0.000 main.py:28(OrganismFitness) | |
107 0.007 0.000 0.053 0.000 main.py:35(Fitness) | |
106 0.006 0.000 0.006 0.000 main.py:43(MultiplyFitness) | |
106 0.007 0.000 0.013 0.000 main.py:49(GenerateLikelihoods) | |
12720 0.092 0.000 1.370 0.000 main.py:57(Breed) | |
106 2.533 0.024 123.545 1.166 main.py:73(CreateNewOrganisms) | |
534038 24.098 0.000 117.678 0.000 main.py:90(WeightedChoice) | |
64618598 33.775 0.000 41.681 0.000 main.py:94(<genexpr>) | |
39716 0.103 0.000 0.109 0.000 random.py:173(randrange) | |
39716 0.040 0.000 0.149 0.000 random.py:237(randint) | |
12797 0.002 0.000 0.002 0.000 {abs} | |
152640 0.173 0.000 0.173 0.000 {hasattr} | |
1220400 0.160 0.000 0.160 0.000 {id} | |
38895 0.007 0.000 0.007 0.000 {len} | |
64745880 7.668 0.000 7.668 0.000 {method 'append' of 'list' objects} | |
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} | |
1067760 0.200 0.000 0.200 0.000 {method 'get' of 'dict' objects} | |
38160 0.010 0.000 0.010 0.000 {method 'iteritems' of 'dict' objects} | |
64124276 7.913 0.000 7.913 0.000 {method 'random' of '_random.Random' objects} | |
38160 0.026 0.000 0.026 0.000 {method 'update' of 'dict' objects} | |
12918 0.005 0.000 0.005 0.000 {range} | |
534038 44.350 0.000 86.031 0.000 {sorted} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment