private void CheckForPotentialPassengerTrips(ModeChoiceTripChainData driverTripChainData, ModeChoiceTripChainData passengerTripChainData,
                                                     int driverIndex, int passengerIndex, int passengerTripChainIndex, Random random)
        {
            var driverTripChainTrips = driverTripChainData.TripChain.Trips;

            for (int j = 0; j < passengerTripChainData.TripData.Length; j++)
            {
                float passengerEpsilon = float.NegativeInfinity;
                for (int i = 0; i < driverTripChainData.TripData.Length; i++)
                {
                    ITrip passengerTrip;
                    if (driverTripChainTrips[i].Mode == PassengerMode.AssociatedMode &&
                        PassengerMode.CalculateV(driverTripChainTrips[i], (passengerTrip = passengerTripChainData.TripChain.Trips[j]), out float v))
                    {
                        // only pop a random variable for the passenger when it is needed (performance)
                        if (passengerEpsilon <= float.NegativeInfinity)
                        {
                            passengerEpsilon = GenerateEpsilon(random);
                        }
                        // This option's U is the v of the tour plus the passenger's random and a new random for the driver
                        // make sure there is an improvement in the utility for the household
                        var deltaU = v + passengerEpsilon + GenerateEpsilon(random) - GetUtilityOfTrips(passengerTripChainData, j, driverTripChainData, i);
                        if (deltaU > 0)
                        {
                            PotentialTrips.Add(new PotentialPassengerTrip(driverTripChainTrips[i],
                                                                          passengerTrip, deltaU, driverIndex, passengerIndex, passengerTripChainIndex, j));
                        }
                    }
                }
            }
        }
 private void FindPotentialOnTourDriverForPassengerTrips(ModeChoiceTripChainData tripChainData, int passengerNumber, int passengerTripChainIndex, Random random)
 {
     // for each person, for each trip chain
     for (int i = 0; i < HouseholdData.PersonData.Length; i++)
     {
         // you can't passenger yourself
         if (passengerNumber != i)
         {
             var personData = HouseholdData.PersonData[i];
             var driverTripChainDataList = personData.TripChainData;
             for (int j = 0; j < driverTripChainDataList.Length; j++)
             {
                 // if it does not use a vehicle, then we can see if we can become a passenger
                 var driverTripChainData = driverTripChainDataList[j];
                 // ignore if they are on a joint trip and are not the representative
                 if (driverTripChainData.TripChain.JointTrip && !driverTripChainData.TripChain.JointTripRep)
                 {
                     continue;
                 }
                 // make sure that it requires a vehicle
                 var mode = driverTripChainData.TripChain.Trips[0].Mode;
                 if (mode.RequiresVehicle != null)
                 {
                     // if we are the representative for the trip (or no one is)
                     if (!driverTripChainData.TripChain.JointTrip || driverTripChainData.TripChain.JointTripRep)
                     {
                         CheckForPotentialPassengerTrips(driverTripChainData, tripChainData, j, passengerNumber, passengerTripChainIndex, random);
                     }
                 }
             }
         }
     }
 }
Exemple #3
0
 public ModeChoicePersonData(List<ITripChain> tripChains, int numberOfModes, int numberOfVehicleTypes)
 {
     var chains = TripChainData = new ModeChoiceTripChainData[tripChains.Count];
     for ( int i = 0; i < chains.Length; i++ )
     {
         chains[i] = new ModeChoiceTripChainData( tripChains[i], numberOfModes, numberOfVehicleTypes );
     }
 }
        public ModeChoicePersonData(List <ITripChain> tripChains, int numberOfModes, int numberOfVehicleTypes)
        {
            var chains = TripChainData = new ModeChoiceTripChainData[tripChains.Count];

            for (int i = 0; i < chains.Length; i++)
            {
                chains[i] = new ModeChoiceTripChainData(tripChains[i], numberOfModes, numberOfVehicleTypes);
            }
        }
        private float GetUtilityOfTrips(ModeChoiceTripChainData passengerTripChainData, int passengerTrip, ModeChoiceTripChainData driverTripChainData = default(ModeChoiceTripChainData), int driverTrip = -1)
        {
            float total = 0;

            if (driverTrip != -1 && driverTripChainData != null)
            {
                var indexOfDriverMode = IndexOf(driverTripChainData.TripChain.Trips[driverTrip].Mode, Modes);
                // add in the utility of the driver's trip
                total += driverTripChainData.TripData[driverTrip].V[indexOfDriverMode]
                         + driverTripChainData.TripData[driverTrip].Error[indexOfDriverMode];
            }
            var indexOfPassengersPreviousMode = IndexOf(passengerTripChainData.TripChain.Trips[passengerTrip].Mode, Modes);

            // add in the utility of the passenger's trip
            total += passengerTripChainData.TripData[passengerTrip].V[indexOfPassengersPreviousMode]
                     + passengerTripChainData.TripData[passengerTrip].Error[indexOfPassengersPreviousMode];
            return(total);
        }
        private void CheckForCarAtHome(Time StartTime, Time EndTime, HouseholdResourceAllocator resourceAllocator,
                                       ModeChoiceTripChainData PassengerTripChainData, int driverIndex, int passengerIndex, int passengerTripChainIndex, Random random,
                                       ITashaPerson driver)
        {
            int totalVehicles = Household.Vehicles.Length;

            if (totalVehicles == 0)
            {
                return;
            }
            int allDrivers = 0;
            PurePassengerTripChain DriverTripChain;

            for (int i = 0; i < Household.Persons.Length; i++)
            {
                if (Household.Persons[i].Licence)
                {
                    allDrivers++;
                }
            }

            if (totalVehicles >= allDrivers)
            {
                //then there is always a car at home (when a driver is home), so we can add a trip chain to the driver's trip.
                DriverTripChain = CreateDriverTripChain(StartTime, EndTime, Household.HomeZone, driver);
                CheckForPurePassengerTrips(DriverTripChain, PassengerTripChainData, driverIndex, passengerIndex, passengerTripChainIndex, random);
            }
            else
            {
                var TimeSlots = resourceAllocator.VehicleAvailability;
                for (int i = 0; i < TimeSlots.Count; i++)
                {
                    if (TimeSlots[i].AvailableCars > 0)
                    {
                        Time intersectionStart, intersectionEnd;
                        if (Time.Intersection(StartTime, EndTime, TimeSlots[i].TimeSpan.Start, TimeSlots[i].TimeSpan.End, out intersectionStart, out intersectionEnd))
                        {
                            DriverTripChain = CreateDriverTripChain(intersectionStart, intersectionEnd, Household.HomeZone, driver);
                            CheckForPurePassengerTrips(DriverTripChain, PassengerTripChainData, driverIndex, passengerIndex, passengerTripChainIndex, random);
                        }
                    }
                }
            }
        }
        private void FindPotentialAtHomeDriverForPassengerTrips(ModeChoiceTripChainData passengerTripChainData, int passengerNumber,
                                                                int passengerTripChainIndex, HouseholdResourceAllocator resourceAllocator, Random random)
        {
            Time startTime;
            Time endTime;

            for (int i = 0; i < HouseholdData.PersonData.Length; i++)
            {
                if (passengerNumber == i)
                {
                    // you can't passenger yourself
                    continue;
                }
                if (!Household.Persons[i].Licence)
                {
                    //The person at home must be a driver
                    continue;
                }
                var personData         = HouseholdData.PersonData[i];
                var numberOfTripChains = personData.TripChainData.Length;

                //Check if driver is home all day
                if (numberOfTripChains == 0)
                {
                    //check car availability and then assign trip chain
                    CheckForCarAtHome(Time.StartOfDay, Time.EndOfDay, resourceAllocator, passengerTripChainData, i, passengerNumber, passengerTripChainIndex, random, Household.Persons[i]);
                }
                else
                {
                    for (int j = 0; j < numberOfTripChains; j++)
                    {
                        var driverTripChainData = personData.TripChainData[j];

                        if (driverTripChainData.TripChain.JointTrip && !driverTripChainData.TripChain.JointTripRep)
                        {
                            continue;
                        }

                        //Check if it is the last trip chain of the driver's day.
                        if (j >= numberOfTripChains - 1)
                        {
                            // check car availability and then assign
                            startTime = driverTripChainData.TripChain.Trips[driverTripChainData.TripChain.Trips.Count - 1].ActivityStartTime;
                            CheckForCarAtHome(startTime, Time.EndOfDay, resourceAllocator, passengerTripChainData, i, passengerNumber, passengerTripChainIndex, random, Household.Persons[i]);
                        }
                        else
                        {
                            var currentTripChainTrips = personData.TripChainData[j].TripChain.Trips;
                            var nextTripChainTrips    = personData.TripChainData[j + 1].TripChain.Trips;

                            //First trip chain of the day
                            if (j == 0)
                            {
                                // that means that you can have them transport a passenger before they leave home.
                                endTime = currentTripChainTrips[0].TripStartTime;
                                CheckForCarAtHome(Time.StartOfDay, endTime, resourceAllocator, passengerTripChainData, i, passengerNumber, passengerTripChainIndex, random, Household.Persons[i]);
                            }

                            if (currentTripChainTrips[currentTripChainTrips.Count - 1].ActivityStartTime < nextTripChainTrips[0].TripStartTime)
                            {
                                //check car availability and then assign trip chain
                                startTime = currentTripChainTrips[currentTripChainTrips.Count - 1].ActivityStartTime;
                                endTime   = nextTripChainTrips[0].TripStartTime;
                                CheckForCarAtHome(startTime, endTime, resourceAllocator, passengerTripChainData, i, passengerNumber, passengerTripChainIndex, random, Household.Persons[i]);
                            }
                        }
                    }
                }
            }
        }
 private void CheckForPurePassengerTrips(PurePassengerTripChain driverTripChain, ModeChoiceTripChainData passengerTripChainData, int driverIndex, int passengerIndex, int passengerTripChainIndex, Random random)
 {
     for (int j = 0; j < passengerTripChainData.TripData.Length; j++)
     {
         float passengerEpsilon = float.NegativeInfinity;
         if (passengerTripChainData.TripChain.Trips[j].Mode.RequiresVehicle != null)
         {
             continue;
         }
         if (PassengerMode.CalculateV(driverTripChain.Trips[0], passengerTripChainData.TripChain.Trips[j], out float v))
         {
             if (passengerEpsilon <= float.NegativeInfinity)
             {
                 passengerEpsilon = GenerateEpsilon(random);
             }
             var deltaU = v + passengerEpsilon + GenerateEpsilon(random) - GetUtilityOfTrips(passengerTripChainData, j);
             if (deltaU > 0)
             {
                 PotentialTrips.Add(new PotentialPassengerTrip(driverTripChain.Trips[0],
                                                               passengerTripChainData.TripChain.Trips[j], deltaU, driverIndex, passengerIndex, passengerTripChainIndex, j));
             }
         }
     }
 }