def init(): drop() print'Initializing...' str=GeneStructure(gsNew=-1,fromDB=1,num=0) g=Gene(structure=str,weights=[],thresholds=[],num=0) for i in range (0,SIZE_SENSOR+SIZE_OUTPUT+1): str.appendNeuron() g.thresholds.append(random.uniform(-0.5,0.5)) for i in range(0,SIZE_SENSOR): str.appendSynapse(origin=i,terminus=SIZE_SENSOR+SIZE_OUTPUT) g.weights.append(random.uniform(-2,2)) str.appendSynapse(origin=SIZE_SENSOR+SIZE_OUTPUT,terminus=i+SIZE_SENSOR) g.weights.append(random.uniform(-2,2)) save.saveStruct(str.pack()) g.set() sp1=Specie(members=[g],appearTime=0) nature=Nature(species=[sp1],generation=0,restResource=0) save.saveCurrent(nature.packCurrent()) print'Initializing has been done.'
def breed(self): if len(self.genes)>7: r=[i for i in (1,WHO_MATE) if i<len(self.genes)] for i in range(0,SPEED_MATE): wife=choice(r) new1=mate(gene1=self.genes[0],gene2=self.genes[wife]) self.newGenesList.append(new1) self.genes.append(new1) if len(self.genes)>5: t=choice(self.genes) new2=Gene(structure=t.structure,weights=t.weights,thresholds=t.thresholds) new3=Gene(structure=t.structure,weights=t.weights,thresholds=t.thresholds) new2.addNRand() self.newSpiecesList.append(new2) for n in new3.getSRand(sizeO=SIZE_OUTPUT,sizeS=SIZE_SENSOR): self.newSpiecesList.append(n) for i in range(0,SPEED_MUTATION): tmp=choice(self.genes) new1=Gene(structure=tmp.structure,weights=tmp.weights,thresholds=tmp.thresholds) new1.mutation() self.newGenesList.append(new1) self.genes.append(new1)
def breed(self): if len(self.genes) > 7: r = [i for i in (1, WHO_MATE) if i < len(self.genes)] for i in range(0, SPEED_MATE): wife = choice(r) new1 = mate(gene1=self.genes[0], gene2=self.genes[wife]) self.newGenesList.append(new1) self.genes.append(new1) if len(self.genes) > 5: t = choice(self.genes) new2 = Gene(structure=t.structure, weights=t.weights, thresholds=t.thresholds) new3 = Gene(structure=t.structure, weights=t.weights, thresholds=t.thresholds) new2.addNRand() self.newSpiecesList.append(new2) for n in new3.getSRand(sizeO=SIZE_OUTPUT, sizeS=SIZE_SENSOR): self.newSpiecesList.append(n) for i in range(0, SPEED_MUTATION): tmp = choice(self.genes) new1 = Gene(structure=tmp.structure, weights=tmp.weights, thresholds=tmp.thresholds) new1.mutation() self.newGenesList.append(new1) self.genes.append(new1)
def debug(self): print 'Infos of \'this\' unit:' print 'Neurons:' for i in range(0,len(self.neurons)): print 'No.',i,':' self.neurons[i].debug() print 'Gene:' self.gene.debug() if __name__=='__main__': from gene.gene import Gene from gene.genestructure import GeneStructure import random str=GeneStructure(gsNew=-1) g=Gene(structure=str,weights=[],thresholds=[]) for i in range (0,60): str.appendNeuron() g.thresholds.append(random.uniform(-0.5,0.5)) for i in range(0,20): for j in range(20,40): str.appendSynapse(origin=i,terminus=j) str.appendSynapse(origin=j,terminus=i+40) g.weights.append(random.uniform(-2,2)) g.weights.append(random.uniform(-2,2)) # str.appendSynapse(origin=0,terminus=4) # str.appendSynapse(origin=0,terminus=5) # str.appendSynapse(origin=1,terminus=5) # str.appendSynapse(origin=4,terminus=3) # str.appendSynapse(origin=4,terminus=2) # str.appendSynapse(origin=5,terminus=2)
structure = GeneStructure(gsNew=-1, fromDB=1, num=num, neurons=neurons, sizeS=size) members = [] for g in genes: curGene = geneBase.find({'_id': g})[0] weights = curGene['weights'] thresholds = curGene['thresholds'] fitness = curGene['fitness'] record = {int(k): v for k, v in dict(curGene['record']).items()} num = curGene['_id'] gene = Gene(structure=structure, weights=weights, thresholds=thresholds, fitness=fitness, record=record, num=num) members.append(gene) spicie = Specie(members=members, appearTime=appear) spicieList.append(spicie) nature = Nature(species=spicieList, generation=generation, restResource=restRes) for i in range(0, MAX_GENERATION_PER): nature.loop() save.saveCurrent(nature.packCurrent())