public void HybridOptimizationFrom1000Addresses()
        {
            // Create the manager with the api key
            Route4MeManager route4Me = new Route4MeManager("11111111111111111111111111111111");

            #region ======= Add scheduled address book locations to an user account ================================
            string   sAddressFile = @"Data/addresses_1000.csv";
            Schedule sched0       = new Schedule("daily", false);
            //var csv = new CsvReader(File.OpenText("file.csv"));

            using (TextReader reader = File.OpenText(sAddressFile))
            {
                var csv = new CsvReader(reader);
                //int iCount = 0;
                while (csv.Read())
                {
                    var lng      = csv.GetField(0);
                    var lat      = csv.GetField(1);
                    var alias    = csv.GetField(2);
                    var address1 = csv.GetField(3);
                    var city     = csv.GetField(4);
                    var state    = csv.GetField(5);
                    var zip      = csv.GetField(6);
                    var phone    = csv.GetField(7);
                    //var sched_date = csv.GetField(8);
                    var sched_mode            = csv.GetField(8);
                    var sched_enabled         = csv.GetField(9);
                    var sched_every           = csv.GetField(10);
                    var sched_weekdays        = csv.GetField(11);
                    var sched_monthly_mode    = csv.GetField(12);
                    var sched_monthly_dates   = csv.GetField(13);
                    var sched_annually_usenth = csv.GetField(14);
                    var sched_annually_months = csv.GetField(15);
                    var sched_nth_n           = csv.GetField(16);
                    var sched_nth_what        = csv.GetField(17);

                    string sAddress = "";

                    if (address1 != null)
                    {
                        sAddress += address1.ToString().Trim();
                    }
                    if (city != null)
                    {
                        sAddress += ", " + city.ToString().Trim();
                    }
                    if (state != null)
                    {
                        sAddress += ", " + state.ToString().Trim();
                    }
                    if (zip != null)
                    {
                        sAddress += ", " + zip.ToString().Trim();
                    }

                    if (sAddress == "")
                    {
                        continue;
                    }

                    AddressBookContact newLocation = new AddressBookContact();

                    if (lng != null)
                    {
                        newLocation.cached_lng = Convert.ToDouble(lng);
                    }
                    if (lat != null)
                    {
                        newLocation.cached_lat = Convert.ToDouble(lat);
                    }
                    if (alias != null)
                    {
                        newLocation.address_alias = alias.ToString().Trim();
                    }
                    newLocation.address_1 = sAddress;
                    if (phone != null)
                    {
                        newLocation.address_phone_number = phone.ToString().Trim();
                    }

                    //newLocation.schedule = new Schedule[]{};
                    if (!sched0.ValidateScheduleMode(sched_mode))
                    {
                        continue;
                    }

                    bool blNth = false;
                    if (sched0.ValidateScheduleMonthlyMode(sched_monthly_mode))
                    {
                        if (sched_monthly_mode == "nth")
                        {
                            blNth = true;
                        }
                    }
                    if (sched0.ValidateScheduleUseNth(sched_annually_usenth))
                    {
                        if (sched_annually_usenth.ToString().ToLower() == "true")
                        {
                            blNth = true;
                        }
                    }

                    Schedule schedule = new Schedule(sched_mode.ToString(), blNth);

                    DateTime dt = DateTime.Now;
                    //if (schedule.ValidateScheduleMode(sched_mode))
                    //{
                    schedule.mode = sched_mode.ToString();
                    if (schedule.ValidateScheduleEnabled(sched_enabled))
                    {
                        schedule.enabled = Convert.ToBoolean(sched_enabled);
                        if (schedule.ValidateScheduleEvery(sched_every))
                        {
                            int iEvery = Convert.ToInt32(sched_every);
                            switch (schedule.mode)
                            {
                            case "daily":
                                schedule.daily.every = iEvery;
                                break;

                            case "weekly":
                                if (schedule.ValidateScheduleWeekdays(sched_weekdays))
                                {
                                    schedule.weekly.every = iEvery;
                                    string[]   arWeekdays = sched_weekdays.Split(',');
                                    List <int> lsWeekdays = new List <int>();
                                    for (int i = 0; i < arWeekdays.Length; i++)
                                    {
                                        lsWeekdays.Add(Convert.ToInt32(arWeekdays[i]));
                                    }
                                    schedule.weekly.weekdays = lsWeekdays.ToArray();
                                }
                                break;

                            case "monthly":
                                if (schedule.ValidateScheduleMonthlyMode(sched_monthly_mode))
                                {
                                    schedule.monthly.every = iEvery;
                                    schedule.monthly.mode  = sched_monthly_mode.ToString();
                                    switch (schedule.monthly.mode)
                                    {
                                    case "dates":
                                        if (schedule.ValidateScheduleMonthDays(sched_monthly_dates))
                                        {
                                            string[]   arMonthdays = sched_monthly_dates.Split(',');
                                            List <int> lsMonthdays = new List <int>();
                                            for (int i = 0; i < arMonthdays.Length; i++)
                                            {
                                                lsMonthdays.Add(Convert.ToInt32(arMonthdays[i]));
                                            }
                                            schedule.monthly.dates = lsMonthdays.ToArray();
                                        }
                                        break;

                                    case "nth":
                                        if (schedule.ValidateScheduleNthN(sched_nth_n))
                                        {
                                            schedule.monthly.nth.n = Convert.ToInt32(sched_nth_n);
                                        }
                                        if (schedule.ValidateScheduleNthWhat(sched_nth_what))
                                        {
                                            schedule.monthly.nth.what = Convert.ToInt32(sched_nth_what);
                                        }
                                        break;
                                    }
                                }
                                break;

                            case "annually":
                                if (schedule.ValidateScheduleUseNth(sched_annually_usenth))
                                {
                                    schedule.annually.every   = iEvery;
                                    schedule.annually.use_nth = Convert.ToBoolean(sched_annually_usenth);
                                    if (schedule.annually.use_nth)
                                    {
                                        if (schedule.ValidateScheduleNthN(sched_nth_n))
                                        {
                                            schedule.annually.nth.n = Convert.ToInt32(sched_nth_n);
                                        }
                                        if (schedule.ValidateScheduleNthWhat(sched_nth_what))
                                        {
                                            schedule.annually.nth.what = Convert.ToInt32(sched_nth_what);
                                        }
                                    }
                                    else
                                    {
                                        if (schedule.ValidateScheduleYearMonths(sched_annually_months))
                                        {
                                            string[]   arYearmonths = sched_annually_months.Split(',');
                                            List <int> lsMonths     = new List <int>();
                                            for (int i = 0; i < arYearmonths.Length; i++)
                                            {
                                                lsMonths.Add(Convert.ToInt32(arYearmonths[i]));
                                            }
                                            schedule.annually.months = lsMonths.ToArray();
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                    newLocation.schedule = (new List <Schedule>()
                    {
                        schedule
                    }).ToArray();
                    //}

                    string             errorString;
                    AddressBookContact resultContact = route4Me.AddAddressBookContact(newLocation, out errorString);

                    showResult(resultContact, errorString);

                    Thread.Sleep(1000);
                }
            };

            #endregion

            Thread.Sleep(2000);

            #region ======= Get Hybrid Optimization ================================
            TimeSpan      tsp1day         = new TimeSpan(1, 0, 0, 0);
            List <string> lsScheduledDays = new List <string>();
            DateTime      curDate         = DateTime.Now;
            for (int i = 0; i < 5; i++)
            {
                curDate += tsp1day;
                lsScheduledDays.Add(curDate.ToString("yyyy-MM-dd"));
            }
            //string[] ScheduledDays = new string[] { "2017-03-06", "2017-03-07", "2017-03-08", "2017-03-09", "2017-03-10" };

            Address[] Depots = new Address[] {
                new Address {
                    AddressString     = "2017 Ambler Ave, Abilene, TX, 79603-2239",
                    IsDepot           = true,
                    Latitude          = 32.474395,
                    Longitude         = -99.7447021,
                    CurbsideLatitude  = 32.474395,
                    CurbsideLongitude = -99.7447021
                },
                new Address {
                    AddressString     = "807 Ridge Rd, Alamo, TX, 78516-9596",
                    IsDepot           = true,
                    Latitude          = 26.170834,
                    Longitude         = -98.116201,
                    CurbsideLatitude  = 26.170834,
                    CurbsideLongitude = -98.116201
                },
                new Address {
                    AddressString     = "1430 W Amarillo Blvd, Amarillo, TX, 79107-5505",
                    IsDepot           = true,
                    Latitude          = 35.221969,
                    Longitude         = -101.835288,
                    CurbsideLatitude  = 35.221969,
                    CurbsideLongitude = -101.835288
                },
                new Address {
                    AddressString     = "3611 Ne 24Th Ave, Amarillo, TX, 79107-7242",
                    IsDepot           = true,
                    Latitude          = 35.236626,
                    Longitude         = -101.795117,
                    CurbsideLatitude  = 35.236626,
                    CurbsideLongitude = -101.795117
                },
                new Address {
                    AddressString     = "1525 New York Ave, Arlington, TX, 76010-4723",
                    IsDepot           = true,
                    Latitude          = 32.720524,
                    Longitude         = -97.080195,
                    CurbsideLatitude  = 32.720524,
                    CurbsideLongitude = -97.080195
                }
            };

            string errorString1;
            string errorString2;
            string errorString3;

            foreach (string ScheduledDay in lsScheduledDays)
            {
                HybridOptimizationParameters hparams = new HybridOptimizationParameters()
                {
                    target_date_string      = ScheduledDay,
                    timezone_offset_minutes = -240
                };

                DataObject resultOptimization = route4Me.GetOHybridptimization(hparams, out errorString1);

                string HybridOptimizationId = "";

                if (resultOptimization != null)
                {
                    HybridOptimizationId = resultOptimization.OptimizationProblemId;
                    Console.WriteLine("Hybrid optimization generating executed successfully");

                    Console.WriteLine("Generated hybrid optimization ID: {0}", HybridOptimizationId);
                }
                else
                {
                    Console.WriteLine("Hybrid optimization generating error: {0}", errorString1);
                    continue;
                }

                //============== Add Depot To Hybrid Optimization ===============
                HybridDepotParameters hDepotParams = new HybridDepotParameters()
                {
                    optimization_problem_id = HybridOptimizationId,
                    delete_old_depots       = true,
                    new_depots = new Address[] { Depots[lsScheduledDays.IndexOf(ScheduledDay)] }
                };

                var addDepotResult = route4Me.AddDepotsToHybridOptimization(hDepotParams, out errorString3);

                Thread.Sleep(5000);

                //============== Reoptimization =================================
                OptimizationParameters optimizationParameters = new OptimizationParameters()
                {
                    OptimizationProblemID = HybridOptimizationId,
                    ReOptimize            = true
                };

                DataObject finalOptimization = route4Me.UpdateOptimization(optimizationParameters, out errorString2);

                Console.WriteLine("");

                if (finalOptimization != null)
                {
                    Console.WriteLine("ReOptimization executed successfully");

                    Console.WriteLine("Optimization Problem ID: {0}", finalOptimization.OptimizationProblemId);
                    Console.WriteLine("State: {0}", finalOptimization.State);
                }
                else
                {
                    Console.WriteLine("ReOptimization error: {0}", errorString2);
                }

                Thread.Sleep(5000);
                //=================================================================
            }

            #endregion
        }
Ejemplo n.º 2
0
        public void HybridOptimizationFrom1000Orders()
        {
            // Create the manager with the api key
            Route4MeManager route4Me = new Route4MeManager("11111111111111111111111111111111");

            #region ======= Add scheduled address book locations to an user account ================================
            string sAddressFile = @"Data/orders_1000.csv";

            using (TextReader reader = File.OpenText(sAddressFile))
            {
                var csv = new CsvReader(reader);
                //int iCount = 0;
                while (csv.Read())
                {
                    var lng        = csv.GetField(0);
                    var lat        = csv.GetField(1);
                    var alias      = csv.GetField(2);
                    var address1   = csv.GetField(3);
                    var city       = csv.GetField(4);
                    var state      = csv.GetField(5);
                    var zip        = csv.GetField(6);
                    var phone      = csv.GetField(7);
                    var sched_date = csv.GetField(8);

                    string sAddress = "";

                    if (address1 != null)
                    {
                        sAddress += address1.ToString().Trim();
                    }
                    if (city != null)
                    {
                        sAddress += ", " + city.ToString().Trim();
                    }
                    if (state != null)
                    {
                        sAddress += ", " + state.ToString().Trim();
                    }
                    if (zip != null)
                    {
                        sAddress += ", " + zip.ToString().Trim();
                    }

                    if (sAddress == "")
                    {
                        continue;
                    }

                    Order newOrder = new Order();

                    if (lng != null)
                    {
                        newOrder.cached_lat = Convert.ToDouble(lng);
                    }
                    if (lat != null)
                    {
                        newOrder.cached_lng = Convert.ToDouble(lat);
                    }
                    if (alias != null)
                    {
                        newOrder.address_alias = alias.ToString().Trim();
                    }
                    newOrder.address_1 = sAddress;
                    if (phone != null)
                    {
                        newOrder.EXT_FIELD_phone = phone.ToString().Trim();
                    }

                    DateTime dt = DateTime.Now;

                    if (sched_date != null)
                    {
                        if (DateTime.TryParse(sched_date.ToString(), out dt))
                        {
                            dt = Convert.ToDateTime(sched_date);
                            newOrder.day_scheduled_for_YYMMDD = dt.ToString("yyyy-MM-dd");
                        }
                    }

                    string errorString;

                    Order resultOrder = route4Me.AddOrder(newOrder, out errorString);
                    showResult(resultOrder, errorString);

                    Thread.Sleep(1000);
                    //iCount++;
                    //if (iCount == 60) break;
                }
            };

            #endregion

            Thread.Sleep(2000);

            #region ======= Get Hybrid Optimization ================================
            TimeSpan      tsp1day         = new TimeSpan(1, 0, 0, 0);
            List <string> lsScheduledDays = new List <string>();
            DateTime      curDate         = DateTime.Now;
            for (int i = 0; i < 5; i++)
            {
                curDate += tsp1day;
                lsScheduledDays.Add(curDate.ToString("yyyy-MM-dd"));
            }

            Address[] Depots = new Address[] {
                new Address {
                    AddressString     = "2017 Ambler Ave, Abilene, TX, 79603-2239",
                    IsDepot           = true,
                    Latitude          = 32.474395,
                    Longitude         = -99.7447021,
                    CurbsideLatitude  = 32.474395,
                    CurbsideLongitude = -99.7447021
                },
                new Address {
                    AddressString     = "807 Ridge Rd, Alamo, TX, 78516-9596",
                    IsDepot           = true,
                    Latitude          = 26.170834,
                    Longitude         = -98.116201,
                    CurbsideLatitude  = 26.170834,
                    CurbsideLongitude = -98.116201
                },
                new Address {
                    AddressString     = "1430 W Amarillo Blvd, Amarillo, TX, 79107-5505",
                    IsDepot           = true,
                    Latitude          = 35.221969,
                    Longitude         = -101.835288,
                    CurbsideLatitude  = 35.221969,
                    CurbsideLongitude = -101.835288
                },
                new Address {
                    AddressString     = "3611 Ne 24Th Ave, Amarillo, TX, 79107-7242",
                    IsDepot           = true,
                    Latitude          = 35.236626,
                    Longitude         = -101.795117,
                    CurbsideLatitude  = 35.236626,
                    CurbsideLongitude = -101.795117
                },
                new Address {
                    AddressString     = "1525 New York Ave, Arlington, TX, 76010-4723",
                    IsDepot           = true,
                    Latitude          = 32.720524,
                    Longitude         = -97.080195,
                    CurbsideLatitude  = 32.720524,
                    CurbsideLongitude = -97.080195
                }
            };

            string errorString1;
            string errorString2;
            string errorString3;

            foreach (string ScheduledDay in lsScheduledDays)
            {
                HybridOptimizationParameters hparams = new HybridOptimizationParameters()
                {
                    target_date_string      = ScheduledDay,
                    timezone_offset_minutes = 480
                };

                DataObject resultOptimization = route4Me.GetOHybridptimization(hparams, out errorString1);

                string HybridOptimizationId = "";

                if (resultOptimization != null)
                {
                    HybridOptimizationId = resultOptimization.OptimizationProblemId;
                    Console.WriteLine("Hybrid optimization generating executed successfully");

                    Console.WriteLine("Generated hybrid optimization ID: {0}", HybridOptimizationId);
                }
                else
                {
                    Console.WriteLine("Hybrid optimization generating error: {0}", errorString1);
                    continue;
                }

                //============== Add Depot To Hybrid Optimization ===============
                HybridDepotParameters hDepotParams = new HybridDepotParameters()
                {
                    optimization_problem_id = HybridOptimizationId,
                    delete_old_depots       = true,
                    new_depots = new Address[] { Depots[lsScheduledDays.IndexOf(ScheduledDay)] }
                };

                var addDepotResult = route4Me.AddDepotsToHybridOptimization(hDepotParams, out errorString3);

                Thread.Sleep(5000);

                //============== Reoptimization =================================
                OptimizationParameters optimizationParameters = new OptimizationParameters()
                {
                    OptimizationProblemID = HybridOptimizationId,
                    ReOptimize            = true
                };

                DataObject finalOptimization = route4Me.UpdateOptimization(optimizationParameters, out errorString2);

                Console.WriteLine("");

                if (finalOptimization != null)
                {
                    Console.WriteLine("ReOptimization executed successfully");

                    Console.WriteLine("Optimization Problem ID: {0}", finalOptimization.OptimizationProblemId);
                    Console.WriteLine("State: {0}", finalOptimization.State);
                }
                else
                {
                    Console.WriteLine("ReOptimization error: {0}", errorString2);
                }

                Thread.Sleep(5000);
                //=================================================================
            }

            #endregion
        }