-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
108 lines (78 loc) · 2.56 KB
/
main.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
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import random
import matplotlib.pyplot as plt
from bruteforce import bruteforce
from heuristic import heuristic
MIN_SPEED = 0.1
MIN_LEN = 0.1
MIN_JOBS = 1
MIN_MACHINES = 1
MIN_BLOCKS = 1
# generate test
def generate(n, m, Jmax, Smax, blocks=None):
n_ = random.randint(MIN_JOBS, n)
m_ = random.randint(MIN_MACHINES, m)
if blocks is None:
blocks = random.randint(MIN_BLOCKS, n_)
J = [] # set of blocks of jobs
M = [] # set of machine speeds
count = n_
# generate job blocks(cliques)
for i in range(blocks):
jobs_in_block = random.randint(1, min(count - blocks + i + 1, m_)) # each block consists of at least one job
# but no more than the num of machines
count -= jobs_in_block
B = []
for j in range(jobs_in_block):
job = random.uniform(MIN_LEN, Jmax)
B.append(job)
J.append(B)
# not the best solution but works
n_ -= count
# generate machine speeds
for i in range(m_):
speed = random.uniform(MIN_SPEED, Smax)
M.append(speed)
return n_, m_, J, M
def test(n, m, J, M):
# print()
# print(n)
# print(m)
# print(J)
# print(M)
# print()
b = bruteforce(n, m, J, M)
h = heuristic(n, m, J, M)
return b, h
def main():
print("ENTER: maximum number of jobs(int)")
n = int(input())
print("ENTER: maximum number of machines(int)")
m = int(input())
print("ENTER: maximum job size(float)")
Jmax = float(input())
print("ENTER: maximum machine speed(float)")
Smax = float(input())
print("ENTER: number of tests(int)")
tests = int(input())
B = []
H = []
OX = [i for i in range(tests)]
for _ in range(tests):
n_, m_, J, M = generate(n, m, Jmax, Smax)
b, h = test(n_, m_, J, M)
B.append(b)
H.append(h)
RATIO = [B[i] / H[i] for i in range(tests)]
print("Bruteforce to heuristic average ratio: ", sum(RATIO) / len(RATIO))
# GRAPH
plt.plot(OX, B, label="bruteforce")
plt.plot(OX, H, label="heuristic")
plt.title("Q|cliques|C_max bruteforce to heuristic comparison")
plt.xlabel("test")
plt.ylabel("C_max")
plt.legend()
plt.show()
if __name__ == '__main__':
main()