コード例 #1
0
        public bool GenerateConditionals(string inputDataFile, string inputDescFile)
        {
            using (var reader = new ConditionalDataReader(inputDataFile))
            {
                string currRow = null;
                currRow = reader.GetNextRow();
                if(currRow != null)
                {
                    SetDimensions(inputDescFile);
                }
                else
                {
                    return false;
                }
                currRow = reader.GetNextRow();

                for(int i = 0; i < DimenionNames.Count; i++)
                {
                    CreateCategoryCombinations(i);
                }
                while(currRow != null)
                {
                    WriteNextConditional(currRow);
                    currRow = reader.GetNextRow();
                }

                foreach(var currTable in CondCollection)
                {
                    using (var writer = new OutputFileWriter(
                        Constants.DATA_DIR + "Census" + currTable.Key + ".csv"))
                    {
                        writer.WriteToFile("Conditional,Count");
                        foreach(var currPair in currTable.Value)
                        {
                            writer.WriteToFile(
                                ((KeyValPair)currPair.Value).Category
                                + "," + ((KeyValPair)currPair.Value)
                                            .Value.ToString());
                        }
                    }
                }
            }
            return true;
        }
コード例 #2
0
ファイル: World.cs プロジェクト: Nasterska/Simpsynz_
        public void CreatePopulationByDwellingType(int seed, string poolfileName,
                string fileName)
        {
            ArrayList hhldPool = GetHouseholdPoolForClonning(poolfileName);

            using (InputDataReader currReader = new InputDataReader(poolfileName))
            using (OutputFileWriter currOutputFile = new OutputFileWriter(fileName))
            {
                ArrayList currPool = new ArrayList();
                RandomNumberGen currRandGen = new RandomNumberGen(seed);
                currOutputFile.WriteToFile("HhldID,SectorID,HhldSize,NbOfWorkers,"
                            + "NbofKids,NbofUnivDegree,IncLvl,NumbCars,"
                            + "DwellTyp(PopSynt),EPFL_SectorID,BuildingID");
                int totSingle = 0;
                int totSemi = 0;
                int totDb = 0;
                int toApt = 0;

                while (currReader.LoadZonalPopulationPool(currPool) == true)
                {
                    string[] currStrTok = ((string)currPool[0]).Split(',');
                    int indx = 0;

                    string currKey = "";
                    if (zonalControlTotals.ContainsKey(currStrTok[1]))
                    {
                        // for each type of dwelling
                        for (int i = 0; i < 4; i++)
                        {
                            ArrayList currDwellHhld = new ArrayList();
                            foreach (string currHhld in currPool)
                            {
                                string[] currHhldVal = currHhld.Split(',');
                                if (currHhldVal[(currHhldVal.Length - 1)] == i.ToString())
                                {
                                    currDwellHhld.Add(currHhld);
                                }
                            }
                            currKey = (string)currStrTok[1];
                            string[] contTotStr = (string[])zonalControlTotals[currKey];

                            // number of dwellings of certain type
                            indx = int.Parse(contTotStr[i + 2]);
                            string ZnID = contTotStr[0];
                            string ZnEFPLID = contTotStr[1];
                            string bldId = "0" + (i + 1).ToString();

                            if (indx > currDwellHhld.Count)
                            {
                                indx = indx - currDwellHhld.Count;
                                for (int x = 0; x < currDwellHhld.Count; x++)
                                {
                                    string[] hhldValues = ((string)
                                            currDwellHhld[x]).Split(',');
                                    string currHhldStr = hhldValues[0] + "," + ZnID.Substring(0, 5)
                                                + "," + hhldValues[3] + "," + hhldValues[4]
                                                + "," + hhldValues[5] + "," + hhldValues[6]
                                                + "," + hhldValues[7] + "," + hhldValues[8]
                                                + "," + hhldValues[9] + ","
                                                + contTotStr[1] + "," + contTotStr[1] + bldId;
                                    currOutputFile.WriteToFile(currHhldStr);
                                    //Console.WriteLine(currHhldStr);
                                    if (i == 0) totSingle++;
                                    else if (i == 1) totSemi++;
                                    else if (i == 2) totDb++;
                                    else toApt++;
                                }
                                ArrayList currRandList = currRandGen.GetNNumbersInRange(0,
                                    hhldPool.Count - 1, indx);
                                for (int j = 0; j < currRandList.Count; j++)
                                {
                                    string[] hhldValues = ((string)
                                        hhldPool[(int)currRandList[j]]).Split(',');
                                    string currHhldStr = ZnID + i.ToString() + j.ToString()
                                            + "," + ZnID.Substring(0, 5)
                                            + "," + hhldValues[0] + "," + hhldValues[1]
                                            + "," + hhldValues[2] + "," + hhldValues[3]
                                            + "," + hhldValues[4] + "," + hhldValues[5]
                                            + "," + hhldValues[6] + ","
                                            + ZnEFPLID + "," + ZnEFPLID + bldId;
                                    currOutputFile.WriteToFile(currHhldStr);
                                    //Console.WriteLine(currHhldStr);
                                    if (i == 0) totSingle++;
                                    else if (i == 1) totSemi++;
                                    else if (i == 2) totDb++;
                                    else toApt++;
                                }
                            }
                            else
                            {
                                ArrayList currRandList = currRandGen.GetNNumbersInRange(0,
                                    currDwellHhld.Count - 1, indx);
                                for (int j = 0; j < currRandList.Count; j++)
                                {
                                    string[] hhldValues = ((string)
                                        currDwellHhld[(int)currRandList[j]]).Split(',');
                                    string currHhldStr = hhldValues[0] + "," + ZnID.Substring(0, 5)
                                            + "," + hhldValues[3] + "," + hhldValues[4]
                                            + "," + hhldValues[5] + "," + hhldValues[6]
                                            + "," + hhldValues[7] + "," + hhldValues[8]
                                            + "," + hhldValues[9] + ","
                                            + contTotStr[1] + "," + contTotStr[1] + bldId;
                                    currOutputFile.WriteToFile(currHhldStr);
                                    //Console.WriteLine(currHhldStr);
                                    if (i == 0) totSingle++;
                                    else if (i == 1) totSemi++;
                                    else if (i == 2) totDb++;
                                    else toApt++;
                                }
                            }
                        }
                    }
                    currPool.Clear();
                }
                Console.WriteLine("Total Detached:\t" + totSingle.ToString()
                              + "\nTotal SemiDetached:\t" + totSemi.ToString()
                              + "\nTotal Attached:\t" + totDb.ToString()
                              + "\nTotal Apartment:\t" + toApt.ToString());
            }
        }
コード例 #3
0
ファイル: World.cs プロジェクト: Nasterska/Simpsynz_
 public void CreateHouseholdPopulation()
 {
     using (var currReader = new InputDataReader(
         Constants.DATA_DIR + "SyntheticHhld_withourImpSamp.csv"))
     {
         ArrayList currPool = new ArrayList();
         RandomNumberGen currRandGen = new RandomNumberGen();
         using (var currOutputFile = new OutputFileWriter(Constants.DATA_DIR + "PopulationRealization20k.csv"))
         {
             currOutputFile.WriteToFile("HhldID,SectorID,HhldSize,NbOfWorkers,"
                         + "NbofKids,NbofUnivDegree,IncLvl,NumbCars,"
                         + "DwellTyp(PopSynt),EPFL_SectorID,BuildingID");
             while (currReader.LoadZonalPopulationPool(currPool) == true)
             {
                 string[] currStrTok = ((string)currPool[0]).Split(',');
                 int indx = 0;
                 string currKey = "";
                 if (zonalControlTotals.ContainsKey(currStrTok[1]))
                 {
                     currKey = (string)currStrTok[1];
                     indx = (int)zonalControlTotals[currKey];
                     ArrayList currRandList = currRandGen.GetNNumbersInRange(0,
                         Constants.POOL_COUNT - 1, indx);
                     for (int i = 0; i < currRandList.Count; i++)
                     {
                         string[] hhldValues = ((string)
                             currPool[(int)currRandList[i]]).Split(',');
                         int bld = Int16.Parse(hhldValues[9]) + 1;
                         currOutputFile.WriteToFile(hhldValues[0] + "," + hhldValues[1]
                                 + "," + hhldValues[3] + "," + hhldValues[4]
                                 + "," + hhldValues[5] + "," + hhldValues[6]
                                 + "," + hhldValues[7] + "," + hhldValues[8]
                                 + "," + hhldValues[9] + ","
                                 + hhldValues[2] + "," + hhldValues[2]
                                 + "0" + bld);
                     }
                 }
                 currPool.Clear();
             }
         }
     }
 }
コード例 #4
0
ファイル: World.cs プロジェクト: Nasterska/Simpsynz_
        public void CreateHoseholdCompositePopulationPool(string fileName)
        {
            List<int[]> PD = new List<int[]>();
            using (TextReader myFileReader = new StreamReader(Path.Combine(Constants.DATA_DIR, "PD_hhldType.txt")))
            {
                string strTok;
                myFileReader.ReadLine();
                while ((strTok = myFileReader.ReadLine()) != null)
                {
                    string[] strToken = strTok.Split(',');

                    int SingleAdult = int.Parse(strToken[1]);
                    int OneAdultOneChild = int.Parse(strToken[2]);
                    int Twoadults = int.Parse(strToken[3]);
                    int TwoAdultsChildren = int.Parse(strToken[4]);
                    int ThreeOrMoreAdults = int.Parse(strToken[5]);
                    int ThreeOrMoreAdultsChildren = int.Parse(strToken[6]);

                    int[] hhldTypes = { SingleAdult, OneAdultOneChild, Twoadults, TwoAdultsChildren
                                         , ThreeOrMoreAdults, ThreeOrMoreAdultsChildren};
                    PD.Add(hhldTypes);

                }
            }

            using (var agentsOutputFile = new OutputFileWriter(fileName))
            {
                uint agentsCreated = 1;
                uint counter = 0;
                StringBuilder builderHhld = new StringBuilder();
                builderHhld.Append("HouseholdID,Zone,ExpansionFactor,DwellingType,NumberOfPersons,NumberOfVehicles");

                StringBuilder builderPers = new StringBuilder();
                builderPers.Append("HouseholdID,PersonNumber,Age,Sex,License,TransitPass,EmploymentStatus,Occupation,FreeParking,StudentStatus,EmploymentZone,SchoolZone");

                agentsOutputFile.WriteToFile(builderHhld.ToString());

                String[] cc = fileName.Split ('.');
                OutputFileWriter personOutputFile = new OutputFileWriter (cc[0] + "pers.csv");
                personOutputFile.WriteToFile(builderPers.ToString());

                foreach (var entry in ZonalCollection)
                {
                    GibbsSampler sampler = new GibbsSampler();
                    SpatialZone currZone = (SpatialZone)entry.Value;
                    int[] types = PD[int.Parse(currZone.GetName()) - 1];
                    foreach (HouseholdSize size in Enum.GetValues(typeof(HouseholdSize)))
                    {
                        var type_entry = types[(int)size];
                        // warmup time
                        sampler.GenerateAgents(currZone,
                                        Constants.WARMUP_ITERATIONS,
                            new HouseholdPersonComposite(new Household(size, currZone.GetName())), true, null,
                            agentsOutputFile,personOutputFile);
                        HhldscompositePool.Clear();
                        sampler.SetAgentCounter(agentsCreated + counter);
                        // actual generation
                        HhldscompositePool = sampler.GenerateAgents(currZone,
                                        Constants.POOL_COUNT * type_entry,
                            new HouseholdPersonComposite(new Household(size, currZone.GetName())), false,  null,
                            agentsOutputFile,personOutputFile);
                        agentsCreated += (uint)HhldscompositePool.Count;
                    }
                }
            }
        }
コード例 #5
0
        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;
        }
コード例 #6
0
        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;
        }
コード例 #7
0
ファイル: GibbsSampler.cs プロジェクト: AnsBal/Simpsynz_
        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;
        }