0

I have three products and five boxes:

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

and dimensions are :

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

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

I want to choose box with minumum volume which all products will be fitted in.

I wrote the following code and I know that I should add constraint to choose only 1 box amoung them. But it is not working(giving infeasible sol) with current state. Code is available below:

Thanks in advance four yor help,

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

Note: I give simple problem (1D) , actually my real problem is 3D problem. in this case i only consider lenght of products and boxes but in real i should also consider width and height.

Adem Aygun
  • 550
  • 2
  • 6
  • 25
  • What goes wrong? What is the (exact) problem? Did you use the debugger? Compared expected values against actual values? – DrKoch Feb 10 '15 at 10:57
  • We want model to choose only 1 box not two of them(with minimum dimension). @Jodrell in what sense you dont understant? this is 1 dimensional problem. All are lengths of boxes(suppose as stick). our furher problem will be 3D – Adem Aygun Feb 10 '15 at 11:05
  • why are you using multi-dimensional arrays? – Jodrell Feb 10 '15 at 11:10
  • @Jodrell it is not important. Actually my problem is 3D. Sample real array : (x,y,z) double[,] boxDimensions = new double[,] {{8,10,20}, {15,10,40}, {30,10,20}, {40,10,20}, {50,10,20}}; I reduced dimensions from 3 to 1 to make problem simpler. – Adem Aygun Feb 10 '15 at 11:29
  • http://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays – Jodrell Feb 10 '15 at 11:38

1 Answers1

0

Your general way of writing the model is ok.

Nevertheless, for x, you set the lower and upper bound to the same value which fixes x. Furthermore, I wonder why you make Gurobi use the Barrier method. I am not sure that this is correct in the MIP setting you are working with.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • thanks for your answer. Actually x is not variable but its parameter and since i am new to c#-gurobi interface i dont know how to define parameter. if you know you can tell to me. Again because of the same reason I dont know what is differences of methods. I took it from other example model. – Adem Aygun Feb 11 '15 at 07:39
  • You can use addConstant (http://www.gurobi.com/documentation/6.0/reference-manual/java_grblinexpr_addconstan) to add constant expressions (or use the overloaded + operator to construct expressions). If you do not know the different optimization methods, just leave the choice to the solver (so just use Optimize()) – J Fabian Meier Feb 16 '15 at 08:47