-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
201 lines (164 loc) · 5.66 KB
/
Copy pathmain.py
File metadata and controls
201 lines (164 loc) · 5.66 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import task
import virtualmachine
import ga
import random
import copy
highWeightageVmlist = []
mediumWeightageVmlist = []
lowWeightageVmlist = []
Tasklist = []
VirtualmachineList = []
previousfitness = 1000
def displayChromosome(dict):
chromosomeStr = ""
for keys in dict:
if(keys != "Fitness_value"):
chromosomeStr += "T" + str(keys.taskid) + \
"->VM" + str(dict[keys].vmid) + " "
else:
chromosomeStr += f"Fitness \
{dict[keys]} "
return chromosomeStr
no_of_tasks = int(input("Enter number of tasks:: "))
# no_of_virtualmachine = int(input("Enter number of virtual machines:: "))
no_of_high = int(input("Enter number of high weightage virtual machines::"))
no_of_medium = int(
input("Enter number of medium weightage virtual machines::"))
no_of_low = int(
input("Enter number of low weightage virtual machines::"))
no_of_virtualmachine = no_of_high+no_of_medium+no_of_low
print(
f"Total number of virtual machine ::{no_of_virtualmachine}")
print("Simulation started......")
print("\nTask entries initiated......\n")
for i in range(no_of_tasks):
taskobj = task.Task(i, random.randint(10000, 50000-1))
Tasklist.append(taskobj)
print("Task entities completed......\n")
print("Virtual machine entities has been started....\n")
j = 0
for i in range(no_of_high):
vmobj = virtualmachine.VirtualMachine(j, 1)
highWeightageVmlist.append(vmobj)
VirtualmachineList.append(vmobj)
j = j+1
for i in range(no_of_medium):
vmobj = virtualmachine.VirtualMachine(j, 2)
mediumWeightageVmlist.append(vmobj)
VirtualmachineList.append(vmobj)
j = j+1
for i in range(no_of_low):
vmobj = virtualmachine.VirtualMachine(j, 3)
lowWeightageVmlist.append(vmobj)
VirtualmachineList.append(vmobj)
j = j+1
initialPopulation = ga.GA.Create_intial_population(
Tasklist, VirtualmachineList, highWeightageVmlist, mediumWeightageVmlist, lowWeightageVmlist)
i = 1
print()
print('_'*40)
print("\n" + " "*11 + "INITIAL POPULATION")
print('-'*40)
print("CHROMOSOME NO. | SOLUTION ")
print('-'*40)
for i in range(15):
print("\t" + str(i+1) + "\t| " +
displayChromosome(initialPopulation[i]) + " ")
print("\nFitness value added to the chromosome\n")
initialPopulation = ga.GA.FitnessOfPopulation(initialPopulation)
for j in range(5):
print("\n"+"="*40+"Generation"+str(j)+"="*40)
i = 1
print()
print('_'*40)
print("\n" + " "*11 + "FITNESS VALUES")
print('-'*40)
print("CHROMOSOME NO. | FITNESS VALUE ")
print('-'*40)
for Chromosome in initialPopulation:
print(f"\t {i} \t| {Chromosome['Fitness_value']} ")
i = i+1
parentlist = ga.GA.Parent(initialPopulation)
print("\nparent list\n")
i = 1
print()
print('_'*40)
print("\n" + " "*11 + "PARENT LIST")
print('-'*40)
print("CHROMOSOME NO. | SOLUTION ")
print('-'*40)
for i in range(len(parentlist)):
print(f"\t {parentlist[i][0]+1} \t| " +
displayChromosome(parentlist[i][1]) + " ")
print("\noffspring\n")
bestParent, notBestParent = parentlist[0][1].copy(
), parentlist[1][1].copy()
offspring = ga.GA.Crossover(bestParent, notBestParent, 10)
# i = 1
# print()
# print('_'*40)
# print("\n" + " "*11 + "After OFFSPRING INITIAL POPULATION")
# print('-'*40)
# print("CHROMOSOME NO. | SOLUTION ")
# print('-'*40)
# for i in range(len(initialPopulation)):
# print("\t" + str(i+1) + "\t| " +
# displayChromosome(initialPopulation[i]) + " ")
i = 1
print()
print('_'*40)
print("\n" + " "*11 + "CROSS OVER")
print('-'*40)
print("CHROMOSOME NO. | OFFSPRING ")
print('-'*40)
print(f"\t {parentlist[0][0]+1}x{parentlist[1][0]+1} \t| " +
displayChromosome(offspring) + " ")
print("\nMutation..\n")
mutatedOffspring = ga.GA.Mutation(offspring, 5)
i = 1
print()
print('_'*40)
print("\n" + " "*11 + "MUTATION")
print('-'*40)
print("CHROMOSOME NO. | OFFSPRING ")
print('-'*40)
print(f"\t {parentlist[0][0]+1}x{parentlist[1][0]+1} \t| " +
displayChromosome(mutatedOffspring) + " ")
# i = 1
# print()
# print('_'*40)
# print("\n" + " "*11 + "BEFORE UPDATED INITIAL POPULATION")
# print('-'*40)
# print("CHROMOSOME NO. | SOLUTION ")
# print('-'*40)
# for i in range(len(initialPopulation)):
# print("\t" + str(i+1) + "\t| " +
# displayChromosome(initialPopulation[i]) + " ")
if(ga.GA.SimulatedAnnealing(mutatedOffspring, previousfitness) == True):
if(mutatedOffspring["Fitness_value"] != previousfitness):
print("\n Mutated Offspring has been added to the Initial Population")
initialPopulation.append(mutatedOffspring)
previousfitness = mutatedOffspring["Fitness_value"]
initialPopulation = ga.GA.SurvivalSelection(initialPopulation)
initialPopulation = ga.GA.SurvivalSelection(initialPopulation)
print("\nFinal Intial Population \n")
i = 1
print()
print('_'*40)
print("\n" + " "*11 + "UPDATED INITIAL POPULATION")
print('-'*40)
print("CHROMOSOME NO. | SOLUTION ")
print('-'*40)
for i in range(len(initialPopulation)):
print("\t" + str(i+1) + "\t| " +
displayChromosome(initialPopulation[i]) + " ")
BestOffspring = ga.GA.BestOffspring(initialPopulation)
i = 1
print()
print('_'*40)
print("\n" + " "*11 + "BEST OFFSPRING")
print('-'*40)
print("CHROMOSOME NO. | SOLUTION ")
print('-'*40)
print("\t" + str(BestOffspring[0]+1) + "\t| " +
displayChromosome(BestOffspring[1]) + " ")