/
main.py
144 lines (98 loc) · 3.63 KB
/
main.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
import numpy as np
import random
from selection_functions import roulette_wheel_selection
from mutation import mutate
from crossover import crossover
def MinOne(x):
global NFE
NFE += 1
n_var = len(x)
indices1 = np.arange(0, n_var, 2)
indices2 = np.arange(1, n_var, 2)
z = sum(x[indices1])-sum(x[indices2])
return z
def main():
global NFE
NFE = 0
CostFunction = MinOne # Cost Function
n_var = 20 # Number of Decision Variables
VarSize = np.array([1, n_var]) # Decision Variables Matrix Size
# GA Parameters
MaxIt = 500 # Maximum Number of Iterations
nPop = 10 # Population Size
pc = 0.8 # crossover Percentage
nc = 2 * round(pc * nPop / 2) # Number of Offsprings (Parnets)
pm = 0.1 # Mutation Percentage
nm = round(pm * nPop) # Number of Mutants
mu = 0.1 # Mutation Rate
beta = 8
class EmptyIndividual():
pass
Best = [EmptyIndividual() for i in range(MaxIt)]
pop = np.array([EmptyIndividual() for i in range(nPop)])
for i in range(nPop):
# Initialize Position
pop[i].Position = np.array([random.randint(0, 1) for i in range(n_var)])
# Evaluation
pop[i].Cost = CostFunction(pop[i].Position)
# Sort Population
Costs = np.array([pop[i].Cost for i in range(nPop)])
SortOrder = np.argsort(Costs)
pop = pop[SortOrder]
# Array to Hold Best Cost Values
BestCost = np.zeros([MaxIt, 1])
# Store Cost
WorstCost = pop[-1].Cost
# Array to Hold Number of Function Evaluations
nfe = np.zeros([MaxIt, 1])
for it in range(MaxIt):
# Calculate Selection Probabilities
P = np.expm1(-beta * Costs / WorstCost)
P = P / sum(P)
# crossover
popc = np.array([[EmptyIndividual() for i in range(int(nc / 2))]
, [EmptyIndividual() for i in range(int(nc / 2))]]).transpose()
for k in range(int(nc / 2)):
i1 = roulette_wheel_selection(P)
i2 = roulette_wheel_selection(P)
# Select Parents
p1 = pop[i1]
p2 = pop[i2]
# Apply crossover
popc[k, 0].Position, popc[k, 1].Position = crossover(p1.Position, p2.Position)
# Evaluate Offsprings
popc[k, 0].Cost = CostFunction(popc[k, 0].Position)
popc[k, 1].Cost = CostFunction(popc[k, 1].Position)
popc = np.array([popc[i] for i in range(len(popc))]).transpose()
popc = np.concatenate((popc[0], popc[1]), axis=0)
# Mutation
popm = np.array([EmptyIndividual() for i in range(nm)])
for k in range(nm):
# Select Parent
i = random.randint(0, nPop - 1)
p = pop[i]
# Apply Mutation
popm[k].Position = mutate(np.array(p.Position), mu)
# Evaluate Mutant
popm[k].Cost = CostFunction(popm[k].Position)
# Create Merged Population
pop = np.concatenate((pop, popc, popm), axis=0)
# Sort Population
Costs = np.array([pop[i].Cost for i in range(len(pop))])
SortOrder = np.argsort(Costs)
Costs = Costs[SortOrder]
pop = pop[SortOrder]
# Truncation
pop = pop[0:nPop]
Costs = Costs[0:nPop]
# Store Best Cost Ever Found
BestCost[it] = pop[0].Cost
Best[it] = pop[0]
# Store Worst Cost Ever Found
WorstCost = pop[-1].Cost
# Store NFE
nfe[it] = NFE
# Show Iteration Information
print('Iteration', it, ': NFE = ', nfe[it], ', Best Cost = ', BestCost[it], 'Chromosome: ', pop[0].Position)
if __name__ == '__main__':
main()