예제 #1
0
    static void Main()
    {
        try {
            // Create environment

            GRBEnv env = new GRBEnv();

            // Create a new model

            GRBModel model = new GRBModel(env);

            // Create variables

            double lb = 0.0, ub = 1.0;

            GRBVar x = model.AddVar(lb, ub, 0.0, GRB.CONTINUOUS, "x");
            GRBVar y = model.AddVar(lb, ub, 0.0, GRB.CONTINUOUS, "y");
            GRBVar z = model.AddVar(lb, ub, 0.0, GRB.CONTINUOUS, "z");

            // Set objective for y

            model.SetObjective(-y);

            // Add piecewise-linear objective functions for x and z

            int      npts = 101;
            double[] ptu  = new double[npts];
            double[] ptf  = new double[npts];
            double[] ptg  = new double[npts];

            for (int i = 0; i < npts; i++)
            {
                ptu[i] = lb + (ub - lb) * i / (npts - 1);
                ptf[i] = f(ptu[i]);
                ptg[i] = g(ptu[i]);
            }

            model.SetPWLObj(x, ptu, ptf);
            model.SetPWLObj(z, ptu, ptg);

            // Add constraint: x + 2 y + 3 z <= 4

            model.AddConstr(x + 2 * y + 3 * z <= 4.0, "c0");

            // Add constraint: x + y >= 1

            model.AddConstr(x + y >= 1.0, "c1");

            // Optimize model as an LP

            model.Optimize();

            Console.WriteLine("IsMIP: " + model.IsMIP);

            Console.WriteLine(x.VarName + " " + x.X);
            Console.WriteLine(y.VarName + " " + y.X);
            Console.WriteLine(z.VarName + " " + z.X);

            Console.WriteLine("Obj: " + model.ObjVal);

            Console.WriteLine();

            // Negate piecewise-linear objective function for x

            for (int i = 0; i < npts; i++)
            {
                ptf[i] = -ptf[i];
            }

            model.SetPWLObj(x, ptu, ptf);

            // Optimize model as a MIP

            model.Optimize();

            Console.WriteLine("IsMIP: " + model.IsMIP);

            Console.WriteLine(x.VarName + " " + x.X);
            Console.WriteLine(y.VarName + " " + y.X);
            Console.WriteLine(z.VarName + " " + z.X);

            Console.WriteLine("Obj: " + model.ObjVal);

            // Dispose of model and environment

            model.Dispose();
            env.Dispose();
        } catch (GRBException e) {
            Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
        }
    }