Capacity constraint not working

103 views
Skip to first unread message

Matias Rivero

unread,
Feb 27, 2019, 10:47:32 AM2/27/19
to or-tools-discuss
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()



Laurent Perron

unread,
Feb 27, 2019, 11:16:22 AM2/27/19
to or-tools-discuss
'if tmp_var' is always true
tmp_var is a non nil object.


Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00



--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages