Ejemplo n.º 1
0
        /// <summary>
        /// Solve the mathematical model
        /// </summary>
        private void Solve()
        {
            Console.WriteLine("Algorithm starts running at {0}", DateTime.Now);
            var startTime = DateTime.Now;

            _solver.Solve();
            _solutionTime = (DateTime.Now - startTime).Seconds;
            _status       = _solver.GetStatus();
            Console.WriteLine("Algorithm stops running at {0}", DateTime.Now);
        }
        /// <summary>
        ///  get the matchingResult
        /// </summary>
        /// <param name="MatchingNodes"></param>
        /// <param name="ResultText"></param>
        public bool getMatchingResult(MatchingResult matchingResult)
        {
            matchingResult.cplx = this.cplex;

            matchingResult.Feasible = true;
            matchingResult.TimeUse  = this.timeUse;
            //System.Console.WriteLine("Solution status = " + cplex.GetCplexStatus());
            //System.Console.WriteLine("Solution value  = " + cplex.ObjValue);
            //string[] te = cplex.GetCplexStatus().ToString().Split(new string[] { "Best Bound" }, StringSplitOptions.None);//[1].Split(new string[] { "        " }, StringSplitOptions.None)[6];

            int nbCols = this.ilpMatrix.NumVars.Count();

            //this.SaveProb2File("./prob.txt");
            //this.solutionSave("./sol.txt");


            Cplex.CplexStatus status = cplex.GetCplexStatus();
            matchingResult.NbNodes      = cplex.Nnodes;
            matchingResult.MaxTreeNodes = treeNode;
            if (status.Equals(Cplex.CplexStatus.MemLimFeas) || status.Equals(Cplex.CplexStatus.MemLimInfeas))
            {
                matchingResult.MemoryOverFlow = true;
                //Console.Out.WriteLine("Feasible");
            }
            if (status.Equals(Cplex.CplexStatus.AbortTimeLim))
            {
                matchingResult.TimeOverFlow = true;
            }

            Cplex.Status status2 = cplex.GetStatus();
            if (status2.Equals(Cplex.Status.Optimal))
            {
                matchingResult.Optimal = true;
                //Console.Out.WriteLine("optimal");
            }
            if (status2.Equals(Cplex.Status.Feasible))
            {
                matchingResult.Feasible = true;
                //Console.Out.WriteLine("Feasible");
            }
            if (status2.Equals(Cplex.Status.Infeasible))
            {
                matchingResult.Feasible = false;
                // Console.Out.WriteLine("no Feasible solution"); //jamais ici
            }

            double ress;



            if (relaxationcontinue == true)
            {
                matchingResult.Optimal  = false;
                matchingResult.Feasible = false;
            }

            if (flagsolveur == false)
            {
                bestsol = cplex.ObjValue;
            }

            double[] sol = cplex.GetValues(ilpMatrix);

            string valName;

            for (int i = 0; i < nbCols; i++)
            {
                ress    = sol[i];
                valName = ilpMatrix.NumVars[i].Name;
                // System.Console.WriteLine("LB  = " + this.ilpMatrix.NumVars[i].LB);

                if ((ress - 1 < 0.0001) && (ress - 1 > -0.0001))
                {
                    if (valName.StartsWith("x_"))
                    {
                        KeyValuePair <string, string> pair = seperateValName(valName);
                        matchingResult.NodeMatchingDictionary.Add(pair.Key, pair.Value);
                    }
                    else if (valName.StartsWith("y_"))
                    {
                        KeyValuePair <string, string> pair = seperateValName(valName);
                        if (!pair.Key.StartsWith("copy_"))
                        {
                            if (pair.Value.StartsWith("copy_"))
                            {
                                matchingResult.EdgeMatchingDictionary.Add(pair.Key, pair.Value.Substring(5));
                            }
                            else
                            {
                                matchingResult.EdgeMatchingDictionary.Add(pair.Key, pair.Value);
                            }
                        }
                    }
                    else if (valName.StartsWith("u_"))
                    {
                        KeyValuePair <string, string> pair = seperateValName(valName);
                        matchingResult.NodeMatchingDictionary.Add(pair.Key, pair.Value);
                    }
                    else if (valName.StartsWith("e_"))
                    {
                        KeyValuePair <string, string> pair = seperateValName(valName);
                        if (!pair.Key.StartsWith("copy_"))
                        {
                            matchingResult.EdgeMatchingDictionary.Add(pair.Key, pair.Value);
                        }
                    }

                    else if (valName.StartsWith("v_"))
                    {
                        KeyValuePair <string, string> pair = seperateValName(valName);
                        matchingResult.NodeMatchingDictionary.Add(pair.Key, pair.Value);
                    }
                    else if (valName.StartsWith("f_"))
                    {
                        KeyValuePair <string, string> pair = seperateValName(valName);
                        if (!pair.Key.StartsWith("Del_copy_"))
                        {
                            matchingResult.EdgeMatchingDictionary.Add(pair.Key, pair.Value);
                        }
                    }
                }
            }
            if (bestsol < 0.000001 && bestsol > -0.000001)
            {
                bestsol = 0;
            }
            matchingResult.Distance = bestsol;
            return(true);
        }