Skip to content

Commit

Permalink
Merge branch 'master' into is_bool_boolval
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout4 committed Mar 1, 2024
2 parents e5bc5a7 + 54d9681 commit 797e3be
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cpmpy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def from_file(fname):
def copy(self):
"""
Makes a shallow copy of the model.
Constraints and variables are shared among the original and copied model.
Constraints and variables are shared among the original and copied model (references to the same Expression objects). The /list/ of constraints itself is different, so adding or removing constraints from one model does not affect the other.
"""
if self.objective_is_min:
return Model(self.constraints, minimize=self.objective_)
Expand All @@ -278,4 +278,4 @@ def copy(self):
# keep for backwards compatibility
def deepcopy(self, memodict={}):
warnings.warn("Deprecated, use copy.deepcopy() instead, will be removed in stable version", DeprecationWarning)
return copy.deepcopy(self, memodict)
return copy.deepcopy(self, memodict)
26 changes: 13 additions & 13 deletions cpmpy/solvers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def param_combinations(all_params, remaining_keys=None, cur_params=None):
cur_params=cur_params)

class SolverLookup():
@staticmethod
def base_solvers():
@classmethod
def base_solvers(cls):
"""
Return ordered list of (name, class) of base CPMpy
solvers
Expand All @@ -78,10 +78,10 @@ def base_solvers():
("exact", CPM_exact),
]

@staticmethod
def solvernames():
@classmethod
def solvernames(cls):
names = []
for (basename, CPM_slv) in SolverLookup.base_solvers():
for (basename, CPM_slv) in cls.base_solvers():
if CPM_slv.supported():
names.append(basename)
if hasattr(CPM_slv, "solvernames"):
Expand All @@ -90,23 +90,23 @@ def solvernames():
names.append(basename+":"+subn)
return names

@staticmethod
def get(name=None, model=None):
@classmethod
def get(cls, name=None, model=None):
"""
get a specific solver (by name), with 'model' passed to its constructor
This is the preferred way to initialise a solver from its name
"""
cls = SolverLookup.lookup(name=name)
solver_cls = cls.lookup(name=name)

# check for a 'solver:subsolver' name
subname = None
if name is not None and ':' in name:
_,subname = name.split(':',maxsplit=1)
return cls(model, subsolver=subname)
return solver_cls(model, subsolver=subname)

@staticmethod
def lookup(name=None):
@classmethod
def lookup(cls, name=None):
"""
lookup a solver _class_ by its name
Expand All @@ -115,7 +115,7 @@ def lookup(name=None):
"""
if name is None:
# first solver class
return SolverLookup.base_solvers()[0][1]
return cls.base_solvers()[0][1]

# split name if relevant
solvername = name
Expand All @@ -124,7 +124,7 @@ def lookup(name=None):
solvername,_ = solvername.split(':',maxsplit=1)


for (basename, CPM_slv) in SolverLookup.base_solvers():
for (basename, CPM_slv) in cls.base_solvers():
if basename == solvername:
# found the right solver
return CPM_slv
Expand Down
2 changes: 1 addition & 1 deletion cpmpy/tools/explain/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def make_assump_model(soft, hard=[], name=None):
soft2 = toplevel_list(soft, merge_and=False)

# make assumption variables
assump = cp.boolvar(shape=(len(soft),), name=name)
assump = cp.boolvar(shape=(len(soft2),), name=name)

# hard + implied soft constraints
model = cp.Model(hard + [assump.implies(soft2)]) # each assumption variable implies a candidate
Expand Down
3 changes: 2 additions & 1 deletion cpmpy/transformations/decompose_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ def decompose_in_tree(lst_of_expr, supported=set(), supported_reified=set(), _to
Supported numerical global functions remain in the expression tree as is. They can be rewritten using
`cpmpy.transformations.reification.reify_rewrite`
The following `bv -> NumExpr <comp> Var/Const` can be rewritten as [bv -> IV0 <comp> Var/Const, NumExpr == IV0].
So even if numerical constraints are not supported in reified context, we can rewrite them to non-reified versions.
So even if numerical constraints are not supported in reified context, we can rewrite them to non-reified versions if they are total.
"""
if _toplevel is None:
_toplevel = []

# swap the arguments of a comparison while maintaining its semantics
flipmap = {"==": "==", "!=": "!=", "<": ">", "<=": ">=", ">": "<", ">=": "<="}

newlist = [] # decomposed constraints will go here
Expand Down

0 comments on commit 797e3be

Please sign in to comment.