Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
箱式包装的Gurobi C#优化_C#_Mathematical Optimization_Cplex_Bin Packing_Gurobi - Fatal编程技术网

箱式包装的Gurobi C#优化

箱式包装的Gurobi C#优化,c#,mathematical-optimization,cplex,bin-packing,gurobi,C#,Mathematical Optimization,Cplex,Bin Packing,Gurobi,我有三种产品和五个盒子: var products = new string[] { "A", "B", "C"}; var boxes = new string[] { "1", "2", "3" ,"4","5"}; 尺寸为: double[,] boxDimensions = new double[,] {{8}, {15}, {3

我有三种产品和五个盒子:

var products = new string[] { "A", "B", "C"};
var boxes = new string[] { "1", "2", "3" ,"4","5"};
尺寸为:

double[,] boxDimensions = new double[,] 
                          {{8},
                          {15},
                          {30},
                          {40},
                          {50}};

double[,] productDimensions = new double[,] 
                        { { 5 },
                          { 10 },
                          { 20 } }; 
我想选择最小体积的盒子,所有的产品都可以装进去

我写了下面的代码,我知道我应该添加约束,只选择其中的一个框。 但它在当前状态下不起作用(给出不可行的sol)。 代码如下:

提前感谢你的帮助

static void Main()
        {
            try
            {

                var products = new string[] { "A", "B", "C" };
                var boxes = new string[] { "1", "2", "3", "4", "5" };

                double[,] boxDimensions = new double[,] {{8},
                                          {15},
                                          {30},
                                          {40},
                                          {50}};

                double[,] productDimensions =
                    new double[,] { { 5 },
                                    { 5 },
                                    { 20 }};

                // Model
                GRBEnv env = new GRBEnv();
                GRBModel model = new GRBModel(env);
                model.Set(GRB.StringAttr.ModelName, "box");

                // Box decision variables: open[p] == 1 if box i is choosen.
                GRBVar[] open = new GRBVar[boxes.Length];
                for (int i = 0; i < boxes.Length; i++)
                {
                    open[i] = model.AddVar(0, 1, boxDimensions[i, 0], GRB.BINARY, boxes[i]);
                }

                GRBVar[] x = new GRBVar[products.Length];

                for (int j = 0; j < products.Length; j++)
                {
                    x[j] = model.AddVar(productDimensions[j, 0], productDimensions[j, 0], 0, GRB.CONTINUOUS, products[j]);
                }


                // The objective is to minimize the total fixed and variable costs
                model.Set(GRB.IntAttr.ModelSense, 1);

                // Update model to integrate new variables
                model.Update();
                GRBLinExpr lhs = 0.0;
                GRBLinExpr rhs = 0.0;
                // Production constraints
                // Note that the right-hand limit sets the production to zero if
                // the plant is closed
                // Constraint: assign exactly shiftRequirements[s] workers
                // to each shift s
                for (int s = 0; s < products.Length; ++s)
                {
                    lhs.AddTerm(1.0, x[s]);
                }

                for (int w = 0; w < boxes.Length; w++)
                {
                    rhs.AddTerm(boxDimensions[w, 0], open[w]);
                }

                model.AddConstr(lhs <= rhs, "BoxConstraint");

                model.GetEnv().Set(GRB.IntParam.Method, GRB.METHOD_BARRIER);

                // Solve
                model.Optimize();

                // Print solution
                int status = model.Get(GRB.IntAttr.Status);
                if (status == GRB.Status.UNBOUNDED)
                {
                    Console.WriteLine("The model cannot be solved "
                        + "because it is unbounded");
                    return;
                }
                if (status == GRB.Status.OPTIMAL)
                {
                    Console.WriteLine("The optimal objective is " +
                        model.Get(GRB.DoubleAttr.ObjVal));
                    return;
                }
                if ((status != GRB.Status.INF_OR_UNBD) &&
                    (status != GRB.Status.INFEASIBLE))
                {
                    Console.WriteLine("Optimization was stopped with status " + status);
                    return;
                }

                // Dispose of model and env
                model.Dispose();
                env.Dispose();

            }
            catch (GRBException e)
            {
                Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
            }
        }
static void Main()
{
尝试
{
var products=新字符串[]{“A”、“B”、“C”};
变量框=新字符串[]{“1”、“2”、“3”、“4”、“5”};
double[,]boxDimensions=新的double[,]{{8},
{15},
{30},
{40},
{50}};
双[,]产品维度=
新的双[,]{5},
{ 5 },
{ 20 }};
//模型
GRBEnv env=新GRBEnv();
GRB模型=新GRB模型(env);
model.Set(GRB.StringAttr.ModelName,“box”);
//框决策变量:如果选择框i,则打开[p]==1。
GRBVar[]打开=新GRBVar[box.Length];
对于(int i=0;iAddConstr(lhs您编写模型的一般方法还可以


然而,对于x,您将下限和上限设置为固定x的相同值。此外,我想知道您为什么让Gurobi使用Barrier方法。我不确定这在您使用的MIP设置中是否正确。

出了什么问题?具体是什么问题问题?您是否使用了调试器?将预期值与实际值进行了比较?我们希望模型只选择一个框,而不是其中两个(最小维度)。@Jodrell在什么意义上您不理解?这是一维问题。所有框都是长框(假设为木棒).我们的问题是3D为什么要使用多维数组?@Jodrell这并不重要。实际上我的问题是3D。示例实数数组:(x,y,z)double[,]boxDimensions=new double[,]{{8,10,20},{15,10,40},{30,10,20}, {40,10,20}, {50,10,20}};为了使问题更简单,我将维度从3降到1。谢谢你的回答。实际上,x不是变量,而是它的参数。由于我对c#-Grubi接口不熟悉,我不知道如何定义参数。如果你知道,你可以告诉我。同样的原因,我不知道方法的区别。我从其他示例模式中获取了它l、 您可以使用addConstant()添加常量表达式(或使用重载+运算符构造表达式)。如果您不知道不同的优化方法,请将选择权留给解算器(因此只需使用Optimize())