-def DisplayBounds(bounds)
-Displays a flattened list of intervals.
def DisplayBounds(bounds):
- """Displays a flattened list of intervals."""
- out = ''
- for i in range(0, len(bounds), 2):
- if i != 0:
- out += ', '
- if bounds[i] == bounds[i + 1]:
- out += str(bounds[i])
- else:
- out += str(bounds[i]) + '..' + str(bounds[i + 1])
- return out
-
-def EvaluateBooleanExpression(literal, solution)
-Evaluate a boolean expression against a solution.
def EvaluateBooleanExpression(literal, solution):
- """Evaluate a boolean expression against a solution."""
- if isinstance(literal, numbers.Integral):
- return bool(literal)
- elif isinstance(literal, IntVar) or isinstance(literal,
- _NotBooleanVariable):
- index = literal.Index()
- if index >= 0:
- return bool(solution.solution[index])
- else:
- return not solution.solution[-index - 1]
- else:
- raise TypeError(
- 'Cannot interpret %s as a boolean expression.' % literal)
-
-def EvaluateLinearExpr(expression, solution)
-Evaluate a linear expression against a solution.
def EvaluateLinearExpr(expression, solution):
- """Evaluate a linear expression against a solution."""
- if isinstance(expression, numbers.Integral):
- return expression
- value = 0
- to_process = [(expression, 1)]
- while to_process:
- expr, coef = to_process.pop()
- if isinstance(expr, _ProductCst):
- to_process.append((expr.Expression(), coef * expr.Coefficient()))
- elif isinstance(expr, _SumArray):
- for e in expr.Expressions():
- to_process.append((e, coef))
- value += expr.Constant() * coef
- elif isinstance(expr, _ScalProd):
- for e, c in zip(expr.Expressions(), expr.Coefficients()):
- to_process.append((e, coef * c))
- value += expr.Constant() * coef
- elif isinstance(expr, IntVar):
- value += coef * solution.solution[expr.Index()]
- elif isinstance(expr, _NotBooleanVariable):
- value += coef * (1 - solution.solution[expr.Not().Index()])
- return value
-
-def ShortName(model, i)
-Returns a short name of an integer variable, or its negation.
def ShortName(model, i):
- """Returns a short name of an integer variable, or its negation."""
- if i < 0:
- return 'Not(%s)' % ShortName(model, -i - 1)
- v = model.variables[i]
- if v.name:
- return v.name
- elif len(v.domain) == 2 and v.domain[0] == v.domain[1]:
- return str(v.domain[0])
- else:
- return '[%s]' % DisplayBounds(v.domain)
-