gpt4 book ai didi

optimization - 动态变量的背包优化

转载 作者:行者123 更新时间:2023-11-30 09:46:21 24 4
gpt4 key购买 nike

我正在尝试解决一个优化问题,它与背包问题非常相似,但无法使用动态规划来解决。我要解决的问题与这个问题非常相似:

enter image description here

enter image description here

enter image description here enter image description here

最佳答案

确实,您可以使用 CPLEX 解决这个问题。让我在 OPL 中向您展示这一点。

模型(.mod)

{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);
}

数据.dat

categories={Carbs,Protein,Fat};
groups=[{Meat,Milk},{Pasta,Bread},{Oil,Butter}];
products=[
{Product11,Product12},{Product21,Product22,Product23},
{Product31,Product32},{Product41,Product42},
{Product51},{Product61,Product62}];

prices=[1,4,1,3,2,4,2,1,3,1,2,1];

// User 1
Uc=[1,1,0];
Ug=[0.8,0.2,0.1,1,0.01,0.6];
budget=3;

//User 2
//Uc=[1,1,0];
//Ug=[0.8,0.2,0.1,1,0.01,0.6];
//budget=2;

这给出了

solution =  {"Product11" "Product21" "Product41"}

关于optimization - 动态变量的背包优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52154866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com