tpaviot / ProcessScheduler

A Python package for automatic and optimized resource scheduling
https://processscheduler.github.io/
GNU General Public License v3.0
58 stars 17 forks source link

Issue with ResourceUnavailable #122

Closed uyot10 closed 1 year ago

uyot10 commented 1 year ago

I'm trying to implement unavailable resources and i get the following error:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
[~\AppData\Local\Temp\ipykernel_16540\3647698839.py](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/0272552/Documents/Python%20Scripts/Constructabilidad/~/AppData/Local/Temp/ipykernel_16540/3647698839.py) in 
      2 # there may be several schedules, add the following constraint
      3 # to find the solution available from the python-MIP documentation
----> 4 ps.ResourceUnavailable('Rec0_0', [(1,5)])
      5 pb.add_objective_makespan()
      6 #Solve

[c:\Users\0272552\Anaconda3\lib\site-packages\processscheduler\resource_constraint.py](file:///C:/Users/0272552/Anaconda3/lib/site-packages/processscheduler/resource_constraint.py) in __init__(self, resource, list_of_time_intervals, optional)
    161         for interval_lower_bound, interval_upper_bound in list_of_time_intervals:
    162             # add constraints on each busy interval
--> 163             for worker in workers:
    164                 for start_task_i, end_task_i in worker.get_busy_intervals():
    165                     self.set_z3_assertions(

UnboundLocalError: local variable 'workers' referenced before assignment

I can't find where the problem comes from since this line of code "ps.ResourceUnavailable('Rec0_0', [(1,5)])" is the last thing I do right before I call the solver. I would appreciate some help.

Thank you in advance

tpaviot commented 1 year ago

it looks like you have to define the Rec0_0 resource before creating the ResourceUnavailable constraint

uyot10 commented 1 year ago

i create all the resources before i try to create the constraint. This is how my resources look like:

[[Rec0_0(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):,
  Rec0_1(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):],
 [Rec1_0(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):,
  Rec1_1(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):],
 [Rec2_0(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):,
  Rec2_1(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):],
 [Rec3_0(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):,
  Rec3_1(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):,
  Rec3_2(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):,
  Rec3_3(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):],
 [Rec4_0(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):,
  Rec4_1(<class 'processscheduler.resource.Worker'>)
  0 assertion(s):,
  Rec4_2(<class 'processscheduler.resource.Worker'>)
...
  0 assertion(s):],

And this is the piece of code i use to create them:

# create the resources
resource_names = ["Rec%i" % i for i in range(len(resources))]
recursos = []
for i, resource in enumerate(resources):
    workers = [ps.Worker("%s_%i" % (resource_names[i], j)) for j in range(resource)]
    recursos.append(workers)
tpaviot commented 1 year ago

can you please insert the complete snippet so that I can just copy/paste and duplicate the issue

tpaviot commented 1 year ago

ok I got it, you have to pass the resource variable identifier to build the constraint, not the resource name. ps.ResourceUnavailable('Rec0_0', [(1,5)]) should be replaced with:

rec0_0  = ps.Worker('Rec0_0')
ps.ResourceUnavailable(rec0_0, [(1,5)])
uyot10 commented 1 year ago

When i use the code you posted i get that a resource with the name rec0_0 already exists.

This is the code i'm using to create the resources:

# create the resources
resource_names = ["Rec%i" % i for i in range(len(resources))]
recursos = []
for i, resource in enumerate(resources):
    workers = [ps.Worker("%s_%i" % (resource_names[i], j)) for j in range(resource)]
    recursos.append(workers)

i don't know exactly what to pass to the ResourceUnavailable class as i always get the error. I am storing the resources in the list recursos.

uyot10 commented 1 year ago

i think i found the answer, i have to pass recursos[0][0]