1 year ago
#377018
Á. Garzón
DataPortal don't correctly read .dat file with few indexed parameters
I'm working with pyomo and need to read data previously wrote in a .dat file.
I'm reading data with following code:
data = DataPortal()
data.load(filename = 'myfilename.dat')
All data is correctly read, except one of my params. This param is defined in my pyomo model as a 3-index param. In my .dat file, this param is wrote as I show (I'll show an example):
param pExample:=
a 0 1 0
a 0 2 0
a 0 3 0
a 1 1 1
a 1 2 1
a 1 3 1
When I read this param with DataPortal, I get the following item in data.items():
pExample = {'a': 1, 1: 1, 2: 1, 3: 1}
It read it as if it would be two dictionaries in two columns each one, while I need something like:
pExample = {('a', 0, 1): 0, ('a', 0, 2): 0, ...}
or maybe like this other one:
pExample = {'a': {0: {1: 0, 2: 0, 3: 0}, 1: {1: 1, 2: 1, 3: 1}}}
Thanks you very much in advance.
========== Adding reproducible example:
following documentation here
my_data.dat
set I :=
a 1 0
a 0 1
b 2 3;
param B :=
a 1 0 12
a 0 1 4.1
b 2 3 -3.6;
executable:
# data load ex
import pyomo.environ as pyo
m = pyo.AbstractModel()
m.I = pyo.Set(dimen=3)
m.B = pyo.Param(m.I)
data = pyo.DataPortal()
data.load(filename='my_data.dat')
instance = m.create_instance(data)
instance.pprint()
Error:
ERROR: Constructing component 'B' from data={'a': 0, 0: 12, 1: 4.1, 'b': 2, 3:
-3.6} failed:
RuntimeError: Failed to set value for param=B, index=a, value=0.
source error message="Index 'a' is not valid for indexed component 'B'"
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyomo/core/base/param.py", line 915, in construct
self._validate_index(key), val)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyomo/core/base/indexed_component.py", line 571, in _validate_index
raise KeyError(
KeyError: "Index 'a' is not valid for indexed component 'B'"
python-3.x
linear-programming
pyomo
0 Answers
Your Answer