-
Notifications
You must be signed in to change notification settings - Fork 8
/
models.py
118 lines (97 loc) · 3.8 KB
/
models.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
import numpy as np
from mipcl_py.mipshell.mipshell import *
class ResourceAllocation(Problem):
"""docstring for ResourceAllocation"""
def __init__(self, parameters):
Problem.__init__(self,"allocation")
# self.arg = arg
self.RN = parameters['RN']
self.N = parameters['N']
self.T = parameters['T']
self.feasibleInd = parameters['feasibleInd']
self.weights = parameters['weights']
self.cost = parameters['cost']
def model(self):
# define optimization problem
# VARIABLES
# decision variables
self.x = x = VarVector([self.N, self.RN, self.T], 'x', BIN)
# CONSTRAINTS
# C1: all requirements must have resources assigned to them
for j in range(self.RN):
for t in range(self.T):
if self.N >= self.RN:
sum_(x[s][j][t] for s in range(self.N)) == 1
else:
sum_(x[s][j][t] for s in range(self.N)) <= 1
# C2: set to zero the variables when the combination i,j is not feasible
for i in range(self.N):
for j in range(self.RN):
for t in range(self.T):
if not self.feasibleInd[i][j]: # if this combination is not feasible
x[i][j][t] == 0
# C3: 1 resource can only be assigned to a requirement at the same time
for i in range(self.N):
for t in range(self.T):
if self.N > self.RN:
sum_(x[i][s][t] for s in range(self.RN)) <= 1
else:
sum_(x[i][s][t] for s in range(self.RN)) == 1
# Objectives
# type
w = self.weights['type']
c = self.cost['type']
J_1 = sum_(w * c[i][j]*x[i][j][t] for i in range(self.N) for j in range(self.RN) for t in range(self.T))
# role
w = self.weights['role']
c = self.cost['role']
J_2 = sum_(w * c[i][j] * x[i][j][t] for i in range(self.N) for j in range(self.RN) for t in
range(self.T))
# sum of all objectives
#J = J_RES
J = J_1 + J_2
# minimize objective
minimize(J)
def getSolution(self):
solution = None
if self.is_infeasible:
print("INFEASIBLE")
if self.is_unbounded:
print("UNBOUNDED")
if self.is_solution is not None:
if self.is_solution:
print("A solution was obtain")
if self.is_solutionOptimal:
print(" OPTIMAL")
else:
print(" NOT OPTIMAL")
solution = dict()
solution['Objective'] = self.getObjVal()
# value of decision variables
solution['Variables'] = self.x
print(solution)
return solution
def printSolution(self):
print("\n\nSOLUTION\nAssignment of Resources (rows) to Resource Needs (columns)")
a = " "
xLabel1 = " "
xLabel2 = " "
for j in range((self.RN)):
a += " " + str(j)
for t in range(self.T):
xLabel1 += a +"|"
xLabel2 += " t=" + str(t) + " | "
print(xLabel2)
print("--------------------------------------------------------------")
print(xLabel1)
print("--------------------------------------------------------------")
x = self.x
for i in range((self.N)):
a = str(i) + ": "
for t in range(self.T):
for j in range((self.RN)):
# print(feasibleInd[i][j])
a += str(int(self.x[i][j][t].val)) + " "
a += " | "
print(a)
print("--------------------------------------------------------------")