-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem_instance.py
More file actions
308 lines (241 loc) · 9.06 KB
/
problem_instance.py
File metadata and controls
308 lines (241 loc) · 9.06 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import copy
import warnings
import cpmpy as cp
from cpmpy.expressions.core import Expression
from cpmpy.expressions.variables import _NumVarImpl, NDVarArray
from cpmpy.expressions.utils import is_any_list
from itertools import combinations
from ..utils import get_scope, replace_variables
class ProblemInstance:
""" Class representing a problem instance with constraints, variables, and parameters. """
def __init__(self, *, init_cl=None, variables=None, params=None, language=None, name=None, bias=None, excluded=None,
visualize=None):
"""
Initialize the ProblemInstance with optional constraints, variables, parameters, and name.
:param init_cl: The set of initially known constraints of the problem, default is an empty list.
:param language: A list of relations to be used as the language, default is an empty list.
:param bias: A list of candidate constraints, default is an empty list.
:param variables: A list of Variable objects, default is an empty list.
:param params: A dictionary of parameters, default is an empty dictionary.
:param name: The name of the problem instance, default is an empty string.
:param excluded: A list of excluded constraints, default is an empty list.
:param visualize: A function to visualize assignments of the problem instance, default is a simple print.
"""
self._X = []
self.variables = variables
self._params = params if params is not None else dict()
self._name = name if name is not None else ""
self._cl = init_cl if init_cl is not None else []
self.language = language if language is not None else []
self._bias = bias if bias is not None else []
self._excluded_constraints = excluded if excluded is not None else []
self._visualize = visualize if visualize is not None else print
if not is_any_list(self._cl) or \
not all(isinstance(c, Expression) for c in self._cl):
raise TypeError(f"'constraints' argument in ProblemInstance should be a list of Constraint objects: "
f"{self._cl}")
if not isinstance(self._variables, NDVarArray) and \
not (is_any_list(self._variables) and all(isinstance(v, _NumVarImpl) for v in list(self._variables))):
raise TypeError(f"'variables' argument in ProblemInstance should be a list of Variable objects: "
f"{type(self._variables)}")
if not isinstance(self._params, dict):
raise TypeError("'params' argument in ProblemInstance should be a dictionary of parameters")
@property
def cl(self):
"""
Get the list of constraints.
:return: The list of constraints.
"""
return self._cl
@cl.setter
def cl(self, cl):
"""
Set the list of constraints.
:param cl: The new list of constraints.
"""
self._cl = cl
@property
def bias(self):
"""
Get the list of candidate constraints.
:return: The list of candidate constraints.
"""
return self._bias
@bias.setter
def bias(self, B):
"""
Set the list of candidate constraints.
:param B: The new list of candidate constraints.
"""
self._bias = B
@property
def excluded_cons(self):
"""
Get the list of excluded constraints.
:return: The list of excluded constraints.
"""
return self._excluded_constraints
@excluded_cons.setter
def excluded_cons(self, C):
"""
Set the list of excluded constraints.
:param C: The new list of excluded constraints.
"""
self._excluded_constraints = C
@property
def variables(self):
"""
Get the list of variables.
:return: The variables.
"""
return self._variables
@variables.setter
def variables(self, vars):
"""
Set the list of variables.
:param vars: The new variables.
"""
self._variables = vars
if vars is not None:
self.X = list(self._variables.flatten())
@property
def X(self):
"""
Get the list of flattened variables.
:return: The list of flattened variables.
"""
return self._X
@X.setter
def X(self, X):
"""
Set the list of variables to operate on.
:param X: The new list of flattened variables.
"""
self._X = X
@property
def language(self):
"""
Get the language.
:return: The list language.
"""
return self._language
@language.setter
def language(self, lang):
"""
Set the language. Must be a list of cpmpy expressions
:param lang: The new language.
"""
assert all(isinstance(r, Expression) for r in lang)
self._language = lang
@property
def params(self):
"""
Get the dictionary of parameters.
:return: The dictionary of parameters.
"""
return self._params
@params.setter
def params(self, params):
"""
Set the dictionary of parameters.
:param params: The new dictionary of parameters.
"""
self._params = params
@property
def name(self):
"""
Get the name of the problem instance.
:return: The name of the problem instance.
"""
return self._name
@name.setter
def name(self, name):
"""
Set the name of the problem instance.
:param name: The new name of the problem instance.
"""
self._name = name
@property
def visualize(self):
"""
Get the visualize function of the problem instance.
"""
return self._visualize
@visualize.setter
def visualize(self, visualize):
"""
Set the visualize function of the problem instance.
"""
self._visualize = visualize
def get_cpmpy_model(self):
if len(self._cl) == 0:
warnings.warn("The model is empty, as no constraint is learned yet for this instance.")
return cp.Model(self._cl)
def construct_bias(self):
"""
Construct the bias (candidate constraints) for the problem instance.
"""
all_cons = []
X = list(self.X)
for relation in self.language:
abs_vars = get_scope(relation)
combs = combinations(X, len(abs_vars))
for comb in combs:
replace_dict = dict()
for i, v in enumerate(comb):
replace_dict[abs_vars[i]] = v
constraint = replace_variables(relation, replace_dict)
all_cons.append(constraint)
self.bias = all_cons
def construct_bias_for_var(self, v1, X=None):
"""
Construct the bias (candidate constraints) for a specific variable.
:param v1: The variable for which to construct the bias.
:param X: The set of variables to consider, default is None.
"""
if X is None:
X = self.X
assert isinstance(X, list) and set(X).issubset(set(self.X)), "When using .construct_bias_for_var(), set parameter X must be a list of variables. Instead, got: " + str(X)
all_cons = []
X = list(set(X) - {v1})
for relation in self.language:
abs_vars = get_scope(relation)
combs = combinations(X, len(abs_vars) - 1)
for comb in combs:
replace_dict = {abs_vars[0]: v1}
for i, v in enumerate(comb):
replace_dict[abs_vars[i + 1]] = v
constraint = replace_variables(relation, replace_dict)
all_cons.append(constraint)
self.bias = all_cons
def __str__(self):
"""
Return a string representation of the ProblemInstance.
:return: A string representation of the ProblemInstance.
"""
parts = [f"ProblemInstance: "]
if self._name is not None and len(self._name) > 0:
parts.append(f"\nName {self._name}.")
if self._params is not None and len(self._params) > 0:
parts.append(f"\nParameters {self._params}.")
if self._variables is not None:
parts.append(f"\nVariables: {self._variables}.")
if self._cl is not None and len(self._cl) > 0:
parts.append(f"\nConstraints: {self._cl}.")
if self.language is not None:
parts.append(f"\nLanguage: {self.language}.")
return "\n".join(parts)
def __repr__(self):
return self.name
def copy(self):
"""
Create a copy of the ProblemInstance.
:return: A copy of the ProblemInstance.
"""
instance = copy.copy(self)
instance._X = self.X.copy()
instance.cl = self.cl.copy()
instance._language = self._language.copy()
instance.bias = self.bias.copy()
instance._excluded_constraints = self._excluded_constraints.copy()
return instance