-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossover.py
157 lines (138 loc) · 4.78 KB
/
crossover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#coding: utf-8
import os
import copy
import numpy as np
import numpy.random as npr
from individual import Individual
from population import Population
class Crossover(object):
"""
Crossover Class
Crossover Algorithm
* One point
* Two points
* Random points
"""
@classmethod
def onePoint(self, parents, n, gene_size, crate):
"""
One point crossover
[imagine]
parent1: [0, 1, 1]
=> crossover => child: [1(p1), 0(p2), 0(p2)]
parent2: [1, 1, 0]
[example]
Crossover.Onepoint(list.parents, 5) => [crossover the parents for individuals size]
:param list[Individual] parents: Selected parents
:param int n: Size of individuals
:param int gene_size: Size of gene
:param int crate: Probability of crossover
:returns: Children
:rtype: list[Individual]
"""
children = []
idx = [i for i in range(gene_size)]
for i in range(n):
child = Individual()
parent1, parent2 = npr.choice(parents, 2, replace=False)
gene1 = copy.copy(parent1.getGene())
gene2 = parent2.getGene()
point = npr.choice(idx, 1)[0]
if (crate>npr.random()):
gene1[point:] = gene2[point:]
child.setGene(gene1)
children.append(child)
return (children)
@classmethod
def twoPoints(self, parents, n, gene_size, crate):
"""
Two points crossover
[imagine]
parent1: [0, 1, 1, 1]
=> crossover => child: [0(p1), 1(p2), 0(p2), 1(p1)]
parent2: [1, 1, 0, 1]
[example]
Crossover.Twopoints(list.parents) => [crossover the parents for individuals size]
:param list[Individual] parents: Selected parents
:param int n: Size of individuals
:param int gene_size: Size of gene
:param int crate: Probability of crossover
:returns: Children
:rtype: list[Individual]
"""
children = []
idx = [i for i in range(gene_size)]
for i in range(n):
child = Individual()
parent1, parent2 = npr.choice(parents, 2, replace=False)
gene1 = copy.copy(parent1.getGene())
gene2 = parent2.getGene()
points = npr.choice(idx, 2)
points.sort()
if (crate>npr.random()):
gene1[points[0]:points[1]+1] = gene2[points[0]:points[1]+1]
child.setGene(gene1)
children.append(child)
return (children)
@classmethod
def randomPoints(self, parents, n, gene_size, crate):
"""
Random points crossover
[imagine]
parent1: [0, 1, 1, 1]
=> crossover => child: [1(p2), 1(p1), 1(p1), 1(p2)]
parent2: [1, 1, 0, 1]
[example]
Crossover.Twopoints(list.parents) => [crossover the parents for individuals size]
:param list[Individual] parents: Selected parents
:param int n: Size of individuals
:param int gene_size: Size of gene
:param int crate: Probability of crossover
:returns: Children
:rtype: list[Individual]
"""
children = []
idx = [i for i in range(gene_size)]
n_random = npr.randint(1, gene_size+1)
for i in range(n):
child = Individual()
parent1, parent2 = npr.choice(parents, 2, replace=False)
gene1 = copy.copy(parent1.getGene())
gene2 = parent2.getGene()
points = npr.choice(idx, n_random, replace=False)
if (crate>npr.random()):
for j in points:
gene1[j] = gene2[j]
child.setGene(gene1)
children.append(child)
return (children)
if __name__ == "__main__":
from readDataset import readDataset
from individual import Individual
from select import Select
dataset = readDataset("./dataset/binary.txt")
population = Population()
for i in range(5):
ind = Individual()
ind.createGene(dataset, 10)
population.addInd(ind)
def evaluate(ind):
fitness = sum(ind)
return (fitness)
population.calcFitness(evaluate)
population.show()
parents = Select.Tournament(population, 5, 3, "max")
for ind in parents:
ind.show()
print("Onepoint")
children = Crossover.onePoint(parents, 5, 10, 0.7)
for ind in children:
ind.show()
print("Twopoints")
children = Crossover.twoPoints(parents, 5, 10, 0.7)
for ind in children:
ind.show()
print("Randompoints")
children = Crossover.randomPoints(parents, 5, 10, 0.7)
for ind in children:
ind.show()