Skip to content

Commit 66b0dde

Browse files
authored
Merge pull request #66 from ourstudio-se/63-implement-puan-rspy-020
63 implement puan rspy 020
2 parents ef52c67 + ae7758c commit 66b0dde

4 files changed

Lines changed: 23 additions & 41 deletions

File tree

puan/logic/plog/__init__.py

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def flatten(self) -> list:
215215
)
216216
)
217217

218-
def to_polyhedron(self, active: bool = False) -> pnd.ge_polyhedron:
218+
def to_polyhedron(self, active: bool = False, reduced: bool = False) -> pnd.ge_polyhedron:
219219

220220
"""
221221
Converts into a polyhedron.
@@ -244,7 +244,7 @@ def to_polyhedron(self, active: bool = False) -> pnd.ge_polyhedron:
244244
)
245245
)
246246
)
247-
lineqs = pst.TheoryPy(
247+
polyhedron_rs = pst.TheoryPy(
248248
list(
249249
map(
250250
lambda x: pst.StatementPy(
@@ -263,47 +263,28 @@ def to_polyhedron(self, active: bool = False) -> pnd.ge_polyhedron:
263263
flatten_dict.values()
264264
)
265265
)
266-
).to_lineqs()
267-
M = np.zeros((len(lineqs), 1+len(variable_id_map)))
268-
for i, lineq in enumerate(lineqs):
269-
M[i, list(map(lambda x: x+1, lineq.indices))] = lineq.coeffs
270-
M[i, 0] = -1*lineq.bias
271-
272-
polyhedron = pnd.ge_polyhedron(
273-
M,
266+
).to_polyhedron(active, reduced)
267+
268+
id_variable_map = dict(variable_id_map.values())
269+
polyedron_array = np.hstack(
270+
(
271+
np.array(polyhedron_rs.b).reshape(-1,1),
272+
np.array(np.array_split(polyhedron_rs.a.val, polyhedron_rs.a.nrows))
273+
)
274+
)
275+
return pnd.ge_polyhedron(
276+
polyedron_array,
274277
variables=[puan.variable.support_vector_variable()]+list(
275-
itertools.chain(
276-
map(
277-
lambda x: x[1].variable if hasattr(x[1], "variable") else x[1],
278-
variable_id_map.values()
279-
)
280-
)
281-
),
282-
index=list(
283278
map(
284-
lambda x: x[1].variable,
285-
filter(
286-
lambda x: type(x[1]) != puan.variable,
287-
variable_id_map.values()
288-
)
279+
maz.compose(
280+
id_variable_map.get,
281+
operator.attrgetter("id")
282+
),
283+
polyhedron_rs.variables
289284
)
290-
)
285+
),
291286
)
292287

293-
# assume top node for now until
294-
# this is default in puan-rspy
295-
if self.variable in polyhedron.variables and active:
296-
297-
polyhedron = polyhedron.reduce_columns(
298-
polyhedron.A.construct(
299-
*{self.variable.id: 1}.items(),
300-
default_value=np.nan,
301-
dtype=float,
302-
)
303-
)
304-
305-
return polyhedron
306-
307288
def negate(self) -> "AtLeast":
308289

309290
"""

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ maz==0.0.1
44
more_itertools>=8.12.0
55
dictdiffer==0.9.0
66
toposort==1.7
7-
puan-rspy>=0.1.8
7+
puan-rspy==0.2.1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"more-itertools",
1616
"maz>=0.0.1",
1717
"dictdiffer==0.9.0",
18-
"puan-rspy>=0.1.8",
18+
"puan-rspy==0.2.1",
1919
"toposort==1.7"
2020
],
2121
packages=[

tests/test_puan.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ def test_json_conversion_id_should_be_returned_if_explicitly_defined(proposition
223223
def test_from_short_wont_crash(short_proposition):
224224

225225
# Should raise if has sub propositions and bounds are other than (0,1)
226-
if len(short_proposition[2]) > 0 and short_proposition[4] != (0,1):
226+
# OR if upper bound is strict lower than lower bound
227+
if (len(short_proposition[2]) > 0 and short_proposition[4] != (0,1)) or (short_proposition[4][1] < short_proposition[4][0]):
227228
with pytest.raises(Exception):
228229
pg.AtLeast.from_short(short_proposition)
229230
else:

0 commit comments

Comments
 (0)