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

for j in range(num_of_teacher):

# 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

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.

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

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

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


## Related Questions

### Conditions required for strong duality to hold for SDPs

1  Asked on August 19, 2021

### Relationship between extreme points and optimal solutions of SDPs

1  Asked on August 19, 2021

### How to parallelize metaheuristics algorithms (Island Model)?

1  Asked on August 19, 2021 by antarctica

### Can we get closed form solution for such a problem?

1  Asked on August 19, 2021

### Can we use reinforcement learning and convex optimization to solve an optimization problem?

2  Asked on August 19, 2021 by qinqinxiaoguai

### Nonlinear integer (0/1) programming solver

6  Asked on March 1, 2021 by rajya

### How can I find the shortest path for all nodes in a graph from a source $s$?

1  Asked on March 1, 2021 by windbreeze

### Free solver for MINP problems

1  Asked on February 18, 2021 by dspinfinity

### Where I can study some job shop scheduling by course (video )?

0  Asked on February 18, 2021 by yue-chao

### Linear objective function with power term in constraint

1  Asked on February 15, 2021 by user152503

### Formulating these logical constraint in an ILP

1  Asked on January 18, 2021

### Modeling the multiplication of two binary decision variables in undirected graph in python

0  Asked on January 18, 2021 by amedeo

### Flexible Job Shop with Preemption

0  Asked on January 15, 2021 by robert-hildebrand

### How to handle an equality constraint in metaheuristic algorithms (like GA, PSO)?

3  Asked on January 11, 2021 by stevgates

### What is the difference between min- cut formulation and (bi) partitioning formulation?

1  Asked on January 8, 2021 by fathese

### Logical constraint in ILP

1  Asked on December 22, 2020 by che

### Quasi-convex function must be “partially monotonic”?

1  Asked on December 13, 2020 by high-gpa

### 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

### Convexity of the variance of a mixture distribution

1  Asked on September 25, 2020 by independentvariable