Search code examples
pyomo

Optional input data


For the problem formulation

import pyomo.environ as pe

model = pe.AbstractModel()
model.I = pe.Set()
model.p = model.Param(model.I)
model.create_instance("input.dat")

and the input.dat

set I := 1 2 3 ;
param p :=
1 0.1
2 0.2
3 0.3
;
param q :=
1 1.1
2 2.2
3 3.3
;

The following error is shown

AttributeError: 'AbstractModel' object has no attribute 'q'

How to silence create_instance in this case? The model is fully specified. The "excess" data (parameter q in this case) is needed for another model and the models share this input.dat. I could go with a try/except for the AttributeError and just carry on I guess, but then I would need to guard each create_instance call. I looked for a "skip_undefined" kwarg or similar in the documentation. Is there another preferred way to handle this situation?


Solution

  • According to the documentation, if you load your data using the method load from the class DataPortal, the parameters not used by the model are omitted.

    Therefore you may try:

    from pyomo.environ import *
    
    
    data = DataPortal()
    model = AbstractModel()
    
    data.load(filename='./input.dat')
    
    model.I = Set()
    model.p = model.Param(model.I)
    
    instance = model.create_instance(data)