这是一个很容易回答的问题。我试图创建一个模型,允许用户为参数值传递None。这样他们就可以定义上限/下限,或者选择不设上限。集合/参数定义、AMPL样式数据文件部分和约束函数的示例问题(饮食问题)如下所示:
设置/参数定义
model.Ingredients = pyo.Set()
model.Properties = pyo.Set()
model.IngredientProperties = pyo.Param(model.Ingredients, model.Properties)
model.MinProperty = pyo.Param(model.Properties, within=pyo.Any)
model.MaxProperty = pyo.Param(model.Properties, within=pyo.Any)
数据文件部分
set Ingredients := Banana Milk Yogurt ;
set Properties := Fat Protein Carbs ;
param: MinProperty MaxProperty :=
Fat 0.009 0.013
Protein 0.200 None
Carbs None 0.070;
param IngredientProperties: Fat Protein Carbs :=
Banana 0.375 0.020 0.010
Milk 0.003 0.075 0.015
Yogurt 0.015 0.650 0.075;
约束功能
def _property_constraint_rule(model, p):
return (model.MinProperty[p], sum(
model.IngredientProperties[i, p]
* model.Blend[i]
for i in model.Ingredients
), model.MaxProperty[p])
不幸的是,当我尝试此操作时,会收到错误消息:TypeError: Cannot treat the value 'None' as a constant.
是否有其他方法将参数定义为无?我知道我可以使用大量的正数/负数,但必须有更好的方法
如果您要继续使用抽象模型,我认为您必须使用默认值,这很好。如果使用的是具体模型,则可以有条件地构造约束,因为数据在构造时是已知的
此外,在数据文件中,需要将此格式中的None值表示为句点
.
。如果使用“None”这个词,它会呕吐,因为它不会被识别为Python的None
这个试用版对我来说很好
产量:
修改的
data.dat
相关问题 更多 >
编程相关推荐