internal int[][] Resolve(ModeChoiceHouseholdData householdData, IVehicleType[] vehicleTypes, int hhldIterations)
        {
            // check if there are no vehicles
            var vehicles = household.Vehicles;

            if (vehicles.Length == 0)
            {
                return(Resolution);
            }
            // if we actually have to compute something go and do it
            if (hhldIterations > 0)
            {
                ClearData();
            }
            // check to see if we have enough resources for everyone
            if (vehicleTypes.Length == 1)
            {
                if (!ProcessSingleVehicleType(householdData, vehicleTypes, vehicles))
                {
                    return(null);
                }
            }
            else
            {
                throw new NotImplementedException("We will get to more than one vehicle type later!");
            }
            return(Resolution);
        }
 private void AssignBestToAll(ModeChoiceHouseholdData householdData)
 {
     for (int i = 0; i < householdData.PersonData.Length; i++)
     {
         var person = householdData.PersonData[i];
         for (int j = 0; j < person.TripChainData.Length; j++)
         {
             var tc    = person.TripChainData[j];
             var bestU = float.NegativeInfinity;
             var index = -1;
             for (int k = 0; k < tc.BestPossibleAssignmentForVehicleType.Length; k++)
             {
                 var assignment = tc.BestPossibleAssignmentForVehicleType[k];
                 if (assignment != null)
                 {
                     if (assignment.U >= bestU)
                     {
                         index = k;
                         bestU = assignment.U;
                     }
                 }
             }
             Resolution[i][j] = index;
         }
     }
 }
 private void AssignBest(ModeChoiceHouseholdData householdData, ITripChain[][] personConflicts, bool[][] bestAssignment)
 {
     for (int i = 0; i < householdData.PersonData.Length; i++)
     {
         var person = householdData.PersonData[i];
         for (int j = 0; j < person.TripChainData.Length; j++)
         {
             var tc    = person.TripChainData[j];
             var bestU = float.NegativeInfinity;
             int index;
             if ((index = IndexOf(tc.TripChain, personConflicts[i])) != -1)
             {
                 index = bestAssignment[i][index] ? 1 : 0;
             }
             else
             {
                 index = -1;
                 for (int k = 0; k < tc.BestPossibleAssignmentForVehicleType.Length; k++)
                 {
                     var assignment = tc.BestPossibleAssignmentForVehicleType[k];
                     if (assignment != null)
                     {
                         if (assignment.U >= bestU)
                         {
                             index = k;
                             bestU = assignment.U;
                         }
                     }
                 }
             }
             Resolution[i][j] = index;
         }
     }
 }
Beispiel #4
0
        private void ProcessRideshare(ModeChoiceHouseholdData householdData)
        {
            if (Rideshare == null)
            {
                return;
            }
            var numberOfPeople = householdData.PersonData.Length;
            var autoIndex      = Root.AllModes.IndexOf(AutoMode);
            var rideshareIndex = Root.AllModes.IndexOf(Rideshare);

            for (int i = 0; i < numberOfPeople; i++)
            {
                var tripChainData      = householdData.PersonData[i].TripChainData;
                var numberOfTripChains = tripChainData.Length;
                for (int j = 0; j < numberOfTripChains; j++)
                {
                    if (tripChainData[j].TripChain.JointTripRep)
                    {
                        var trips         = tripChainData[j].TripChain.Trips;
                        var numberOfTrips = trips.Count;
                        for (int k = 0; k < numberOfTrips; k++)
                        {
                            if (trips[k].Mode == AutoMode)
                            {
                                trips[k].Mode = Rideshare;
                                householdData.PersonData[i].TripChainData[j].TripData[k].V[rideshareIndex]
                                    = householdData.PersonData[i].TripChainData[j].TripData[k].V[autoIndex];
                                householdData.PersonData[i].TripChainData[j].TripData[k].Error[rideshareIndex]
                                    = householdData.PersonData[i].TripChainData[j].TripData[k].Error[autoIndex];
                            }
                        }
                    }
                }
            }
        }
        private bool ProcessSingleVehicleType(ModeChoiceHouseholdData householdData, IVehicleType[] vehicleTypes, IVehicle[] vehicles)
        {
            // check to see if there are as many vehicles as number of people
            int activeVehicles = vehicles.Length;
            var numberOfPeople = Resolution.Length;

            // See if we have at least the same number of the vehicle type than people that can use it
            if (activeVehicles >= numberOfPeople || CheckPossibleUsers(householdData, vehicleTypes, vehicles))
            {
                // Assign to anyone who wants one
                AssignBestToAll(householdData);
                return(true);
            }

            //Check users at time of day
            if (CheckPossibleUsersAtTimeOfDay(householdData, vehicleTypes, vehicles))
            {
                AssignBestToAll(householdData);
                return(true);
            }

            if (ProcessHardSingleVehicleCase(householdData, vehicleTypes, vehicles))
            {
                return(true);
            }
            return(false);
            //throw new XTMFRuntimeException( "Failed to assign vehicles for household #" + this.household.HouseholdId + "!" );
        }
        private bool RecursiveExplore(ModeChoiceHouseholdData householdData, int numberOfResource, bool[][] assignResource, bool[][] bestAssignment, ITripChain[][] personConflicts, int person, int tIndex)
        {
            var any = false;

            if (tIndex < assignResource[person].Length)
            {
                assignResource[person][tIndex] = false;
                if (RecursiveExplore(householdData, numberOfResource, assignResource, bestAssignment, personConflicts, person, tIndex + 1))
                {
                    any = true;
                }
                assignResource[person][tIndex] = true;
                if (RecursiveExplore(householdData, numberOfResource, assignResource, bestAssignment, personConflicts, person, tIndex + 1))
                {
                    any = true;
                }
                return(any);
            }
            if (assignResource.Length == person + 1)
            {
                if (IsValidAssignment(numberOfResource, assignResource, personConflicts, householdData, out float utility))
                {
                    if (utility > BestConflictUtility)
                    {
                        BestConflictUtility = utility;
                        AssignToBest(assignResource, bestAssignment);
                    }
                    return(true);
                }
                return(false);
            }
            return(RecursiveExplore(householdData, numberOfResource, assignResource, bestAssignment, personConflicts, person + 1, 0));
        }
 public PassengerMatchingAlgorithm(ITashaHousehold household, ModeChoiceHouseholdData householdData, ITashaPassenger passengerMode, ITashaMode[] modes)
 {
     Household        = household;
     HouseholdData    = householdData;
     PassengerMode    = passengerMode;
     Modes            = modes;
     PotentialTrips   = new List <PotentialPassengerTrip>();
     Conflicts        = new List <Conflict>();
     IndexOfPassenger = IndexOf(passengerMode, modes);
 }
Beispiel #8
0
 private void FinalAssignment(ModeChoiceHouseholdData householdData, int householdIteration)
 {
     ModeChoicePersonData[] personData = householdData.PersonData;
     for (int i = 0; i < personData.Length; i++)
     {
         var tripChainData = personData[i].TripChainData;
         for (int j = 0; j < tripChainData.Length; j++)
         {
             tripChainData[j].FinalAssignment(householdIteration);
         }
     }
 }
        private bool CheckPossibleUsers(ModeChoiceHouseholdData householdData, IVehicleType[] vehicleTypes, IVehicle[] vehicles)
        {
            int numberOfUsers = 0;
            var typeZero      = vehicleTypes[0];

            for (int i = 0; i < Resolution.Length; i++)
            {
                if (typeZero.CanUse(household.Persons[i]))
                {
                    numberOfUsers++;
                }
            }
            return(vehicles.Length >= numberOfUsers);
        }
Beispiel #10
0
 private void RegenerateErrorTerms(ModeChoiceHouseholdData householdData, Random random)
 {
     for (int i = 0; i < householdData.PersonData.Length; i++)
     {
         var person = householdData.PersonData[i];
         for (int j = 0; j < person.TripChainData.Length; j++)
         {
             var tripChain = person.TripChainData[j];
             if (!(tripChain.TripChain.JointTrip && !tripChain.TripChain.JointTripRep))
             {
                 // now we can compute the random terms
                 tripChain.GenerateRandomTerms(random, VarianceScale);
             }
         }
     }
 }
Beispiel #11
0
        private void AssignBestPerVehicle(List <IVehicleType> list, ModeChoiceHouseholdData householdData)
        {
            var modes = Root.NonSharedModes;

            // Go through all of the possible assignments and get the best one per vehicle
            for (int i = 0; i < householdData.PersonData.Length; i++)
            {
                var person = householdData.PersonData[i];
                for (int j = 0; j < person.TripChainData.Length; j++)
                {
                    var tripChain = person.TripChainData[j];
                    if (!(tripChain.TripChain.JointTrip && !tripChain.TripChain.JointTripRep))
                    {
                        tripChain.SelectBestPerVehicleType(modes, list);
                    }
                }
            }
        }
        private bool ProcessHardSingleVehicleCase(ModeChoiceHouseholdData householdData, IVehicle[] vehicles)
        {
            var persons        = household.Persons;
            var numberOfPeople = persons.Length;

            ITripChain[][] personConflicts = new ITripChain[numberOfPeople][];
            bool[][]       assignResource  = new bool[numberOfPeople][];
            bool[][]       bestAssignment  = new bool[numberOfPeople][];
            // Initialize our data with the unique trip chains in conflict
            InitializeProcessHardSingleVehicleCaseData(personConflicts, assignResource, bestAssignment);
            BestConflictUtility = float.NegativeInfinity;
            if (RecursiveExplore(householdData, vehicles.Length, assignResource, bestAssignment, personConflicts, 0, 0))
            {
                AssignBest(householdData, personConflicts, bestAssignment);
                return(true);
            }
            return(false);
        }
Beispiel #13
0
        private void AssignModes(int[][] resolution, ModeChoiceHouseholdData householdData)
        {
            var modes          = Root.NonSharedModes;
            var numberOfPeople = resolution.Length;

            for (int i = 0; i < numberOfPeople; i++)
            {
                var tripChainData      = householdData.PersonData[i].TripChainData;
                var numberOfTripChains = tripChainData.Length;
                for (int j = 0; j < numberOfTripChains; j++)
                {
                    if (!(tripChainData[j].TripChain.JointTrip && !tripChainData[j].TripChain.JointTripRep))
                    {
                        tripChainData[j].Assign(resolution[i][j], modes);
                    }
                }
            }
        }
        private bool ComputeUtilityOfAssignment(ModeChoiceHouseholdData householdData, bool[][] assignment, ITripChain[][] conflictTripChains, out float utility)
        {
            var sum = 0f;

            for (int i = 0; i < assignment.Length; i++)
            {
                var row = assignment[i];
                for (int j = 0; j < row.Length; j++)
                {
                    var u = householdData.PersonData[i].TripChainData[IndexOf(conflictTripChains[i][j], household.Persons[i].TripChains)]
                            .BestPossibleAssignmentForVehicleType[row[j] ? 1 : 0];
                    if (u == null)
                    {
                        utility = float.NegativeInfinity;
                        return(false);
                    }
                    sum += u.U;
                }
            }
            utility = sum;
            return(true);
        }
Beispiel #15
0
        private bool Pass1(ModeChoiceHouseholdData householdData, Random random)
        {
            var nonSharedModes = NonSharedModes;

            for (int i = 0; i < householdData.PersonData.Length; i++)
            {
                var person = householdData.PersonData[i];
                for (int j = 0; j < person.TripChainData.Length; j++)
                {
                    var tripChain = person.TripChainData[j];
                    if (!(tripChain.TripChain.JointTrip && !tripChain.TripChain.JointTripRep))
                    {
                        if (!tripChain.Pass1(nonSharedModes))
                        {
                            return(false);
                        }
                        // now we can compute the random terms
                        tripChain.GenerateRandomTerms(random, VarianceScale);
                    }
                }
            }
            return(true);
        }
Beispiel #16
0
 private void RegenerateErrorTerms(ModeChoiceHouseholdData householdData, Random random)
 {
     var allModes = AllModes;
     for ( int i = 0; i < householdData.PersonData.Length; i++ )
     {
         var person = householdData.PersonData[i];
         for ( int j = 0; j < person.TripChainData.Length; j++ )
         {
             var tripChain = person.TripChainData[j];
             if ( !( tripChain.TripChain.JointTrip && !tripChain.TripChain.JointTripRep ) )
             {
                 // now we can compute the random terms
                 tripChain.GenerateRandomTerms( random, VarianceScale );
             }
         }
     }
 }
        private bool IsValidAssignment(int numberOfResource, bool[][] assignment, ITripChain[][] conflictTripChains, ModeChoiceHouseholdData householdData, out float utility)
        {
            var persons           = household.Persons;
            var conflicts         = Conflicts;
            var numberOfConflicts = conflicts.Count;

            utility = float.NegativeInfinity;
            for (int i = 0; i < numberOfConflicts; i++)
            {
                var chainsInConflict         = conflicts[i].TripChains;
                var numberOfChainsInConflict = chainsInConflict.Count;
                var assignedCount            = 0;
                for (int j = 0; j < numberOfChainsInConflict; j++)
                {
                    var personIndex = IndexOf(chainsInConflict[j].Person, persons);
                    var chainIndex  = IndexOf(chainsInConflict[j], conflictTripChains[personIndex]);
                    if (assignment[personIndex][chainIndex])
                    {
                        assignedCount++;
                    }
                }
                if (assignedCount > numberOfResource)
                {
                    return(false);
                }
            }
            // Since this is a valid assignment compute the utility of the assignment
            return(ComputeUtilityOfAssignment(householdData, assignment, conflictTripChains, out utility));
        }
        private bool CheckPossibleUsersAtTimeOfDay(ModeChoiceHouseholdData householdData, IVehicleType[] vehicleTypes, IVehicle[] vehicles)
        {
            if (Conflicts == null)
            {
                Conflicts = new List <Conflict>();
            }
            else
            {
                Conflicts.Clear();
            }
            if (Scan == null)
            {
                Scan = new CurrentPosition[Resolution.Length];
            }
            for (int i = 0; i < Scan.Length; i++)
            {
                Scan[i] = new CurrentPosition()
                {
                    Position = 0, TripChains = household.Persons[i].TripChains, HasBeenProcessed = false
                };
            }
            List <ITripChain> activeTours = new List <ITripChain>(vehicles.Length);
            var endOfDay = new Time()
            {
                Hours = 28
            };

            while (true)
            {
                int  nextPerson = -1;
                Time nextTime   = endOfDay;
                for (int i = 0; i < Scan.Length; i++)
                {
                    if (Scan[i].Position < Scan[i].TripChains.Count)
                    {
                        var personNextStartTime = Scan[i].TripChains[Scan[i].Position].StartTime;
                        if (personNextStartTime < nextTime)
                        {
                            nextTime   = personNextStartTime;
                            nextPerson = i;
                        }
                    }
                }
                if (nextPerson == -1)
                {
                    break;
                }
                for (int i = 0; i < activeTours.Count; i++)
                {
                    if (activeTours[i].EndTime <= nextTime)
                    {
                        activeTours.RemoveAt(i);
                        i--;
                    }
                }
                var nextTourData = householdData.PersonData[nextPerson].TripChainData[Scan[nextPerson].Position];
                // if it isn't a joint trip not made by the representative
                if (!(nextTourData.TripChain.JointTrip && !nextTourData.TripChain.JointTripRep))
                {
                    // and they want a car
                    if (nextTourData.BestPossibleAssignmentForVehicleType[1] != null &&
                        nextTourData.BestPossibleAssignmentForVehicleType[1].U >= (nextTourData.BestPossibleAssignmentForVehicleType[0] == null ? float.NegativeInfinity : nextTourData.BestPossibleAssignmentForVehicleType[0].U))
                    {
                        var tc = Scan[nextPerson].TripChains[Scan[nextPerson].Position];
                        for (int j = 0; j < tc.Trips.Count; j++)
                        {
                            tc.Trips[j].Mode = Modes[nextTourData.BestPossibleAssignmentForVehicleType[1].PickedModes[j]];
                        }
                        activeTours.Add(tc);
                        // There could be a conflict for this vehicle's allocation
                        if (activeTours.Count > vehicles.Length)
                        {
                            Conflicts.Add(new Conflict()
                            {
                                TripChains = Clone(activeTours)
                            });
                        }
                    }
                }
                Scan[nextPerson].Position++;
            }
            return(Conflicts.Count == 0);
        }
Beispiel #19
0
        public bool Run(ITashaHousehold household)
        {
            if (MaxTripChainSize > 0)
            {
                if (AnyOverMaxTripChainSize(household))
                {
                    return(false);
                }
            }
            Random random = new Random(RandomSeed + household.HouseholdId);
            ModeChoiceHouseholdData    householdData = new ModeChoiceHouseholdData(household, AllModes.Length, VehicleTypes.Length + 1);
            HouseholdResourceAllocator householdResourceAllocator = new HouseholdResourceAllocator(household, AllModes);
            PassengerMatchingAlgorithm passengerMatchingAlgorithm = null;

            // attach this so analysis modules can look at it later
            household.Attach("ModeChoiceData", householdData);
            household.Attach("ResourceAllocator", householdResourceAllocator);
            if (PassengerMode != null)
            {
                passengerMatchingAlgorithm = new PassengerMatchingAlgorithm(household, householdData, PassengerMode, AllModes);
                household.Attach("PassengerMatchingAlgorithm", passengerMatchingAlgorithm);
            }

            for (int i = 0; i < PostHouseholdIteration.Length; i++)
            {
                PostHouseholdIteration[i].HouseholdStart(household, HouseholdIterations);
            }
            if (!Pass1(householdData, random))
            {
                for (int i = 0; i < PostHouseholdIteration.Length; i++)
                {
                    PostHouseholdIteration[i].HouseholdComplete(household, false);
                }
                return(false);
            }
            for (int householdIteration = 0; householdIteration < HouseholdIterations; householdIteration++)
            {
                if (householdIteration > 0)
                {
                    RegenerateErrorTerms(householdData, random);
                }
                // Start of Pass 2
                AssignBestPerVehicle(Root.VehicleTypes, householdData);
                var resolution = householdResourceAllocator.Resolve(householdData, VehicleTypes, householdIteration);
                if (resolution == null)
                {
                    for (int i = 0; i < PostHouseholdIteration.Length; i++)
                    {
                        PostHouseholdIteration[i].HouseholdComplete(household, false);
                    }
                    // failure
                    return(false);
                }
                AssignModes(resolution, householdData);
                householdResourceAllocator.BuildVehicleAvailabilities(householdData, household.Vehicles);
                // Start of Pass 2.5 ( rideshare )
                ProcessRideshare(householdData);

                // Start of Pass 3 (Passenger attaching to trip chains)
                if (passengerMatchingAlgorithm != null)
                {
                    passengerMatchingAlgorithm.GeneratePotentialPassengerTrips(random);
                    passengerMatchingAlgorithm.ResolvePassengerTrips();
                    // Start of Pass 4 (Passenger attaching to new trips coming from home)
                    passengerMatchingAlgorithm.GeneratePotentialPassengerTrips(random, false, householdResourceAllocator);
                    passengerMatchingAlgorithm.ResolvePassengerTrips();
                }
                // Now at the end add to chosen modes (And assign joint trips)
                FinalAssignment(householdData, householdIteration);
                for (int i = 0; i < PostHouseholdIteration.Length; i++)
                {
                    PostHouseholdIteration[i].HouseholdIterationComplete(household, householdIteration, HouseholdIterations);
                }
            }
            for (int i = 0; i < PostHouseholdIteration.Length; i++)
            {
                PostHouseholdIteration[i].HouseholdComplete(household, true);
            }
            return(true);
        }
Beispiel #20
0
        public bool Run(ITashaHousehold household)
        {
            if(MaxTripChainSize > 0)
            {
                if(AnyOverMaxTripChainSize(household))
                {
                    return false;
                }
            }
            Random random = new Random( RandomSeed + household.HouseholdId );
            ModeChoiceHouseholdData householdData = new ModeChoiceHouseholdData( household, AllModes.Length, VehicleTypes.Length + 1 );
            HouseholdResourceAllocator householdResourceAllocator = new HouseholdResourceAllocator( household, AllModes );
            PassengerMatchingAlgorithm passengerMatchingAlgorithm = null;
            // attach this so analysis modules can look at it later
            household.Attach( "ModeChoiceData", householdData );
            household.Attach( "ResourceAllocator", householdResourceAllocator );
            if ( PassengerMode != null )
            {
                passengerMatchingAlgorithm = new PassengerMatchingAlgorithm( household, householdData, PassengerMode, AllModes );
                household.Attach( "PassengerMatchingAlgorithm", passengerMatchingAlgorithm );
            }

            for ( int i = 0; i < PostHouseholdIteration.Length; i++ )
            {
                PostHouseholdIteration[i].HouseholdStart( household, HouseholdIterations );
            }
            if ( !Pass1( householdData, random ) )
            {
                for ( int i = 0; i < PostHouseholdIteration.Length; i++ )
                {
                    PostHouseholdIteration[i].HouseholdComplete( household, false );
                }
                return false;
            }
            for ( int householdIteration = 0; householdIteration < HouseholdIterations; householdIteration++ )
            {
                if ( householdIteration > 0 )
                {
                    RegenerateErrorTerms( householdData, random );
                }
                // Start of Pass 2
                AssignBestPerVehicle( Root.VehicleTypes, householdData, householdIteration );
                var resolution = householdResourceAllocator.Resolve( householdData, VehicleTypes, householdIteration );
                if ( resolution == null )
                {
                    for ( int i = 0; i < PostHouseholdIteration.Length; i++ )
                    {
                        PostHouseholdIteration[i].HouseholdComplete( household, false );
                    }
                    // failure
                    return false;
                }
                AssignModes( resolution, householdData );
                householdResourceAllocator.BuildVehicleAvailabilities( householdData, household.Vehicles );
                // Start of Pass 2.5 ( rideshare )
                ProcessRideshare( householdData );

                // Start of Pass 3 (Passenger attaching to trip chains)
                if ( passengerMatchingAlgorithm != null )
                {
                    passengerMatchingAlgorithm.GeneratePotentialPassengerTrips( random, true );
                    passengerMatchingAlgorithm.ResolvePassengerTrips();
                    // Start of Pass 4 (Passenger attaching to new trips coming from home)
                    passengerMatchingAlgorithm.GeneratePotentialPassengerTrips( random, false, householdResourceAllocator );
                    passengerMatchingAlgorithm.ResolvePassengerTrips();
                }
                // Now at the end add to chosen modes (And assign joint trips)
                FinalAssignment( householdData, householdIteration );
                for ( int i = 0; i < PostHouseholdIteration.Length; i++ )
                {
                    PostHouseholdIteration[i].HouseholdIterationComplete( household, householdIteration, HouseholdIterations );
                }
            }
            for ( int i = 0; i < PostHouseholdIteration.Length; i++ )
            {
                PostHouseholdIteration[i].HouseholdComplete( household, true );
            }
            return true;
        }
Beispiel #21
0
 private void AssignBestPerVehicle(List<IVehicleType> list, ModeChoiceHouseholdData householdData, int householdIteration)
 {
     var modes = Root.NonSharedModes;
     // Go through all of the possible assignments and get the best one per vehicle
     for ( int i = 0; i < householdData.PersonData.Length; i++ )
     {
         var person = householdData.PersonData[i];
         for ( int j = 0; j < person.TripChainData.Length; j++ )
         {
             var tripChain = person.TripChainData[j];
             if ( !( tripChain.TripChain.JointTrip && !tripChain.TripChain.JointTripRep ) )
             {
                 tripChain.SelectBestPerVehicleType( modes, list );
             }
         }
     }
 }
Beispiel #22
0
 private void AssignModes(int[][] resolution, ModeChoiceHouseholdData householdData)
 {
     var modes = Root.NonSharedModes;
     var numberOfPeople = resolution.Length;
     for ( int i = 0; i < numberOfPeople; i++ )
     {
         var tripChainData = householdData.PersonData[i].TripChainData;
         var numberOfTripChains = tripChainData.Length;
         for ( int j = 0; j < numberOfTripChains; j++ )
         {
             if ( !( tripChainData[j].TripChain.JointTrip && !tripChainData[j].TripChain.JointTripRep ) )
             {
                 tripChainData[j].Assign( resolution[i][j], modes );
             }
         }
     }
 }
        public void BuildVehicleAvailabilities(ModeChoiceHouseholdData householdData, IVehicle[] vehicles)
        {
            int numberOfVehicles = vehicles.Length;
            var numberOfPeople   = Resolution.Length;

            if (numberOfVehicles == 0)
            {
                return;
            }
            VehicleAvailability = new List <VehicleAllocationWindow>();
            if (Scan == null)
            {
                Scan = new CurrentPosition[numberOfPeople];
            }
            for (int i = 0; i < numberOfPeople; i++)
            {
                Scan[i] = new CurrentPosition()
                {
                    Position = 0, TripChains = household.Persons[i].TripChains, HasBeenProcessed = false
                };
            }
            List <ITripChain> activeTours = new List <ITripChain>(numberOfVehicles);
            var endOfDay = new Time()
            {
                Hours = 28
            };
            Time previousAllocationTime = Time.StartOfDay;

            while (true)
            {
                int  nextPerson = -1;
                Time nextTime   = endOfDay;
                for (int i = 0; i < numberOfPeople; i++)
                {
                    if (Scan[i].Position < Scan[i].TripChains.Count)
                    {
                        var personNextStartTime = Scan[i].TripChains[Scan[i].Position].StartTime;
                        if (personNextStartTime < nextTime)
                        {
                            nextTime   = personNextStartTime;
                            nextPerson = i;
                        }
                    }
                }
                if (nextPerson == -1)
                {
                    break;
                }

                while (true)
                {
                    Time earliestEnd   = nextTime;
                    int  earliestIndex = -1;
                    var  length        = activeTours.Count;
                    for (int i = 0; i < length; i++)
                    {
                        if (activeTours[i].EndTime <= earliestEnd)
                        {
                            earliestIndex = i;
                            earliestEnd   = activeTours[i].EndTime;
                        }
                    }
                    if (earliestIndex >= 0)
                    {
                        VehicleAvailability.Add(new VehicleAllocationWindow()
                        {
                            TimeSpan      = new TashaTimeSpan(previousAllocationTime, earliestEnd),
                            AvailableCars = numberOfVehicles - activeTours.Count
                        });
                        previousAllocationTime = earliestEnd;
                        activeTours.RemoveAt(earliestIndex);
                        continue;
                    }
                    break;
                }

                var nextTourData = householdData.PersonData[nextPerson].TripChainData[Scan[nextPerson].Position];
                // if it isn't a joint trip not made by the representative
                if (!(nextTourData.TripChain.JointTrip && !nextTourData.TripChain.JointTripRep))
                {
                    if (Resolution[nextPerson][Scan[nextPerson].Position] > 0)
                    {
                        var endTime = nextTourData.TripChain.StartTime;
                        VehicleAvailability.Add(new VehicleAllocationWindow()
                        {
                            TimeSpan      = new TashaTimeSpan(previousAllocationTime, endTime),
                            AvailableCars = numberOfVehicles - activeTours.Count
                        });
                        previousAllocationTime = endTime;
                        activeTours.Add(nextTourData.TripChain);
                    }
                }
                Scan[nextPerson].Position++;
                // now check to see if the next trip wants to be
                // at the end of this step fail if there are more active users than vehicles
            }
            while (true)
            {
                Time earliestEnd   = Time.EndOfDay;
                int  earliestIndex = -1;
                var  length        = activeTours.Count;
                for (int i = 0; i < length; i++)
                {
                    if (activeTours[i].EndTime <= earliestEnd)
                    {
                        earliestIndex = i;
                        earliestEnd   = activeTours[i].EndTime;
                    }
                }
                if (earliestIndex >= 0)
                {
                    VehicleAvailability.Add(new VehicleAllocationWindow()
                    {
                        TimeSpan      = new TashaTimeSpan(previousAllocationTime, earliestEnd),
                        AvailableCars = numberOfVehicles - activeTours.Count
                    });
                    previousAllocationTime = earliestEnd;
                    activeTours.RemoveAt(earliestIndex);
                    continue;
                }
                break;
            }
            VehicleAvailability.Add(new VehicleAllocationWindow()
            {
                TimeSpan      = new TashaTimeSpan(previousAllocationTime, Time.EndOfDay),
                AvailableCars = numberOfVehicles // activeTours.Count is 0
            });
        }
Beispiel #24
0
 private void FinalAssignment(ModeChoiceHouseholdData householdData, int householdIteration)
 {
     ModeChoicePersonData[] personData = householdData.PersonData;
     for ( int i = 0; i < personData.Length; i++ )
     {
         var tripChainData = personData[i].TripChainData;
         for ( int j = 0; j < tripChainData.Length; j++ )
         {
             tripChainData[j].FinalAssignment( householdIteration );
         }
     }
 }
Beispiel #25
0
 private void ProcessRideshare(ModeChoiceHouseholdData householdData)
 {
     if ( Rideshare == null )
     {
         return;
     }
     var numberOfPeople = householdData.PersonData.Length;
     var autoIndex = Root.AllModes.IndexOf( AutoMode );
     var rideshareIndex = Root.AllModes.IndexOf( Rideshare );
     for ( int i = 0; i < numberOfPeople; i++ )
     {
         var tripChainData = householdData.PersonData[i].TripChainData;
         var numberOfTripChains = tripChainData.Length;
         for ( int j = 0; j < numberOfTripChains; j++ )
         {
             if ( tripChainData[j].TripChain.JointTripRep )
             {
                 var trips = tripChainData[j].TripChain.Trips;
                 var numberOfTrips = trips.Count;
                 for ( int k = 0; k < numberOfTrips; k++ )
                 {
                     if ( trips[k].Mode == AutoMode )
                     {
                         trips[k].Mode = Rideshare;
                         householdData.PersonData[i].TripChainData[j].TripData[k].V[rideshareIndex]
                             = householdData.PersonData[i].TripChainData[j].TripData[k].V[autoIndex];
                         householdData.PersonData[i].TripChainData[j].TripData[k].Error[rideshareIndex]
                             = householdData.PersonData[i].TripChainData[j].TripData[k].Error[autoIndex];
                     }
                 }
             }
         }
     }
 }
Beispiel #26
0
        private bool Pass1(ModeChoiceHouseholdData householdData, Random random)
        {
            var allModes = AllModes;
            var nonSharedModes = NonSharedModes;

            for ( int i = 0; i < householdData.PersonData.Length; i++ )
            {
                var person = householdData.PersonData[i];
                for ( int j = 0; j < person.TripChainData.Length; j++ )
                {
                    var tripChain = person.TripChainData[j];
                    if ( !( tripChain.TripChain.JointTrip && !tripChain.TripChain.JointTripRep ) )
                    {
                        if ( !tripChain.Pass1( nonSharedModes ) )
                        {
                            return false;
                        }
                        // now we can compute the random terms
                        tripChain.GenerateRandomTerms( random, VarianceScale);
                    }
                }
            }
            return true;
        }