示例#1
0
        private List<SimulationObject> GenerateHousholdsComposite(SpatialZone currZone, int numHousehold,
                        HouseholdPersonComposite initAgent, bool warmUpStatus,
                        OutputFileWriter currWriterHhld, OutputFileWriter currWriterPers)
        {
            int seltdDim = 0;
            int seltAgnt = 0;

            List<ConditionalDistribution> condHhldList = currZone.GetDataHhldCompositeCollectionsListH();
            List<ConditionalDistribution> condPerList = currZone.GetDataHhldCompositeCollectionsListP();

            var generatedAgents = new List<SimulationObject>();
            HouseholdPersonComposite prevAgent = initAgent;
            ImportanceSampler currImpSampler = new ImportanceSampler();
            Random rnd = new Random();

            int iter = 0;
            if (warmUpStatus == true)
            {
                iter = Constants.WARMUP_ITERATIONS;
            }
            else
            {
                iter = Constants.SKIP_ITERATIONS * numHousehold;
            }
            HouseholdPersonComposite newAgent;
            StringBuilder builderHhld = new StringBuilder();
            StringBuilder builderPers = new StringBuilder();

            for (int i = 0; i < iter; i++)
            {
                // with equal probablity select one of the hhld or persons
                seltAgnt = randGen.NextInRange(0, prevAgent.getPersons().Count());
                //Change Hhld object
                if (seltAgnt == 0) {
                    seltdDim = randGen.NextInRange(0, condHhldList.Count - 1);
                    ConditionalDistribution currDist = condHhldList[seltdDim];

                    var currComm = currDist.GetCommulativeValue (
                        prevAgent
                        , currZone, -1);
                    newAgent = (HouseholdPersonComposite)GenerateNextAgent (currComm, prevAgent,
                        currDist.GetDimensionName (), -1);
                }
                //Change person object
                else {
                    seltdDim = randGen.NextInRange(0, condPerList.Count - 1);
                    ConditionalDistribution currDist = condPerList[seltdDim];

                    // Select randomly one person from the collection, whose attribute is changed
                    int personId = rnd.Next(0, prevAgent.getPersons().Count - 1);

                    // Importance sampling for the Age
                    if (currDist.GetDimensionName() == "Age")
                    {
                        newAgent = (HouseholdPersonComposite) currImpSampler.GetNextAgent(
                            currZone.GetPersonAgeMarginal(),
                            currDist, currDist.GetDimensionName(),
                            prevAgent, currZone, personId);
                    }
                    else
                    {
                        var currComm = currDist.GetCommulativeValue (
                            prevAgent
                            , currZone, personId);
                        newAgent = (HouseholdPersonComposite)GenerateNextAgent (currComm, prevAgent,
                            currDist.GetDimensionName (), personId);
                    }
                    //Consistency check
                    ((HouseholdPersonComposite) newAgent).CheckSexConsisteny(currZone.GetPersonSexMarginal());
                }

                prevAgent = newAgent;
                if (warmUpStatus == false && (i % Constants.SKIP_ITERATIONS == 0))
                {
                    generatedAgents.Add(newAgent);
                    builderHhld.Append(newAgent.getHousehold().GetAgentID()); builderHhld.Append(',');
                    builderHhld.Append(newAgent.getHousehold().GetZoneID()); builderHhld.Append(',');
                    builderHhld.Append(',');
                    builderHhld.Append((int)newAgent.getHousehold().GetDwellingType()); builderHhld.Append(',');
                    builderHhld.Append((int)newAgent.getHousehold().GetHhldSize()); builderHhld.Append(',');
                    builderHhld.Append((int)newAgent.getHousehold().GetNumOfKids()); builderHhld.Append(',');
                    builderHhld.Append((int)newAgent.getHousehold().GetNumOfCars()); builderHhld.Append(',');
                    int personNumber = 1;

                    foreach (Person person in newAgent.getPersons())
                    {
                        builderPers.Append((int)newAgent.getHousehold().GetAgentID()); builderPers.Append(',');
                        builderPers.Append(personNumber++); builderPers.Append(',');
                        builderPers.Append((int)person.GetContAge()); builderPers.Append(',');
                        builderPers.Append((int)person.GetSex()); builderPers.Append(',');
                        builderPers.Append((int)person.GetDrivingLicense()); builderPers.Append(',');
                        builderPers.Append('0'); builderPers.Append(',');
                        builderPers.Append((int)person.GetEmploymentStatus()); builderPers.Append(',');
                        builderPers.Append((int)person.GetOccupation()); builderPers.Append(',');
                        builderPers.Append("0,0,0,0");

                        currWriterPers.WriteToFile(builderPers.ToString());
                        builderPers.Clear ();
                    }
                    currWriterHhld.WriteToFile(builderHhld.ToString());
                    builderHhld.Clear();
                }
            }
            return generatedAgents;
        }