0

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

1 Answer 1

2

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])

4
  • AddMaxEquality and AddMinEquality and AddAbsEquality take IntVar as parameters, in my case i have LinearExp of SumArray. after my search i found that in C++ it supports AddLinMinEquality and AddLinMaxEquality and AddLinAbsEauality which accepts LinearExpr. but not sure why its not available in C# Could you help me in this or a twerk, or away to convert LinearExp or SumArray to IntVar? Jun 30, 2020 at 22:38
  • I think this might be the solution using python, stackoverflow.com/questions/53356121/fair-shift-distribution Jun 30, 2020 at 22:43
  • 1
    Yes, I believe you have to create intermediate variables and equal them to each linearexp
    – Stradivari
    Jun 30, 2020 at 23:02
  • AddLinMaxEquality is experimental code. It is not ready to be exported in all languages. Jul 1, 2020 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.