-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.py
69 lines (48 loc) · 1.35 KB
/
Model.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
import copy
class Model:
def __init__(self, n, U, w_data, b_data):
self.w_data = w_data
self.b_data = b_data
self.n = n
self.U = U
self.check_validity()
def check_validity(self):
"""
Verify that data stored expose only valid values
"""
if not self.is_valid():
raise NotValidModelError()
def is_valid(self):
return self.n >= 0 and self.U >= 0 and \
self._is_valid_w() and self._is_valid_b()
def copy(self):
return copy.copy(self)
def set_all_b(self, new_b_val):
self.b_data = [new_b_val for x in self.b_data]
def remove_U(self):
if self.U <= 1:
raise NotValidModelError("Invalid value of U")
self.U -= 1
self.b_data = self.b_data[:-1]
# remove last row (which refers to the last depot) and last column
self.w_data = [x[:-1] for x in self.w_data][:-1]
self.check_validity()
def _is_valid_w(self):
if len(self.w_data) != self.n+self.U:
return False
for w_arr in self.w_data:
if len(w_arr) != self.n+self.U:
return False
for w_elem in w_arr:
if w_elem < 0.0:
return False
return True
def _is_valid_b(self):
if len(self.b_data) != self.U:
return False
for b_val in self.b_data:
if b_val < 0.0:
return False
return True
class NotValidModelError(Exception):
pass