AnswerBun.com

How to balance the workload of teachers in OR-Tools (maximization of the minimum)

Operations Research Asked by neverletgo on December 18, 2021

I am very new to optimization and OR-Tools. I am trying to solve a very simple question.

Let’s assume that we have $n$ students. Each student needs to be assigned to only one teacher as a supervisor. There are several constraints that are not very critical for now. However, there are two types of supervisors.
But at the same time, I am trying to keep the balance of the workload of the teachers as much as possible.

I am trying to write the objective but for some reason, it does not work correctly. I believe that I am missing something important.

model = cp_model.CpModel()

# Declare the variables.
x = []
for i in range(num_of_students):
    t = []
    for j in range(num_of_teacher):
        t.append(model.NewIntVar(0, 2, "x[%i,%i]" % (i, j))) #0 not supervisor, 1 1st type, 2nd type
    x.append(t)

workload =[]
for j in range(num_of_teacher):
    workload.append(sum([x[i][j] for i in range(num_of_students)]))

# Constraints
# Each student is assigned to EXACTLY one teacher.
[model.Add(sum(x[i][j] for j in range(num_of_teacher)) == 1)
for i in range(num_of_students)]

#objective
model.Maximize( min(workload))

solver = cp_model.CpSolver()
status = solver.Solve(model)
print (solver.ObjectiveValue())

When I run the code, each student (I have 11 students and 3 teachers) is assigned to only one teacher. But all of them are the same teacher. When I look at the workloads, it is [0, 0, 11].

However, system displays 11 as the ObjectiveValue. But min([0,0,11] is 0, right?.

I also tried to write the objective as
model.Minimize( max(workload)) but all of the students assigned to only one teacher again.

Please help me!

One Answer

min, max, functions do not work in OR-Tools, you should use AddMinEquality instead:

...
workload = []
for j in range(num_of_teacher):
    tmp = model.NewIntVar(0, num_of_students, "")
    model.Add(tmp == sum([x[i][j] for i in range(num_of_students)]))
    workload.append(tmp)

...
obj = model.NewIntVar(0, num_of_students, "")
model.AddMinEquality(obj, workload)
model.Maximize(obj)

Related links:

Answered by Stradivari on December 18, 2021

Add your own answers!

Related Questions

Free solver for MINP problems

1  Asked on February 18, 2021 by dspinfinity

     

Flexible Job Shop with Preemption

0  Asked on January 15, 2021 by robert-hildebrand

         

Constraint programming resources

3  Asked on November 28, 2020 by joffrey-l

     

Pyomo variable creation dilemma

1  Asked on October 31, 2020 by ethan-deakins

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP