Skip to content
Snippets Groups Projects
Commit 63681c50 authored by Simon de Givry's avatar Simon de Givry
Browse files

[python] merge pytoulbar2 from master and add vaclin option

parent 214195a2
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@ class CFN:
ubinit (decimal cost or None): initial upper bound.
resolution (int): decimal precision of costs.
vac (int): if non zero, maximum solver depth minus one where virtual arc consistency algorithm is applied (1: VAC only in preprocessing).
vaclin (bool): if true and vac is non-zero then virtual arc consistency algorithm is also applied on (generalized) linear constraints
configuration (bool): if True then special settings for preference learning using incremental solving (see car configuration tutorial).
vns (int or None): if None then solves using branch-and-bound methods else using variable neighborhood search heuristic
(-1: initial solution at random, -2: minimum domain values, -3: maximum domain values,
......@@ -47,11 +48,12 @@ class CFN:
See pytoulbar2test.py example in src repository.
"""
def __init__(self, ubinit = None, resolution = 0, vac = 0, configuration = False, vns = None, seed = 1, verbose = -1):
def __init__(self, ubinit = None, resolution = 0, vac = 0, configuration = False, vns = None, seed = 1, verbose = -1, vaclin = True):
tb2.init()
tb2.option.decimalPoint = resolution # decimal precision of costs
tb2.option.vac = vac # if no zero, maximum search depth-1 where VAC algorithm is performed (use 1 for preprocessing only)
tb2.option.VAClin = vaclin # if true then VAC is also applied on linear constraints
tb2.option.seed = seed # random seed number (use -1 if a pseudo-randomly generated seed is wanted)
tb2.option.verbose = verbose # verbosity level of toulbar2 (-1:no message, 0:search statistics, 1:search tree, 2-7: propagation information)
......@@ -163,10 +165,16 @@ class CFN:
raise RuntimeError("Out of range variable index:"+str(v))
iscope.append(v)
if (len(iscope) == 0):
assert(isinstance(costs, (int, float)))
if (len(iscope) == 0 and isinstance(costs, (int, float))):
self.CFN.wcsp.postNullaryConstraint(costs)
elif (len(iscope) == 1):
return
mincost = min(costs)
maxcost = max(costs)
if (mincost == maxcost):
self.CFN.wcsp.postNullaryConstraint(mincost)
return
assert(len(iscope) >= 1)
if (len(iscope) == 1):
assert(self.CFN.wcsp.getDomainInitSize(iscope[0]) == len(costs))
self.CFN.wcsp.postUnaryConstraint(iscope[0], costs, incremental)
elif (len(iscope) == 2):
......@@ -178,11 +186,7 @@ class CFN:
else:
if incremental:
raise NameError('Sorry, incremental ' + str(len(iscope)) + '-arity cost functions not implemented yet in toulbar2.')
mincost = min(costs)
maxcost = max(costs)
self.CFN.wcsp.postNullaryConstraint(mincost)
if (mincost == maxcost):
return
idx = self.CFN.wcsp.postNaryConstraintBegin(iscope, 0, len(costs) - costs.count(0))
tuple = [self.CFN.wcsp.toValue(v, 0) for v in iscope]
for cost in costs:
......@@ -231,7 +235,14 @@ class CFN:
if (len(iscope) == 0):
assert(len(tuples) == 0)
self.CFN.wcsp.postNullaryConstraint(defcost)
elif (len(iscope) == 1):
return
mincost = min(defcost, min(tcosts))
maxcost = max(defcost, max(tcosts))
if (mincost == maxcost):
self.CFN.wcsp.postNullaryConstraint(mincost)
return
assert(len(iscope) >= 1)
if (len(iscope) == 1):
costs = [defcost] * self.CFN.wcsp.getDomainInitSize(iscope[0])
for i, tuple in enumerate(tuples):
costs[self.CFN.wcsp.toIndex(iscope[0], tuple[0])] = tcosts[i]
......@@ -249,11 +260,8 @@ class CFN:
else:
if incremental:
raise NameError('Sorry, incremental ' + str(len(iscope)) + '-arity cost functions not implemented yet in toulbar2.')
mincost = min(defcost, min(tcosts))
maxcost = max(defcost, max(tcosts))
self.CFN.wcsp.postNullaryConstraint(mincost)
if (mincost == maxcost):
return
idx = self.CFN.wcsp.postNaryConstraintBegin(iscope, int((defcost - mincost) * 10 ** tb2.option.decimalPoint), len(tcosts))
for i, tuple in enumerate(tuples):
self.CFN.wcsp.postNaryConstraintTuple(idx, [self.CFN.wcsp.toValue(iscope[x], self.CFN.wcsp.toIndex(iscope[x], v)) for x,v in enumerate(tuple)], int((tcosts[i] - mincost) * 10 ** tb2.option.decimalPoint))
......
......@@ -165,6 +165,7 @@ PYBIND11_MODULE(pytb2, m)
.def_readwrite_static("opb", &ToulBar2::opb)
.def_readwrite_static("addAMOConstraints", &ToulBar2::addAMOConstraints)
.def_readwrite_static("knapsackDP", &ToulBar2::knapsackDP)
.def_readwrite_static("VAClin", &ToulBar2::VAClin)
.def_readwrite_static("divNbSol", &ToulBar2::divNbSol)
.def_readwrite_static("divBound", &ToulBar2::divBound)
.def_readwrite_static("divWidth", &ToulBar2::divWidth)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment