Example #1
0
        public void InitializeVehicleFlexibleRoute(Vehicle solutionVehicle, List <Stop> solutionVehicleStops, List <Customer> solutionVehicleCustomers, List <long[]> solutionTimeWindows)
        {
            if (!Context.VehicleFleet.Contains(solutionVehicle))
            {
                solutionVehicle.StartStop = Context.Depot;
                solutionVehicle.EndStop   = Context.Depot;
                Context.VehicleFleet.Add(solutionVehicle); //adds the vehicle to the vehicle fleet
            }

            //Adds the flexible trip to the solutionVehicle

            if (solutionVehicleStops.Count >= 2 && solutionVehicleStops[0] != solutionVehicleStops[1]) //if solutionRoute is a valid one
            {
                if (solutionVehicle.TripIterator?.Current == null)                                     //initializes vehicle trip and route, if the trip has not yet been initalized
                {
                    var trip = new Trip(20000 + solutionVehicle.Id, "Flexible trip " + solutionVehicle.Id);
                    trip.StartTime            = (int)solutionTimeWindows[0][0] + 1;     //start time, might need to change!
                    trip.Route                = Context.Routes.Find(r => r.Id == 1000); //flexible route Id
                    trip.Stops                = solutionVehicleStops;
                    trip.ExpectedCustomers    = solutionVehicleCustomers;
                    trip.ScheduledTimeWindows = solutionTimeWindows;
                    solutionVehicle.AddTrip(trip);              //adds the new flexible trip to the vehicle
                    if (solutionVehicle.ServiceTrips.Count > 0) //if the vehicle has services to be done
                    {
                        if (solutionVehicle.TripIterator.Current == null)
                        {
                            solutionVehicle.TripIterator.Reset();
                            solutionVehicle.TripIterator.MoveNext();         //initializes the serviceIterator
                        }

                        if (solutionVehicle.TripIterator.Current != null)
                        {
                            var arriveEvt =
                                EventGenerator.GenerateVehicleArriveEvent(solutionVehicle,
                                                                          trip.StartTime); //Generates the first arrive event for every vehicle that has a route assigned in the solution
                            Events.Add(arriveEvt);
                        }
                    }
                    Console.WriteLine("Vehicle " + solutionVehicle.Id + " route was successfully assigned!");
                }
            }
        }