在Gurobi-C#中VRP结果不可行

时间:2017-01-13 07:14:19

标签: c# routing gurobi

我尝试在C#中使用Gurobi解决定价问题。它已经在AMPL和LINGO上运行了,但是当我尝试使用C#时它是不可行的。也许我错误的索引或其他什么,请帮助我

这是我在C#中的代码

    static void Main()
    {
        try
        {
            // Model
            GRBEnv env = new GRBEnv();
            GRBModel model = new GRBModel(env);

            model.ModelName = "OLRPTW Subproblem";

            GRBVar[,] x = new GRBVar[DC1, DC1];     //variable of route
            GRBVar[] D = new GRBVar[DC1];     //variable of duration

            for (int i = 0; i < DC1; i++)
            {
                for (int j = 0; j < DC1; j++)
                {
                    x[i, j] = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "x" + i + "," + j);
                }
            }

            for (int i = 0; i < DC1; i++)
            {
                D[i] = model.AddVar(A[i], B[i], 0.0, GRB.CONTINUOUS, "D" + i);
            }

            for (int i = 0; i < DC1; i++)
            {
                for (int j = 0; j < DC1; j++)
                {
                    time[i, j] = Distance[i, j] + servicetime[i];
                }
            }

            for (int i = 0; i < DC1; i++)
            {
                for (int j = 0; j < DC1; j++)
                {
                   ReducedCost[i,j] = Distance[i, j]-DualU[i] + Demand[i]*DualV;                       
                }
            }

            // Integrate new variables
            model.Update();


            //objective function

            GRBLinExpr sumRC = 0.0;
            for (int i = 0; i < DC1; i++)
            {
                for (int j = 0; j < DC1; j++)
                {
                    sumRC += ReducedCost[i, j] * x[i, j];
                }
            }

            model.SetObjective(sumRC, GRB.MINIMIZE);

            //constraint 1
            GRBLinExpr sumI = 0.0;
            for (int i = 0; i < DC1; i++)
            {
                int j = 0;
                sumI += x[i, j];
            }
            model.AddConstr(sumI, GRB.EQUAL, 1.0, "C1.1");

            GRBLinExpr sumJ = 0.0;
            for (int j = 0; j < DC1; j++)
            {
                int i = 0;
                sumI += x[i, j];
            }
            model.AddConstr(sumJ, GRB.EQUAL,1.0, "C1.2");

            //constraint 2
            for (int h = 0; h < C; h++)
            {
                GRBLinExpr sumIH = 0.0;
                GRBLinExpr sumHJ = 0.0;
                for (int i = 0; i < DC1; i++)
                {
                    sumIH += x[i, h];
                }
                for (int j = 0; j < DC1; j++)
                {
                    sumHJ += x[h, j];
                }
                model.AddConstr(sumIH - sumHJ, GRB.EQUAL, 0.0, "C2");
            }

            //constraint 3
            GRBLinExpr sumCap = 0.0;
            for (int i = 0; i < DC1; i++)
            {
                for (int j = 0; j < DC1; j++)
                {
                    sumCap += Demand[i] * x[i, j];
                }
            }
            model.AddConstr(sumCap, GRB.LESS_EQUAL, Capacity, "Cap");

            //constraint 4                    
            for (int i = 0; i < C; i++)
            {
                for (int j = 0; j < C; j++)
                    if(i!=j)
                {
                    GRBLinExpr Time = D[i] + time[i, j] + D[j];
                    GRBLinExpr M = 10000 * (1 - x[i, j]);
                    model.AddConstr(Time <= M, "Time" + i + j);
                }
            }

            for (int i = 0; i < DC1; i++)
            {
                model.AddConstr(x[i, i], GRB.EQUAL, 0,"C*");
            }

            // Solve
            model.Optimize();

0 个答案:

没有答案