private object ReceiveHouseholds(Stream fromHost)
        {
            var reader             = new BinaryReader(fromHost);
            int numberOfHouseholds = reader.ReadInt32();
            int numberOfVehicles   = reader.ReadInt32();

            if (numberOfVehicles != VehicleTypes.Count)
            {
                throw new XTMFRuntimeException("We were expecting to have '" + VehicleTypes.Count + "' different types of vehicles but the host has '" + numberOfVehicles + "'");
            }
            for (int i = 0; i < numberOfVehicles; i++)
            {
                string temp;
                if (VehicleTypes[i].VehicleName != (temp = reader.ReadString()))
                {
                    throw new XTMFRuntimeException("We were expecting the vehicle type to be named '" + VehicleTypes[i].VehicleName + "' and instead found '" + temp + "'");
                }
            }
            TashaHousehold[] households = new TashaHousehold[numberOfHouseholds];
            var autoType  = AutoType;
            var zoneArray = ZoneSystem.ZoneArray;

            for (int i = 0; i < numberOfHouseholds; i++)
            {
                households[i] = LoadHousehold(reader, zoneArray);
            }
            reader = null;
            return(households);
        }
        private TashaHousehold LoadHousehold(BinaryReader reader, Datastructure.SparseArray <IZone> zoneArray)
        {
            var household = new TashaHousehold();
            int numberOfPeople;

            household.HouseholdId = reader.ReadInt32();
            // Learn how many people this household has and their number of vehicles
            household.Persons = new ITashaPerson[(numberOfPeople = reader.ReadInt32())];
            var vehicleList = new List <IVehicle>();

            // Produce the vehicles, all auto since it is the only type of resource we have
            for (int i = 0; i < VehicleTypes.Count; i++)
            {
                var numberOfVehicles = reader.ReadInt32();
                for (int j = 0; j < numberOfVehicles; j++)
                {
                    vehicleList.Add(TashaVehicle.MakeVehicle(VehicleTypes[i]));
                }
            }
            household.Vehicles = vehicleList.ToArray();
            household.HomeZone = zoneArray[reader.ReadInt32()];
            LoadKeys(reader, household);
            // now we can go and load the people
            for (int i = 0; i < numberOfPeople; i++)
            {
                household.Persons[i] = LoadPerson(reader, zoneArray, household, i);
            }
            // Link in the joint trip chain trip chains
            foreach (var person in household.Persons)
            {
                foreach (var tc in person.TripChains)
                {
                    if (tc.JointTrip)
                    {
                        if (tc.JointTripRep)
                        {
                            ((SchedulerTripChain)tc).GetRepTripChain = tc;
                        }
                        else
                        {
                            ((SchedulerTripChain)tc).GetRepTripChain = FindRepTripChain((SchedulerTripChain)tc, person.Household);
                        }
                    }
                }
            }
            return(household);
        }
        private void SetToMax(int householdIndex, TashaHousehold hhld)
        {
            double fitness = 0;
            var    ammount = (float)Math.Log(1.00 / (HouseholdIterations + 1));

            foreach (var p in hhld.Persons)
            {
                foreach (var chain in p.TripChains)
                {
                    foreach (var trip in chain.Trips)
                    {
                        fitness += ammount;
                        Array.Clear(trip.ModesChosen, 0, trip.ModesChosen.Length);
                    }
                }
            }
            HouseholdEvaluation[householdIndex] = (float)fitness;
        }
Esempio n. 4
0
        private void GraphTripPurpose(Distribution.DistributionInformation[] distributionData)
        {
            TashaHousehold Household   = new TashaHousehold();
            TashaPerson    person      = new TashaPerson();
            List <int>     primaryWork = new List <int>();

            person.Licence = false;
            person.Male    = GenderLocal;

            SchedulerHousehold.CreateHouseholdProjects(Household);
            SchedulerPerson.InitializePersonalProjects(person);
            SchedulerPerson.GenerateWorkSchoolSchedule(person, null);
            SchedulerTripChain.GetTripChain(person);
            var trip = SchedulerTrip.GetTrip(0);

            Occupation[] Occupations = { Occupation.Professional, Occupation.Manufacturing, Occupation.Retail, Occupation.Office, Occupation.Unknown, Occupation.NotEmployed };

            LoadDistributioNumbers(person, primaryWork, Occupations);

            float[] data = new float[this.StartTimeQuantums];
            foreach (int ID in primaryWork)
            {
                var table = distributionData[ID].StartTimeFrequency;
                for (int i = 0; i < this.StartTimeQuantums; i++)
                {
                    for (int j = 0; j < this.MaxFrequencyLocal; j++)
                    {
                        data[i] += table[i][j];
                    }
                }
            }

            // Make all data in terms of percentages of total.

            float sum = data.Sum();

            for (int number = 0; number < data.Length; number++)
            {
                data[number] = data[number] / sum * 100;
                Writer.WriteLine("{0}, {1}", (Time.FromMinutes((60 * 4) + number * (1440 / this.StartTimeQuantums))), data[number]);
            }

            GenerateChart(String.Format("OfficeDur.png"), data, "Time of Day", "Probability");
        }
        private float EvaluateHousehold(TashaHousehold household)
        {
            double fitness             = 0;
            var    householdIterations = HouseholdIterations;

            foreach (var p in household.Persons)
            {
                foreach (var chain in p.TripChains)
                {
                    foreach (var trip in chain.Trips)
                    {
                        var value = Math.Log((EvaluateTrip(trip) + 1) / (householdIterations + 1));
                        fitness += value;
                        Array.Clear(trip.ModesChosen, 0, trip.ModesChosen.Length);
                        //trip.Release();
                    }
                    //chain.Release();
                }
                //p.Release();
            }
            //household.Release();
            return((float)fitness);
        }
        private TashaPerson LoadPerson(BinaryReader reader, Datastructure.SparseArray <IZone> zoneArray, TashaHousehold household, int personID)
        {
            TashaPerson person = new TashaPerson();

            person.Household        = household;
            person.Id               = personID;
            person.Age              = reader.ReadInt32();
            person.Female           = reader.ReadBoolean();
            person.EmploymentStatus = (TTSEmploymentStatus)reader.ReadInt32();
            person.Occupation       = (Occupation)reader.ReadInt32();
            person.EmploymentZone   = zoneArray[reader.ReadInt32()];
            person.StudentStatus    = (StudentStatus)reader.ReadInt32();
            person.SchoolZone       = zoneArray[reader.ReadInt32()];
            person.Licence          = reader.ReadBoolean();
            person.FreeParking      = reader.ReadBoolean();
            int numberOfTripChains;

            LoadKeys(reader, person);
            person.TripChains = new List <ITripChain>(numberOfTripChains = reader.ReadInt32());
            for (int i = 0; i < numberOfTripChains; i++)
            {
                person.TripChains.Add(LoadTripChain(reader, zoneArray, person));
            }
            return(person);
        }
Esempio n. 7
0
 public void LoadData()
 {
     if (!Loaded)
     {
         lock (this)
         {
             if (!Loaded)
             {
                 bool householdsRecieved = false;
                 this.ToHost.RegisterCustomReceiver(this.HouseholdDataChannel, (stream) =>
                 {
                     byte[] key  = GetKey()
                     , iv        = GetIV();
                     byte[] data = new byte[(int)stream.Length];
                     stream.Read(data, 0, (int)stream.Length);
                     Task.Factory.StartNew(() =>
                     {
                         MemoryStream memStream = null;
                         try
                         {
                             memStream = new MemoryStream(data);
                             using (var fromHost = new CryptoStream(memStream,
                                                                    new RijndaelManaged().CreateDecryptor(key, iv), CryptoStreamMode.Read))
                             {
                                 memStream              = null;
                                 var reader             = new BinaryReader(fromHost);
                                 int numberOfHouseholds = reader.ReadInt32();
                                 int numberOfVehicles   = reader.ReadInt32();
                                 if (numberOfVehicles != this.Root.VehicleTypes.Count)
                                 {
                                     throw new XTMFRuntimeException("We were expecting to have '" + this.Root.VehicleTypes.Count + "' different types of vehicles but the host has '" + numberOfVehicles + "'");
                                 }
                                 for (int i = 0; i < numberOfVehicles; i++)
                                 {
                                     string temp;
                                     if (this.Root.VehicleTypes[i].VehicleName != (temp = reader.ReadString()))
                                     {
                                         throw new XTMFRuntimeException("We were expecting the vehicle type to be named '" + this.Root.VehicleTypes[i].VehicleName + "' and instead found '" + temp + "'");
                                     }
                                 }
                                 TashaHousehold[] households = new TashaHousehold[numberOfHouseholds];
                                 var zoneArray = this.Root.ZoneSystem.ZoneArray;
                                 for (int i = 0; i < numberOfHouseholds; i++)
                                 {
                                     households[i] = LoadHousehold(reader, zoneArray);
                                 }
                                 reader             = null;
                                 this.Households    = households;
                                 householdsRecieved = true;
                             }
                         }
                         finally
                         {
                             if (memStream != null)
                             {
                                 memStream.Dispose();
                                 memStream = null;
                             }
                         }
                     });
                     return(null);
                 });
                 this.ToHost.RegisterCustomSender(this.HouseholdDataChannel, (data, stream) =>
                 {
                     // do nothing
                 });
                 this.ToHost.RegisterCustomMessageHandler(this.HouseholdDataChannel, (householdArray) =>
                 {
                     // do nothing
                 });
                 // Tell the host that we want our households
                 this.ToHost.SendCustomMessage(null, this.HouseholdDataChannel);
                 while (!householdsRecieved)
                 {
                     System.Threading.Thread.Sleep(0);
                     System.Threading.Thread.MemoryBarrier();
                 }
                 this.Loaded = true;
             }
         }
     }
 }