static void Main(string[] args)
        {
            //string data_path = "Server = PC-201606172102\\SQLExpress;DataBase = 乘务计划;Integrated Security = true";
            //string data_path = "Server = PC-201606172102\\SQLExpress;DataBase = 乘务计划CSP1440;Integrated Security = true";
            // string data_dir = @"\data\京津";
            Stopwatch sw_all       = new Stopwatch();
            Stopwatch sw_creat_net = new Stopwatch();
            Stopwatch sw_IS        = new Stopwatch();
            Stopwatch sw_BandB     = new Stopwatch();

            sw_all.Start();
            string test_case = "沪杭";                     //"BigScale";////

            CrewRules.All_Num_Crew = 240;                //京津-60;沪杭-240;bigscal-300;bigscal2-400
            string     data_dir = @"\data\" + test_case; //京津";
            DataReader Data     = new DataReader();

            List <string> csvfiles;

            Data.Connect_csvs(out csvfiles, data_dir);
            Data.LoadRules_csv(); //该函数并未考虑时间窗
            //考虑时间窗的话,为了测试方便,直接在代码里设置时间窗的参数,而不是放在文件中

            Data.CrewRules.DisplayRules();
            Data.LoadData_csv(Data.CrewRules.MaxDays);

            sw_creat_net.Start();
            NetWork Net = new NetWork();

            Net.CreateNetwork(Data);
            Net.IsAllTripsCovered();
            sw_creat_net.Stop();

            sw_IS.Start();
            //检查无误
            InitialSolution IS = new InitialSolution(Net);

            //IS.GetFeasibleSolutionByPenalty();
            IS.GetFeasibleSolutionByMethod1();//顺逆向标号在多基地时有问题:如对点i,顺向时最短路对应基地为B1,逆向时最短路对应基地为B2.错误
            sw_IS.Stop();

            Report Report_IS = new Report(IS.PathSet);

            Console.WriteLine(Report_IS.TransferSolution());
            Console.WriteLine("平均纯乘务时间:{0} 平均换乘时间{1} 平均task数{2}", Report_IS.summary_mean.mean_PureCrew,
                              Report_IS.summary_mean.mean_Trans, Report_IS.summary_mean.mean_Tasks);
            //string initial_solution_dir = System.Environment.CurrentDirectory + "\\结果\\京津\\初始解.txt";
            string initial_solution_dir = System.Environment.CurrentDirectory + "\\结果\\" + test_case + "\\初始解.txt";

            //Report_IS.WriteCrewPaths(@"D:\代码项目\C#\CG-version2.1_cost_1440-重构\CG_CSP_1440\jj158ISS_LineList.txt");
            Report_IS.WriteCrewPaths(initial_solution_dir);
            Console.WriteLine(sw_IS.Elapsed.TotalSeconds);
            //checked:OK
            /*********<<下面开始测试CG>>***************************************************************************/
            sw_BandB.Start();
            CSP csp = new CSP(Net);

            csp.testCase = test_case;
            csp.Branch_and_Price(IS);
            sw_BandB.Stop();
            sw_all.Stop();

            StreamWriter obj_iter = new StreamWriter(System.Environment.CurrentDirectory + "\\结果\\" + test_case + "\\OBJ迭代.csv");

            obj_iter.WriteLine("ObjValue");
            foreach (var obj in csp.obj_iter)
            {
                obj_iter.WriteLine(obj);
            }
            obj_iter.Close();


            string       path     = System.Environment.CurrentDirectory + "\\结果\\" + test_case + "\\求解信息.txt";
            FileStream   fs       = new FileStream(path, FileMode.Create);
            StreamWriter strwrite = new StreamWriter(fs);

            strwrite.WriteLine("建网时间:{0}", sw_creat_net.Elapsed.TotalSeconds);
            strwrite.WriteLine("接续弧的数量:{0}", Net.ArcSet.Count());
            strwrite.WriteLine("网络中节点数量:{0}", Net.NodeSet.Count());
            strwrite.WriteLine("初始解产生时间:{0}", sw_IS.Elapsed.TotalSeconds);
            strwrite.WriteLine("分支定界用时:{0}", sw_BandB.Elapsed.TotalSeconds);
            strwrite.WriteLine("总乘务组数量:{0}", csp.num_all_crew);
            strwrite.WriteLine("模型总共求解时间:{0}", sw_all.Elapsed.TotalSeconds);
            strwrite.WriteLine("总目标函数:{0}", /*csp.OBJVALUE);*/ csp.All_obj);
            strwrite.WriteLine("最短路总计算次数:{0}", csp.total_nums_rcspp);
            strwrite.WriteLine("初始解列数:{0}", IS.PathSet.Count);
            strwrite.WriteLine("列池总数:{0}", csp.ColumnPool.Count);
            strwrite.Close();
            fs.Close();

            string dualprice_iter = System.Environment.CurrentDirectory + "\\结果\\" + test_case + "\\对偶乘子迭代.csv";

            strwrite = new StreamWriter(dualprice_iter, false, Encoding.Default);
            //strwrite.WriteLine("iter,dual_price");
            csp.task_dualPrice.OrderBy(k => k.Key).ToDictionary(k => k.Key, v => v.Value);
            int i = 0;

            for (i = 1; i < csp.task_dualPrice.Keys.Count; i++)
            {
                strwrite.Write("task" + i + ",");
            }
            strwrite.WriteLine("task" + i);
            for (int j = 0; j < csp.task_dualPrice[1].Count; j++)
            {
                for (i = 1; i < csp.task_dualPrice.Keys.Count; i++)
                {
                    strwrite.Write(csp.task_dualPrice[i][j] + ",");
                }
                strwrite.WriteLine(csp.task_dualPrice[i][j]);
            }
            strwrite.Close();

            string num_label_iter = System.Environment.CurrentDirectory + "\\结果\\" + test_case + "\\标号数量与求解时间迭代(2).csv";

            strwrite = new StreamWriter(num_label_iter, false, Encoding.Default);
            strwrite.WriteLine("iter,label num,time*10000");
            for (i = 0; i < csp.num_label_iter.Count; i++)
            {
                strwrite.WriteLine(i + 1 + "," + csp.num_label_iter[i] + "," + csp.time_rcspp[i] * 10000);
            }
            strwrite.Close();

            //string time_rcspp_iter = System.Environment.CurrentDirectory + "\\结果\\" + test_case + "\\最短路求解时间迭代.csv";
            //strwrite = new StreamWriter(time_rcspp_iter, false, Encoding.Default);
            //strwrite.WriteLine("iter,time");
            //for (i = 0; i < csp.time_rcspp.Count; i++)
            //{
            //    strwrite.WriteLine(i+1 + "," );
            //}
            //strwrite.Close();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            string data_path = "Server = PC-201606172102\\SQLExpress;DataBase = 乘务计划;Integrated Security = true";
            /*--LOAD DATA FROM SQL--*/
            NetWork Network = new NetWork();

            Network.CreateNetwork(data_path);
            Network.IsAllTripsCovered();
            //建网,一天的没问题,但未删除出入度为0的点与弧
            //出度为0,说明到达站不在乘务基地,说明其到达时间太晚,无法接续 其他到站为乘务基地的车次
            //入度为0,说明出发站不在乘务基地,说明其出发时间太早,发站为乘务基地的车次 无法接续 到它
            //两天的网,再说
            System.Diagnostics.Stopwatch Net_start = new System.Diagnostics.Stopwatch();
            Net_start.Start();
            InitialSolution IS = new InitialSolution(Network); //京津,200车次,初始解耗时:107s;建网仅0.6s

            IS.GetFeasibleSolutionByPenalty();                 //Add 2-21-2019
            Net_start.Stop();
            TimeSpan initiail_solution = Net_start.Elapsed;

            Console.WriteLine("time for get inition solution : " + initiail_solution.TotalSeconds);

            //test path content of initial feasible solution
            List <Pairing> initial_Paths = new List <Pairing>();

            initial_Paths = IS.PathSet;
            Node trip  = new Node();
            int  count = 0;
            int  i;

            foreach (Pairing path in initial_Paths)
            {
                Console.WriteLine("route " + count);
                for (i = 0; i < path.Arcs.Count; i++)
                {
                    trip = path.Arcs[i].D_Point;
                    Console.Write(trip.ID + " -> ");
                }
                count++;
            }


            /*--BRANCH AND PRICE--*/
            CSP Csp = new CSP(Network);

            //Csp.Build_RMP(IS);
            Csp.Build_RMP(IS);
            Csp.LinearRelaxation();

            string LP_result_schedule = "C:\\Users\\Administrator\\Desktop\\LP_CYY2.txt";
            //Csp.WriteCrewPaths(LP_result_schedule);

            Cplex          masterModel = Csp.masterModel;
            List <INumVar> vars        = Csp.DvarSet;

            //masterModel.WriteSolutions("D:\\Crew_Solution.txt");
            masterModel.ExportModel("D:\\MP2.lp");

            int j;

            count = 0;
            for (i = 0; i < vars.Count; i++)
            {
                if (masterModel.GetValue((INumVar)(vars[i])) >= 0.5)
                {
                    for (j = 0; j < Csp.PathSet[i].Arcs.Count; j++)
                    {
                        Console.Write(Csp.PathSet[i].Arcs[j].D_Point.ID + " -> ");
                    }
                    //Console.WriteLine();
                    Console.WriteLine("route " + count++);
                }
            }

            for (int t = 0; t < vars.Count; t++)
            {
                masterModel.Add(masterModel.Conversion((INumVar)vars[t], NumVarType.Int));
            }
            //masterModel.SetParam(Cplex.IntParam.VarSel, 3);//强分支
            //masterModel.SetParam(Cplex.DoubleParam.EpGap, 0.05);//相对GAP
            masterModel.SetParam(Cplex.DoubleParam.TiLim, 1800);//求解时间1200s
            masterModel.Solve();
            masterModel.WriteSolutions("D:\\IP_Solutions");

            string IP_result_schedule = "C:\\Users\\Administrator\\Desktop\\IP_Crew_Schedule2.txt";

            //Csp.WriteCrewPaths(IP_result_schedule);

            count = 0;
            for (i = 0; i < vars.Count; i++)
            {
                if (masterModel.GetValue((INumVar)(vars[i])) >= 0.5)
                {
                    for (j = 0; j < Csp.PathSet[i].Arcs.Count; j++)
                    {
                        Console.Write(Csp.PathSet[i].Arcs[j].D_Point.ID + " -> ");
                    }
                    //Console.WriteLine();
                    Console.WriteLine("route " + count++);
                }
            }
            int all_covered_num = 0; count = 0;

            int[] trip_coverd = new int[151];
            for (i = 0; i < 151; i++)
            {
                trip_coverd[i] = 0;
            }
            for (i = 0; i < vars.Count; i++)
            {
                if (masterModel.GetValue((INumVar)(vars[i])) >= 0.5)
                {
                    Console.Write("route " + (++count) + ",");
                    Console.Write(Csp.PathSet[i].Arcs.Count - 1 + "\t");
                    all_covered_num = all_covered_num + Csp.PathSet[i].Arcs.Count - 1;
                    Node trip2 = new Node();
                    for (j = 0; j < Csp.PathSet[i].Arcs.Count; j++)
                    {
                        if (Csp.PathSet[i].Arcs[j].O_Point.LineID != 0)
                        {
                            for (int k = 0; k < 151; k++)
                            {
                                if (Csp.PathSet[i].Arcs[j].O_Point.LineID == (k + 1))
                                {
                                    trip_coverd[k]++;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            Console.WriteLine("all covered node num:" + all_covered_num);
            for (int t = 0; t < trip_coverd.Length; t++)
            {
                if (trip_coverd[t] < 1)
                {
                    Console.Write("<< trip" + (t + 1) + " coverd " + trip_coverd[t] + " times " + " >>  ");
                }
            }
        }