'Pyomo: "No value for uninitialized NumericValue object", trying to evaluate a Binary constraint
Working with Pyomo and GLPK 4.65, I'm trying to optimize the production of several factories for several products.
One of the costs to be reduced in the optimization is associated to the fact that if a factory must be started, there is a Starting Cost of $100.
# I define the Binary Constraint to know if the factory was producing, by seeing a 'factories_list', and by using 'big M'
model.factory_was_producing = Var(model.T, model.i, model.k, domain=Binary)
def factory_was_producing_rule(model, T, i, k):
return factories_list[0, i, k] <= model.factory_was_producing[T,i,k] * 1000000
model.factory_was_producing_constraint = Constraint(model.T, model.i, model.k, rule=factory_was_producing_rule)
# I try to define the Starting costs, by multiplying the $100 by the binary constraing 'factory_was_producing_constraint'
def f_Factory_Starting_Costs(model, T, i, k):
return 100 * value(model.factory_was_producing_constraint[T,i,k])
model.Factory_Starting_Costs = Param(model.T, model.i, model.k, rule=f_Factory_Starting_Costs)
However, I get the following error:
ValueError: No value for uninitialized NumericValue object factory_was_producing[1,Boston,Product_A]
The 'factory_was_producing' constraint seems to return righ values:
factory_was_producing : Size=24, Index=factory_was_producing_index
Key : Lower : Value : Upper : Fixed : Stale : Domain
(1, 'Boston', 'Product_A') : 0 : 0.0 : 1 : False : False : Binary
(1, 'Florida', 'Product_A') : 0 : 1.0 : 1 : False : False : Binary
(1, 'New York', 'Product_A') : 0 : 0.0 : 1 : False : False : Binary
I've been browsing solutions for this common error, dealing with 'either-or' constraints and conditional constraints, but I'm not able to understand them to be used in my problem.
Solution 1:[1]
You are popping that error because in this statement:
# I try to define the Starting costs, by multiplying the $100 by the binary constraing 'factory_was_producing_constraint'
def f_Factory_Starting_Costs(model, T, i, k):
return 100 * value(model.factory_was_producing_constraint[T,i,k])
model.Factory_Starting_Costs = Param(model.T, model.i, model.k, rule=f_Factory_Starting_Costs)
You are using value() during the creation of the model and at that point the variable value is unknown*. Also... You cannot make this into a Param which is a fixed value inside the model. I'm not sure what you are trying to accomplish there. If you capture the value of the startup cost in the previous constraint, you can just use that in your objective function. In pseudocode:
obj = <some cost summation stuff> + model.start_factory[...] * 100
I do think you probably have some more thinking to do regarding your time indexing. :) Is it possible to "start" the factory in every tilmestep independently or maybe just not index that variable by time.... ?
*yes, you could initialize the value and get something here, but that would be only a snapshot and incorrect structure.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AirSquid |
