我需要解决一个与背包问题相似的优化问题。我在这篇文章中详细介绍了优化问题: knapsack optimization with dynamic variables 实际上,我实际上需要使用python而不是OPL,因此为了使用cplex优化框架,我已经安装了docplex和clpex软件包。
这是我想使用docplex转换为python的OPL代码
touch ~/.vimrc
这是我第一次尝试代码:
{string} categories=...;
{string} groups[categories]=...;
{string} allGroups=union (c in categories) groups[c];
{string} products[allGroups]=...;
{string} allProducts=union (g in allGroups) products[g];
float prices[allProducts]=...;
int Uc[categories]=...;
float Ug[allGroups]=...;
float budget=...;
dvar boolean z[allProducts]; // product out or in ?
dexpr int xg[g in allGroups]=(1<=sum(p in products[g]) z[p]);
dexpr int xc[c in categories]=(1<=sum(g in groups[c]) xg[g]);
maximize
sum(c in categories) Uc[c]*xc[c]+
sum(c in categories) sum(g in groups[c]) Uc[c]*Ug[g]*xg[g];
subject to
{
ctBudget:
sum(p in allProducts) z[p]*prices[p]<=budget;
}
{string} solution={p | p in allProducts : z[p]==1};
execute
{
writeln("solution = ",solution);
}
我实际上不知道如何正确建模OPL代码中的变量xg,xc和z?
有关如何正确建模的任何想法。 预先谢谢你
编辑:这是@HuguesJuille建议之后的修改,我已经清理了代码,现在可以正常工作了。
from collections import namedtuple
from docplex.mp.model import Model
# --------------------------------------------------------------------
# Initialize the problem data
# --------------------------------------------------------------------
Categories_groups = {"Carbs": ["Meat","Milk"],"Protein":["Pasta","Bread"], "Fat": ["Oil","Butter"]}
Groups_Products = {"1":["Product11","Product12"], "2": ["Product21","Product22","Product23"], "3":["Product31","Product32"],"4":["Product41","Product42"], "5":["Product51"],"6":["Product61","Product62"]}
Products_Prices ={"Product11":1,"Product12":4,"Product21":1,"Product22":3,"Product23":2,"Product31":4,"Product32":2,"Product41":1,"Product42":3,"Product51":1,"Product61":2,"Product62":1}
Uc=[1,1,0];
Ug=[0.8,0.2,0.1,1,0.01,0.6];
budget=3;
def build_diet_model(**kwargs):
allcategories = Categories_groups.keys()
allgroups = Groups_Products.keys()
prices=Products_Prices.values()
# Model
mdl = Model(name='summary', **kwargs)
for g, products in Groups_Products.items():
xg = mdl.sum(z[p] for p in products)# this line is not correct as I dont know how to add the condition like in the OPL code, and I was unable to model the variable z and add it as decision variable to the model.
mdl.add_constraint(mdl.sum(Products_Prices[p] * z[p] for p in Products_Prices.keys() <= budget)
mdl.maximize(mdl.sum(Uc[c] * xc[c] for c in Categories_groups.keys()) +
model.sum(xg[g] * Uc[c] * Ug[g] for c, groups in Categories_groups.items() for g in groups))
mdl.solve()
if __name__ == '__main__':
build_diet_model()
我希望这可以帮助像我这样的初学者遇到同样的问题。
答案 0 :(得分:2)
如果我正确理解了您的数据模型(不确定示例中的数据是否一致(Categories_groups和Groups_Products的'groups'值集不相同)。),则为决策变量定义表达式将如下所示:
z = mdl.binary_var_dict(allProducts, name='z([%s])')
xg = {g: 1 <= mdl.sum(z[p] for p in Groups_Products[g]) for g in allgroups}
xc = {c: 1 <= mdl.sum(xg[g] for g in Categories_groups[c]) for c in allcategories}
在这里,“ z”决策变量定义为字典。然后可以轻松将其编入索引。
还可以在以下位置找到有关编写docplex模型的文档:https://rawgit.com/IBMDecisionOptimization/docplex-doc/master/docs/mp/creating_model.html
请注意,如果您需要构建处理大型数据集的模型,则使用熊猫来定义复杂切片可能会更有效。