Skip to content

Commit fc41768

Browse files
committed
musx: compatible with ort 9.0
1 parent 8f4670d commit fc41768

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

examples/advanced/musx.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def musx(soft_constraints, hard_constraints=[], verbose=False):
4848
for con in soft_constraints:
4949
# see if solver supports reification of 'con'
5050
try:
51-
m = Model(BoolVar().implies(con))
51+
m = Model([BoolVar().implies(con)])
5252
CPMpyORTools(m).solve()
5353
# it did
5454
soft_assum.append(con)
@@ -89,21 +89,28 @@ def musx_pure(soft_constraints, hard_constraints=[], verbose=False):
8989
hard = flatten_constraint(hard_constraints) # batch flatten
9090
soft = [flatten_constraint(c) for c in soft_constraints]
9191

92+
if Model(hard+soft).solve():
93+
if verbose:
94+
print("Unexpectedly, the model is SAT")
95+
return []
96+
9297
mus_idx = [] # index into 'soft_constraints' that belong to the MUS
9398

9499
# init solver with hard constraints
95-
s_base = CPMpyORTools(Model(hard))
100+
#s_base = CPMpyORTools(Model(hard))
101+
m_base = Model(hard)
96102
for i in range(len(soft_constraints)):
97-
s_without_i = copy.deepcopy(s_base) # deep copy solver state
103+
#s_without_i = copy.deepcopy(s_base) # deep copy solver state
98104
# add all other remaining (flattened) constraints
105+
s_without_i = CPMpyORTools(m_base)
99106
s_without_i += soft[i+1:]
100107

101108
if s_without_i.solve():
102109
# with all but 'i' it is SAT, so 'i' belongs to the MUS
103110
if verbose:
104111
print("\tSAT so in MUS:", soft_constraints[i])
105112
mus_idx.append(i)
106-
s_base += [soft[i]]
113+
m_base += [soft[i]]
107114
else:
108115
# still UNSAT, 'i' does not belong to the MUS
109116
if verbose:
@@ -149,6 +156,8 @@ def musx_assum(soft_constraints, hard_constraints=[], verbose=False):
149156
else:
150157
# unsat core is an unsatisfiable subset
151158
mus_vars = assum_solver.get_core()
159+
if verbose:
160+
assert (not assum_solver.solve(assumptions=mus_vars)), "core is SAT!?"
152161

153162
# now we shrink the unsatisfiable subset further
154163
i = 0 # we wil dynamically shrink mus_vars

0 commit comments

Comments
 (0)