-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenetic.py
118 lines (76 loc) · 2.75 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
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
import random
import sys
import math
from BranchAndBound import BranchAndBound
# INCREASE MAXIMUM RECURSION SIZE
sys.setrecursionlimit(1500)
# DEFINE GENETIC ALGORITHM'S VALUE
PopulationSize = 100
MutationRate = 0.2
MAX_DEPTH = 1000
def GeneratePopulation(data, BaseState):
State = [0] * data.Size()
BranchAndBound(data, State)
BaseState.append(State.copy())
for i in range(data.Size()):
State[i] = 1
BaseState.append(State.copy())
State[i] = 0
State.clear()
for _ in range(PopulationSize * 100):
State.clear()
for _ in range(0, data.Size()):
State.append(random.randint(0, 1))
BaseState.append(State.copy())
BaseState.sort(reverse = True, key = data.CalcFitness)
del BaseState[PopulationSize:]
def FlipBit(x):
if (x == 0):
return 1
else:
return 0
def Genetic(data, BaseState, Depth):
if (Depth >= MAX_DEPTH):
return
State = []
count = 0
for i in range(PopulationSize - 1):
if (i == 0):
NumLoop = math.ceil(PopulationSize / 2)
else:
NumLoop = random.randint(0, math.ceil(PopulationSize / 6))
while (NumLoop != 0):
NumLoop -= 1
j = random.randint(i + 1, PopulationSize - 1)
State.clear()
count += 1
#pos = random.randint(1, data.Size() - 1)
pos = random.randint(int(data.Size() * 0.25), int(data.Size() * 0.75))
State = BaseState[i][:pos] + BaseState[j][pos:]
if (State not in BaseState and data.CalcWeight(State) <= data.Capacity()):
BaseState.append(State.copy())
if (count == PopulationSize):
break
if (count == PopulationSize):
break
BaseState.sort(reverse = True, key = data.CalcFitness)
del BaseState[PopulationSize:]
# Mutation
for _ in range(math.ceil(MutationRate * len(BaseState))):
i = random.randint(1, len(BaseState) - 1)
State = BaseState[i].copy()
k = random.randint(0, data.Size() - 1)
State[k] = FlipBit(State[k])
if (State not in BaseState and data.CalcWeight(State) <= data.Capacity()):
BaseState[i][k] = FlipBit(BaseState[i][k])
BaseState.sort(reverse = True, key = data.CalcFitness)
Genetic(data, BaseState, Depth + 1)
def Genetic_Main(data, FileParameter):
BaseState = list()
GeneratePopulation(data, BaseState)
Genetic(data, BaseState, 0)
for State in BaseState:
if (data.InAllClass(State) and data.CalcWeight(State) <= data.Capacity()):
data.WriteResult(FileParameter, State)
return
data.WriteResult(FileParameter, [0] * data.Size())