/// <summary>
        /// Gera a saída do problema para escrita na planilha
        /// </summary>
        /// <param name="entrada">Instância de <see cref="EntradaViewModel"/> contendo os dados informados na planilha de dados</param>
        /// <returns>Instancia de <see cref="SaidaViewModel"/> com a quantidade de produção diária para atender a demanda</returns>
        private SaidaViewModel GeraSaidaViewModel(EntradaViewModel entrada)
        {
            var saidaViewModel = new SaidaViewModel
            {
                Produtos = entrada.Produtos
            };

            saidaViewModel.Produtos.ForEach(produto =>
            {
                foreach (var diaSemana in Enum.GetValues(typeof(DiaDaSemana)))
                {
                    var diaSemanaEnum = (DiaDaSemana)diaSemana;

                    var producaoHR = Convert.ToInt32(_grbModel.GetVarByName(produto.GetNomeVariavel(diaSemanaEnum)).X);
                    var producaoHE = Convert.ToInt32(_grbModel.GetVarByName(produto.GetNomeVariavel(diaSemanaEnum, true)).X);
                    var excesso    = Convert.ToInt32(_grbModel.GetVarByName(produto.GetNomeVariavel(diaSemanaEnum, isExcesso: true)).X);

                    if (producaoHE > 0)
                    {
                        saidaViewModel.TeveHoraExtra[diaSemanaEnum] = true;
                    }

                    produto.Producao[diaSemanaEnum] = producaoHR + producaoHE;
                    produto.Excesso[diaSemanaEnum]  = excesso;
                }
            });

            return(saidaViewModel);
        }
        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);
            }
        }
Exemple #3
0
 public void Output()
 {
     Console.WriteLine("---x value---");
     foreach (Node n in Frmk.Data.NodeSet)
     {
         double x = _model.GetVarByName("x_" + n.ID).X;
         Console.WriteLine("x_{0}={1}", n.ID, x.ToString());
     }
 }
Exemple #4
0
        private void GenerateObj()
        {
            GRBLinExpr objExpr = 0;

            // maximizing the total amount of movements
            foreach (Movement m in DataRepository.MovementList)
            {
                if (m is TrainSegmentMovement)
                {
                    objExpr += model.GetVarByName("x_" + m.ToString());
                }
            }
            model.SetObjective(objExpr, GRB.MAXIMIZE);
        }
Exemple #5
0
            private bool AddColumn()
            {
                if (_grbModel_SchemeGenerateSub.ObjVal > _grbModel_FlowAssignmentSub.ObjVal)
                {
                    if (_grbModel_SchemeGenerateSub.ObjVal > 0.0000001)
                    {
                        Scheme s = new Scheme();
                        foreach (Node n in Data.NodeSet)
                        {
                            double Y = _grbModel_SchemeGenerateSub.GetVarByName("y_" + n.ID).X;

                            s.Value.Add(n, Convert.ToInt32(Y));
                        }
                        s.ID = SchemeColumnPool.Count;
                        SchemeColumnPool.Add(s);
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    if (_grbModel_FlowAssignmentSub.ObjVal > 0.0000001)
                    {
                        ExtremePoint e = new ExtremePoint();
                        foreach (Node n in Data.NodeSet)
                        {
                            double G = _grbModel_FlowAssignmentSub.GetVarByName("g_" + n.ID).X;
                            e.Value.Add(n, G);
                        }
                        foreach (Arc a in Data.ArcSet)
                        {
                            double flow = _grbModel_FlowAssignmentSub.GetVarByName("x_" + a.FromNode.ID + "_" + a.ToNode.ID).X +
                                          _grbModel_FlowAssignmentSub.GetVarByName("x_" + a.ToNode.ID + "_" + a.FromNode.ID).X;
                            e.TotalCost += flow * Data.FlowFeePerUnit;
                        }
                        e.ID = EPColumnPool.Count;
                        EPColumnPool.Add(e);
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
Exemple #6
0
            private void BuildObj_FA()
            {
                GRBLinExpr expr1 = 0;

                expr1 += SchemeDualValue * 0 + FADualValue * 1;
                foreach (Node n in Data.NodeSet)
                {
                    expr1 += DualSolution[n] * (-_grbModel_FlowAssignmentSub.GetVarByName("g_" + n.ID));
                }

                foreach (Arc a in Data.ArcSet)
                {
                    expr1 += -Data.FlowFeePerUnit * (_grbModel_FlowAssignmentSub.GetVarByName("x_" + a.FromNode.ID + "_" + a.ToNode.ID) + _grbModel_FlowAssignmentSub.GetVarByName("x_" + a.ToNode.ID + "_" + a.FromNode.ID));
                }
                _grbModel_FlowAssignmentSub.SetObjective(expr1, GRB.MAXIMIZE);
            }
Exemple #7
0
            /// <summary>
            /// 求解主问题
            /// </summary>
            /// <returns></returns>
            public bool Solve()
            {
                _env              = new GRBEnv();
                _env.OutputFlag   = 0;
                _env.LogToConsole = 0;

                _model = new GRBModel(_env);

                BuildVar();
                BuildConst();

                _model.Write("Benders_Master.lp");
                _model.Optimize();
                if (_model.Status == GRB.Status.OPTIMAL)
                {
                    Frmk.SlaveObj = new SlaveObjCoef();
                    foreach (Node n in Frmk.Data.NodeSet)
                    {
                        double x = _model.GetVarByName("x_" + n.ID).X;
                        Frmk.SlaveObj.x.Add(n, Convert.ToInt32(x));
                    }
                    Output();
                    return(true);
                }
                else
                {
                    throw new ApplicationException("没可行解!");
                    return(false);
                }
            }
Exemple #8
0
        private void BuildBranchingTree(BranchNode branchNode)
        {
            Dictionary <Node, double> nodeTestingValue = new Dictionary <Node, double>();

            foreach (Node n in Data.NodeSet)
            {
                double nodeValue = 0;
                foreach (Scheme s in SchemeColumnPool)
                {
                    double var = _grbModelMaster.GetVarByName("lambda_" + s.ID).X;
                    nodeValue += s.Value[n] * var;
                }
                nodeTestingValue.Add(n, nodeValue);
            }
            Node currentNode = nodeTestingValue.Max().Key;


            BranchNode leftBranchNode = new BranchNode();/*优势分支*/

            branchNode.LeftChild      = leftBranchNode;
            leftBranchNode.ParentNode = branchNode;

            leftBranchNode.ConstrPool = new Dictionary <Node, double>(branchNode.ConstrPool);/*generate constraint pool*/
            if (nodeTestingValue[currentNode] > 0.5)
            {
                leftBranchNode.ConstrPool.Add(currentNode, 1);
            }
            else
            {
                leftBranchNode.ConstrPool.Add(currentNode, 0);
            }
            Branching(leftBranchNode);

            BranchNode rightBranchNode = new BranchNode();                                     /*劣势分支*/

            rightBranchNode.ConstrPool = new Dictionary <Node, double>(branchNode.ConstrPool); /*generate constraint pool*/
            if (nodeTestingValue[currentNode] > 0.5)
            {
                rightBranchNode.ConstrPool.Add(currentNode, 1);
            }
            else
            {
                rightBranchNode.ConstrPool.Add(currentNode, 0);
            }
            Branching(rightBranchNode);
        }
Exemple #9
0
 private void BuildConstr()
 {
     foreach (Node n in Frmk.Data.NodeSet)
     {
         GRBVar alpha = _model.GetVarByName("alpha_" + n.ID);
         GRBVar beta  = _model.GetVarByName("beta_" + n.ID);
         _model.AddConstr(-alpha + beta <= 0, "ct_g_" + n.ID);
     }
     foreach (Arc a in Frmk.Data.ArcSet)
     {
         GRBVar fromBeta = _model.GetVarByName("beta_" + a.FromNode.ID);
         GRBVar toBeta   = _model.GetVarByName("beta_" + a.ToNode.ID);
         GRBVar gamma    = _model.GetVarByName("gamma_" + a.FromNode.ID + "_" + a.ToNode.ID);
         _model.AddConstr(-fromBeta + toBeta - gamma <= Frmk.Data.FlowFeePerUnit, "ct_f+_" + a.FromNode.ID + "_" + a.ToNode.ID);
         _model.AddConstr(fromBeta - toBeta - gamma <= Frmk.Data.FlowFeePerUnit, "ct_f-_" + a.FromNode.ID + "_" + a.ToNode.ID);
     }
 }
Exemple #10
0
        private bool AddColumn()
        {
            if (_grbModel_SchemeGenerateSub.ObjVal > _grbModel_FlowAssignmentSub.ObjVal)
            {
                if (_grbModel_SchemeGenerateSub.ObjVal > 0)
                {
                    Scheme s = new Scheme();
                    foreach (Node n in Data.NodeSet)
                    {
                        double Y = _grbModel_SchemeGenerateSub.GetVarByName("y_" + n.ID).X;

                        s.Value.Add(n, Convert.ToInt32(Y));
                    }
                    s.ID = SchemeColumnPool.Count;
                    SchemeColumnPool.Add(s);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                if (_grbModel_FlowAssignmentSub.ObjVal > 0)
                {
                    foreach (Node n in Data.NodeSet)
                    {
                        ExtremePoint e = new ExtremePoint();
                        double       G = _grbModel_FlowAssignmentSub.GetVarByName("g_" + n.ID).X;
                        e.Value.Add(n, G);
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        void BuildModel_RestrMaster()
        {
            _env = new GRBEnv("SolutionLog.log");
            _env.Set(GRB.DoubleParam.MIPGap, 0.0);
            _env.Set(GRB.DoubleParam.TimeLimit, 500);
            _grbModel = new GRBModel(_env);

            //决策变量
            foreach (Node n in Data.NodeSet)
            {
                n.Result_GenerateFlow = _grbModel.AddVar(0.0, M, 0.0, GRB.CONTINUOUS, "g_" + n.ID);
            }

            foreach (Dictionary <Node, int> scheme in SchemeSet)
            {
                _grbModel.AddVar(0.0, 1.0, 0.0, GRB.CONTINUOUS, "k_" + SchemeSet.IndexOf(scheme));
            }

            foreach (Arc a in Data.ArcSet)
            {
                a.Result_FlowF = _grbModel.AddVar(0.0, M, 0.0, GRB.CONTINUOUS, "fF_" + a.FromNode.ID + "_" + a.ToNode.ID);
                a.Result_FlowR = _grbModel.AddVar(0.0, M, 0.0, GRB.CONTINUOUS, "fR_" + a.FromNode.ID + "_" + a.ToNode.ID);
            }
            _grbModel.Update();

            //目标函数
            GRBLinExpr expr1 = 0;

            foreach (Dictionary <Node, int> scheme in SchemeSet)
            {
                foreach (Node n in Data.NodeSet)
                {
                    expr1 += scheme[n] * Data.ServerInstalationFee * _grbModel.GetVarByName("k_" + SchemeSet.IndexOf(scheme));
                }
            }
            GRBLinExpr expr2 = 0;

            foreach (Arc a in Data.ArcSet)
            {
                expr2 += (a.Result_FlowF + a.Result_FlowR) * Data.FlowFeePerUnit;
            }

            _grbModel.SetObjective(expr1 + expr2, GRB.MINIMIZE);


            //约束条件

            foreach (Node n in Data.NodeSet)
            {
                GRBLinExpr expr = 0;
                foreach (Dictionary <Node, int> scheme in SchemeSet)
                {
                    expr += scheme[n] * _grbModel.GetVarByName("k_" + SchemeSet.IndexOf(scheme)) * M;
                }
                _grbModel.AddConstr(expr - n.Result_GenerateFlow >= 0, "ct1_" + n.ID);
            }

            foreach (Node n in Data.NodeSet)
            {
                GRBLinExpr sum1 = 0;
                GRBLinExpr sum2 = 0;
                GRBLinExpr sum3 = 0;
                GRBLinExpr sum4 = 0;
                foreach (Arc a in n.ArcSet)
                {
                    if (a.ToNode == n)//进
                    {
                        sum1 += a.Result_FlowF;
                        sum3 += a.Result_FlowR;
                    }
                    else//出
                    {
                        sum2 += a.Result_FlowR;
                        sum4 += a.Result_FlowF;
                    }
                }
                _grbModel.AddConstr(n.Result_GenerateFlow + sum1 + sum2 == n.Demand + sum3 + sum4, "ct2_" + n.ID);
            }
            foreach (Arc a in Data.ArcSet)
            {
                _grbModel.AddConstr(a.Result_FlowF + a.Result_FlowR <= a.Capacity, "ct3_" + a.FromNode.ID + "_" + a.ToNode.ID);
            }

            GRBLinExpr exprCtr = 0;

            foreach (Dictionary <Node, int> scheme in SchemeSet)
            {
                exprCtr += _grbModel.GetVarByName("k_" + SchemeSet.IndexOf(scheme));
            }
            _grbModel.AddConstr(exprCtr == 1, "ct4");
        }
Exemple #12
0
            public bool GenerateInitialFeasibleSolution()
            {
                //foreach (Node n in Data.NodeSet)
                //{
                //    Scheme s = new LargeScaleFrmk.CG.Scheme();
                //    foreach (Node n2 in Data.NodeSet)
                //    {
                //        if (n == n2)
                //        {
                //            s.Value.Add(n2, 1);
                //            continue;
                //        }
                //        s.Value.Add(n2, 0);
                //    }
                //    s.ID = SchemeColumnPool.Count;
                //    SchemeColumnPool.Add(s);
                //}

                Scheme s = new LargeScaleFrmk.CG.Scheme();

                foreach (Node n2 in Data.NodeSet)
                {
                    if (CurrentBranchNode.ConstrPool.ContainsKey(n2))
                    {
                        s.Value.Add(n2, Convert.ToInt32(CurrentBranchNode.ConstrPool[n2]));
                    }
                    else
                    {
                        s.Value.Add(n2, 1);
                    }
                }
                s.ID = SchemeColumnPool.Count;
                SchemeColumnPool.Add(s);


                //s = new LargeScaleFrmk.CG.Scheme();
                //foreach (Node n2 in Data.NodeSet)
                //{
                //    if (n2.ID == "1")
                //    {
                //        s.Value.Add(n2, 1);
                //        continue;
                //    }
                //    s.Value.Add(n2, 0);
                //}
                //s.ID = SchemeColumnPool.Count;
                //SchemeColumnPool.Add(s);


                _env_FlowAssignmentSub      = new GRBEnv();
                _grbModel_FlowAssignmentSub = new GRBModel(_env_FlowAssignmentSub);

                BuildVar_FA();
                //BuildObj_FA();
                BuildConst_FA();

                _grbModel_FlowAssignmentSub.Write("FASub.lp");

                _grbModel_FlowAssignmentSub.Optimize();

                if (_grbModel_FlowAssignmentSub.Status == GRB.Status.INFEASIBLE)
                {
                    _grbModel_FlowAssignmentSub.Dispose();
                    _env_FlowAssignmentSub.Dispose();
                    return(false);
                }


                ExtremePoint e = new ExtremePoint();

                foreach (Node n in Data.NodeSet)
                {
                    double value = _grbModel_FlowAssignmentSub.GetVarByName("g_" + n.ID).X;
                    e.Value.Add(n, value);
                }
                foreach (Arc a in Data.ArcSet)
                {
                    double flow = _grbModel_FlowAssignmentSub.GetVarByName("x_" + a.FromNode.ID + "_" + a.ToNode.ID).X +
                                  _grbModel_FlowAssignmentSub.GetVarByName("x_" + a.ToNode.ID + "_" + a.FromNode.ID).X;
                    e.TotalCost += flow * Data.FlowFeePerUnit;
                }
                e.ID = 0;

                EPColumnPool.Add(e);

                _grbModel_FlowAssignmentSub.Dispose();
                _env_FlowAssignmentSub.Dispose();

                return(true);
            }
Exemple #13
0
 public double GetLambda(Scheme s)
 {
     return(_grbModelMaster.GetVarByName("lambda_" + s.ID).X);
 }
Exemple #14
0
            public bool Solve()
            {
                _env              = new GRBEnv();
                _env.OutputFlag   = 0;
                _env.LogToConsole = 0;

                _model = new GRBModel(_env);
                _model.Parameters.InfUnbdInfo = 1;

                BuildVar();
                BuildConstr();

                _model.Write("Benders_Slave.lp");
                _model.Optimize();

                if (_model.Status == GRB.Status.OPTIMAL)
                {
                    LazyConstr newConstr = new LargeScaleFrmk.Benders.LazyConstr();
                    foreach (Node n in Frmk.Data.NodeSet)
                    {
                        newConstr.Alpha.Add(n, _model.GetVarByName("alpha_" + n.ID).X);
                        newConstr.Beta.Add(n, _model.GetVarByName("beta_" + n.ID).X);
                    }
                    foreach (Arc a in Frmk.Data.ArcSet)
                    {
                        newConstr.Gamma.Add(a, _model.GetVarByName("gamma_" + a.FromNode.ID + "_" + a.ToNode.ID).X);
                    }
                    newConstr.Type = "OPTIMAL";
                    newConstr.ID   = Frmk.ConstrPool.Count;
                    Frmk.ConstrPool.Add(newConstr);

                    Output();
                    return(true);
                }
                else if (_model.Status == GRB.Status.UNBOUNDED)
                {
                    LazyConstr newConstr = new LazyConstr();
                    foreach (Node n in Frmk.Data.NodeSet)
                    {
                        newConstr.Alpha.Add(n, _model.GetVarByName("alpha_" + n.ID).Get(GRB.DoubleAttr.UnbdRay));
                        newConstr.Beta.Add(n, _model.GetVarByName("beta_" + n.ID).Get(GRB.DoubleAttr.UnbdRay));
                    }
                    foreach (Arc a in Frmk.Data.ArcSet)
                    {
                        newConstr.Gamma.Add(a, _model.GetVarByName("gamma_" + a.FromNode.ID + "_" + a.ToNode.ID).Get(GRB.DoubleAttr.UnbdRay));
                    }
                    newConstr.Type = "FEASIBLE";
                    newConstr.ID   = Frmk.ConstrPool.Count;
                    Frmk.ConstrPool.Add(newConstr);

                    Console.WriteLine("---Unbounded!!---");
                    return(true);
                }
                else if (_model.Status == GRB.Status.INFEASIBLE)
                {
                    return(false);
                }
                else
                {
                    return(false);
                }
            }