Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a division inequality constraint

I need to resolve this simple optimization problem with Google OR-Tools CP-SAT solver in Python:

Goal = MAXIMIZE (X+Y+Z)
Constraint: Z/(X+Y+Z) <= 0.25

I don't know how to write the constraint properly since it is not linear. Could you help me?

like image 806
stefano guerrieri Avatar asked Nov 30 '25 16:11

stefano guerrieri


1 Answers

You have to create an intermediate variable and set its value using model.AddDivisionEquality. You also have to scale some variables up as CP-SAT works with integers.

scaling = 1000
x = model.NewIntVar(0, 10, 'x')
y = model.NewIntVar(0, 10, 'y')
z = model.NewIntVar(0, 10, 'z')

scaled_z = model.NewIntVar(0, 10 * scaling, 'z_scaled')
denom = model.NewIntVar(1, 3 * 10, 'x+y+z')
division = model.NewIntVar(0, 10 * scaling, 'z/(x+y+z)')

model.Add(scaled_z == z * scaling)
model.Add(denom == x + y + z)
model.AddDivisionEquality(division, scaled_z, denom)
model.Add(division <= int(0.25 * scaling))
model.Maximize(x + y + z)

solver.Solve(model)

print('x =', solver.Value(x))
print('y =', solver.Value(y))
print('z =', solver.Value(z))
print('z/(x+y+z) =', solver.Value(division) / scaling)
like image 134
Stradivari Avatar answered Dec 02 '25 04:12

Stradivari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!