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!

`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

1 Asked on August 19, 2021

1 Asked on August 19, 2021

1 Asked on August 19, 2021 by antarctica

2 Asked on August 19, 2021 by qinqinxiaoguai

6 Asked on March 1, 2021 by rajya

integer programming linearization nonlinear programming quadratic programming

1 Asked on March 1, 2021 by windbreeze

1 Asked on February 18, 2021 by dspinfinity

0 Asked on February 18, 2021 by yue-chao

1 Asked on February 15, 2021 by user152503

1 Asked on January 18, 2021

linear programming linearization logical constraints mixed integer programming

0 Asked on January 18, 2021 by amedeo

0 Asked on January 15, 2021 by robert-hildebrand

integer programming optimization scheduling simulated annealing solver

3 Asked on January 11, 2021 by stevgates

1 Asked on January 8, 2021 by fathese

1 Asked on December 22, 2020 by che

binary variable linear programming linearization logical constraints mixed integer programming

1 Asked on December 13, 2020 by high-gpa

3 Asked on November 28, 2020 by joffrey-l

1 Asked on September 25, 2020 by independentvariable

convex optimization convexity nonconvex programming probability distributions

Get help from others!

Recent Answers

- haakon.io on Why fry rice before boiling?
- Lex on Does Google Analytics track 404 page responses as valid page views?
- Jon Church on Why fry rice before boiling?
- Peter Machado on Why fry rice before boiling?
- Joshua Engel on Why fry rice before boiling?

Recent Questions

- Iv’e designed a space elevator using a series of lasers. do you know anybody i could submit the designs too that could manufacture the concept and put it to use
- Need help finding a book. Female OP protagonist, magic
- Why is the WWF pending games (“Your turn”) area replaced w/ a column of “Bonus & Reward”gift boxes?
- Does Google Analytics track 404 page responses as valid page views?
- Why fry rice before boiling?

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