Search code examples
pythonoptimizationroutespyomo

Pyomo: ValueError: Constraint 'VehicleNumCapRule' does not have a proper value. Found '<function vehicle_num_cap_rule at 0x7f0d6cfd4c80>'


Again, as a pyomo enthusiast and an ardent lover of python, I was trying to write vehicle routing problem with time windows (vrptw) in pyomo. I created time_window parameter as:

model.time_windows = Param(model.V)     # model.V is the set of nodes

In my .dat file, I declared the parameter as follows:

param Time_windows: a b :=

0   1400 1500
1   0000 2400
2   0000 2400
3   0700 2400
4   0000 2400
5   0000 0700
6   0700 2400
7   0700 2400
8   0000 0700
9   0000 2400
10  0000 2400
11  0000 2400
12  0700 2400
13  0000 2400
14  0000 0700
15  0000 0800
16  0000 2400
17  0000 2400
18  0700 1200
19  0000 2400
;

However, when i run my script, i fall into this error:

ERROR: Constructing component 'time_windows' from data={(9, 'a'): 0, (18,
'b'): 1200, (13, 'b'): 2400, (17, 'a'): 0, (6, 'a'): 700, (2, 'a'): 0,
(19, 'b'): 2400, (4, 'b'): 2400, (14, 'b'): 700, (10, 'a'): 0, (12, 'b'):
2400, (18, 'a'): 700, (1, 'b'): 2400, (3, 'a'): 700, (9, 'b'): 2400, (11,
'a'): 0, (17, 'b'): 2400, (14, 'a'): 0, (0, 'b'): 1500, (4, 'a'): 0, (12,
'a'): 700, (8, 'b'): 700, (3, 'b'): 2400, (7, 'b'): 2400, (16, 'b'): 2400,
(15, 'a'): 0, (11, 'b'): 2400, (6, 'b'): 2400, (5, 'a'): 0, (13, 'a'): 0,
(0, 'a'): 1400, (15, 'b'): 800, (8, 'a'): 0, (7, 'a'): 700, (16, 'a'): 0,
(2, 'b'): 2400, (19, 'a'): 0, (1, 'a'): 0, (10, 'b'): 2400, (5, 'b'): 700}
failed:
    RuntimeError: Failed to set value for param=time_windows, index=(9,
    'a'), value=0.
    source error message="Index '(9, 'a')' is not valid for indexed component
    'time_windows'"
Traceback (most recent call last):
  File "main.py", line 16, in <module>
instance = model.create_instance('data.dat')
  File "/usr/local/lib/python2.7/dist-packages/pyomo/core/base/PyomoModel.py", line 723, in create_instance
profile_memory=profile_memory )
  File "/usr/local/lib/python2.7/dist-packages/pyomo/core/base/PyomoModel.py", line 806, in load
profile_memory=profile_memory)
  File "/usr/local/lib/python2.7/dist-packages/pyomo/core/base/PyomoModel.py", line 870, in _load_model_data
self._initialize_component(modeldata, namespaces, component_name, profile_memory)
  File "/usr/local/lib/python2.7/dist-packages/pyomo/core/base/PyomoModel.py", line 925, in _initialize_component
declaration.construct(data)
  File "/usr/local/lib/python2.7/dist-packages/pyomo/core/base/param.py", line 858, in construct
% (self.name, str(key), str(val), str(msg)) )
RuntimeError: Failed to set value for param=time_windows, index=(9, 'a'), value=0.
source error message="Index '(9, 'a')' is not valid for indexed component 'time_windows'"

Can someone tell me what is happening. Thanks in advance

EDIT: This is the part containing model.V

model.Vo = Set()                           
model.Vf = Set()                           
model.Vc = Set()                           
model.Vfc = model.Vf | model.Vc             
model.V = model.Vo | model.Vf | model.Vc   # set of nodes

and in the .dat file I have:

set Vo := 0;
set Vf := 1 2;
set Vc := 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19;

Solution

  • Your example code attempts to initialize a 1-dimensional model.time_windows parameter with 2-dimensional data.

    model.time_windows = Param(model.V, ['a', 'b']) ought to work.