Hi all,
I want to solve a simplified problem where my variables are tasks that can be performed in a given week inside a given time window.
In the example I am using three tasks. The first can be done in week 2 or 3, the second can be done in week 2, 3 or 4 and so on. Also, each task requires pre-defined time to complete.
I'm looking for a feasible solution according to constraints. Two constraints are required, the first one is that in the same week, less than three tasks can be performed.
The second one, which is giving me lot of troubles, is that in each week the total time that require all tasks asigned to that week cannot be greater than 41.
I ran out all possible alternatives. Below I pasted a code that runs but is not giving the expected results. I would really appreciate some help on this.
Thanks a lot in advance.
from __future__ import print_function
from ortools.sat.python import cp_model
def MaintOptimSolver():
model = cp_model.CpModel()
task_list = []
time = []
task_list.append(model.NewIntVar(2,3,'task1'))
time.append(10)
task_list.append(model.NewIntVar(2,4,'task2'))
time.append(20)
task_list.append(model.NewIntVar(2,3,'task3'))
time.append(10)
for k in range(2,5):
tmp_array_avail = []
tmp_array_time = []
for i in range(len(task_list)):
tmp_var = model.NewBoolVar('')
model.Add(task_list[i] == k).OnlyEnforceIf(tmp_var)
model.Add(task_list[i] != k).OnlyEnforceIf(tmp_var.Not())
tmp_array_avail.append(tmp_var)
tmp_array_time.append(time[i] if tmp_var else 0) #<----NOT WORKING
model.Add(sum(tmp_array_avail) < 3)
model.Add(sum(tmp_array_time) < 41)
solver = cp_model.CpSolver()
status = solver.Solve(model)
if(status == cp_model.FEASIBLE):
print("FEASIBLE Solution")
for i in range(len(task_list)):
print(solver.Value(task_list[i]))
MaintOptimSolver()