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
Recent Questions
© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP