鉴于以下情况,您如何在保持最低成本的同时优化“风格点”?您必须从每个班级中选择1个项目,并从“附件”类中选择2个单独的项目。
costlimit = 100
分类:项目:(样式点,成本)
裤子:牛仔裤:(5,25),卡其裤:(3,15),短裤:(2,10) 上衣:tshirt1:(5,28),tshirt2:(4,20),tshirt3:(2,10) 鞋子:鞋子1:(8,50),鞋子2:(4,30),鞋子3:(2,15) 附件:Acc1:(1,5),Acc2:(1,4),Acc3:(2,6),Acc4:(3,9),Acc5:(4,15)
我试图找到这个问题的名称,我认为这是0-1背包问题的一个旋转。
答案 0 :(得分:0)
这是一个优化问题,也许你想要线性代数。
答案 1 :(得分:0)
可以使用像MiniZinc这样的约束求解器对这些问题进行建模和求解。
我的尝试:
int: Jeans = 1;
int: Khakis = 2;
int: Shorts = 3;
int: Tshirt1 = 4;
int: Tshort2 = 5;
int: Tshirt3 = 6;
int: Shoes1 = 7;
int: Shoes2 = 8;
int: Shoes3 = 9;
int: Acc1 = 10;
int: Acc2 = 11;
int: Acc3 = 12;
int: Acc4 = 13;
int: Acc5 = 14;
set of int: Items = Jeans .. Acc5;
set of int: Pants = Jeans .. Shorts;
set of int: Tops = Tshirt1 .. Tshirt3;
set of int: Shoes = Shoes1 .. Shoes3;
set of int: Accessories = Acc1 .. Acc5;
array[Items] of int: Costs;
array[Items] of int: StylePoints;
array[Items] of string: Names;
int: Costlimit = 100;
Costs = [25,15,10,28,20,10,50,30,15, 5, 4, 6, 9,15];
StylePoints = [5 , 3, 2, 5, 4, 2, 8, 4, 2, 1, 1, 2, 3, 4];
Names = ["Jeans","Khakis","Shorts",
"Tshirt1","Tshort2","Tshirt3",
"Shoes1","Shoes2","Shoes3",
"Acc1","Acc2","Acc3","Acc4","Acc5"];
array[Items] of var 0 .. 1: Select;
%% take 1 item from each class
constraint
1 = sum(i in Pants)(Select[i]);
constraint
1 = sum(i in Tops)(Select[i]);
constraint
1 = sum(i in Shoes)(Select[i]);
%% take 2 separate items from accessories
constraint
2 = sum(i in Accessories)(Select[i]);
%% stay in budget
constraint
Costlimit >= sum(i in Items)(Select[i] * Costs[i]);
%% maximize style points
solve maximize
sum (i in Items) (Select[i] * StylePoints[i]);
%
% Output solution as table of variable value assignments
%%
output
["\nSelected items:"] ++
["\n" ++ show(Names[i]) ++ ": "
++ show(Select[i])
++ "( style " ++ show(StylePoints[i])
++ "; cost " ++ show(Costs[i]) ++ ")" | i in Items] ++
["\n"] ++
["\nCosts: " ++ show(sum(i in Items)(Select[i] * Costs[i]))] ++
["\nStyle points: " ++ show(sum(i in Items)(Select[i] * StylePoints[i]))];
我使用了[0..1]
个整数数组作为决策变量。如果选择了相应的项,则数组元素为1
。否则为0
。 MiniZinc 能够找到解决方案 - 如果存在 - 满足所有给定的约束并最大化目标值。
生成的解决方案:
Selected items:
Jeans: 1( style 5; cost 25)
Khakis: 0( style 3; cost 15)
Shorts: 0( style 2; cost 10)
Tshirt1: 0( style 5; cost 28)
Tshort2: 0( style 4; cost 20)
Tshirt3: 1( style 2; cost 10)
Shoes1: 1( style 8; cost 50)
Shoes2: 0( style 4; cost 30)
Shoes3: 0( style 2; cost 15)
Acc1: 0( style 1; cost 5)
Acc2: 0( style 1; cost 4)
Acc3: 1( style 2; cost 6)
Acc4: 1( style 3; cost 9)
Acc5: 0( style 4; cost 15)
Costs: 100
Style points: 20