Pyomo / PyomoGallery

A collection of Pyomo examples
Other
284 stars 158 forks source link

diet.py example seems to be broken #18

Closed joshua-cogliati-inl closed 3 years ago

joshua-cogliati-inl commented 3 years ago

When I run the diet.py example I get an error:

$ pyomo solve --solver=glpk diet.py diet.dat || echo fail
[    0.00] Setting up Pyomo environment
[    0.00] Applying Pyomo preprocessing actions
[    0.00] Creating model
ERROR: Rule failed when generating expression for constraint nutrient_limit
    with index Cal: PyomoException: Cannot convert non-constant expression to
    bool. This error is usually caused by using an expression in a boolean
    context such as an if statement. For example,
        m.x = Var() if m.x <= 0:
            ...
    would cause this exception.
ERROR: Constructing component 'nutrient_limit' from data=None failed:
        PyomoException: Cannot convert non-constant expression to bool. This
        error is usually caused by using an expression in a boolean context
        such as an if statement. For example, m.x = Var() if m.x <= 0:
            ...
    would cause this exception.
[    0.02] Pyomo Finished
ERROR: Unexpected exception while running model:
        Cannot convert non-constant expression to bool. This error is usually
        caused by using an expression in a boolean context such as an if
        statement. For example, m.x = Var() if m.x <= 0:
            ...
    would cause this exception.
errorcode: 1
$ pyomo --version
Pyomo 6.0.1 (CPython 3.7.10 on Darwin 18.7.0)
joshua-cogliati-inl commented 3 years ago

If I change the nutrient rule to:

# Limit nutrient consumption for each nutrient
def nutrient_rule(model, j):
    a_value = sum(model.a[i,j]*model.x[i] for i in model.F)
    return model.Nmin[j] <= value(a_value) <= model.Nmax[j]

I get a different error:

$ pyomo solve --solver=glpk diet_mod.py diet.dat || echo fail
[    0.00] Setting up Pyomo environment
[    0.00] Applying Pyomo preprocessing actions
[    0.00] Creating model
ERROR: evaluating object as numeric value: x[Cheeseburger]
        (object: <class 'pyomo.core.base.var._GeneralVarData'>)
    No value for uninitialized NumericValue object x[Cheeseburger]
ERROR: evaluating object as numeric value: 510*x[Cheeseburger] + 370*x[Ham
    Sandwich] + 500*x[Hamburger] + 370*x[Fish Sandwich] + 400*x[Chicken
    Sandwich] + 220*x[Fries] + 345*x[Sausage Biscuit] + 110*x[Lowfat Milk] +
    80*x[Orange Juice]
        (object: <class 'pyomo.core.expr.numeric_expr.SumExpression'>)
    No value for uninitialized NumericValue object x[Cheeseburger]
ERROR: Rule failed when generating expression for constraint nutrient_limit
    with index Cal: ValueError: No value for uninitialized NumericValue object
    x[Cheeseburger]
ERROR: Constructing component 'nutrient_limit' from data=None failed:
        ValueError: No value for uninitialized NumericValue object
        x[Cheeseburger]
[    0.02] Pyomo Finished
ERROR: Unexpected exception while running model:
        No value for uninitialized NumericValue object x[Cheeseburger]
errorcode: 1
wedsall commented 3 years ago

Hi, Was able to reproduce. If I change the nutrient_rule to a tuple form (documented here: https://pyomo.readthedocs.io/en/stable/pyomo_modeling_components/Constraints.html) it seems to run. Is this equivalent?

Limit nutrient consumption for each nutrient

def nutrient_rule(model, j): value = sum(model.a[i,j]*model.x[i] for i in model.F)

:~/mini/pyomogallery/PyomoGallery/diet$ pyomo solve --solver=glpk diet.py diet.dat [ 0.00] Setting up Pyomo environment [ 0.00] Applying Pyomo preprocessing actions [ 0.00] Creating model [ 0.01] Applying solver [ 0.02] Processing results Number of solutions: 1 Solution Information Gap: 0.0 Status: optimal Function Value: 15.05 Solver results file: results.yml [ 0.02] Applying Pyomo postprocessing actions [ 0.02] Pyomo Finished errorcode: 0 retval: instance: <pyomo.core.base.PyomoModel.ConcreteModel object at 0x7fa659c33580> local: time_initial_import: 0.0017545223236083984 usermodel: <module 'diet' from '../PyomoGallery/diet/diet.py'> options: <pyomo.common.config.ConfigDict object at 0x7fa659c83d60> results: {'Problem': [{'Name': 'unknown', 'Lower bound': 15.05, 'Upper bound': 15.05, 'Number of objectives': 1, 'Number of constraints': 10, 'Number of variables': 10, 'Number of nonzeros': 77, 'Sense': 'minimize'}], 'Solver': [{'Status': 'ok', 'Termination condition': 'optimal', 'Statistics': {'Branch and bound': {'Number of bounded subproblems': '23', 'Number of created subproblems': '23'}}, 'Error rc': 0, 'Time': 0.003423929214477539}], 'Solution': [OrderedDict([('number of solutions', 1), ('number of solutions displayed', 1)]), {'Gap': 0.0, 'Status': 'optimal', 'Message': None, 'Problem': {}, 'Objective': {'cost': {'Value': 15.05}}, 'Variable': {'x[Cheeseburger]': {'Value': 4.0}, 'x[Ham Sandwich]': {'Value': 0.0}, 'x[Hamburger]': {'Value': 0.0}, 'x[Fish Sandwich]': {'Value': 1.0}, 'x[Chicken Sandwich]': {'Value': 0.0}, 'x[Fries]': {'Value': 5.0}, 'x[Sausage Biscuit]': {'Value': 0.0}, 'x[Lowfat Milk]': {'Value': 4.0}, 'x[Orange Juice]': {'Value': 0.0}}, 'Constraint': {}}]}

On Wed, Jul 14, 2021 at 6:10 PM Joshua J. Cogliati @.***> wrote:

If I change the nutrient rule to:

Limit nutrient consumption for each nutrientdef nutrient_rule(model, j):

a_value = sum(model.a[i,j]*model.x[i] for i in model.F)
return model.Nmin[j] <= value(a_value) <= model.Nmax[j]

I get a different error:

$ pyomo solve --solver=glpk diet_mod.py diet.dat || echo fail [ 0.00] Setting up Pyomo environment [ 0.00] Applying Pyomo preprocessing actions [ 0.00] Creating model ERROR: evaluating object as numeric value: x[Cheeseburger] (object: <class 'pyomo.core.base.var._GeneralVarData'>) No value for uninitialized NumericValue object x[Cheeseburger] ERROR: evaluating object as numeric value: 510x[Cheeseburger] + 370x[Ham Sandwich] + 500x[Hamburger] + 370x[Fish Sandwich] + 400x[Chicken Sandwich] + 220x[Fries] + 345x[Sausage Biscuit] + 110x[Lowfat Milk] + 80*x[Orange Juice] (object: <class 'pyomo.core.expr.numeric_expr.SumExpression'>) No value for uninitialized NumericValue object x[Cheeseburger] ERROR: Rule failed when generating expression for constraint nutrient_limit with index Cal: ValueError: No value for uninitialized NumericValue object x[Cheeseburger] ERROR: Constructing component 'nutrient_limit' from data=None failed: ValueError: No value for uninitialized NumericValue object x[Cheeseburger] [ 0.02] Pyomo Finished ERROR: Unexpected exception while running model: No value for uninitialized NumericValue object x[Cheeseburger] errorcode: 1

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Pyomo/PyomoGallery/issues/18#issuecomment-880243031, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABPIAXD4UMGKAP4H6NDVF5LTXYDNRANCNFSM5AMJ5NGQ .

jsiirola commented 3 years ago

@wedsall: yes: the tuple notation is equivalent.

The support for the "chained" inequality syntax (a <= b <= c) was deprecated in Pyomo 5.6 and removed in Pyomo 6.0. The preferred syntax is inequality(lower, body, upper). PR #19 updates this example.