bool SolveMaster()
        {
            _grbModel.Optimize();
            int status   = _grbModel.Get(GRB.IntAttr.Status);
            int solution = _grbModel.Get(GRB.IntAttr.SolCount);

            if (status == GRB.Status.OPTIMAL || (status == GRB.Status.TIME_LIMIT && solution > 0))
            {
                foreach (Node n in Data.NodeSet)
                {
                    GRBConstr constr = _grbModel.GetConstrByName("ct1_" + n.ID);
                    Dual[n] = constr.Get(GRB.DoubleAttr.Pi);
                    n.ParseSolution(2);
                    n.ParseSolution(1);
                }
                foreach (Arc a in Data.ArcSet)
                {
                    a.ParseSolution();
                }

                double k0 = _grbModel.GetVarByName("k_0").Get(GRB.DoubleAttr.X);
                double k1 = _grbModel.GetVarByName("k_1").Get(GRB.DoubleAttr.X);

                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #2
0
        static void SolvePrimal(GRBEnv env, HashSet <string> nodes_set, List <Arc> arcs)
        {
            GRBModel m = new GRBModel(env);

            Dictionary <string, GRBVar> distances = new Dictionary <string, GRBVar>(); // pi

            foreach (string node in nodes_set)
            {
                distances[node] = m.AddVar(0, GRB.INFINITY, 0, GRB.CONTINUOUS, "distance." + node);
            }
            m.Update();
            distances[ORIGIN].Set(GRB.DoubleAttr.Obj, -1);
            distances[DESTINATION].Set(GRB.DoubleAttr.Obj, 1);
            m.Set(GRB.IntAttr.ModelSense, -1);

            Dictionary <Arc, GRBConstr> constrs = new Dictionary <Arc, GRBConstr>();

            foreach (Arc a in arcs)
            {
                constrs[a] = m.AddConstr(distances[a.dest] <= distances[a.source] + a.length, "distance_con." + a.source + "." + a.dest);
            }

            m.Update();
            m.Write("shortest_path.lp");
            m.Optimize();

            foreach (var pair in distances)
            {
                Console.WriteLine("distance to {0} is {1}", pair.Key, pair.Value.Get(GRB.DoubleAttr.X));
            }

            foreach (var pair in constrs)
            {
                GRBConstr con = pair.Value;
                if (con.Get(GRB.DoubleAttr.Pi) > 0.5)
                {
                    Console.WriteLine("Arc {0}, {1} is in shortest path", pair.Key.source, pair.Key.dest);
                }
            }
            Console.WriteLine("Length of shortest path is {0}", m.Get(GRB.DoubleAttr.ObjVal));
            m.Dispose();
        }
        //Add variable
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            {
                try
                {
                    //change GRBConst into GRBConst[]
                    GRBConstr[] CON = new GRBConstr[1];
                    CON[0] = constraint;

                    double var_coco;
                    double.TryParse(txtAddCon.Text, out var_coco);
                    //change double into double[]
                    Double[] COEFF = new Double[1];
                    COEFF[0] = var_coco;

                    //add variable to constraint
                    MyGlobals.model.ChgCoeff(constraint, variable, var_coco);


                    MyGlobals.model.Update();
                    populate_cmbConstraint();
                    BtnOK.Enabled     = true;
                    BtnCancel.Enabled = false;

                    lblUpdate.Text = "";
                    lblUpdate.Text = constraint.Get(GRB.StringAttr.ConstrName) + ": ";

                    GRBLinExpr con_info = (GRBLinExpr)MyGlobals.model.GetRow(constraint);

                    for (int n = 0; n < con_info.Size; n++)
                    {
                        lblUpdate.Text += " " + con_info.GetCoeff(n) + con_info.GetVar(n).Get(GRB.StringAttr.VarName) + " +";
                    }
                    lblUpdate.Text += " " + constraint.Get(GRB.CharAttr.Sense) + " " + constraint.Get(GRB.DoubleAttr.RHS) + "\n";
                }
                catch (GRBException exc)
                {
                    MessageBox.Show("Error code: " + exc.ErrorCode + ". " + exc.Message, "Error occured", MessageBoxButtons.OK);
                }
            }
        }