-
Notifications
You must be signed in to change notification settings - Fork 1
/
grey_wolf.py
78 lines (58 loc) · 2.75 KB
/
grey_wolf.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
#!/usr/bin/python
import sys
import sqlite3
import database
import random
import program
import pack
def print_final_answer(final_answer_list, empty_genes, filename, programs):
c = sqlite3.connect(filename)
cnt = 0
for node_id in final_answer_list:
for row in c.execute("SELECT * FROM NODES WHERE ID = " + str(node_id)):
ram = programs[cnt].get_ram()
cpu = programs[cnt].get_cpu()
cpu_type = programs[cnt].get_cpu_type()
if cnt in empty_genes:
report = "Processing program -> ram : " + str(ram) + " cpu : " + str(cpu) + " ,cpu_type : " + str(cpu_type) + " -> NOT ENOUGH RESOURCE "
else:
report = """Processing program -> ram : """ + str(ram) + """ ,cpu : """ + str(cpu) + " ,cpu_type : " + str(cpu_type) + """\nCOMPUTING_CENTER -> id : """ \
+ str(row[1]) + """\nNODE -> id : """ \
+ str(node_id) + " ,available ram : """ + str(row[2] - ram) + """ ,available cpu : """ + str(row[3]- cpu) + " ,cpu_type : " + str(cpu_type)
cnt += 1
with open('GWO.txt', 'a') as out:
out.write(report + '\n\n' + "#################" + '\n\n')
c.execute('''UPDATE NODES SET ram = ram - ?, cpu = cpu - ? WHERE id = ?''', (ram, cpu, node_id))
c.execute('''UPDATE COMPUTING_CENTERS SET ram = ram - ?, cpu = cpu - ? WHERE id = ?''', (ram, cpu, row[1]))
c.commit()
c.close()
def main():
filename = input()
connection = database.ComputingCenters(filename)
connection.create_tables()
connection.insert_db()
num_of_nodes = connection.get_num_of_nodes()
num_of_programs = random.randint(20, 30)
print("num of programs :")
print(num_of_programs)
programs, programs_queue = program.create_list(num_of_programs)
init_pack_size = 40
max_number_of_iterations = 1000
UCE = 1
new_pack = pack.Pack(init_pack_size, num_of_programs, filename, programs, num_of_nodes,max_number_of_iterations, UCE)
while new_pack.get_iteraiton() < max_number_of_iterations:
print("iteration : ", new_pack.get_iteraiton())
new_pack.chasing_the_prey()
new_pack.harassing_the_prey()
best_ans = new_pack.get_best_fitness_n(1)
print("best fitness", best_ans.get_fitness())
final_answer_list = best_ans.get_genes()
print("final_answer_list.size : ", len(final_answer_list))
print("len(programs)", len(programs))
empty_genes = best_ans.get_empty_genes()
empty_nodes_count = best_ans.calc_empty_nodes()
print("empty_nodes_count : ", empty_nodes_count)
print("len(empty_genes)", len(empty_genes))
print_final_answer(final_answer_list, empty_genes, filename, programs)
if __name__ == "__main__":
main()