google / or-tools

Google's Operations Research tools:
https://developers.google.com/optimization/
Apache License 2.0
11k stars 2.11k forks source link

Cumulative sum constraint for LP solver #930

Closed tgsmith61591 closed 5 years ago

tgsmith61591 commented 5 years ago

I've been looking through examples and documentation, and I cannot figure out how to create a cumulative constraint for an LP solver. If my solver is, for instance:

from ortools.linear_solver import pywraplp

solver = pywraplp.Solver('SolveRoutingProblem',
                         pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

On inspecting the solver attributes, I see nothing related to cumulative constraints:

>>> dir(solver)
['ABNORMAL', 'AT_LOWER_BOUND', 'AT_UPPER_BOUND', 'Add', 'BASIC', 'BOP_INTEGER_PROGRAMMING', 'BoolVar', 'CBC_MIXED_INTEGER_PROGRAMMING', 'CLP_LINEAR_PROGRAMMING', 'Clear', 'ComputeConstraintActivities', 'ComputeExactConditionNumber', 'Constraint', 'EnableOutput', 'ExportModelAsLpFormat', 'ExportModelAsMpsFormat', 'FEASIBLE', 'FIXED_VALUE', 'FREE', 'GLOP_LINEAR_PROGRAMMING', 'INFEASIBLE', 'Infinity', 'IntVar', 'InterruptSolve', 'Iterations', 'LoadModelFromProto', 'LookupConstraint', 'LookupVariable', 'Maximize', 'Minimize', 'NOT_SOLVED', 'NumConstraints', 'NumVar', 'NumVariables', 'OPTIMAL', 'Objective', 'RowConstraint', 'SetSolverSpecificParametersAsString', 'SetTimeLimit', 'Solve', 'Sum', 'SupportsProblemType', 'SuppressOutput', 'UNBOUNDED', 'VerifySolution', 'WallTime', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__swig_destroy__', '__swig_getmethods__', '__swig_setmethods__', '__weakref__', 'infinity', 'iterations', 'nodes', 'set_time_limit', 'this', 'wall_time']

However I found this example on the mailing list that suggests it can be done for CP solvers:


def test_cumulative_api():
  solver = pywrapcp.Solver('Problem')

  #Vars
  S=[solver.FixedDurationIntervalVar(0, 10, 5,False, "S_%s"%a)
     for a in range(10)]

  C = solver.IntVar(2, 5)
  D = [a % 3 + 2 for a in range(10)]
  solver.Add(solver.Cumulative(S, D, C, "cumul"))

Any workaround you know of? Thanks!

lperron commented 5 years ago

The LP solver only deals with linear constraints.

See https://github.com/google/or-tools/blob/master/ortools/linear_solver/samples/linear_programming_example.py, and https://github.com/google/or-tools/blob/master/ortools/linear_solver/samples/integer_programming_example.py.

If you wish to look at CP examples, I recommend using the CP-SAT solver:

See https://github.com/google/or-tools/blob/master/ortools/sat/doc/index.md

Note that we are revamping https://developers.google.com/optimization/introduction/overview to add the CP-SAT solver for all CP examples. This is not yet finished.

Thanks

Laurent Perron | Operations Research | lperron@google.com | (33) 1 42 68 53 00

Le mar. 13 nov. 2018 à 16:27, Taylor G Smith notifications@github.com a écrit :

I've been looking through examples and documentation, and I cannot figure out how to create a cumulative constraint for an LP solver. If my solver is, for instance:

from ortools.linear_solver import pywraplp

solver = pywraplp.Solver('SolveRoutingProblem', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

On inspecting the solver attributes, I see nothing related to cumulative constraints:

dir(solver) ['ABNORMAL', 'AT_LOWER_BOUND', 'AT_UPPER_BOUND', 'Add', 'BASIC', 'BOP_INTEGER_PROGRAMMING', 'BoolVar', 'CBC_MIXED_INTEGER_PROGRAMMING', 'CLP_LINEAR_PROGRAMMING', 'Clear', 'ComputeConstraintActivities', 'ComputeExactConditionNumber', 'Constraint', 'EnableOutput', 'ExportModelAsLpFormat', 'ExportModelAsMpsFormat', 'FEASIBLE', 'FIXED_VALUE', 'FREE', 'GLOP_LINEAR_PROGRAMMING', 'INFEASIBLE', 'Infinity', 'IntVar', 'InterruptSolve', 'Iterations', 'LoadModelFromProto', 'LookupConstraint', 'LookupVariable', 'Maximize', 'Minimize', 'NOT_SOLVED', 'NumConstraints', 'NumVar', 'NumVariables', 'OPTIMAL', 'Objective', 'RowConstraint', 'SetSolverSpecificParametersAsString', 'SetTimeLimit', 'Solve', 'Sum', 'SupportsProblemType', 'SuppressOutput', 'UNBOUNDED', 'VerifySolution', 'WallTime', 'class', 'del', 'delattr', 'dict', 'dir', 'doc', 'eq', 'format', 'ge', 'getattr', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'swig_destroy', 'swig_getmethods', '__swig_setmethods', 'weakref__', 'infinity', 'iterations', 'nodes', 'set_time_limit', 'this', 'wall_time']

However I found this example https://groups.google.com/forum/#!topic/or-tools-discuss/CMWBUzgRbgA on the mailing list that suggests it can be done for CP solvers:

def test_cumulative_api(): solver = pywrapcp.Solver('Problem')

Vars

S=[solver.FixedDurationIntervalVar(0, 10, 5,False, "S_%s"%a) for a in range(10)]

C = solver.IntVar(2, 5) D = [a % 3 + 2 for a in range(10)] solver.Add(solver.Cumulative(S, D, C, "cumul"))

Any workaround you know of? Thanks!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/or-tools/issues/930, or mute the thread https://github.com/notifications/unsubscribe-auth/AKj17ZZn9AgPhq9XfBb_UpsGVWoVNqFfks5uuuTWgaJpZM4Yb44N .