public TF_Demand Gen_Inbound_demand()
        {
            TF_Demand inbound = new TF_Demand(this.interval, this.dimension, "I", "B");

            for (int k = 0; k < 1; k++)
            {
                for (int i = 0; i < this.dimension; i++)
                {
                    for (int j = 0; j < this.dimension; j++)
                    {
                        if (i > j)
                        {
                            int m = this.dimension - 1 - i;
                            int l = dimension - 1 - j;
                            inbound.demand[k][m, l] = this.demand[k][i, j];
                        }
                        else
                        {
                            inbound.demand[k][j, i] = 0;
                        }
                    }
                }
            }
            return(inbound);
        }
 static public void new_orchestrator_of_service(TF_Demand outbound_demand, Train_obj train, List <Service> services)
 {
     for (int i = 0; i < outbound_demand.demand.Count; i++)
     {
         outbound_demand.get_demand(i);
         while (!isDemandEmpty(outbound_demand.demand[i]))
         {
             float p;
             int   s = 0;
             (s, p) = index_of_most_utilize_service(outbound_demand.cal_demand, train, services);
             Console.WriteLine("\n\nthis is CARRY_matrix \n\n\n");
             showarray(outbound_demand.carry_matrix);
             Console.WriteLine("this is PERCENT " + p);
             if (p <= 60)
             {
                 for (int out_loop = 0; out_loop < outbound_demand.dimension; out_loop++)
                 {
                     for (int in_loop = out_loop + 1; in_loop < outbound_demand.dimension; in_loop++)
                     {
                         if (outbound_demand.demand[i][out_loop, in_loop] > 0)
                         {
                             outbound_demand.carry_matrix[out_loop, in_loop] = i;
                         }
                     }
                 }
                 break;
             }
             Console.WriteLine("----IN ACTUAL ---- " + "\n\n");
             actual_run(outbound_demand, train, services[s], i);
             Console.WriteLine("----OUT ACTUAL ---- " + "\n\n");
         }
         Console.WriteLine("----END--OF--orchestrate----LOOP--- " + i + "\n\n\n\n\n");
     }
     Console.WriteLine("----END--OF--orchestrate-------- ");
 }
 static public void actual_run(TF_Demand demands, Train_obj train, Service service, int timeframe)
 {
     Console.WriteLine("ACTUAL_RUN . ");
     Console.WriteLine(service.ServiceId);
     Train_a_b_c_d_e(demands, train, service, timeframe);
     Console.WriteLine("This is remainning demand . ");
     showarray(demands.cal_demand);
     Console.WriteLine("This is CURRENT demand . ");
     showarray(demands.demand[timeframe]);
     Console.WriteLine("------------------ ");
 }
 public static void clear_remain_demand(TF_Demand demands, int timeframe, int i, int j)
 {
     if (demands.carry_matrix[i, j] == -1)
     {
         demands.demand[timeframe][i, j] = 0;
     }
     else
     {
         for (int k = demands.carry_matrix[i, j]; k <= timeframe; k++)
         {
             demands.demand[k][i, j] = 0;
         }
     }
 }
 public static void update_remain_demand(TF_Demand demands, int timeframe, int fill_demand, int i, int j)
 {
     if (demands.carry_matrix[i, j] == -1)
     {
         demands.demand[timeframe][i, j] -= fill_demand;
     }
     else
     {
         for (int k = demands.carry_matrix[i, j]; k <= timeframe; k++)
         {
             if (demands.demand[k][i, j] < fill_demand)
             {
                 fill_demand            -= demands.demand[k][i, j];
                 demands.demand[k][i, j] = 0;
             }
             else
             {
                 demands.demand[k][i, j] -= fill_demand;
                 return;
             }
         }
     }
 }
        public TF_Demand Gen_Outbound_demand()
        {
            TF_Demand outbound = new TF_Demand(1440, this.dimension, "O", "B");



            for (int i = 0; i < this.dimension; i++)
            {
                for (int j = 0; j < this.dimension; j++)
                {
                    if (i < j)
                    {
                        outbound.demand[0][i, j] = this.demand[0][i, j];
                    }
                    else
                    {
                        outbound.demand[0][i, j] = 0;
                    }
                }
            }

            return(outbound);
        }
        public static (int[, ], int[, ]) serve_demand_form_station(TF_Demand demands, Train_obj train, Service aService, int timeframe)
        {
            Console.WriteLine("\n\nACTUAL_RUN __ serve_demand_form_station ");
            Console.WriteLine(aService.ServiceId);
            showarray(demands.demand[0]);

            int[,] actual_getoff        = new int[5, 5];
            int[,] actual_getoff_income = new int[5, 5];
            int get_off_next_station = 0;
            int i, j, k, next_station_index = -1;
            int fill_demand;

            for (i = 0; i < 5; i++)
            {
                if (aService.StopStation[i] == 0)
                {
                    continue;
                }
                for (int a = 4; a > i; a--)
                {
                    if (aService.StopStation[a] == 1)
                    {
                        next_station_index = a;
                    }
                }

                int demand_at_station = 0;
                //Console.WriteLine("Remainning Seat : " + train.remain_cap);
                get_off_next_station = train.passenger[i];
                // Console.WriteLine("Number of getting off passenger at station " + i + " = " + get_off_next_station);
                train.getOff(i);
                // Console.WriteLine("Remainning Seat after get off : " + train.remain_cap);
                for (k = i + 1; k < 5; k++) // sum of demand at station i
                {
                    if (aService.StopStation[k] == 0)
                    {
                        continue;
                    }
                    demand_at_station += demands.demand[0][i, k];
                    //  Console.WriteLine("Demand at station " + i + " to station " + k + " is " + demands.demand[0][i, k]);
                }
                //  Console.WriteLine("All of Demand at station " + i + " is " + demand_at_station);
                if (demand_at_station < train.remain_cap)
                {
                    for (j = i + 1; j < 5; j++)
                    {
                        if (aService.StopStation[j] == 0)
                        {
                            continue;
                        }
                        train.getOn(demands.demand[0][i, j], j);
                        actual_getoff[i, j]        = demands.demand[0][i, j];
                        actual_getoff_income[i, j] = demands.demand[0][i, j]; // set for income calculation
                        demands.demand[0][i, j]    = 0;
                        clear_remain_demand(demands, timeframe, i, j);
                    }

                    if (i + 1 < 5)
                    {
                        actual_getoff[i, i + 1] += train.remain_cap;
                    }
                }
                else
                {
                    if (demand_at_station == 0)
                    {
                        demand_at_station += 1;
                    }
                    double ratio = (1.0 * train.remain_cap) / demand_at_station;
                    demand_at_station = 0; //reset
                    fill_demand       = 0; //reset
                    for (j = i + 1; j < 5; j++)
                    {
                        if (aService.StopStation[j] == 0)
                        {
                            continue;
                        }
                        fill_demand        = (int)(demands.demand[0][i, j] * ratio);
                        demand_at_station += fill_demand;
                    }
                    int remain_cap = train.remain_cap;
                    // Console.WriteLine("..............Debug demand at station  " + demand_at_station);
                    remain_cap -= demand_at_station;
                    for (j = i + 1; j < 5; j++)
                    {
                        if (aService.StopStation[j] == 0)
                        {
                            continue;
                        }
                        //Console.WriteLine("..............Debug train remainning seat  " + train.remain_cap);
                        //Console.WriteLine("..............Debug train remainning seat  " + remain_cap);
                        //Console.WriteLine("..............Debug Demand at station      " + demands.demand[0][i, j]);
                        //Console.WriteLine("..............Debug ratio      " + ratio);
                        fill_demand = (int)(demands.demand[0][i, j] * ratio);
                        Console.WriteLine("..............Debug fill_demand  " + fill_demand);


                        if (remain_cap > 0 && demands.demand[0][i, j] > fill_demand)
                        {// ROUND UP IN CASE OF REMAINING a few demand
                            //        Console.WriteLine("ROUND UP AT : " + i+"_"+j);
                            train.getOn(fill_demand + 1, j);
                            update_remain_demand(demands, timeframe, fill_demand + 1, i, j);
                            remain_cap                -= 1;
                            actual_getoff[i, j]        = fill_demand + 1;
                            actual_getoff_income[i, j] = fill_demand + 1;
                        }
                        else
                        {// NO FEW demand left
                            train.getOn(fill_demand, j);
                            update_remain_demand(demands, timeframe, fill_demand, i, j);
                            actual_getoff[i, j]        = fill_demand;
                            actual_getoff_income[i, j] = fill_demand;
                        }
                        demand_at_station += fill_demand;
                    }
                    Console.WriteLine("..............train remainning seat AFTER  " + train.remain_cap);
                }
            }
            Console.WriteLine("ACTUAL GETOFF  ");
            showarray(actual_getoff); //served demand
            return(actual_getoff, actual_getoff_income);
        }
        public static void Train_a_b_c_d_e(TF_Demand demands, Train_obj train, Service aService, int timeframe)
        {
            int[,] actual_getoff = new int[5, 5];
            int get_off_next_station = 0;
            int i, j, k, next_station_index = -1;

            for (i = 0; i < 5; i++)
            {
                if (aService.StopStation[i] == 0)
                {
                    continue;
                }
                for (int a = 4; a > i; a--)
                {
                    if (aService.StopStation[a] == 1)
                    {
                        next_station_index = a;
                    }
                }

                int demand_at_station = 0;
                Console.WriteLine("Remainning Seat : " + train.remain_cap);
                get_off_next_station = train.passenger[i];
                Console.WriteLine("Number of getting off passenger at station " + i + " = " + get_off_next_station);
                train.getOff(i);
                Console.WriteLine("Remainning Seat after get off : " + train.remain_cap);
                for (k = i + 1; k < 5; k++) // sum of demand at station i
                {
                    if (aService.StopStation[k] == 0)
                    {
                        continue;
                    }
                    demand_at_station += demands.cal_demand[i, k];
                    Console.WriteLine("Demand at station " + i + " to station " + k + " is " + demands.cal_demand[i, k]);
                }
                Console.WriteLine("All of Demand at station " + i + " is " + demand_at_station);
                if (demand_at_station < train.remain_cap)
                {
                    for (j = i + 1; j < 5; j++)
                    {
                        if (aService.StopStation[j] == 0)
                        {
                            continue;
                        }
                        train.getOn(demands.cal_demand[i, j], j);

                        demands.cal_demand[i, j] = 0;
                        clear_remain_demand(demands, timeframe, i, j);
                    }
                }
                else
                {
                    double ratio = 1.0 * train.remain_cap / demand_at_station;
                    demand_at_station = 0;
                    int fill_demand;
                    for (j = i + 1; j < 5; j++)
                    {
                        if (aService.StopStation[j] == 0)
                        {
                            continue;
                        }

                        fill_demand = (int)(demands.cal_demand[i, j] * ratio);


                        demand_at_station += fill_demand;
                    }
                    int remain_cap = train.remain_cap;
                    remain_cap -= demand_at_station;
                    for (j = i + 1; j < 5; j++)
                    {
                        if (aService.StopStation[j] == 0)
                        {
                            continue;
                        }
                        Console.WriteLine("..............Debug train remainning seat  " + train.remain_cap);
                        Console.WriteLine("..............Debug Demand at station      " + demands.cal_demand[i, j]);
                        Console.WriteLine("..............Debug ratio      " + ratio);
                        fill_demand = (int)(demands.cal_demand[i, j] * ratio);

                        Console.WriteLine("..............Debug fill_demand  " + fill_demand);
                        actual_getoff[i, j] = fill_demand;
                        if (remain_cap > 0 && demands.cal_demand[i, j] > fill_demand)
                        {
                            demands.cal_demand[i, j] -= (fill_demand + 1);
                            train.getOn(fill_demand + 1, j);
                            update_remain_demand(demands, timeframe, fill_demand + 1, i, j);
                            remain_cap -= 1;
                        }
                        else
                        {
                            demands.cal_demand[i, j] -= fill_demand;
                            train.getOn(fill_demand, j);
                            update_remain_demand(demands, timeframe, fill_demand, i, j);
                        }
                        demand_at_station += fill_demand;
                    }
                    Console.WriteLine("..............train remainning seat AFTER  " + train.remain_cap);
                }
            }
        }
        public static (List <Service_summary>, float) genService(List <Service> _services)
        {
            float                  sum_waiting_time = 0;
            Train_obj              train            = new Train_obj(PhysicalData.TrainSize);
            List <Station>         stations         = new List <Station>();
            List <int[, ]>         demStation       = new List <int[, ]>();
            List <Service_summary> summaries        = new List <Service_summary>();

            stations.InsertRange(0, Form1.stations); // copy demand form 1 TF of each station

            int[] tf_memo = init_memo(stations, 0);  // index of start(depart)_time
            int[,] carry_demand = init_carry_demand(tf_memo, stations);
            List <Service> services = new List <Service>();

            services = _services;
            Console.WriteLine(PrettyPrintArrays(tf_memo));
            Console.WriteLine("____demand_of_this_service");
            showarray(carry_demand);
            var   k = 0;
            float last_util_percent_memo = 1;

            while (services[0].depart_time[0].TimeOfDay <= stations[0].start_time.Last().TimeOfDay || last_util_percent_memo >= PhysicalData.utilize_percent)
            {
                Console.WriteLine("CONDITION 1 : " + (services[0].depart_time[0].TimeOfDay <= stations[0].start_time.Last().TimeOfDay).ToString());
                Console.WriteLine("CONDITION 2 : " + (last_util_percent_memo >= PhysicalData.utilize_percent).ToString());
                Console.WriteLine(" " + services[0].depart_time[0].TimeOfDay);
                Console.WriteLine("-------------NEWLOOP-----station-- : " + stations[0].start_time[0]);
                Console.WriteLine("-------------NEWLOOP-----service-- : " + services[0].depart_time[0]);
                //    Console.WriteLine(" " +last_util_percent_memo);
                Console.WriteLine("-------------NEWLOOP------------ : " + k);

                if (services[0].depart_time[0].Day != 1)
                {
                    break;                                      // new day not allow
                }
                for (var i = 0; i < services.Count; i++)
                {
                    Console.WriteLine(services[i].depart_time[0].ToShortTimeString());
                    Console.WriteLine(services[i].depart_time[1].ToShortTimeString());
                    Console.WriteLine(services[i].depart_time[2].ToShortTimeString());
                    Console.WriteLine(services[i].depart_time[3].ToShortTimeString());
                    Console.WriteLine(services[i].depart_time[4].ToShortTimeString());
                    demStation.Insert(i,
                                      demand_of_this_service(services[i].depart_time, stations, carry_demand, tf_memo));
                }


                for (var i = 0; i < services.Count; i++)
                {
                    Console.WriteLine("SERVICE : " + services[i].ServiceId);

                    if (!isDemandEmpty(demStation[i]))
                    {
                        showarray(demStation[i]);
                    }
                    else
                    {
                        Console.WriteLine("-------------THIS DEMAND EMPTY------------ : ");
                    }
                }

                var(s, p) = new_index_of_most_utilize_service(demStation, train, services);

                Console.WriteLine("S and P index : " + s + " _ " + p);
                last_util_percent_memo = p;            //set lastest utilize percent

                if (p >= PhysicalData.utilize_percent) //util handle
                {
                    TF_Demand outboundDemand = new TF_Demand(demStation[s]).Gen_Outbound_demand();
                    var(served_demand, served_demand_income) = serve_demand_form_station(outboundDemand, train, services[s], 0);
                    Console.WriteLine("__ REMAINNING \n");
                    showarray(outboundDemand.demand[0]);
                    Console.WriteLine("__ REMAINNING 22222222\n");
                    Console.WriteLine(services[s].depart_time[0].ToShortTimeString());
                    update_memo(services[s].depart_time, stations, tf_memo); //set memo
                    Console.WriteLine(services[s].depart_time[0].ToShortTimeString());

                    Console.WriteLine("UPDATED_ MEMO " + PrettyPrintArrays(tf_memo));
                    Console.WriteLine("REMAIN CARRY DEMAND");
                    carry_demand = outboundDemand.demand[0]; //set carry demand
                    showarray(carry_demand);
                    Console.WriteLine("NEXT" + PhysicalData.headway + "  MINUTE\n");

                    var test_depart = services[s].depart_time.Clone() as DateTime[];
                    int income      = calIncome(served_demand_income);
                    Console.WriteLine("_____________________________________ REMAINNING ___________________________________________________\n");
                    showarray(served_demand_income);
                    Service_summary _summary = new Service_summary(services[s].ServiceId, services[s].StopStation, test_depart, served_demand, p, income, served_demand_income);

                    foreach (var _service in services)
                    {
                        _service.add_starttime(PhysicalData.headway);
                    }
                    k++;

                    summaries.Add(_summary);
                }
                else
                {
                    Console.WriteLine("-------BELOW UTIL PERCENT ------- ");

                    foreach (var _service in services)
                    {
                        _service.add_starttime(PhysicalData.under_util_plus_min);
                    }
                }
            }

            update_remaining_demand(summaries, stations);

            return(summaries, sum_waiting_time);
        }