add more API on cp_model.py; gate_scheduling sat problem from or-tools.discuss

This commit is contained in:
Laurent Perron
2017-10-17 14:15:44 +02:00
parent 2c7a8bf8f6
commit 5f6096d901
3 changed files with 32 additions and 7 deletions

View File

@@ -69,15 +69,20 @@ def main():
demands.append(jobs[i][1])
performed_on_m0 = model.NewBoolVar('perform_%i_on_m0' % i)
performed.append(performed_on_m0)
start0 = model.NewIntVar(0, horizon, 'start_%i_on_m0' % i)
end0 = model.NewIntVar(0, horizon, 'end_%i_on_m0' % i)
start0 = model.NewOptionalIntVar(
0, horizon, performed_on_m0, 'start_%i_on_m0' % i)
end0 = model.NewOptionalIntVar(
0, horizon, performed_on_m0, 'end_%i_on_m0' % i)
interval0 = model.NewOptionalIntervalVar(
start0, duration, end0, performed_on_m0, 'interval_%i_on_m0' % i)
intervals0.append(interval0)
start1 = model.NewIntVar(0, horizon, 'start_%i_on_m1' % i)
end1 = model.NewIntVar(0, horizon, 'end_%i_on_m1' % i)
start1 = model.NewOptionalIntVar(
0, horizon, performed_on_m0.Not(), 'start_%i_on_m1' % i)
end1 = model.NewOptionalIntVar(
0, horizon, performed_on_m0.Not(), 'end_%i_on_m1' % i)
interval1 = model.NewOptionalIntervalVar(
start1, duration, end1, performed_on_m0.Not(), 'interval_%i_on_m1' % i)
intervals1.append(interval1)

View File

@@ -338,12 +338,25 @@ MUST_USE_RESULT bool MarkConstraintAsFalse(ConstraintProto* ct,
bool PresolveEnforcementLiteral(ConstraintProto* ct, PresolveContext* context) {
if (!HasEnforcementLiteral(*ct)) return false;
const int literal = ct->enforcement_literal(0);
if (context->LiteralIsTrue(literal)) {
context->UpdateRuleStats("true enforcement literal");
ct->clear_enforcement_literal();
return true;
}
// TODO(user): because the cumulative and disjunctive constraint refer to this
// interval, we cannot simply remove the constraint even if we know that this
// optional interval will not be present. We could fix that by removing this
// interval from these constraints, but it is difficult to do that in a
// general code, so we will need the presolve for these constraint to take
// care of that, and then we would be able to remove this interval if it is
// not longer used.
if (ct->constraint_case() == ConstraintProto::ConstraintCase::kInterval) {
return false;
}
if (context->LiteralIsFalse(literal)) {
context->UpdateRuleStats("false enforcement literal");
return RemoveConstraint(ct, context);

View File

@@ -296,13 +296,16 @@ class SumArray(IntegerExpression):
class IntVar(IntegerExpression):
"""Represents a IntegerExpression containing only a single variable."""
def __init__(self, model, lb, ub, name):
def __init__(self, model, lb, ub, is_present_index, name):
self.__model = model
self.__index = len(model.variables)
self.__var = model.variables.add()
self.__var.domain.extend([lb, ub])
self.__var.name = name
self.__negation = None
if is_present_index is not None:
self.__var.enforcement_literal.append(is_present_index)
def Index(self):
return self.__index
@@ -449,10 +452,14 @@ class CpModel(object):
# Integer variable.
def NewIntVar(self, lb, ub, name):
return IntVar(self.__model, lb, ub, name)
return IntVar(self.__model, lb, ub, None, name)
def NewOptionalIntVar(self, lb, ub, is_present, name):
is_present_index = self.GetOrMakeBooleanIndex(is_present)
return IntVar(self.__model, lb, ub, is_present_index, name)
def NewBoolVar(self, name):
return IntVar(self.__model, 0, 1, name)
return IntVar(self.__model, 0, 1, None, name)
# Integer constraints.