public void InitCG(CreateNetWork_db net)
 {
     num_task    = net.LineList.Count;
     RMP         = new Cplex();
     DvarSet     = new List <INumVar>(net.CrewList.Count);
     CoverMatrix = new List <int[]>();
     DualSet     = new double[num_task];
     constraints = new IRange[num_task];
     ColumnPool  = new List <Pairing>();
 }
 private void getOptSolnContent(CreateNetWork_db net)
 {
     foreach (var pairing in OptSoln)
     {
         string str = "";
         double T = 0, dis = 0;
         LR.TranslationCSP(pairing.Route, net, ref str, ref T, ref dis, 0, 0);
         LR.YUB.Add(str);
     }
 }
        public void LR_and_CG(ref CreateNetWork_db net, int lr_iter_num)
        {
            //1.initial solution
            LR.GenerateLRSoln(ref net, lr_iter_num);
            double lr_lb      = LR.BestLB;
            double ub_with    = 0;
            double ub_without = 0;

            //2.RMP
            //BuildRMP(LR.SortedPathSet);
            //AddColumnsToRMP(LR.LB_PathSet);
            BuildRMP(LR.g_LB_PathSet);


            //求整数解
            IConversion mip = RMP.Conversion(DvarSet.ToArray(), NumVarType.Int);

            RMP.Add(mip);

            RMP.Solve();
            Console.WriteLine("MIP_OBJ:" + RMP.GetObjValue());

            int real_crew_num = 0;

            calLRUB_with_and_without_penalry(net.virtualRoutingCost, ref real_crew_num,
                                             ref ub_with, ref ub_without);
            Console.WriteLine("ub_with:{0}, ub_without:{1}", ub_with, ub_without);
            Console.WriteLine("real crew num:" + real_crew_num);


            //******stop when met stop-condition
            //LR.BestUB = ub;
            LR.BestUB_with_penalty    = ub_with;
            LR.BestUB_without_penalty = ub_without;
            setOptSoln(net.LineList);
            sortOptSolnByASC();
            getOptSolnContent(net);
        }
        public Summery(Lagrange lag, CreateNetWork_db net, double netConstructTime, double solveTime)
        {
            for (int i = 0; i < lag.YUB.Count; i++)
            {
                crew_schedule_content += "乘务交路" + (i + 1).ToString() + "为:" + lag.YUB[i] + "\n";
            }

            //opt_lb_with = lag.BestLB;
            //opt_ub_with = lag.BestUB;
            opt_lb_with    = lag.BestLB_with_penalty;
            opt_ub_with    = lag.BestUB_with_penalty;
            opt_lb_without = lag.BestLB_without_penalty;
            opt_ub_without = lag.BestUB_without_penalty;

            gap_with    = (opt_ub_with - opt_lb_with) / opt_ub_with;
            gap_without = (opt_ub_without - opt_lb_without) / opt_ub_without;

            iteration_num      = lag.k;
            state_node_num     = net.T_S_S_NodeList.Count;
            state_arc_num      = net.T_S_S_ArcList.Count;
            dvar_num           = state_arc_num * lag.CrewCount;
            net_construct_time = netConstructTime;
            solve_time         = solveTime;
        }
        public void RunTest()
        {
            for (int caseIndex = 1; caseIndex < 2 /*TestInstances.Length*/; caseIndex++)
            {
                string caseName = TestInstances[caseIndex];

                //caseName = "SmallCaseSet\\small01";

                string input_dir = @".\DATA\" + caseName + "\\";

                for (int windowIndex = 0; windowIndex < 1 /*FixedMealWindowsDict.Count*/; windowIndex++)
                {
                    KeyValuePair <string, FixedMealWindow> windowNameToParam =
                        FixedMealWindowsDict.ElementAt(windowIndex);
                    string          meal_window   = windowNameToParam.Key;
                    FixedMealWindow curMealWindow = windowNameToParam.Value;
                    string          output_dir    = @".\12算例结果\" + caseName + "\\" + meal_window + "\\";

                    Console.WriteLine("**********START TEST CASE [{0}] CONSIDERING MEAL TIME WINDOW [{1}]**********",
                                      caseName, meal_window);

                    Stopwatch solveTimer = new Stopwatch();
                    solveTimer.Start();
                    Stopwatch netTimer = new Stopwatch();
                    netTimer.Start();

                    //CreateNetWork net = new CreateNetWork();
                    CreateNetWork_db net = new CreateNetWork_db();

                    net.LoadData_csv(1,
                                     input_dir + "Timetable.csv",
                                     input_dir + "CrewBase.csv",
                                     input_dir + "Crew.csv",
                                     input_dir + "Station.csv");

                    net.SetBasicTimeRules(crewRules[caseIndex][0], crewRules[caseIndex][1],
                                          crewRules[caseIndex][2], crewRules[caseIndex][3],
                                          crewRules[caseIndex][4], crewRules[caseIndex][5]);
                    net.ShowBasicTimeRules();
                    //net.CreateT_S_NetWork();
                    net.CreateT_S_NetWork_db();

                    net.SetMealWindows(curMealWindow.lunch_start, curMealWindow.lunch_end,
                                       curMealWindow.supper_start, curMealWindow.supper_end);

                    //net.SetMealTime(30, 40);
                    net.SetMealTime(InstanceMealSpan[caseName][0], 40);
                    if (meal_window == "INF_h")   //不考虑时间窗,则用餐时间设为0
                    {
                        net.minMealTime = 0;
                    }
                    net.SetVirRoutingCost(1000);//(InstanceVirRoutingCostMap[caseName]);

                    //net.CreateT_S_S_NetWork(net_params[caseIndex][0], net_params[caseIndex][1],
                    //    net_params[caseIndex][2], net_params[caseIndex][3],
                    //    net_params[caseIndex][4], net_params[caseIndex][5],
                    //    net_params[caseIndex][6], net_params[caseIndex][7],
                    //    net_params[caseIndex][8], net_params[caseIndex][9]);

                    //net.SetBasicTimeRules(20, 450, 15, 400, 20, 450); //smallcase01

                    //net.SetArcPenaltyMealViolate(crewRules[caseIndex][5]);
                    net.SetArcPenaltyMealViolate(1400);

                    net.CreateT_S_S_Network_db();

                    netTimer.Stop();

                    //StreamWriter logFile = new StreamWriter(output_dir + "stateNodeSet_db.csv");
                    //logFile.WriteLine("ID,NodeType,OStation,LineID,TrainCode,Station,TimeCode,SuperPointID,SuperPointType,PrevNodeID," +
                    //    "DriveTime,DayCrewTime,Lunch,Dinner," +
                    //    "PassLines,PassStateNodes");
                    //foreach (var node in net.T_S_S_NodeList) {
                    //    logFile.Write(Logger.stateNodeInfoToStr(node, OutputMode.file));
                    //    logFile.Write(",");
                    //    logFile.WriteLine(Logger.stateNodePartialPathToStr(node, OutputMode.file));
                    //    //Console.WriteLine(Logger.stateNodePartialPathToStr(node, OutputMode.console));
                    //}
                    //logFile.Close();

                    Console.WriteLine("state arc num: {0}", net.T_S_S_ArcList.Count);
                    Console.WriteLine("state node num: {0}", net.T_S_S_NodeList.Count);


                    bool isnotcontain = true;
                    isnotcontain = net.AllLineContain();
                    if (isnotcontain == false)
                    {
                        CGandLR LR_CG = new CGandLR();
                        LR_CG.InitLR(net, InstanceStepSize[caseName]);
                        //LR_CG.InitLR(net, 100);
                        Lagrange lag = LR_CG.LR;

                        LR_CG.InitCG(net);

                        LR_CG.LR_and_CG(ref net, InstanceLRIteration[caseName]);

                        solveTimer.Stop();

                        Summery summery = new Summery(lag, net, netTimer.Elapsed.TotalSeconds, solveTimer.Elapsed.TotalSeconds);
                        saveSoln(output_dir, summery, lag, 1000 /*InstanceVirRoutingCostMap[caseName]*/);

                        Logger.GetScheduleForVisualize(LR_CG.GetOptPairingSet(), output_dir, caseName, meal_window);
                    }
                    else
                    {
                        throw new Exception("*************current network is not covered all lines, there might be some errors!!!*************\n");
                    }

                    Console.WriteLine("total time spended in solve this case is {0} s", solveTimer.Elapsed.TotalSeconds);
                    Console.WriteLine("**********END TEST CASE [{0}] CONSIDERING MEAL TIME WINDOW [{1}]", caseName, windowIndex);
                }
            }
        }
 public void InitLR(CreateNetWork_db net, int stepSize)
 {
     LR = new Lagrange(net, stepSize);
 }