I am new to Mixed Integer programming. i have used gurobi tool before to build this model. but beacuse of the cost i wanted to switch to or-tools, but i was not able to do the same logic.
the problem i am trying to solve is to minimize the differences of the sum of variables.
in other word, i have employess I want to distribute them equally on a schedule keeping their assignment fair to everyone.
so i have build my model following these steps:
1- create IntVar for every employee per shift per day. 2- i have added a constrain that every employee should have one shift every day
now i want my model to solve against sum of number of shits attended by every employee and try to minimize the difference between the minimum and the maximum shifts attended by the employees.
this can be achieved by doing it in multiple ways
1- get the min and the max of the linearExpr (the sum of the employees) and minimize the difference 2- or , the absolute sum of the differences between the average of shifts and every number of shifts attended by every employee.
Unfortunately, I was not able to find max, min, or absolute implementation for this.
if you guys have a suggestion that i can reach this please explain.
thanks
The functions that you are looking for are:
But in this official example, they minimize the delta to the average:
https://github.com/google/or-tools/blob/master/examples/python/reallocate_sat.py
Here is a simplified version:
from ortools.sat.python import cp_model
model = cp_model.CpModel()
total = 35
n = 5
avg = total // n
ints = [model.NewIntVar(0, 10, str(i)) for i in range(n)]
delta = model.NewIntVar(0, total, "delta")
for i in ints:
model.Add(i >= avg - delta)
model.Add(i <= avg + delta)
model.Minimize(delta)
solver = cp_model.CpSolver()
solver.Solve(model)
print([solver.Value(i) for i in ints])