-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblemInterface.py
235 lines (173 loc) · 6.25 KB
/
problemInterface.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from scipy.sparse import csr_matrix, csc_matrix
import numpy as np
from typing import List, Tuple
from enum import Enum
from utils import INF, EPSGE, EPSLE, EPSEQ, flatten_list
from abc import ABC, abstractmethod
class Sense(Enum):
LE = "<"
GE = ">"
EQ = "="
EMPTY = ""
class ProblemInterface(ABC):
@abstractmethod
def add_cons(self, sense: Sense, rhs: float) -> int:
pass
@abstractmethod
def set_coeff(self, cons_idx: int, var_idx: int, val: float):
pass
@abstractmethod
def add_column(self) -> int:
pass
@abstractmethod
def is_ub_inf(self, var_idx: int) -> bool:
pass
@abstractmethod
def is_lb_inf(self, var_idx: int) -> bool:
pass
@abstractmethod
def is_lb_zero(self, var_idx: int) -> float:
pass
@abstractmethod
def set_ub(self, var: int, val: float):
pass
@abstractmethod
def set_lb(self, var: int, val: float):
pass
@abstractmethod
def get_ub(self, var: int) -> float:
pass
@abstractmethod
def get_lb(self, var: int) -> float:
pass
@abstractmethod
def get_coeff(self, cons: int, var: int) -> float:
pass
@abstractmethod
def get_cost(self, var: int) -> float:
pass
@abstractmethod
def set_cost(self, var:int, val: float):
pass
@abstractmethod
def get_sense(self, cons: int) -> Sense:
pass
@abstractmethod
def set_sense(self, cons: int, val: Sense):
pass
@abstractmethod
def get_rhs(self, cons: int) -> float:
pass
@abstractmethod
def set_rhs(self, cons: int, val: float):
pass
@abstractmethod
def check_problem_validity(self, slack_basis: List[int]) -> None:
pass
@abstractmethod
def to_csc(self) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
pass
@abstractmethod
def copy_cons(self, cons: int) -> int:
pass
class ProblemDense(ProblemInterface):
def __init__(self, ncons: int, nvars: int, coeffs: List[float], col_indices: List[float], row_ptrs: List[float], lbs: List[float], ubs: List[float], senses: List[Sense], rhss: List[float], costs: List[float]):
self.ncons = ncons
self.nvars = nvars
self.A = csr_matrix((coeffs, col_indices, row_ptrs), shape=(ncons, nvars)).todense()
self.lbs = lbs
self.ubs = ubs
self.senses = senses
self.b = rhss
self.costs = costs
def add_cons(self, sense: Sense, rhs: float) -> int:
self.A=np.vstack([self.A, np.zeros(self.nvars)])
self.ncons += 1
self.senses.append(sense)
self.b.append(rhs)
return self.ncons -1 # index of the added cons
def copy_cons(self, cons: int) -> int:
new_cons_idx = self.add_cons(self.get_sense(cons), self.get_rhs(cons))
for var in range(self.nvars):
self.set_coeff(new_cons_idx, var, self.get_coeff(cons, var))
return new_cons_idx # return index of the new cons
def set_coeff(self, cons_idx: int, var_idx: int, val: float):
self.A[cons_idx, var_idx] = val
def add_column(self) -> int:
coeffs = np.zeros((self.ncons, 1))
self.A = np.hstack([self.A, coeffs])
self.costs.append(0)
self.ubs.append(INF)
self.lbs.append(0.0)
self.nvars += 1
return self.nvars-1 # index of the added column
def is_ub_inf(self, var_idx: int) -> bool:
return EPSGE(self.ubs[var_idx], INF)
def is_lb_inf(self, var_idx: int) -> bool:
return EPSLE(self.lbs[var_idx], -INF)
def is_lb_zero(self, var_idx: int) -> float:
return EPSEQ(self.lbs[var_idx], 0.0)
def set_ub(self, var: int, val: float):
self.ubs[var] = val
def set_lb(self, var: int, val: float):
self.lbs[var] = val
def get_ub(self, var: int) -> float:
return self.ubs[var]
def get_lb(self, var: int) -> float:
return self.lbs[var]
def get_coeff(self, cons: int, var: int) -> float:
return self.A[cons, var]
def get_cost(self, var: int) -> float:
return self.costs[var]
def set_cost(self, var:int, val: float):
self.costs[var] = val
def get_sense(self, cons: int) -> Sense:
return self.senses[cons]
def set_sense(self, cons: int, val: Sense):
self.senses[cons] = val
def get_rhs(self, cons: int) -> float:
return self.b[cons]
def set_rhs(self, cons: int, val: float):
self.b[cons] = val
def check_problem_validity(self, slack_basis: List[int]) -> None:
assert len(self.ubs) == self.nvars
assert len(self.lbs) == self.nvars
assert len(self.costs) == self.nvars
assert self.A.shape[0] == self.ncons
assert self.A.shape[1] == self.nvars
assert len(self.b) == self.ncons
assert len(self.senses) == self.ncons
for sense in self.senses:
assert sense == Sense.EQ
for var in range(self.nvars):
assert self.is_ub_inf(var)
assert self.is_lb_zero(var)
self.check_for_zero_cols()
self.check_if_slack_basis_is_identity(slack_basis)
def check_for_zero_cols(self) -> None:
for col in range(self.nvars):
assert any(flatten_list(self.A[:,col]))
def check_if_slack_basis_is_identity(self, slack_basis: List[int]) -> None:
basis = self.A[:,slack_basis]
for i in range(self.ncons):
for j in range(self.ncons):
if i == j:
assert np.isclose(1.0, abs(basis[i, j])) # can be + or - 1
else:
assert np.isclose(0.0, basis[i, j])
def to_csc(self) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
csc = csc_matrix(self.A)
return csc.data, csc.indices, csc.indptr
def get_problem(
ncons: int,
nvars: int,
coeffs: List[float],
col_indices: List[float],
row_ptrs: List[float],
lbs: List[float],
ubs: List[float],
senses: List[Sense],
rhss: List[float],
costs: List[float]
) -> ProblemInterface:
return ProblemDense(ncons, nvars, coeffs, col_indices, row_ptrs, lbs, ubs, senses, rhss, costs)