コード例 #1
0
ファイル: qcp_cs.cs プロジェクト: nachonase/docker-files
    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);
        }
    }
コード例 #2
0
        private void cmbVarName_SelectedIndexChanged(object sender, EventArgs e)
        {
            variable = ((KeyValuePair <string, GRBVar>)cmbVarName.SelectedItem).Value;

            char var_type = variable.Get(GRB.CharAttr.VType);

            switch (var_type)
            {
            case 'C':
                cmbVarType.Text = "CONTINUOUS";
                break;

            case 'B':
                cmbVarType.Text = "BINARY";
                break;

            case 'I':
                cmbVarType.Text = "INTEGER";
                break;

            case 'S':
                cmbVarType.Text = "SEMI-CONTINUOUS";
                break;

            case 'N':
                cmbVarType.Text = "SEMI-INTEGER";
                break;
            }

            cmbVarType.Enabled = false;

            double var_lb = variable.Get(GRB.DoubleAttr.LB);

            txtlb.Text    = var_lb.ToString();
            txtlb.Enabled = false;

            double var_ub = variable.Get(GRB.DoubleAttr.UB);

            txtub.Text    = var_ub.ToString();
            txtub.Enabled = false;

            double var_obcoeff = variable.Get(GRB.DoubleAttr.Obj);

            txtObCo.Text    = var_obcoeff.ToString();
            txtObCo.Enabled = false;

            lblVar.Text = "";
            GRBColumn Constraints = MyGlobals.model.GetCol(variable);

            for (int i = 0; i < Constraints.Size; i++)
            {
                lblVar.Text = lblVar.Text + "\nIn Constraint " + Constraints.GetConstr(i).Get(GRB.StringAttr.ConstrName) + " with Coefficient " + Constraints.GetCoeff(i);
            }
        }
コード例 #3
0
    static void Main()
    {
        try {
            GRBEnv   env   = new GRBEnv("mip1.log");
            GRBModel model = new GRBModel(env);

            // Create variables

            GRBVar x = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "x");
            GRBVar y = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "y");
            GRBVar z = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "z");

            // Integrate new variables

            model.Update();

            // Set objective: maximize x + y + 2 z

            model.SetObjective(x + y + 2 * z, GRB.MAXIMIZE);

            // 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

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

            // Dispose of model and env

            model.Dispose();
            env.Dispose();
        } catch (GRBException e) {
            Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
        }
    }
コード例 #4
0
        //Edit variable
        private void cmbVarName2_SelectedIndexChanged(object sender, EventArgs e)
        {
            variable = ((KeyValuePair <string, GRBVar>)cmbVarName2.SelectedItem).Value;
            double var_obcoeff = variable.Get(GRB.DoubleAttr.Obj);

            txtCoeff2.Text = var_obcoeff.ToString();
        }
コード例 #5
0
    private static int solveAndPrint(GRBModel model, GRBVar totSlack,
                                     int nWorkers, String[] Workers,
                                     GRBVar[] totShifts)
    {
        model.Optimize();
        int status = model.Get(GRB.IntAttr.Status);

        if ((status == GRB.Status.INF_OR_UNBD) ||
            (status == GRB.Status.INFEASIBLE) ||
            (status == GRB.Status.UNBOUNDED))
        {
            Console.WriteLine("The model cannot be solved "
                              + "because it is infeasible or unbounded");
            return(status);
        }
        if (status != GRB.Status.OPTIMAL)
        {
            Console.WriteLine("Optimization was stopped with status " + status);
            return(status);
        }

        // Print total slack and the number of shifts worked for each worker
        Console.WriteLine("\nTotal slack required: " +
                          totSlack.Get(GRB.DoubleAttr.X));
        for (int w = 0; w < nWorkers; ++w)
        {
            Console.WriteLine(Workers[w] + " worked " +
                              totShifts[w].Get(GRB.DoubleAttr.X) + " shifts");
        }
        Console.WriteLine("\n");
        return(status);
    }
コード例 #6
0
 public void ParseSolution()
 {
     if (Result_FlowF as object != null)
     {
         FlowF = Result_FlowF.Get(GRB.DoubleAttr.X);
     }
     if (Result_FlowR as object != null)
     {
         FlowR = Result_FlowR.Get(GRB.DoubleAttr.X);
     }
 }
コード例 #7
0
 public void ParseSolution(int i)
 {
     if (i == 2 && Result_GenerateFlow as object != null)
     {
         GenerateFlow = Result_GenerateFlow.Get(GRB.DoubleAttr.X);
     }
     if (i == 1 && Result_IsServerLoacationSelected as object != null)
     {
         IsServerLocationSelected = Result_IsServerLoacationSelected.Get(GRB.DoubleAttr.X);
     }
 }
コード例 #8
0
        //populating fields on variable selection
        private void cmbVarName_SelectedIndexChanged(object sender, EventArgs e)
        {
            variable = ((KeyValuePair <string, GRBVar>)cmbVarName.SelectedItem).Value;

            char var_type = variable.Get(GRB.CharAttr.VType);

            switch (var_type)
            {
            case 'C':
                cmbVarType.Text = "CONTINUOUS";
                break;

            case 'B':
                cmbVarType.Text = "BINARY";
                break;

            case 'I':
                cmbVarType.Text = "INTEGER";
                break;

            case 'S':
                cmbVarType.Text = "SEMI-CONTINUOUS";
                break;

            case 'N':
                cmbVarType.Text = "SEMI-INTEGER";
                break;
            }

            double var_lb = variable.Get(GRB.DoubleAttr.LB);

            txtlb.Text = var_lb.ToString();

            double var_ub = variable.Get(GRB.DoubleAttr.UB);

            txtub.Text = var_ub.ToString();

            double var_obcoeff = variable.Get(GRB.DoubleAttr.Obj);

            txtObCo.Text = var_obcoeff.ToString();
        }
コード例 #9
0
        //Add LHS
        private void btnAddTerm_Click(object sender, EventArgs e)
        {
            try
            {
                term = ((KeyValuePair <string, GRBVar>)cmbVarName.SelectedItem).Value;
                double coeff;
                double.TryParse(txtCoeff.Text, out coeff);


                MyGlobals.obj.AddTerm(coeff, term);
                btn_Undo.Enabled = true;

                lblConstraint.Text = lblConstraint.Text + " " + coeff + " " + term.Get(GRB.StringAttr.VarName) + " +";
            }
            catch (GRBException exc)
            {
                MessageBox.Show("Error code: " + exc.ErrorCode + ". " + exc.Message, "Error occured", MessageBoxButtons.OK);
            }
            txtCoeff.Clear();
        }
コード例 #10
0
    static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            Console.Out.WriteLine("Usage: sensitivity_cs filename");
            return;
        }

        try {
            // Read model
            GRBEnv   env = new GRBEnv();
            GRBModel a   = new GRBModel(env, args[0]);
            a.Optimize();
            a.GetEnv().Set(GRB.IntParam.OutputFlag, 0);

            // Extract variables from model
            GRBVar[] avars = a.GetVars();

            for (int i = 0; i < avars.Length; ++i)
            {
                GRBVar v = avars[i];
                if (v.Get(GRB.CharAttr.VType) == GRB.BINARY)
                {
                    // Create clone and fix variable
                    GRBModel b  = new GRBModel(a);
                    GRBVar   bv = b.GetVars()[i];
                    if (v.Get(GRB.DoubleAttr.X) - v.Get(GRB.DoubleAttr.LB) < 0.5)
                    {
                        bv.Set(GRB.DoubleAttr.LB, bv.Get(GRB.DoubleAttr.UB));
                    }
                    else
                    {
                        bv.Set(GRB.DoubleAttr.UB, bv.Get(GRB.DoubleAttr.LB));
                    }

                    b.Optimize();

                    if (b.Get(GRB.IntAttr.Status) == GRB.Status.OPTIMAL)
                    {
                        double objchg =
                            b.Get(GRB.DoubleAttr.ObjVal) - a.Get(GRB.DoubleAttr.ObjVal);
                        if (objchg < 0)
                        {
                            objchg = 0;
                        }
                        Console.WriteLine("Objective sensitivity for variable " +
                                          v.Get(GRB.StringAttr.VarName) + " is " + objchg);
                    }
                    else
                    {
                        Console.WriteLine("Objective sensitivity for variable " +
                                          v.Get(GRB.StringAttr.VarName) + " is infinite");
                    }

                    // Dispose of model
                    b.Dispose();
                }
            }

            // Dispose of model and env
            a.Dispose();
            env.Dispose();
        } catch (GRBException e) {
            Console.WriteLine("Error code: " + e.ErrorCode + ". " +
                              e.Message);
        }
    }
コード例 #11
0
    static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            Console.Out.WriteLine("Usage: fixanddive_cs filename");
            return;
        }

        try {
            // Read model
            GRBEnv   env   = new GRBEnv();
            GRBModel model = new GRBModel(env, args[0]);

            // Collect integer variables and relax them
            List <GRBVar> intvars = new List <GRBVar>();
            foreach (GRBVar v in model.GetVars())
            {
                if (v.Get(GRB.CharAttr.VType) != GRB.CONTINUOUS)
                {
                    intvars.Add(v);
                    v.Set(GRB.CharAttr.VType, GRB.CONTINUOUS);
                }
            }

            model.GetEnv().Set(GRB.IntParam.OutputFlag, 0);
            model.Optimize();

            // Perform multiple iterations. In each iteration, identify the first
            // quartile of integer variables that are closest to an integer value
            // in the relaxation, fix them to the nearest integer, and repeat.

            for (int iter = 0; iter < 1000; ++iter)
            {
                // create a list of fractional variables, sorted in order of
                // increasing distance from the relaxation solution to the nearest
                // integer value

                List <GRBVar> fractional = new List <GRBVar>();
                foreach (GRBVar v in intvars)
                {
                    double sol = Math.Abs(v.Get(GRB.DoubleAttr.X));
                    if (Math.Abs(sol - Math.Floor(sol + 0.5)) > 1e-5)
                    {
                        fractional.Add(v);
                    }
                }

                Console.WriteLine("Iteration " + iter + ", obj " +
                                  model.Get(GRB.DoubleAttr.ObjVal) + ", fractional " +
                                  fractional.Count);

                if (fractional.Count == 0)
                {
                    Console.WriteLine("Found feasible solution - objective " +
                                      model.Get(GRB.DoubleAttr.ObjVal));
                    break;
                }

                // Fix the first quartile to the nearest integer value

                fractional.Sort(new FractionalCompare());
                int nfix = Math.Max(fractional.Count / 4, 1);
                for (int i = 0; i < nfix; ++i)
                {
                    GRBVar v      = fractional[i];
                    double fixval = Math.Floor(v.Get(GRB.DoubleAttr.X) + 0.5);
                    v.Set(GRB.DoubleAttr.LB, fixval);
                    v.Set(GRB.DoubleAttr.UB, fixval);
                    Console.WriteLine("  Fix " + v.Get(GRB.StringAttr.VarName) +
                                      " to " + fixval + " ( rel " + v.Get(GRB.DoubleAttr.X) + " )");
                }

                model.Optimize();

                // Check optimization result

                if (model.Get(GRB.IntAttr.Status) != GRB.Status.OPTIMAL)
                {
                    Console.WriteLine("Relaxation is infeasible");
                    break;
                }
            }

            // Dispose of model and env
            model.Dispose();
            env.Dispose();
        } catch (GRBException e) {
            Console.WriteLine("Error code: " + e.ErrorCode + ". " +
                              e.Message);
        }
    }
コード例 #12
0
    static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            Console.Out.WriteLine("Usage: lpmod_cs filename");
            return;
        }

        try {
            // Read model and determine whether it is an LP
            GRBEnv   env   = new GRBEnv();
            GRBModel model = new GRBModel(env, args[0]);
            if (model.Get(GRB.IntAttr.IsMIP) != 0)
            {
                Console.WriteLine("The model is not a linear program");
                Environment.Exit(1);
            }

            model.Optimize();

            int status = model.Get(GRB.IntAttr.Status);

            if ((status == GRB.Status.INF_OR_UNBD) ||
                (status == GRB.Status.INFEASIBLE) ||
                (status == GRB.Status.UNBOUNDED))
            {
                Console.WriteLine("The model cannot be solved because it is "
                                  + "infeasible or unbounded");
                Environment.Exit(1);
            }

            if (status != GRB.Status.OPTIMAL)
            {
                Console.WriteLine("Optimization was stopped with status " + status);
                Environment.Exit(0);
            }

            // Find the smallest variable value
            double minVal = GRB.INFINITY;
            GRBVar minVar = null;
            foreach (GRBVar v in model.GetVars())
            {
                double sol = v.Get(GRB.DoubleAttr.X);
                if ((sol > 0.0001) && (sol < minVal) &&
                    (v.Get(GRB.DoubleAttr.LB) == 0.0))
                {
                    minVal = sol;
                    minVar = v;
                }
            }

            Console.WriteLine("\n*** Setting " +
                              minVar.Get(GRB.StringAttr.VarName) + " from " + minVal +
                              " to zero ***\n");
            minVar.Set(GRB.DoubleAttr.UB, 0.0);

            // Solve from this starting point
            model.Optimize();

            // Save iteration & time info
            double warmCount = model.Get(GRB.DoubleAttr.IterCount);
            double warmTime  = model.Get(GRB.DoubleAttr.Runtime);

            // Reset the model and resolve
            Console.WriteLine("\n*** Resetting and solving "
                              + "without an advanced start ***\n");
            model.Reset();
            model.Optimize();

            double coldCount = model.Get(GRB.DoubleAttr.IterCount);
            double coldTime  = model.Get(GRB.DoubleAttr.Runtime);

            Console.WriteLine("\n*** Warm start: " + warmCount + " iterations, " +
                              warmTime + " seconds");
            Console.WriteLine("*** Cold start: " + coldCount + " iterations, " +
                              coldTime + " seconds");

            // Dispose of model and env
            model.Dispose();
            env.Dispose();
        } catch (GRBException e) {
            Console.WriteLine("Error code: " + e.ErrorCode + ". " +
                              e.Message);
        }
    }
コード例 #13
0
        static void Main(string[] args)
        {
            double[] Demand   = new double[] { 15, 18, 14, 20 };
            double[] Capacity = new double[] { 20, 22, 17, 19, 18 };
            double[,] ShipCosts =
                new double[, ] {
                { 4000, 2500, 1200, 2200 },
                { 2000, 2600, 1800, 2600 },
                { 3000, 3400, 2600, 3100 },
                { 2500, 3000, 4100, 3700 },
                { 4500, 4000, 3000, 3200 }
            };
            int nWarehouses = Capacity.Length;
            int nCustomers  = Demand.Length;

            GRBEnv   env = new GRBEnv();
            GRBModel m   = new GRBModel(env);

            GRBVar[,] Ship = new GRBVar[nWarehouses, nCustomers];
            for (int i = 0; i < nWarehouses; ++i)
            {
                for (int j = 0; j < nCustomers; ++j)
                {
                    Ship[i, j] = m.AddVar(0, GRB.INFINITY, 0, GRB.CONTINUOUS, "ship." + i + "." + j);
                }
            }

            GRBVar[] Shortage = new GRBVar[nCustomers];
            for (int j = 0; j < nCustomers; ++j)
            {
                Shortage[j] = m.AddVar(0, GRB.INFINITY, 0, GRB.CONTINUOUS, "shortage." + j);
            }

            GRBVar TotalShortage     = m.AddVar(0, GRB.INFINITY, 0, GRB.CONTINUOUS, "TotalShortage");
            GRBVar TotalShippingCost = m.AddVar(0, GRB.INFINITY, 1, GRB.CONTINUOUS, "TotalShippingCost");

            m.Update();

            GRBConstr[] DemandCon = new GRBConstr[nCustomers];
            for (int j = 0; j < nCustomers; ++j)
            {
                GRBLinExpr lhs = 0.0;
                for (int i = 0; i < nWarehouses; ++i)
                {
                    lhs += Ship[i, j];
                }
                DemandCon[j] = m.AddConstr(lhs == Demand[j] - Shortage[j], "demand." + j);
            }

            GRBConstr[] CapacityCon = new GRBConstr[nWarehouses];
            for (int i = 0; i < nWarehouses; ++i)
            {
                GRBLinExpr lhs = 0.0;
                for (int j = 0; j < nCustomers; ++j)
                {
                    lhs += Ship[i, j];
                }
                CapacityCon[i] = m.AddConstr(lhs <= Capacity[i], "capacity." + i);
            }

            GRBLinExpr expr = 0.0;

            for (int j = 0; j < nCustomers; ++j)
            {
                expr += Shortage[j];
            }
            GRBConstr TotalShortageCon = m.AddConstr(expr == TotalShortage, "total_shortage");

            expr = 0.0;
            for (int i = 0; i < nWarehouses; ++i)
            {
                for (int j = 0; j < nCustomers; ++j)
                {
                    expr += ShipCosts[i, j] * Ship[i, j];
                }
            }
            GRBConstr TotalShippingCon = m.AddConstr(expr == TotalShippingCost, "total_shipping");

            while (true)
            {
                m.Optimize();
                double OptShortage = TotalShortage.Get(GRB.DoubleAttr.X);
                double OptShipping = TotalShippingCost.Get(GRB.DoubleAttr.X);
                Console.WriteLine("TotalShortage = {0}", OptShortage);
                Console.WriteLine("TotalShippingCost= {0}", OptShipping);
                if (OptShortage < EPS)
                {
                    break;
                }
                double ObjectiveBound = TotalShortage.Get(GRB.DoubleAttr.SAObjUp);
                TotalShortage.Set(GRB.DoubleAttr.Obj, ((1 + EPS) * ObjectiveBound));
            }
        }
コード例 #14
0
    static void Main()
    {
        try {
            // Sample data
            // Sets of days and workers
            string[] Shifts =
                new string[] { "Mon1", "Tue2", "Wed3", "Thu4", "Fri5", "Sat6",
                               "Sun7", "Mon8", "Tue9", "Wed10", "Thu11", "Fri12", "Sat13",
                               "Sun14" };
            string[] Workers =
                new string[] { "Amy", "Bob", "Cathy", "Dan", "Ed", "Fred", "Gu" };

            int nShifts  = Shifts.Length;
            int nWorkers = Workers.Length;

            // Number of workers required for each shift
            double[] shiftRequirements =
                new double[] { 3, 2, 4, 4, 5, 6, 5, 2, 2, 3, 4, 6, 7, 5 };

            // Worker availability: 0 if the worker is unavailable for a shift
            double[,] availability =
                new double[, ] {
                { 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1 },
                { 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 },
                { 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
                { 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
                { 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1 },
                { 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1 },
                { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
            };

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

            // Assignment variables: x[w][s] == 1 if worker w is assigned
            // to shift s. This is no longer a pure assignment model, so we must
            // use binary variables.
            GRBVar[,] x = new GRBVar[nWorkers, nShifts];
            for (int w = 0; w < nWorkers; ++w)
            {
                for (int s = 0; s < nShifts; ++s)
                {
                    x[w, s] =
                        model.AddVar(0, availability[w, s], 0, GRB.BINARY,
                                     Workers[w] + "." + Shifts[s]);
                }
            }

            // Slack variables for each shift constraint so that the shifts can
            // be satisfied
            GRBVar[] slacks = new GRBVar[nShifts];
            for (int s = 0; s < nShifts; ++s)
            {
                slacks[s] =
                    model.AddVar(0, GRB.INFINITY, 0, GRB.CONTINUOUS,
                                 Shifts[s] + "Slack");
            }

            // Variable to represent the total slack
            GRBVar totSlack = model.AddVar(0, GRB.INFINITY, 0, GRB.CONTINUOUS,
                                           "totSlack");

            // Variables to count the total shifts worked by each worker
            GRBVar[] totShifts = new GRBVar[nWorkers];
            for (int w = 0; w < nWorkers; ++w)
            {
                totShifts[w] = model.AddVar(0, GRB.INFINITY, 0, GRB.CONTINUOUS,
                                            Workers[w] + "TotShifts");
            }

            // Update model to integrate new variables
            model.Update();

            GRBLinExpr lhs;

            // Constraint: assign exactly shiftRequirements[s] workers
            // to each shift s, plus the slack
            for (int s = 0; s < nShifts; ++s)
            {
                lhs = new GRBLinExpr();
                lhs.AddTerm(1.0, slacks[s]);
                for (int w = 0; w < nWorkers; ++w)
                {
                    lhs.AddTerm(1.0, x[w, s]);
                }
                model.AddConstr(lhs == shiftRequirements[s], Shifts[s]);
            }

            // Constraint: set totSlack equal to the total slack
            lhs = new GRBLinExpr();
            for (int s = 0; s < nShifts; ++s)
            {
                lhs.AddTerm(1.0, slacks[s]);
            }
            model.AddConstr(lhs == totSlack, "totSlack");

            // Constraint: compute the total number of shifts for each worker
            for (int w = 0; w < nWorkers; ++w)
            {
                lhs = new GRBLinExpr();
                for (int s = 0; s < nShifts; ++s)
                {
                    lhs.AddTerm(1.0, x[w, s]);
                }
                model.AddConstr(lhs == totShifts[w], "totShifts" + Workers[w]);
            }

            // Objective: minimize the total slack
            model.SetObjective(1.0 * totSlack);

            // Optimize
            int status = solveAndPrint(model, totSlack, nWorkers, Workers, totShifts);
            if (status != GRB.Status.OPTIMAL)
            {
                return;
            }

            // Constrain the slack by setting its upper and lower bounds
            totSlack.Set(GRB.DoubleAttr.UB, totSlack.Get(GRB.DoubleAttr.X));
            totSlack.Set(GRB.DoubleAttr.LB, totSlack.Get(GRB.DoubleAttr.X));

            // Variable to count the average number of shifts worked
            GRBVar avgShifts =
                model.AddVar(0, GRB.INFINITY, 0, GRB.CONTINUOUS, "avgShifts");

            // Variables to count the difference from average for each worker;
            // note that these variables can take negative values.
            GRBVar[] diffShifts = new GRBVar[nWorkers];
            for (int w = 0; w < nWorkers; ++w)
            {
                diffShifts[w] = model.AddVar(-GRB.INFINITY, GRB.INFINITY, 0,
                                             GRB.CONTINUOUS, Workers[w] + "Diff");
            }

            // Update model to integrate new variables
            model.Update();

            // Constraint: compute the average number of shifts worked
            lhs = new GRBLinExpr();
            for (int w = 0; w < nWorkers; ++w)
            {
                lhs.AddTerm(1.0, totShifts[w]);
            }
            model.AddConstr(lhs == nWorkers * avgShifts, "avgShifts");

            // Constraint: compute the difference from the average number of shifts
            for (int w = 0; w < nWorkers; ++w)
            {
                model.AddConstr(totShifts[w] - avgShifts == diffShifts[w],
                                Workers[w] + "Diff");
            }

            // Objective: minimize the sum of the square of the difference from the
            // average number of shifts worked
            GRBQuadExpr qobj = new GRBQuadExpr();
            for (int w = 0; w < nWorkers; ++w)
            {
                qobj.AddTerm(1.0, diffShifts[w], diffShifts[w]);
            }
            model.SetObjective(qobj);

            // Optimize
            status = solveAndPrint(model, totSlack, nWorkers, Workers, totShifts);
            if (status != GRB.Status.OPTIMAL)
            {
                return;
            }

            // Dispose of model and env
            model.Dispose();
            env.Dispose();
        } catch (GRBException e) {
            Console.WriteLine("Error code: " + e.ErrorCode + ". " +
                              e.Message);
        }
    }
コード例 #15
0
        //updating changes to variable
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string var_type = this.cmbVarType.GetItemText(this.cmbVarType.SelectedItem);
                string var_name = variable.Get(GRB.StringAttr.VarName);
                double var_lb, var_ub;
                double var_obco;
                double.TryParse(txtlb.Text, out var_lb);
                double.TryParse(txtub.Text, out var_ub);
                double.TryParse(txtObCo.Text, out var_obco);


                switch (var_type)
                {
                case "BINARY":
                    if (use_infinite_ub == false)
                    {
                        variable.Set(GRB.DoubleAttr.LB, var_lb);
                        variable.Set(GRB.DoubleAttr.UB, var_ub);
                        variable.Set(GRB.DoubleAttr.Obj, var_obco);
                        variable.Set(GRB.CharAttr.VType, 'B');
                    }
                    else
                    {
                        variable.Set(GRB.DoubleAttr.LB, var_lb);
                        variable.Set(GRB.DoubleAttr.UB, GRB.INFINITY);
                        variable.Set(GRB.DoubleAttr.Obj, var_obco);
                        variable.Set(GRB.CharAttr.VType, 'B');
                    }
                    break;

                case "CONTINUOUS":
                    if (use_infinite_ub == false)
                    {
                        variable.Set(GRB.DoubleAttr.LB, var_lb);
                        variable.Set(GRB.DoubleAttr.UB, var_ub);
                        variable.Set(GRB.DoubleAttr.Obj, var_obco);
                        variable.Set(GRB.CharAttr.VType, 'C');
                    }
                    else
                    {
                        variable.Set(GRB.DoubleAttr.LB, var_lb);
                        variable.Set(GRB.DoubleAttr.UB, GRB.INFINITY);
                        variable.Set(GRB.DoubleAttr.Obj, var_obco);
                        variable.Set(GRB.CharAttr.VType, 'C');
                    }
                    break;

                case "INTERGER":
                    if (use_infinite_ub == false)
                    {
                        variable.Set(GRB.DoubleAttr.LB, var_lb);
                        variable.Set(GRB.DoubleAttr.UB, var_ub);
                        variable.Set(GRB.DoubleAttr.Obj, var_obco);
                        variable.Set(GRB.CharAttr.VType, 'I');
                    }
                    else
                    {
                        variable.Set(GRB.DoubleAttr.LB, var_lb);
                        variable.Set(GRB.DoubleAttr.UB, GRB.INFINITY);
                        variable.Set(GRB.DoubleAttr.Obj, var_obco);
                        variable.Set(GRB.CharAttr.VType, 'I');
                    }
                    break;

                case "SEMI-CONTINUOUS":
                    if (use_infinite_ub == false)
                    {
                        variable.Set(GRB.DoubleAttr.LB, var_lb);
                        variable.Set(GRB.DoubleAttr.UB, var_ub);
                        variable.Set(GRB.DoubleAttr.Obj, var_obco);
                        variable.Set(GRB.CharAttr.VType, 'S');
                    }
                    else
                    {
                        variable.Set(GRB.DoubleAttr.LB, var_lb);
                        variable.Set(GRB.DoubleAttr.UB, GRB.INFINITY);
                        variable.Set(GRB.DoubleAttr.Obj, var_obco);
                        variable.Set(GRB.CharAttr.VType, 'S');
                    }
                    break;

                case "SEMI-INTERGER":
                    if (use_infinite_ub == false)
                    {
                        variable.Set(GRB.DoubleAttr.LB, var_lb);
                        variable.Set(GRB.DoubleAttr.UB, var_ub);
                        variable.Set(GRB.DoubleAttr.Obj, var_obco);
                        variable.Set(GRB.CharAttr.VType, 'N');
                    }
                    else
                    {
                        variable.Set(GRB.DoubleAttr.LB, var_lb);
                        variable.Set(GRB.DoubleAttr.UB, GRB.INFINITY);
                        variable.Set(GRB.DoubleAttr.Obj, var_obco);
                        variable.Set(GRB.CharAttr.VType, 'N');
                    }
                    break;
                }
                MyGlobals.model.Update();
                lblVar.Text = "";
                GRBVar[] allVar = MyGlobals.model.GetVars();
                for (int i = 0; i < allVar.Length; i++)
                {
                    lblVar.Text = lblVar.Text + "\n" + allVar[i].Get(GRB.DoubleAttr.LB) + " > " + allVar[i].Get(GRB.StringAttr.VarName) + " > " + allVar[i].Get(GRB.DoubleAttr.UB);
                }
                BtnOK.Enabled = true;
            }
            catch (GRBException exc)
            {
                MessageBox.Show("Error code: " + exc.ErrorCode + ". " + exc.Message, "Error occured", MessageBoxButtons.OK);
            }


            txtlb.Clear();
            txtub.Clear();
            txtObCo.Clear();
        }
コード例 #16
0
ファイル: qp_cs.cs プロジェクト: astikg3/docker-files
    static void Main()
    {
        try {
            GRBEnv   env   = new GRBEnv("qp.log");
            GRBModel model = new GRBModel(env);

            // Create variables

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

            // Integrate new variables

            model.Update();

            // Set objective

            GRBQuadExpr obj = x * x + x * y + y * y + y * z + z * z + 2 * x;
            model.SetObjective(obj);

            // 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

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


            // Change variable types to integer

            x.Set(GRB.CharAttr.VType, GRB.INTEGER);
            y.Set(GRB.CharAttr.VType, GRB.INTEGER);
            z.Set(GRB.CharAttr.VType, GRB.INTEGER);

            // 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);
        }
    }
コード例 #17
0
 public static bool IsSetTo(this GRBVar _var, double _value)
 {
     return(_var.Get(GRB.DoubleAttr.LB) == _value && _var.Get(GRB.DoubleAttr.UB) == _value);
 }