-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_allocation.py
172 lines (135 loc) · 6.48 KB
/
task_allocation.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
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
#!/usr/bin/python
import sys
import database
import sqlite3
import program
import random
from shutil import copyfile
def first_fit(available_id_cc, ram, cpu, type_of_cpu, filename):
final_answer_id = -1
final_answer_foreign_id, selected_cpu, selected_ram, type = [-1, -1, -1, -1]
ccc = sqlite3.connect(filename)
found = 0
for id_cc in available_id_cc:
if found == 0:
counter = 0
for row in ccc.execute("SELECT * FROM NODES WHERE ram > " + str(ram) + " AND cpu > " + str(cpu)+" AND CC_ID = "+str(id_cc)+" AND CPU_TYPE = "+str(type_of_cpu)):
if counter == 0:
final_answer_id = row[0]
final_answer_foreign_id = row[1]
selected_ram = row[2]
selected_cpu = row[3]
type = row[4]
counter += 1
found = 1
break
if found == 1:
break
ccc.execute('''UPDATE NODES SET ram = ram - ?, cpu = cpu - ? WHERE id = ?''', (ram,cpu,final_answer_id))
ccc.execute('''UPDATE COMPUTING_CENTERS SET ram = ram - ?, cpu = cpu - ? WHERE id = ?''', (ram, cpu, final_answer_foreign_id))
ccc.commit()
ccc.close()
print_report(final_answer_id, final_answer_foreign_id, ram, cpu, type_of_cpu, type, selected_ram, selected_cpu,'first_fit.txt')
def best_fit(available_id_cc, ram, cpu, type_of_cpu, filename):
final_answer_id = -1
final_answer_foreign_id, selected_cpu, selected_ram, type = [-1, -1, -1, -1]
cc = sqlite3.connect(filename)
counter = 0
for id_cc in available_id_cc:
for row in cc.execute("SELECT * FROM NODES WHERE ram > " + str(ram) + " AND cpu > " + str(
cpu) + " AND CC_ID = " + str(id_cc) + " AND CPU_TYPE = " + str(type_of_cpu)):
if counter == 0:
min = row[2] + row[3]
final_answer_id = row[0]
final_answer_foreign_id = row[1]
selected_ram = row[2]
selected_cpu = row[3]
type = row[4]
counter += 1
elif (row[2] + row[3]) < min:
min = row[2] + row[3]
final_answer_id = row[0]
final_answer_foreign_id = row[1]
selected_ram = row[2]
selected_cpu = row[3]
type = row[4]
cc.execute('''UPDATE NODES SET ram = ram - ?, cpu = cpu - ? WHERE id = ?''', (ram, cpu, final_answer_id))
cc.execute('''UPDATE COMPUTING_CENTERS SET ram = ram - ?, cpu = cpu - ? WHERE id = ?''',
(ram, cpu, final_answer_foreign_id))
cc.commit()
cc.close()
print_report(final_answer_id, final_answer_foreign_id, ram, cpu, type_of_cpu, type, selected_ram, selected_cpu,'best_fit.txt')
def worst_fit(available_id_cc, ram, cpu, type_of_cpu, filename):
final_answer_id = -1
final_answer_foreign_id, selected_cpu, selected_ram, type = [-1, -1, -1, -1]
cworst = sqlite3.connect(filename)
counter = 0
for id_cc in available_id_cc:
for row in cworst.execute("SELECT * FROM NODES WHERE ram > " + str(ram) + " AND cpu > " + str(
cpu) + " AND CC_ID = " + str(id_cc) + " AND CPU_TYPE = " + str(type_of_cpu)):
if counter == 0:
max = row[2] + row[3]
final_answer_id = row[0]
final_answer_foreign_id = row[1]
selected_ram = row[2]
selected_cpu = row[3]
type = row[4]
counter += 1
elif (row[2] + row[3]) > max:
max = row[2] + row[3]
final_answer_id = row[0]
final_answer_foreign_id = row[1]
selected_ram = row[2]
selected_cpu = row[3]
type = row[4]
cworst.execute('''UPDATE NODES SET ram = ram - ?, cpu = cpu - ? WHERE id = ?''', (ram, cpu, final_answer_id))
cworst.execute('''UPDATE COMPUTING_CENTERS SET ram = ram - ?, cpu = cpu - ? WHERE id = ?''',
(ram, cpu, final_answer_foreign_id))
cworst.commit()
cworst.close()
print_report(final_answer_id, final_answer_foreign_id, ram, cpu, type_of_cpu, type, selected_ram, selected_cpu,'worst_fit.txt')
def print_report(final_answer_id, final_answer_foreign_id, ram, cpu, type_of_cpu, type, selected_ram, selected_cpu,algorithm_type):
if final_answer_id == -1:
report = "Processing program -> ram : " + str(ram) + " cpu : " + str(cpu) + " ,cpu_type : " + str(
type_of_cpu) + " -> NOT ENOUGH RESOURCE "
else:
report = """Processing program -> ram : """ + str(ram) + """ ,cpu : """ + str(cpu) + " ,cpu_type : " + str(
type_of_cpu) + """\nCOMPUTING_CENTER -> id : """ \
+ str(final_answer_foreign_id) + """\nNODE -> id : """ \
+ str(final_answer_id) + " ,available ram : """ + str(
selected_ram - ram) + """ ,available cpu : """ + str(selected_cpu - cpu) + " ,cpu_type : " + str(type)
with open(algorithm_type, 'a') as out:
out.write(report + '\n\n' + "#################" + '\n\n')
def main():
filename = input()
connection = database.ComputingCenters(filename)
connection.create_tables()
connection.insert_db()
copyfile(filename, 'f.db')
copyfile(filename, 'w.db')
c = sqlite3.connect(filename)
c1 = sqlite3.connect('f.db')
c2 = sqlite3.connect('w.db')
programs_queue = program.create_queue(random.randint(10, 40))
while programs_queue.empty() == False:
available_id_cc = []
available_id_cc1 = []
available_id_cc2 = []
processing_program = programs_queue.get()
r = processing_program.get_ram()
cp = processing_program.get_cpu()
cpu_type = processing_program.get_cpu_type()
for row in c.execute("SELECT ID FROM COMPUTING_CENTERS WHERE ram > " + str(r) + " AND cpu > " + str(cp)):
available_id_cc.append(row[0])
best_fit(available_id_cc, r, cp, cpu_type, filename)
for row in c1.execute("SELECT ID FROM COMPUTING_CENTERS WHERE ram > " + str(r) + " AND cpu > " + str(cp)):
available_id_cc1.append(row[0])
first_fit(available_id_cc1, r, cp, cpu_type, 'f.db')
for row in c2.execute("SELECT ID FROM COMPUTING_CENTERS WHERE ram > " + str(r) + " AND cpu > " + str(cp)):
available_id_cc2.append(row[0])
worst_fit(available_id_cc2, r, cp, cpu_type, 'w.db')
c.close()
c1.close()
c2.close()
if __name__ == "__main__":
main()