enable public API to modify the CP-SAT model protobuf directly

This commit is contained in:
Laurent Perron
2019-03-25 13:32:12 +01:00
parent 180346c6f9
commit c4f08e2bab
8 changed files with 21 additions and 22 deletions

View File

@@ -86,8 +86,8 @@ class BoolVar {
return cp_model_->mutable_variables(index_);
}
// Returns the index of the variable in the model. If the variable is the negation of
// another variable v, its index is -v.index() - 1.
// Returns the index of the variable in the model. If the variable is the
// negation of another variable v, its index is -v.index() - 1.
int index() const { return index_; }
private:

View File

@@ -56,13 +56,11 @@ public class IntervalVar
return index_;
}
public IntervalConstraintProto Proto
{
public IntervalConstraintProto Proto {
get { return interval_; }
set { interval_ = value; }
}
public override string ToString()
{
return model_.Constraints[index_].ToString();

View File

@@ -647,9 +647,8 @@ def step_function_sample_sat():
# -- -- --------- 2
# 1
# -- --- 0
# 0==================20
# 0 ================ 20
#
expr = model.NewIntVar(0, 3, 'expr')
# expr == 0 on [5, 6] U [8, 10]
@@ -715,7 +714,6 @@ void StepFunctionSampleSat() {
// -- --- 0
// 0 ================ 20
//
IntVar expr = cp_model.NewIntVar({0, 3});
// expr == 0 on [5, 6] U [8, 10]
@@ -798,7 +796,6 @@ public class StepFunctionSampleSat {
// -- --- 0
// 0 ================ 20
//
IntVar expr = model.newIntVar(0, 3, "expr");
// expr == 0 on [5, 6] U [8, 10]
@@ -900,9 +897,8 @@ public class StepFunctionSampleSat
// -- -- --------- 2
// 1
// -- --- 0
// 0 20
// 0 ================ 20
//
IntVar expr = model.NewIntVar(0, 3, "expr");
// expr == 0 on [5, 6] U [8, 10]

View File

@@ -339,9 +339,11 @@ class IntVar(LinearExpression):
self.__negation = None
def Index(self):
"""Returns the index of the variable in the model."""
return self.__index
def Proto(self):
"""Returns the variable protobuf."""
return self.__var
def __str__(self):
@@ -480,9 +482,11 @@ class Constraint(object):
return self
def Index(self):
"""Returns the index of the constraint in the model."""
return self.__index
def ConstraintProto(self):
def Proto(self):
"""Returns the constraint protobuf."""
return self.__constraint
@@ -518,8 +522,13 @@ class IntervalVar(object):
self.__ct.name = name
def Index(self):
"""Returns the index of the interval constraint in the model."""
return self.__index
def Proto(self):
"""Returns the interval protobuf."""
return self.__ct.interval
def __str__(self):
return self.__ct.name
@@ -1207,7 +1216,7 @@ class CpModel(object):
def __str__(self):
return str(self.__model)
def ModelProto(self):
def Proto(self):
return self.__model
def Negated(self, index):
@@ -1400,14 +1409,14 @@ class CpSolver(object):
def Solve(self, model):
"""Solves the given model and returns the solve status."""
self.__solution = pywrapsat.SatHelper.SolveWithParameters(
model.ModelProto(), self.parameters)
model.Proto(), self.parameters)
return self.__solution.status
def SolveWithSolutionCallback(self, model, callback):
"""Solves a problem and pass each solution found to the callback."""
self.__solution = (
pywrapsat.SatHelper.SolveWithParametersAndSolutionCallback(
model.ModelProto(), self.parameters, callback))
model.Proto(), self.parameters, callback))
return self.__solution.status
def SearchForAllSolutions(self, model, callback):
@@ -1433,7 +1442,7 @@ class CpSolver(object):
self.parameters.enumerate_all_solutions = True
self.__solution = (
pywrapsat.SatHelper.SolveWithParametersAndSolutionCallback(
model.ModelProto(), self.parameters, callback))
model.Proto(), self.parameters, callback))
# Restore parameters.
self.parameters.enumerate_all_solutions = enumerate_all
return self.__solution.status

View File

@@ -52,9 +52,8 @@ public class StepFunctionSampleSat
// -- -- --------- 2
// 1
// -- --- 0
// 0 20
// 0 ================ 20
//
IntVar expr = model.NewIntVar(0, 3, "expr");
// expr == 0 on [5, 6] U [8, 10]

View File

@@ -41,7 +41,6 @@ public class StepFunctionSampleSat {
// -- --- 0
// 0 ================ 20
//
IntVar expr = model.newIntVar(0, 3, "expr");
// expr == 0 on [5, 6] U [8, 10]

View File

@@ -34,7 +34,6 @@ void StepFunctionSampleSat() {
// -- --- 0
// 0 ================ 20
//
IntVar expr = cp_model.NewIntVar({0, 3});
// expr == 0 on [5, 6] U [8, 10]

View File

@@ -53,9 +53,8 @@ def step_function_sample_sat():
# -- -- --------- 2
# 1
# -- --- 0
# 0==================20
# 0 ================ 20
#
expr = model.NewIntVar(0, 3, 'expr')
# expr == 0 on [5, 6] U [8, 10]