public GibbsSampler()
 {
     warmupTime = 0;
     samplingInterval = 0;
     agentIDCounter = 0;
     myImportantSampler = new ImportanceSampler();
     myMHSampler = new MetropolisHasting();
     Initialze();
 }
        private List<SimulationObject> GenerateHousholds(SpatialZone currZone, int numHousehold,
                        Household initAgent, bool warmUpStatus,
                        List<ConditionalDistribution> mobelCond,
                        OutputFileWriter currWriter)
        {
            int seltdDim = 0;
            List<ConditionalDistribution> condList = currZone.GetDataHhldCollectionsList();
            for(int i = 0; i < mobelCond.Count; i++)
            {
                condList.Add(mobelCond[i]);
            }
            var generatedAgents = new List<SimulationObject>();
            Household prevAgent = initAgent;

            ImportanceSampler currImpSampler = new ImportanceSampler();
            MetropolisHasting currMHSampler = new MetropolisHasting();
            int iter = 0;
            if(warmUpStatus == true)
            {
                iter = Constants.WARMUP_ITERATIONS;
            }
            else
            {
                iter = Constants.SKIP_ITERATIONS * numHousehold;
            }
            Household newAgent = new Household();
            StringBuilder builder = new StringBuilder();
            for(int i = 0; i < iter; i++)
            {
                seltdDim = randGen.NextInRange(0, condList.Count - 1);

                ConditionalDistribution currDist = condList[seltdDim];

                // If the selected distribution is dwelling/cars
                // call important sampling

                /*if (currDist.GetDimensionName() == "DwellingType")
                {
                    newAgent = currImpSampler.GetNextAgent(currZone.myDwellMarginal,
                        currDist, currDist.GetDimensionName(),
                        prevAgent, currZone);
                }
                else if (currDist.GetDimensionName() == "NumOfCars")
                {
                    newAgent = currImpSampler.GetNextAgent(currZone.myCarsMarginal,
                        currDist, currDist.GetDimensionName(),
                        prevAgent, currZone);
                }*/

                // If the selected distribution is income
                // call MH
                //                else if (((ConditionalDistribution)condList[seltdDim])
                //                                .GetDimensionName() == "IncomeLevel")
                //                {
                //                    newAgent = myMHSampler.GetNextAgent((ModelDistribution)currDist,
                //                            currDist.GetDimensionName(), prevAgent, currZone);
                //                }
                if(currDist.GetDimensionName() == "HouseholdSize")
                {
                    newAgent = (Household)currImpSampler.GetNextAgent(
                        currZone.GetHousholdSizeDist(),
                        currDist, currDist.GetDimensionName(),
                        prevAgent, currZone);
                }
                else
                {
                    var currComm = currDist.GetCommulativeValue(
                        prevAgent.GetNewJointKey(currDist.GetDimensionName())
                        , currZone);
                    newAgent = (Household)GenerateNextAgent(currComm, prevAgent,
                        currDist.GetDimensionName());
                }

                prevAgent = newAgent;
                if(warmUpStatus == false && (i % Constants.SKIP_ITERATIONS == 0))
                {
                    generatedAgents.Add(newAgent);
                    uint currIncome = IncomeConvertor.GetEuroIncome((uint)
                                        newAgent.GetIncome());
                    builder.Append(newAgent.GetZoneID()); builder.Append(',');
                    builder.Append(currZone.GetEPFLName()); builder.Append(',');
                    builder.Append((int)newAgent.GetHhldSize()); builder.Append(',');
                    builder.Append((int)newAgent.GetNumOfWorkers()); builder.Append(',');
                    builder.Append((int)newAgent.GetNumOfKids()); builder.Append(',');
                    builder.Append((int)newAgent.GetNumOfUnivDegree()); builder.Append(',');
                    builder.Append((int)newAgent.GetIncomeLevel()); builder.Append(',');
                    builder.Append((int)newAgent.GetNumOfCars()); builder.Append(',');
                    builder.Append((int)newAgent.GetDwellingType());
                    currWriter.WriteToFile(builder.ToString());
                    builder.Clear();
                }
            }
            return generatedAgents;
        }
        private List<SimulationObject> GeneratePersons(SpatialZone currZone, int numPerson,
                            Person initAgent, bool warmUpStatus,
                            OutputFileWriter currWriter)
        {
            int seltdDim = 0;
            List<ConditionalDistribution> condList = currZone.GetPersonDataCollectionsList();
            var generatedAgents = new List<SimulationObject>();
            Person prevAgent = initAgent;
            ImportanceSampler currImpSampler = new ImportanceSampler();

            int iter = 0;
            if(warmUpStatus == true)
            {
                iter = Constants.WARMUP_ITERATIONS;
            }
            else
            {
                iter = Constants.SKIP_ITERATIONS * numPerson;
            }
            Person newAgent = new Person();
            StringBuilder builder = new StringBuilder();
            for(int i = 0; i < iter; i++)
            {
                seltdDim = randGen.NextInRange(0, condList.Count - 1);

                DiscreteCondDistribution currDist =
                    (DiscreteCondDistribution)condList[seltdDim];

                /*if (currDist.GetDimensionName() == "HouseholdSize2")
                {
                    newAgent = (Person) currImpSampler.GetNextAgent(
                                currZone.myHhldSize2Marginal,
                                currDist, currDist.GetDimensionName(),
                                (SimulationObject) prevAgent, currZone);
                }
                else if (currDist.GetDimensionName() == "Age")
                {
                    newAgent = (Person)currImpSampler.GetNextAgent(
                                currZone.myAgeMarginal,
                                currDist, currDist.GetDimensionName(),
                                (SimulationObject)prevAgent, currZone);
                }
                else*/
                if(currDist.GetDimensionName() == "Sex")
                {
                    newAgent = (Person)currImpSampler.GetNextAgent(
                                currZone.mySexMarginal,
                                currDist, currDist.GetDimensionName(),
                                (SimulationObject)prevAgent, currZone);
                }
                /*else if (currDist.GetDimensionName() == "EducationLevel")
                {
                    newAgent = (Person)currImpSampler.GetNextAgent(
                                currZone.myEducationMarginal,
                                currDist, currDist.GetDimensionName(),
                                (SimulationObject)prevAgent, currZone);
                }*/
                else
                {
                    List<KeyValPair> currComm = currDist.GetCommulativeValue(
                         prevAgent.GetNewJointKey(currDist.GetDimensionName())
                            , currZone);
                    newAgent = (Person)GenerateNextAgent(currComm,
                            (SimulationObject)prevAgent,
                            currDist.GetDimensionName());
                }

                prevAgent = newAgent;
                if(warmUpStatus == false && (i % Constants.SKIP_ITERATIONS == 0))
                {
                    //generatedAgents.Add(newAgent);
                    builder.Append((int)newAgent.GetAge()); builder.Append(',');
                    builder.Append(newAgent.GetZoneID()); builder.Append(',');
                    builder.Append((int)newAgent.GetSex()); builder.Append(',');
                    builder.Append((int)newAgent.GetHhldSize()); builder.Append(',');
                    builder.Append((int)newAgent.GetEducationLevel());
                    currWriter.WriteToFile(builder.ToString());
                    builder.Clear();
                }
            }
            return generatedAgents;
        }
Example #4
0
        private List<SimulationObject> GenerateHousholdsComposite(SpatialZone currZone, int numHousehold,
                        HouseholdPersonComposite initAgent, bool warmUpStatus,
                        List<ConditionalDistribution> mobelCond,
                        OutputFileWriter currWriter)
        {
            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 builder = 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.GetHousholdSizeDist(),
                            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).CheckConsistency();
                }

                prevAgent = newAgent;
                if (warmUpStatus == false && (i % Constants.SKIP_ITERATIONS == 0))
                {
                    generatedAgents.Add(newAgent);
                    builder.Append(newAgent.getHousehold().GetZoneID()); builder.Append(',');
                    builder.Append((int)newAgent.getHousehold().GetDwellingType()); builder.Append(',');
                    builder.Append((int)newAgent.getHousehold().GetHhldSize()); builder.Append(',');
                    builder.Append((int)newAgent.getHousehold().GetNumOfKids()); builder.Append(',');
                    builder.Append((int)newAgent.getHousehold().GetNumOfCars()); builder.Append(',');
                    //builder.Append(currZone.GetEPFLName()); builder.Append(',');
                    //builder.Append((int)newAgent.getHousehold().GetNumOfWorkers()); builder.Append(',');
                    //builder.Append((int)newAgent.getHousehold().GetNumOfUnivDegree()); builder.Append(',');
                    //builder.Append((int)newAgent.getHousehold().GetIncomeLevel()); builder.Append(',');

                    foreach (Person person in newAgent.getPersons())
                    {
                        builder.Append((int)person.GetAge()); builder.Append(',');
                        builder.Append((int)person.GetDrivingLicense()); builder.Append(',');
                        builder.Append((int)person.GetEmploymentStatus()); builder.Append(',');
                        builder.Append((int)person.GetOccupation()); builder.Append(',');
                        builder.Append((int)person.GetSex());
                        //builder.Append((int)person.GetEducationLevel());
                    }

                    currWriter.WriteToFile(builder.ToString());
                    builder.Clear();
                }
            }
            return generatedAgents;
        }