-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_C_vs_Q.py
70 lines (55 loc) · 2.16 KB
/
plot_C_vs_Q.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
import matplotlib.pyplot as plt
import numpy as np
from algorithm import C_min, C_max, Q, lower, upper
x = range(int(C_min),int(C_max)+1)
y = []
MAX_ITERATION = 500
def findMin(T_11,C, iterations):
# append the 12th index to T_11 to make T
T = np.append(T_11, C-np.sum(T_11))
# compute Q
Q_current = Q(T)
#if the number of iterations is greater than maximum iterations, return
if iterations > MAX_ITERATION:
return Q_current
# record minimum Q and the move that minimize Q
Q_min = Q_current
#target_move[0] is the index to change, target_move[1] is the direction of change, either -1 or 1, 0 means not moving
target_move = [0,0]
# iterate through all possible moves
for i in range(11):
for j in [-1,1]:
# whether the new condition lie within the upper and lower bounds for each index
if lower[i]<=T[i]+j and T[i]+j <= upper[i] and lower[11]<=T[11]-j and T[11]-j <= upper[11]:
# the new schedule
T_new = T
T_new[i]+=j
T_new[11]-=j
# compute Q for the new schedule
Q_new = Q(T_new)
# If the new schedule has a lower Q, than record that move
if Q_new < Q_min:
target_move = [i,j]
Q_min = Q_new
# if not moving is the best option, then the current Q is minimum
if target_move[1] == 0:
return Q_current
# do the move
T_11_new = T_11
T_11_new[target_move[0]]+=target_move[1]
# iterate
return findMin(T_11_new, C, iterations+1)
#Check whether the cost has any possible schedule within the constraint
def min_Q(C):
#initial position
T_11 = np.round((upper[:11]-lower[:11])*(C-C_min)/(C_max-C_min))+lower[:11]
#random initial position
#T_11 = np.array([np.random.randint(l, u + 1) for l, u in zip(lower[:11], upper[:11])])
return findMin(T_11,C,0)
for i in x:
#compute minimum Q for each cost
y.append(np.log(min_Q(i)))
# plot
plt.plot(x,y)
plt.xlabel("C") # X-axis label
plt.ylabel("Q") # Y-axis label