from ortools.linear_solver import pywraplp
import math
def main():
solver = pywraplp.Solver('SolveStigler',
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
objective = solver.Objective()
objective.SetMaximization()
indicator = solver.IntVar(0.0, 1.0, 'indicator')
value = solver.NumVar(0.0, math.inf, 'value')
M = 10000000
m = 1
bad_value = 0.5
# Constraint is value >= m - M * (1 - indicator)
# Equivalent to value - M * indicator >= m - M
constraint1 = solver.Constraint(m - M, math.inf)
constraint1.SetCoefficient(value, 1.0)
constraint1.SetCoefficient(indicator, -M)
# Constraint is value <= M * indicator or value - M * indicator <= 0
constraint2 = solver.Constraint(-math.inf, 0.0)
constraint2.SetCoefficient(value, 1.0)
constraint2.SetCoefficient(indicator, -M)
# Constraint is we'll set value to have to equal to 0.5
constraint3 = solver.Constraint(bad_value, bad_value)
constraint3.SetCoefficient(value, 1.0)
status = solver.Solve()
if status == solver.OPTIMAL:
print("{} >= {} - {} * (1 - {})".format(value.solution_value(), m, M, indicator.solution_value()))
left_side = value.solution_value()
right_side = m - M * (1 - indicator.solution_value())
print(left_side >= right_side)
else:
print('expectedly failed')
if __name__ == '__main__':
main()