-
Notifications
You must be signed in to change notification settings - Fork 6
/
genetic.py
84 lines (63 loc) · 2.31 KB
/
genetic.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
from random import randint, uniform
import numpy as np
def inverse_mutation(sequence):
# Inverts slice between start and end
jobs = len(sequence)
# Generating start and end indices randomly
start = randint(0, jobs - 2)
end = randint(start + 1, jobs - 1)
sequence[start:end] = np.fliplr([sequence[start:end]])[0]
return sequence
def pairwise_swap_mutation(sequence):
# Swaps start and end positions
jobs = len(sequence)
# Generating start and end indices randomly
start = randint(0, jobs - 2)
end = randint(start + 1, jobs - 1)
sequence[start], sequence[end] = sequence[end], sequence[start]
return sequence
def ordered_crossover(parent1, parent2):
jobs = len(parent1)
# Generating start and end indices randomly
start = randint(0, jobs - 2)
end = randint(start + 1, jobs - 1)
# Initialize child list
child = [-1] * jobs
# Copy the alleles between start-end from parent1
child[start:end + 1] = parent1[start:end + 1]
# Start from 2nd crossover point and
# copy the rest of the elements in order, cyclically
parent_index = (end + 1) % jobs
child_index = (end + 1) % jobs
while child_index != start:
if parent2[parent_index] not in child:
child[child_index] = parent2[parent_index]
child_index = (child_index + 1) % jobs
parent_index = (parent_index + 1) % jobs
return child
def roulette_wheel(sequence, makespan):
# Store inverses of makespan values in a list
inverse = []
for i in makespan:
j = 1 / i
inverse.append(j)
total_sum = 0
# Calculating sum of all the inverted values
for i in inverse:
total_sum = total_sum + i
# Generate arrays of newsize = 3 * previous size, according to RWS
newsize = 3 * len(makespan)
seq = np.ndarray((newsize, sequence.shape[1]), dtype=int)
mks = np.empty(newsize)
# Generate 'newsize' number of sequences
for r in range(newsize):
# Generating random value between 0 and total_sum
x = uniform(0, total_sum)
partial_sum = 0
for i in range(len(inverse)):
partial_sum = partial_sum + inverse[i]
if partial_sum >= x:
mks[r] = makespan[i]
seq[r] = sequence[i]
break
return seq, mks