Beispiel #1
0
def new_child(pairs):
    child_list = list()
    for pair in pairs:
        tmp = randint(1, 5)
        child_list.append(
            Individ(pair[0].gen & (64 - 2**tmp) | pair[1].gen & (2**tmp - 1)))
        child_list.append(
            Individ(pair[1].gen & (64 - 2**tmp) | pair[0].gen & (2**tmp - 1)))

    return child_list
Beispiel #2
0
    def __init__(self, repo):
        self.__repo = repo
        self.__scores = {}
        self.__found = False
        self.__currentSolution = Individ(self.__repo.getN())
        self.__n = repo.getN()
        self.__generation = []
        list1 = []
        for i in range(1, self.__n + 1):
            list1.append(i)

        self.__perms = list(product(list1, repeat=2))
Beispiel #3
0
def generate_initial_population(N, n):
    """
    N - размер популяции
    n - количество хромосом
    """
    population = []

    for i in xrange(N):
        individ = Individ(N)
        individ.generate_individ(n)
        population.append(individ)

    return population
Beispiel #4
0
 def iteration(self):
     parents = range(self.nrInd)
     nrChilds = len(parents) // 2
     offspring = Population(nrChilds, self.problem)
     for i in range(nrChilds):
         ind = Individ(self.problem)
         offspring.population[i] = ind.crossover(
             self.population.population[i << 1],
             self.population.population[(i << 1) | 1],
             self.probability_crossover)
         offspring.population[i].mutate(self.probability_mutate)
     offspring.evaluate()
     self.population.reunion(offspring)
     self.population.selection(self.nrInd)
Beispiel #5
0
 def __init__(self, nrInd, problem):
     self.nrInd = nrInd
     self.problem = problem
     self.population = []
     for ind in range(nrInd):
         self.population.append(Individ(self.problem))
     self.size = len(self.population)
Beispiel #6
0
def gen_alg(count, alg_type):
    seed(time.time())
    individ_list = list()

    for _ in range(4):
        individ_list.append(Individ(randint(0, 63)))

    calc_gen_alg(individ_list, count, alg_type)
Beispiel #7
0
    def generateSolutions(self):

        randomRow = randint(0, self.__n - 1)
        newGen = []

        newIndivid = Individ(self.__n)

        for perm in list(combinations(self.__perms, 3)):
            sol = self.__currentSolution.getIndivid()

            sol[0] = list(perm).copy()
            '''
            for item in sol:
                print(item)
            print("new")
            '''
            newIndivid.setIndivid(sol)
            newGen.append(newIndivid)

        self.__generation = newGen.copy()
Beispiel #8
0
    def next_generation(self):
        self.sort()
        new_population = []
        for i in range(
                Population.no_individuals //
                10):  #elitism  -  the top 10% are automatically preserved
            new_population.append(self.population[i])
        while (len(new_population) < Population.no_individuals):
            parent1 = self.selection()
            parent2 = self.selection()
            child1, child2 = Individ.crossover(parent1, parent2)
            child1.mutate()
            child2.mutate()
            new_population.append(child1)
            new_population.append(child2)

        self.population = new_population
Beispiel #9
0
 def generate(self, problem, num_individs):
     self.num_individs = num_individs
     self.individs = []
     for i in range(0, self.num_individs):
         individ = Individ(problem.num_vertices())
         self.individs.append(individ)
Beispiel #10
0
 def populate(self):
     for i in range(self.__no):
         new = Individ(self.__n)
         self.__population.append(new)
Beispiel #11
0
    def crossOver(self):

        population = self.getPopulation()

        newPopulation = []

        for i in range(0, len(population) // 2):
            newIndivid1 = Individ(population[0].getSize())
            newIndivid2 = Individ(population[0].getSize())
            first = population[i].getIndivid()
            second = population[i + 1].getIndivid()

            if (len(first) == 3):
                firstNew = []
                secondNew = []

                firstNew.append(second[2])
                firstNew.append(first[1])
                firstNew.append(second[0])

                secondNew.append(first[2])
                secondNew.append(second[1])
                secondNew.append(first[0])

                newIndivid1.setIndivid(firstNew)
                newIndivid2.setIndivid(secondNew)

                newPopulation.append(newIndivid1)
                newPopulation.append(newIndivid2)

            if (len(first) == 4):
                firstNew = []
                secondNew = []

                firstNew.append(second[3])
                firstNew.append(second[1])
                firstNew.append(second[2])
                firstNew.append(second[0])

                secondNew.append(first[3])
                secondNew.append(first[1])
                secondNew.append(first[2])
                secondNew.append(first[2])

                newIndivid1.setIndivid(firstNew)
                newIndivid2.setIndivid(secondNew)

                newPopulation.append(newIndivid1)
                newPopulation.append(newIndivid2)

            if (len(first) == 5):
                firstNew = []
                secondNew = []

                firstNew.append(second[0])
                firstNew.append(second[2])
                firstNew.append(second[1])
                firstNew.append(second[4])
                firstNew.append(second[3])

                secondNew.append(first[0])
                secondNew.append(first[2])
                secondNew.append(first[1])
                secondNew.append(first[4])
                secondNew.append(first[3])

                newIndivid1.setIndivid(firstNew)
                newIndivid2.setIndivid(secondNew)

                newPopulation.append(newIndivid1)
                newPopulation.append(newIndivid2)

        self.__repo.setPopulation(newPopulation)
Beispiel #12
0
class HillController:
    def __init__(self, repo):
        self.__repo = repo
        self.__scores = {}
        self.__found = False
        self.__currentSolution = Individ(self.__repo.getN())
        self.__n = repo.getN()
        self.__generation = []
        list1 = []
        for i in range(1, self.__n + 1):
            list1.append(i)

        self.__perms = list(product(list1, repeat=2))

    def getCurrentSolution(self):
        return self.__currentSolution

    def isFound(self):
        return self.__found

    def isSolution(self):
        s = self.__currentSolution.getIndivid()
        size = self.__currentSolution.getSize()
        ok = True

        for column in range(0, size):
            if ok == False:
                break
            for i in range(size - 1):
                if ok == False:
                    break
                for j in range(i + 1, size):
                    if s[i][column][0] == s[j][column][0]:
                        ok = False
                        break
                    if s[i][column][1] == s[j][column][1]:
                        ok = False

        for row in range(0, size):
            if ok == False:
                break
            for i in range(size - 1):
                if ok == False:
                    break
                for j in range(i + 1, size):
                    if s[row][i][0] == s[row][j][0]:
                        ok = False
                        break
                    if s[row][i][1] == s[row][j][1]:
                        ok = False

        self.__found = ok

    def generateSolutions(self):

        randomRow = randint(0, self.__n - 1)
        newGen = []

        newIndivid = Individ(self.__n)

        for perm in list(combinations(self.__perms, 3)):
            sol = self.__currentSolution.getIndivid()

            sol[0] = list(perm).copy()
            '''
            for item in sol:
                print(item)
            print("new")
            '''
            newIndivid.setIndivid(sol)
            newGen.append(newIndivid)

        self.__generation = newGen.copy()

    def setScores(self):
        score = 0
        population = self.__generation
        self.__scores = {}

        for item in population:
            score = 0
            duplicates = {
                str((1, 1)): -1,
                str((1, 2)): -1,
                str((1, 3)): -1,
                str((1, 4)): -1,
                str((1, 5)): -1,
                str((2, 1)): -1,
                str((2, 2)): -1,
                str((2, 3)): -1,
                str((2, 4)): -1,
                str((2, 5)): -1,
                str((3, 1)): -1,
                str((3, 2)): -1,
                str((3, 3)): -1,
                str((3, 4)): -1,
                str((3, 5)): -1,
                str((4, 1)): -1,
                str((4, 2)): -1,
                str((4, 3)): -1,
                str((4, 4)): -1,
                str((4, 5)): -1,
                str((5, 1)): -1,
                str((5, 2)): -1,
                str((5, 3)): -1,
                str((5, 4)): -1,
                str((5, 5)): -1
            }

            for row in item.getIndivid():
                for i in range(len(row)):
                    duplicates[str(row[i])] += 1

            values = list(duplicates.values())

            for v in values:
                if v > 0:
                    score += v

            self.__scores[item] = score

    def selectTheBest(self):

        x = self.__scores
        a = dict(sorted(x.items(), key=operator.itemgetter(1)))

        print(list(a.values())[0])

        self.__currentSolution = list(a.keys())[0]
        self.isSolution()
Beispiel #13
0
 def __init__(self):
     self.population = []
     for i in range(Population.no_individuals):
         p = Individ([])
         self.population.append(p)