本文介绍了将长度为n的杆最佳切割为3种可能的4种尺寸中的任何一种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否在堆叠中的正确区域内-所以我首先道歉..

I am not sure if I am in the correct area on stack - so my apologies first ..

我需要知道如何计算适合n个长度的杆的所有可能组合(对于3种不同的固定长度).

I need to know how I can calculate all possible combinations (for 3 different fixed lengths) that can fit into a Rod of n length.

例如,如果我有3个固定长度的8、10、12和一根长度可变的杆,则说50';我想知道我可以做的所有可能的削减.

So for example if I have 3 fixed lengths of 8, 10, 12 and a Rod of variable Length n say 50'; I want to know all the possible cuts that I can make.

推荐答案

使用 Microsoft Solver Foundation :

const int rodLength = 50;
const int lengthA = 8, lengthB = 10, lengthC = 12;

var solver = SolverContext.GetContext();
var model = solver.CreateModel();

var decisionA = new Decision(Domain.IntegerNonnegative, "A");
model.AddDecision(decisionA);

var decisionB = new Decision(Domain.IntegerNonnegative, "B");
model.AddDecision(decisionB);

var decisionC = new Decision(Domain.IntegerNonnegative, "C");
model.AddDecision(decisionC);

model.AddGoal("Goal", GoalKind.Minimize,
    rodLength - (decisionA * lengthA) - (decisionB * lengthB) - (decisionC * lengthC));

int maxItems = (rodLength / new [] { lengthA, lengthB, lengthC }.Min());
model.AddConstraint("MaxItems", decisionA + decisionB + decisionC < maxItems);

var solution = solver.Solve();
Console.WriteLine("A " + decisionA.GetDouble());
Console.WriteLine("B " + decisionB.GetDouble());
Console.WriteLine("C " + decisionC.GetDouble());

我们试图最大程度地减少杆长度与总和之间的差异,从而限制了总件数(在您的情况下,最大为50/8 = 6件).

where we trying to minimize the difference between the rod length and the sum of the items constraining the number of items (in your case max 50 / 8 = 6 items).

这篇关于将长度为n的杆最佳切割为3种可能的4种尺寸中的任何一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 23:15