예제 #1
0
파일: evoalg.py 프로젝트: didrikjonassen/ea
def main(phenotype_class):
	popsize = int(input("Input population size: "))
	babymaker = production.production(phenotype_class)
	generations = int(input("Input number of generations: "))
	goal = float(input("Input fitness goal, 0 if not applicable: "))
	protocol = adult_selection.protocols[raw_input("Input the wanted selection protocol (" + "/".join(adult_selection.protocols.keys()) + "): ")](popsize)
	mechanism = selection_mechanisms.mechanisms[raw_input("Input the wanted selection mechanism (" + "/".join(selection_mechanisms.mechanisms.keys()) + "): ")](protocol.litter_size)

	pop = population.population([phenotype_class() for i in range(popsize)])
	
	population_list = [pop]
	
	stdout.write("\nProgress: generation ")
	
	generation = 0
	generation_s = ""
	while (goal == 0 and generation <= generations) or (goal != 0 and population_list[-1].max_fitness() < goal and generation <= generations):
		parents = mechanism.select(pop)
		litter = babymaker.produce(parents)
		pop = protocol.select(pop, litter)
		
		population_list += [deepcopy(pop)]
		
		stdout.write("\b"*len(generation_s))
		generation += 1
		generation_s = str(generation)
		stdout.write(generation_s)
		
	stdout.write("\nDone!\n")
		
	return population_list
예제 #2
0
	def produce(this, parents):
		pairs = [(parents[i], parents[i+1]) for i in xrange(0, len(parents), 2)]
		litter = []
		for mom, dad in pairs:
			child1, child2 = mom.crossover(dad, this.crossover_points, this.crossover_rate)
			this.mutation_type(child1, this.mutation_rate)
			this.mutation_type(child2, this.mutation_rate)
			litter += [this.ptype_class(gene=child1.gene), this.ptype_class(gene=child2.gene)]
		return population.population(litter)
예제 #3
0
def populate(data):
    ranges = load_data(data)
    p = population.population(9, ranges)
    for i in range(0, 20):
        for c in p.colony:
            c.fitness_score = c.cal_fitness(data)
        p.next_generation()
        p.keep_size()

    return p.best_gene()
예제 #4
0
파일: evolution.py 프로젝트: ICSSZJU/EPR
traits=parameter.__getstate__()

real_trans=parameter.real_trans

m=parameter.m #每期重复的轮次

n=parameter.n #期数

resource_rate=parameter.resource_rate #拥有资源的概率

imitate_rate=parameter.imitate_rate #变异率

c=parameter.battle_cost #战斗成本

evo_pop=population([parameter.number_of_B,parameter.number_of_X,parameter.number_of_E])   #设定B、X、E三者的人数

endowment_effect=parameter.endowment_effect #禀赋效应

pro_func=eval("lambda x:"+parameter.profit_function)

w=parameter.selection_strength #选择强度

class evolution(object):
    def __init__(self,evo_pop,pro_func,endowment_effect,resource_rate,battle_cost,imitate_rate,selection_strength,m,n):
        self.evo_pop=evo_pop
        self.profit_func=pro_func
        self.endowment_effect=endowment_effect
        self.resource_rate=resource_rate
        self.battle_cost=battle_cost
        self.imitate_rate=imitate_rate
    # create the new dir
    import os
##    os.mkdir(dirName) # didnt work

    try:
        os.makedirs(dirName) # so lets try
        # make a copy of the origenal gene
        originalGene.save(dirName+os.sep+'originalGene.model')
    except:
        print('what went rong?')
        print(sys.exc_type)
        print(sys.exc_value)
        print(sys.exc_traceback)

    # start a new population
    pop=population()
    # set GA settings
    pop.maxTime=timedelta(hours=60)
    # create a population using the orignial gene
    pop.seed(originalGene)
    pop.evolve()

    #
    # make a models dir
##    os.mkdir(dirName+'models') # sometimes does not work on linux for some reason
    os.makedirs(dirName+os.sep+'models'+str(uniqueNumber))
    for i in range(len(pop.population)):
        pop.population[i].save(dirName+os.sep+'models'+str(uniqueNumber)+os.sep+'model_'+str(i)+'.model')

    # save history
    pop.saveProgress(dirName+os.sep+'log'+str(uniqueNumber)+'.txt')
예제 #6
0
def test_generator_creates_enough_members():
    p = population(100, lambda: randint(0, 100))
    assert (len(p.pop) == 100)
예제 #7
0
def get_random_population(size):
    return population(size, random_bitarray_chromosome)
예제 #8
0
from population import population
from chromosome import chromosome
import random

noOfGen = 25
popDim = 25

pop = population(popDim)
pop.initPop()

f = open("out.txt", "w")
bestArr = []

for i in range(noOfGen):
    popAux = []
    pop.fitPop()

    best = pop.getBest()
    bestArr.append(best.getVelocity())

    print(best.getFitness())

    for j in range(popDim):
        c1 = pop.selection()
        c2 = pop.selection()
        c = pop.xo(c1, c2)

        if random.uniform(0, 1) < 0.15:
            c.mutation()
        popAux.append(c)