Pyomo:ValueError:约束'VehicleNumCapRule'没有适当的值。找到'<function vehicle_num_cap_rule =“” at =“” 0x7f0d6cfd4c80 =“”>

时间:2018-07-13 03:34:26

标签: python optimization routing pyomo

再次,作为pyomo的狂热者和对python的热爱,我试图在pyomo中编写带有时间窗口(vrptw)的车辆路径问题。我将time_window参数创建为:

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

在.dat文件中,我声明了以下参数:

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
;

但是,当我运行脚本时,我会遇到此错误:

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'"

有人可以告诉我发生了什么事吗?预先感谢

编辑:这是包含模型的部分。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

在.dat文件中,我有:

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;

1 个答案:

答案 0 :(得分:0)

您的示例代码尝试使用2维数据初始化1维model.time_windows参数。

model.time_windows = Param(model.V, ['a', 'b'])应该起作用。