def __init__(self, number_of_threads, genotyp=[0]):
		super(OnePlusOneParalleledStrategy, self).__init__(number_of_threads, genotyp)
		if genotyp == [0]:
			phen = phenotype.Phenotype(size = solution.NUMBER_OF_CARDS)
		else:
			phen = phenotype.Phenotype(genotype = genotyp)
		for index in range(0,len(self.population)):
			self.population[index] = phen
		self.calc_fitness()
		self.best = self.population[0]
	def __init__(self, number_of_individuals, gen=[0]):
		if gen == [0]:
			self.population = [phenotype.Phenotype(size = solution.NUMBER_OF_CARDS)
								for i in range(number_of_individuals)]
		else:
			self.population = [phenotype.Phenotype(genotype=gen)
								for i in range(number_of_individuals)]
		self.num_iterations = 0
		self.number_of_individuals = number_of_individuals
		self.expected_sum_A = solution.SUM_A
		self.expected_sum_B = solution.SUM_B
		self.max_iterations = solution.MAX_ITERATIONS
Beispiel #3
0
    def init_generation(self):
        """ Initializes generation 0 with a population of random phenotypes."""
        gen = []
        for i in range(self.size):
            gen.append(phenotype.Phenotype(self.X, self.y))

        return gen
Beispiel #4
0
 def step(self):
     # differential weight [0,2]
     F = 1
     # crossover probability [0,1]
     CR = 0.5
     for j in range(self.number_of_individuals):
         x = random.randint(0, self.number_of_individuals - 1)
         a = x
         b = x
         c = x
         while a == x:
             a = random.randint(0, self.number_of_individuals - 1)
         while b == x or b == a:
             b = random.randint(0, self.number_of_individuals - 1)
         while c == x or c == a or c == b:
             c = random.randint(0, self.number_of_individuals - 1)
         R = random.randint(0, len(self.population[0].genotype) - 1)
         candidate = phenotype.Phenotype(
             genotype=self.population[x].get_genotype())
         for k in range(len(self.population[0].genotype)):
             if (random.randint(0,
                                len(self.population[0].genotype) - 1) == R
                     or random.random() < CR):
                 candidate.set_bit(k, (self.population[a].get_bit(k) + F *
                                       (self.population[b].get_bit(k) -
                                        self.population[c].get_bit(k))))
                 self.population[x].calc_fitness_function(
                     self.destination_sum, self.destination_product)
                 candidate.calc_fitness_function(self.destination_sum,
                                                 self.destination_product)
                 if candidate.get_fitness() == 0:
                     break
         if candidate.get_fitness() > self.population[x].get_fitness():
             del self.population[x]
             self.population.append(candidate)
Beispiel #5
0
 def __init__(self, number_of_individuals, size_of_genotype):
     self.population = [
         phenotype.Phenotype(size=size_of_genotype)
         for i in range(number_of_individuals)
     ]
     self.num_iterations = 0
     self.number_of_individuals = number_of_individuals
     self.destination_sum = 0
     self.destination_product = 0
     self.bit_probability_table = prepare_lookup_table(size_of_genotype)
	def step(self):
		self.num_iterations += 1
		y = phenotype.Phenotype(size=solution.NUMBER_OF_CARDS, genotype=self.population[0].genotype[:])
		index = random.randint(0, solution.NUMBER_OF_CARDS-1)
		y.mutate(index)
		y.calc_fitness_function(self.expected_sum_A, self.expected_sum_B)
		if y.fitness < self.population[0].fitness:
			self.population[0].genotype = y.genotype
			self.population[0].calc_fitness_function(self.expected_sum_A, self.expected_sum_B)
		
		return self.population[0].fitness
	def step(self):
		self.num_iterations += 1
		
		for index in range(0, self.number_of_individuals):
			child = (phenotype.Phenotype(size=solution.NUMBER_OF_CARDS, genotype=self.population[index].genotype[:]))
			child.mutate(random.randint(0, solution.NUMBER_OF_CARDS-1))
			child.calc_fitness_function(self.expected_sum_A, self.expected_sum_B)
			
			self.population.append(child)
		
		self.sort()
		self.population = self.RankingSelection(self.number_of_individuals)		
		
		return self.population[0].fitness
	def step(self):
		self.num_iterations += 1
		
		for i in range(0, self.number_of_individuals-1):
			y = phenotype.Phenotype(size=solution.NUMBER_OF_CARDS, genotype=self.population[i].genotype[:])
			index = random.randint(0, solution.NUMBER_OF_CARDS-1)
			y.mutate(index)
			y.calc_fitness_function(self.expected_sum_A, self.expected_sum_B)
			if y.fitness < self.best.fitness:
				self.best = y
		
		for index in range(0, self.number_of_individuals):
			self.population[index] = self.best
		self.calc_fitness()
		
		return self.best.fitness
Beispiel #9
0
 def birth(self, zygote):
     """ Instance a new phenotype based on the parent's DNA."""
     return phenotype.Phenotype(self.X, self.y, dna=zygote)
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

import phenotype
import random
import solution


def int_to_list(iter):
    li = [int(x) for x in bin(iter)[2:]]

    while len(li) < solution.NUMBER_OF_CARDS:
        li = [0] + li
    return li


last = 2**solution.NUMBER_OF_CARDS
best = phenotype.Phenotype(genotype=int_to_list(0))
best.calc_fitness_function(solution.SUM_A, solution.SUM_B)
iter = 1
while iter < last:
    y = phenotype.Phenotype(genotype=int_to_list(iter))
    y.calc_fitness_function(solution.SUM_A, solution.SUM_B)
    if y.fitness == 0:
        best = y
        break
    elif y.fitness < best.fitness:
        best = y
    iter += 1

print best
Beispiel #11
0
def find_solution(argv):
    while True:
        genotype = phenotype.Phenotype(size=NUMBER_OF_CARDS)
        genotype.calc_fitness_function(SUM_A, SUM_B)
        if genotype.fitness > MIN:
            break
    if len(argv) == 0:
        print "First  parameter like: 1Plus1 or 1Plus1Paralleled or EvolutionaryProgramming or MiPlusLambda or MiLambda!"
        return
    if argv[0] != "1Plus1" and argv[0] != "1Plus1Paralleled" and argv[
            0] != "EvolutionaryProgramming" and argv[
                0] != "MiPlusLambda" and argv[0] != "MiLambda":
        print "First  parameter like: 1Plus1 or 1Plus1Paralleled or EvolutionaryProgramming or MiPlusLambda or MiLambda!"

    if argv[0] == "MiPlusLambda" or argv[0] == "MiLambda":
        if len(argv) == 1:
            print "Second parameter like: RouletteSelection or TournamentSelection or RankingSelection!"
            return
        if argv[1] == "RouletteSelection" or argv[
                1] == "TournamentSelection" or argv[1] == "RankingSelection":
            if len(argv) == 2:
                argv.append("single-point")
            if argv[2] != "single-point" and argv[2] != "two-point" and argv[
                    2] != "uniform":
                print "Third parameter like: single-point or two-point or uniform!"
                return
        else:
            print "Second parameter like: RouletteSelection or TournamentSelection or RankingSelection!"
            return

    if argv[0] == "1Plus1":
        h = generation.OnePlusOneStrategy(genotype.genotype)
        #print "\n",argv[0],"strategy:\nBest before:", h.get_best().fitness
        print h.get_best().fitness
        print h.get_avg_fitness()
        while h.num_iterations < h.max_iterations:
            h.step()
            print h.get_best().fitness
            print h.get_avg_fitness()
            if h.population[0].fitness < MIN:
                break
        #print "Best after: ",h.population[0].fitness, ". Iterations:", h.num_iterations, "\n"

    if argv[0] == "1Plus1Paralleled":
        h = generation.OnePlusOneParalleledStrategy(MI, genotype.genotype)
        #print "\n",argv[0], "strategy:\nBest before:", h.get_best().fitness
        print h.get_best().fitness
        print h.get_avg_fitness()
        while h.num_iterations < h.max_iterations:
            h.step()
            print h.get_best().fitness
            print h.get_avg_fitness()
            if h.population[0].fitness < MIN:
                break
        #print "Best after: ",h.population[0].fitness, ". Iterations:", h.num_iterations, "\n"

    if argv[0] == "MiPlusLambda":
        h = generation.MiPlusLambdaStrategy(MI, LAMBDA, argv[1], argv[2])
        while h.get_best().fitness < MIN + 1:
            h = generation.MiPlusLambdaStrategy(MI, LAMBDA, argv[1], argv[2])
        #print "\n", argv[0], argv[1], argv[2], "\nBest before:", h.get_best().fitness
        print h.get_best().fitness
        print h.get_avg_fitness()
        while h.num_iterations < h.max_iterations:
            h.step()
            print h.get_best().fitness
            print h.get_avg_fitness()
            if h.population[0].fitness < MIN:
                break
        #print "Best after: ",h.population[0].fitness, ". Iterations:", h.num_iterations, "\n"

    if argv[0] == "MiLambda":
        h = generation.MiLambdaStrategy(MI, LAMBDA, argv[1], argv[2])
        while h.get_best().fitness < MIN + 1:
            h = generation.MiLambdaStrategy(MI, LAMBDA, argv[1], argv[2])
        #print "\n", argv[0], "strategy",argv[1], argv[2], "\nBest before:", h.get_best().fitness
        print h.get_best().fitness
        print h.get_avg_fitness()
        while h.num_iterations < h.max_iterations:
            h.step()
            print h.get_best().fitness
            print h.get_avg_fitness()
            if h.population[0].fitness < MIN:
                break
        #print "Best after: ",h.population[0].fitness, ". Iterations:", h.num_iterations, "\n"

    if argv[0] == "EvolutionaryProgramming":
        h = generation.EvolutionaryProgrammingStrategy(MI)
        while h.get_best().fitness < MIN + 1:
            h = generation.EvolutionaryProgrammingStrategy(MI)
        #print "\n", argv[0], "strategy", argv[1], argv[2], "\nBest before:", h.get_best().fitness
        print h.get_best().fitness
        print h.get_avg_fitness()
        while h.num_iterations < h.max_iterations:
            h.step()
            print h.get_best().fitness
            print h.get_avg_fitness()
            if h.population[0].fitness < MIN:
                break
        #print "Best after: ",h.population[0].fitness, ". Iterations:", h.num_iterations, "\n"

    return