Example #1
0
    static void Main()
    {
        try {
            GRBEnv   env   = new GRBEnv("qcp.log");
            GRBModel model = new GRBModel(env);

            // Create variables

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

            // Integrate new variables

            model.Update();

            // Set objective

            GRBLinExpr obj = x;
            model.SetObjective(obj, GRB.MAXIMIZE);

            // Add linear constraint: x + y + z = 1

            model.AddConstr(x + y + z == 1.0, "c0");

            // Add second-order cone: x^2 + y^2 <= z^2

            model.AddQConstr(x * x + y * y <= z * z, "qc0");

            // Add rotated cone: x^2 <= yz

            model.AddQConstr(x * x <= y * z, "qc1");

            // Optimize model

            model.Optimize();

            Console.WriteLine(x.Get(GRB.StringAttr.VarName)
                              + " " + x.Get(GRB.DoubleAttr.X));
            Console.WriteLine(y.Get(GRB.StringAttr.VarName)
                              + " " + y.Get(GRB.DoubleAttr.X));
            Console.WriteLine(z.Get(GRB.StringAttr.VarName)
                              + " " + z.Get(GRB.DoubleAttr.X));

            Console.WriteLine("Obj: " + model.Get(GRB.DoubleAttr.ObjVal) + " " +
                              obj.Value);

            // Dispose of model and env

            model.Dispose();
            env.Dispose();
        } catch (GRBException e) {
            Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
        }
    }
 public static void AddConstr(this GRBModel _m, GRBTempConstr _constr)
 {
     try
     {
         _m.AddConstr(_constr, null);
     }
     catch (GRBException _e) when(_e.ErrorCode == 10003)
     {
         _m.AddQConstr(_constr, null);
     }
 }
Example #3
0
        public static void AddConstr(this GRBModel _m, GRBTempConstr _constr)
        {
            try
            {
                _m.AddConstr(_constr, null);
            }
            catch (GRBException _e)
            {
                switch (_e.ErrorCode)
                {
                case 10003:
                    _m.AddQConstr(_constr, null);
                    break;

                case 20001:
                    _m.Update();
                    _m.AddConstr(_constr);
                    break;
                }
            }
        }
Example #4
0
    static void Main()
    {
        try {
            GRBEnv   env   = new GRBEnv("bilinear.log");
            GRBModel model = new GRBModel(env);

            // Create variables

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

            // Set objective

            GRBLinExpr obj = x;
            model.SetObjective(obj, GRB.MAXIMIZE);

            // Add linear constraint: x + y + z <= 10

            model.AddConstr(x + y + z <= 10, "c0");

            // Add bilinear inequality: x * y <= 2

            model.AddQConstr(x * y <= 2, "bilinear0");

            // Add bilinear equality: x * z + y * z == 1

            model.AddQConstr(x * z + y * z == 1, "bilinear1");

            // Optimize model

            try {
                model.Optimize();
            } catch (GRBException e) {
                Console.WriteLine("Failed (as expected) " + e.ErrorCode + ". " + e.Message);
            }

            model.Set(GRB.IntParam.NonConvex, 2);
            model.Optimize();

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

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

            x.Set(GRB.CharAttr.VType, GRB.INTEGER);
            model.Optimize();

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

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

            // Dispose of model and env

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