def test_run_model_with_known_buildings(self):
        storage = self.storage
        storage.write_table(
            table_name = 'buildings',
            table_data = {
                  'building_id':    array([1,2,3,4,5,6,7]),
                  'parcel_id':      array([1,1,2,2,2,3,3]),
                  'is_residential': array([0,0,0,1,1,0,0])
                          }
                            )
        
        storage.write_table(
            table_name = 'employment_events',
            table_data = {
           'parcel_id':                     array([2,       2,    -1,     -1,   1]),
           'building_id':                   array([-1,     -1,     6,      7,  -1]),
           'scheduled_year':                array([2006, 2006,   2006,    2006, 2006]),
           'number_of_non_home_based_jobs': array([3500, 500,    -100,     0,   100]),
           'number_of_home_based_jobs':     array([0,     20,       0,    10,    0]),
           'sector_id':                     array([1,     2,       15,     2,    1]),
           'replace_non_home_based_jobs':   array([0,     0,        0,     1,    0])
                          }
                            )
        
        # change in 2006
        ############   
#        parcel/sector       1              2              3
#            1            +100nhb         +3500nhb        --
#            2            =0nhb           +500nhb/+20hb  +10hb
#            15             --             --           -100nhb
       
        dataset_pool = DatasetPool(storage=storage, package_order=['urbansim_parcel', 'urbansim'])
        job_set = JobDataset(in_storage=storage)
        job_set.modify_attribute('building_id', array(6000*[1] + 4000*[3] + 3000*[6]))
        dataset_pool.add_datasets_if_not_included({'job':job_set})
        model = EmploymentEventsModel(dataset_pool=dataset_pool)

        model.run(dataset_pool.get_dataset('employment_event'), job_set, current_year=2006)
        buildings = dataset_pool.get_dataset('building')
        jobs_in_sec_1 = buildings.compute_variables(['urbansim_parcel.building.number_of_jobs_of_sector_1'],
                                                    dataset_pool=dataset_pool)
        jobs_in_sec_2 = buildings.compute_variables(['urbansim_parcel.building.number_of_jobs_of_sector_2'],
                                                    dataset_pool=dataset_pool)
        jobs_in_sec_15 = buildings.compute_variables(['urbansim_parcel.building.number_of_jobs_of_sector_15'],
                                                    dataset_pool=dataset_pool)

        self.assertEqual(jobs_in_sec_1[0:2].sum()==4100, True) # parcel 1
        self.assertEqual(jobs_in_sec_1[2] == 5500, True) # parcel 2 non-residential
        self.assertEqual(jobs_in_sec_1[5:7].sum()==1000, True) # parcel 3
        self.assertEqual(jobs_in_sec_2[0:2].sum()==1000, True) # parcel 1
        self.assertEqual(jobs_in_sec_2[2] == 1500, True) # parcel 2 non-residential
        self.assertEqual(jobs_in_sec_2[3:5].sum() == 20, True) # parcel 2 residential
        self.assertEqual(jobs_in_sec_2[5]==100, True) # parcel 3, building 6
        self.assertEqual(jobs_in_sec_2[6]==10, True) # parcel 3, building 7
        self.assertEqual(jobs_in_sec_15[0:2].sum()==1000, True) # parcel 2 non-residential
        self.assertEqual(jobs_in_sec_15[2] == 1000, True) # parcel 2 residential
        self.assertEqual(jobs_in_sec_15[5]==900, True) # parcel 3, building 6
Exemple #2
0
aggr_var2 = "neighborhood.aggregate(gridcell.capacity, intermediates=[zone], function=sum)"
neighborhoods.compute_variables(aggr_var2, dataset_pool=dataset_pool)

neighborhoods.add_primary_attribute(name="is_cbd", data=[0,0,1])
disaggr_var = "is_cbd = gridcell.disaggregate(neighborhood.is_cbd, intermediates=[zone])"
locations.compute_variables(disaggr_var, dataset_pool=dataset_pool)

from opus_core.datasets.alldata_dataset import AlldataDataset
alldata = AlldataDataset(dataset_name="alldata")
alldata.compute_variables(
                      "total_capacity = alldata.aggregate_all(gridcell.capacity, function=sum)",
                      dataset_pool=dataset_pool)

# Number of agents
households.modify_attribute(name="location", data=[2, 8, 3, 1, 5, 4, 9, 7, 3, 6])
dataset_pool.add_datasets_if_not_included({'household': households})
locations.compute_variables("gridcell.number_of_agents(household)",
                        dataset_pool=dataset_pool)
neighborhoods.compute_variables("neighborhood.number_of_agents(zone)",
                            dataset_pool=dataset_pool)

# Creating a model
from opus_core.model import Model
from opus_core.logger import logger
class MyModel(Model):
    model_name = "my model"
    def run(self):
        logger.log_status("I'm running!")
        return

MyModel().run()
Exemple #3
0
    def test_run_model_with_known_buildings(self):
        storage = self.storage
        storage.write_table(table_name='buildings',
                            table_data={
                                'building_id': array([1, 2, 3, 4, 5, 6, 7]),
                                'parcel_id': array([1, 1, 2, 2, 2, 3, 3]),
                                'is_residential': array([0, 0, 0, 1, 1, 0, 0])
                            })

        storage.write_table(table_name='employment_events',
                            table_data={
                                'parcel_id':
                                array([2, 2, -1, -1, 1]),
                                'building_id':
                                array([-1, -1, 6, 7, -1]),
                                'scheduled_year':
                                array([2006, 2006, 2006, 2006, 2006]),
                                'number_of_non_home_based_jobs':
                                array([3500, 500, -100, 0, 100]),
                                'number_of_home_based_jobs':
                                array([0, 20, 0, 10, 0]),
                                'sector_id':
                                array([1, 2, 15, 2, 1]),
                                'replace_non_home_based_jobs':
                                array([0, 0, 0, 1, 0])
                            })

        # change in 2006
        ############
        #        parcel/sector       1              2              3
        #            1            +100nhb         +3500nhb        --
        #            2            =0nhb           +500nhb/+20hb  +10hb
        #            15             --             --           -100nhb

        dataset_pool = DatasetPool(
            storage=storage, package_order=['urbansim_parcel', 'urbansim'])
        job_set = JobDataset(in_storage=storage)
        job_set.modify_attribute('building_id',
                                 array(6000 * [1] + 4000 * [3] + 3000 * [6]))
        dataset_pool.add_datasets_if_not_included({'job': job_set})
        model = EmploymentEventsModel(dataset_pool=dataset_pool)

        model.run(dataset_pool.get_dataset('employment_event'),
                  job_set,
                  current_year=2006)
        buildings = dataset_pool.get_dataset('building')
        jobs_in_sec_1 = buildings.compute_variables(
            ['urbansim_parcel.building.number_of_jobs_of_sector_1'],
            dataset_pool=dataset_pool)
        jobs_in_sec_2 = buildings.compute_variables(
            ['urbansim_parcel.building.number_of_jobs_of_sector_2'],
            dataset_pool=dataset_pool)
        jobs_in_sec_15 = buildings.compute_variables(
            ['urbansim_parcel.building.number_of_jobs_of_sector_15'],
            dataset_pool=dataset_pool)

        self.assertEqual(jobs_in_sec_1[0:2].sum() == 4100, True)  # parcel 1
        self.assertEqual(jobs_in_sec_1[2] == 5500,
                         True)  # parcel 2 non-residential
        self.assertEqual(jobs_in_sec_1[5:7].sum() == 1000, True)  # parcel 3
        self.assertEqual(jobs_in_sec_2[0:2].sum() == 1000, True)  # parcel 1
        self.assertEqual(jobs_in_sec_2[2] == 1500,
                         True)  # parcel 2 non-residential
        self.assertEqual(jobs_in_sec_2[3:5].sum() == 20,
                         True)  # parcel 2 residential
        self.assertEqual(jobs_in_sec_2[5] == 100, True)  # parcel 3, building 6
        self.assertEqual(jobs_in_sec_2[6] == 10, True)  # parcel 3, building 7
        self.assertEqual(jobs_in_sec_15[0:2].sum() == 1000,
                         True)  # parcel 2 non-residential
        self.assertEqual(jobs_in_sec_15[2] == 1000,
                         True)  # parcel 2 residential
        self.assertEqual(jobs_in_sec_15[5] == 900,
                         True)  # parcel 3, building 6
class BTMTests(StochasticTestCase):

    def setUp( self ):
        """here, we simulate 50 residential units
        and 5000 commercial, industrial, and governmental sqft added to each of the gridcells in previous years.
        """

        ### TODO: do not redefine these constants.
        self.comc = 1
        self.indc = 3
        self.govc = 2
        self.sfhc = 4
        self.mfhc = 5

        storage = StorageFactory().get_storage('dict_storage')

        gridcells_table_name = 'gridcells'
#            create 100 gridcells, each with 200 residential units and space for 100 commercial jobs,
#            100 industrial jobs, and residential, industrial, and commercial value at $500,000 each
        storage.write_table(
            table_name=gridcells_table_name,
            table_data={
                "grid_id": arange( 1, 100+1 ),
                "commercial_sqft_per_job":array( 100*[100] ),
                "industrial_sqft_per_job":array( 100*[100] ),
                "single_family_improvement_value":array( 100*[500000] ),
                "commercial_improvement_value":array( 100*[500000] ),
                "industrial_improvement_value":array( 100*[500000] )
                }
            )
        self.gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name)

        buildings_table_name = 'buildings'
#            2000 buildings (1000 with 20 residential units each, 500 with 20 commercial job and 500 with 20 industrial job each)
        storage.write_table(
            table_name=buildings_table_name,
            table_data={
                "building_id":arange( 1, 2000+1 ), # 2000 buildings
                "grid_id":array( 20*range( 1, 100+1 ), dtype=int32 ), # spread evenly across 100 gridcells
                "building_type_id":array(1000*[self.sfhc] +
                                         500*[self.comc] +
                                         500*[self.indc], dtype=int8),
                "sqft": array(1000*[0] +
                              500*[2000] +
                              500*[2000], dtype=int32),
                "residential_units": array(1000*[20] +
                                           500* [0] +
                                           500* [0], dtype=int32),
                "improvement_value": array(1000*[50] +
                                           500* [50] +
                                           500* [50], dtype=float32),
                "year_built": array(1000*[1940] +
                                    500* [1940] +
                                    500* [1940], dtype=int32)
                }
            )
        self.buildings = BuildingDataset(in_storage=storage, in_table_name=buildings_table_name)

        households_table_name = 'households'
#            create 10000 households, 100 in each of the 100 gridcells.
#            there will initially be 100 vacant residential units in each gridcell then.
        storage.write_table(
            table_name=households_table_name,
            table_data={
                "household_id":arange( 1, 10000+1 ),
                "grid_id":array( 100*range( 1, 100+1 ), dtype=int32 )
                }
            )
        self.households = HouseholdDataset(in_storage=storage, in_table_name=households_table_name)

        building_types_table_name = 'building_types'
        storage.write_table(
            table_name=building_types_table_name,
            table_data={
                "building_type_id":array([self.govc,self.comc,self.indc, self.sfhc, self.mfhc], dtype=int8),
                "name": array(["governmental", "commercial", "industrial", "single_family","multiple_family"]),
                "units": array(["governmental_sqft", "commercial_sqft", "industrial_sqft", "residential_units", "residential_units"]),
                "is_residential": array([0,0,0,1,1], dtype='?')
                }
            )
        self.building_types = BuildingTypeDataset(in_storage=storage, in_table_name=building_types_table_name)

        job_building_types_table_name = 'job_building_types'
        storage.write_table(
            table_name=job_building_types_table_name,
            table_data={
                "id":array([self.govc,self.comc,self.indc, self.sfhc, self.mfhc], dtype=int8),
                "name": array(["governmental", "commercial", "industrial", "single_family","multiple_family"])
                }
            )
        self.job_building_types = JobBuildingTypeDataset(in_storage=storage, in_table_name=job_building_types_table_name)

        jobs_table_name = 'jobs'
#            create 2500 commercial jobs and distribute them equally across the 100 gridcells,
#            25 commercial buildings/gridcell
        storage.write_table(
            table_name=jobs_table_name,
            table_data={
                "job_id":arange( 1, 2500+1 ),
                "grid_id":array( 25*range( 1, 100+1 ), dtype=int32 ),
                "sector_id":array( 2500*[1], dtype=int32 ),
                "building_type":array(2500*[self.comc], dtype=int8)
                }
            )
        self.jobs = JobDataset(in_storage=storage, in_table_name=jobs_table_name)

        self.dataset_pool = DatasetPool()
        self.dataset_pool.add_datasets_if_not_included({
                                            "household":self.households,
                                            "job":self.jobs,
                                            "building":self.buildings,
                                            "building_type": self.building_types,
                                            "job_building_type": self.job_building_types})

        self.building_categories = {'commercial': array([1000,5000]),
                                    'industrial': array([500,800,1000])}

    def test_no_development_with_zero_target_vacancy( self ):
        """If the target vacany ratest are 0%, then no development should occur and thus,
        the building set should remain unchanged (should have the same size).
        """

        """specify that the target vacancies for the year 2000 should be 0% for
        commercial building type."""
        storage = StorageFactory().get_storage('dict_storage')

        target_vacancies_table_name = 'target_vacancies'
        storage.write_table(
            table_name=target_vacancies_table_name,
            table_data={
                "year":array( [2000] ),
                "target_total_commercial_vacancy":array( [0.0] )
                }
            )
        target_vacancies = TargetVacancyDataset(in_storage=storage, in_table_name=target_vacancies_table_name)

        nbuildings = self.buildings.size()
        btm = BuildingTransitionModel()
        results = btm.run(self.buildings, self.building_types,
                           target_vacancies,
                           2000,
                           self.gridcells, building_categories=self.building_categories,
                           dataset_pool=self.dataset_pool)

        self.assertEqual( results, 0, "No buildings should've been added/developed" )
        self.assertEqual( nbuildings, self.buildings.size(), "No buildings should've been added/developed" )

    def test_development_with_nonzero_target_vacancy( self ):
        """Test basic cases, where current single family vacancy = 50%, target single family vacancy is 75%,
        current commercial vacancy is 75%, and target commercial vacancy is 50%.
        Single family development projects should occur, and none for commercial"""

        storage = StorageFactory().get_storage('dict_storage')

        target_vacancies_table_name = 'target_vacancies'
        storage.write_table(
            table_name=target_vacancies_table_name,
            table_data={
                "year":array( [2001], dtype=int32 ),
                "target_total_single_family_vacancy":array( [0.75] ),
                "target_total_commercial_vacancy":array( [0.50] )
                }
            )
        target_vacancies = TargetVacancyDataset(in_storage=storage, in_table_name=target_vacancies_table_name)

        resunits_before, commercial_before, industrial_before, tmp1, tmp2, tmp3 = self.get_residential_commercial_industrial_units(self.buildings)

        btm = BuildingTransitionModel()
        results = btm.run(self.buildings, self.building_types,
                           target_vacancies,
                           2001,
                           self.gridcells, building_categories=self.building_categories,
                           dataset_pool=self.dataset_pool )

        """20000 residential units should've been added because current ratio of
        10000 unoccupied / 20000 total = 0.5, and target residential vacancy rate
        is 0.75. add 20000 to numerator and denominator, and 30000 / 40000 = 0.75"""
        resunits_after, commercial_after, industrial_after, tmp1, tmp2, tmp3 = self.get_residential_commercial_industrial_units(self.buildings)

        self.assertEqual( resunits_after-resunits_before, 20000,
                         """Exactly 20000 residential units should've been added/developed.
                         Instead, got %s""" % ( resunits_after-resunits_before, ) )

        """Anytime the target vacancy rate is less than the current vacancy rate,
        no new development should occur."""
        self.assertEqual( commercial_before - commercial_after, 0,
                         "No commercial units should've been added/developed." )

        self.assertEqual( industrial_before-industrial_after, 0,
                         "No industrial units should've been added/developed." )

        """Check categories"""
        self.assertEqual(ma.allequal(self.buildings.get_categories("commercial"), self.building_categories["commercial"]), True,
                         "Error in creating categories for commercial buildings.")
        self.assertEqual(ma.allequal(self.buildings.get_categories("industrial"), self.building_categories["industrial"]), True,
                         "Error in creating categories for industrial buildings.")

    def test_development_with_99_percent_target_vacancy( self ):
        """Not too different from the basic case above, just trying the other extreme.
        Notice that a 100% target vacancy rate doesn't really make sense and is not possible unless
        the current vacancy rate is also 100% (also not feasible)."""

        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(
            table_name='target_vacancies',
            table_data={
                'year':array([2001], dtype=int32),
                'target_total_single_family_vacancy':array([0.99]),
                'target_total_commercial_vacancy':array([0.99]),
                'target_total_industrial_vacancy':array([0.99])
                },
            )
        target_vacancies = TargetVacancyDataset(in_storage=storage, in_table_name='target_vacancies')

        resunits_before, commercial_before, industrial_before, tmp1, tmp2, tmp3 = self.get_residential_commercial_industrial_units(self.buildings)

        btm = BuildingTransitionModel()
        results = btm.run(self.buildings, self.building_types,
                           target_vacancies,
                           2001,
                           self.gridcells, building_categories=self.building_categories,
                           dataset_pool=self.dataset_pool)

        """20000 residential units should've been added because current ratio of
        10000 unoccupied / 20000 total = 0.5, and target residential vacancy rate
        is 0.75. add 20000 to numerator and denominator, and 30000 / 40000 = 0.75"""
        resunits_after, commercial_after, industrial_after, tmp1, tmp2, tmp3 = self.get_residential_commercial_industrial_units(self.buildings)

        """
        .01 = 10000 / (20000 + x)

        x = (10000 - (.01*20000))/.01
        """
        residential_units_developed = (10000 - (.01*20000))/.01
        max_difference = 50
        self.assert_(self.is_close(resunits_after - resunits_before, residential_units_developed, max_difference),
                         """Approximately %s residential units should've been added/developed.
                         Instead, got %s""" % (residential_units_developed, resunits_after - resunits_before))

        """
        2500 commercial jobs * 100 occupied square feet per commercial job is
        250,000 commercial square feet occupied

        250,000 / (1,000,000 + x) = .01

        which converts into:
        x = (250,000 - .01*1,000,000)/.01

        x = 24,000,000
        """
        commercial_sqft_developed = (250000 - (.01*1000000))/.01
        max_difference = 5000
        self.assert_(self.is_close(commercial_after - commercial_before, commercial_sqft_developed, max_difference),
                         """Approximately %s commercial sqft should've been added/developed.
                         Instead, got %s""" % (commercial_sqft_developed, commercial_after - commercial_before))

        self.assertEqual(industrial_before - industrial_after, 0,
                         "No industrial units should've been added/developed.")

    def get_residential_commercial_industrial_units(self, buildings):
        resunits = buildings.get_attribute("residential_units").sum()
        buildings.compute_variables([
                  "urbansim.building.is_building_type_commercial", "urbansim.building.is_building_type_industrial",
                  "urbansim.building.is_building_type_single_family"],
                                         dataset_pool=self.dataset_pool)
        commercial = (buildings.get_attribute("sqft")*buildings.get_attribute("is_building_type_commercial")).sum()
        industrial = (buildings.get_attribute("sqft")*buildings.get_attribute("is_building_type_industrial")).sum()
        return (resunits, commercial, industrial,
                buildings.get_attribute("is_building_type_single_family").sum(),
                buildings.get_attribute("is_building_type_commercial").sum(),
                buildings.get_attribute("is_building_type_industrial").sum())

    def is_close(self, first_value, second_value, max_difference):
        return abs(first_value - second_value) <= max_difference
aggr_var2 = "neighborhood.aggregate(gridcell.capacity, intermediates=[zone], function=sum)"
neighborhoods.compute_variables(aggr_var2, dataset_pool=dataset_pool)

neighborhoods.add_primary_attribute(name="is_cbd", data=[0,0,1])
disaggr_var = "is_cbd = gridcell.disaggregate(neighborhood.is_cbd, intermediates=[zone])"
locations.compute_variables(disaggr_var, dataset_pool=dataset_pool)

from opus_core.datasets.alldata_dataset import AlldataDataset
alldata = AlldataDataset(dataset_name="alldata")
alldata.compute_variables(
                      "total_capacity = alldata.aggregate_all(gridcell.capacity, function=sum)",
                      dataset_pool=dataset_pool)

# Number of agents
households.modify_attribute(name="location", data=[2, 8, 3, 1, 5, 4, 9, 7, 3, 6])
dataset_pool.add_datasets_if_not_included({'household': households})
locations.compute_variables("gridcell.number_of_agents(household)",
                        dataset_pool=dataset_pool)
neighborhoods.compute_variables("neighborhood.number_of_agents(zone)",
                            dataset_pool=dataset_pool)

# Creating a model
from opus_core.model import Model
from opus_core.logger import logger
class MyModel(Model):
    model_name = "my model"
    def run(self):
        logger.log_status("I'm running!")
        return

MyModel().run()