def run_model(): hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set=gridcells, compute_capacity_flag=False, choices="opus_core.random_choices_from_index", sample_size_locations=8) hlcm.run(specification, coefficients, agent_set=households, debuglevel=1) # get results gridcells.compute_variables( ["urbansim.gridcell.number_of_households"], resources=Resources({"household": households})) result_more_attractive = gridcells.get_attribute_by_id( "number_of_households", arange(ngcs_attr) + 1) result_less_attractive = gridcells.get_attribute_by_id( "number_of_households", arange(ngcs_attr + 1, ngcs + 1)) households.set_values_of_one_attribute(attribute="grid_id", values=hh_grid_ids) gridcells.delete_one_attribute("number_of_households") result = concatenate( (result_more_attractive, result_less_attractive)) return result
def run_model_2(): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='households', table_data=household_data) households = HouseholdDataset(in_storage=storage, in_table_name='households') storage.write_table(table_name='gridcells', table_data=gridcell_data) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set=gridcells, compute_capacity_flag=False, choices="opus_core.random_choices_from_index", sample_size_locations=8) hlcm.run(specification, coefficients, agent_set=households, debuglevel=1) # get results gridcells.compute_variables( ["urbansim.gridcell.number_of_households"], resources=Resources({"household": households})) result_more_attractive = gridcells.get_attribute_by_id( "number_of_households", arange(ngcs_attr) + 1) result_less_attractive = gridcells.get_attribute_by_id( "number_of_households", arange(ngcs_attr + 1, ngcs + 1)) return array( [result_more_attractive.sum(), result_less_attractive.sum()])
def test_unplaced_agents_decrease_available_space(self): """Using the household location choice model, create a set of available spaces and 2000 unplaced agents (along with 5000 placed agents). Run the model, and check that the unplaced agents were placed, and the number of available spaces has decreased""" storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='households', table_data={ 'grid_id': array(2000 * [0] + 5000 * [1]), 'household_id': arange(7000) + 1 }) storage.write_table(table_name='gridcells', table_data={ 'residential_units': array(50 * [10000]), 'grid_id': arange(50) + 1 }) households = HouseholdDataset(in_storage=storage, in_table_name='households') gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') coefficients = Coefficients(names=("dummy", ), values=(0.1, )) specification = EquationSpecification( variables=("gridcell.residential_units", ), coefficients=("dummy", )) """need to specify to the household location choice model exactly which households are moving, because by default it assumes all current households want to move, but in this test, the 5000 households already in gridcell #1 shouldn't move. here, we specify that only the unplaced households should be moved.""" agents_index = where(households.get_attribute("grid_id") == 0)[0] hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set=gridcells, choices="opus_core.random_choices_from_index", sample_size_locations=30) hlcm.run(specification, coefficients, agent_set=households, agents_index=agents_index, debuglevel=1) gridcells.compute_variables( ["urbansim.gridcell.vacant_residential_units"], resources=Resources({"household": households})) vacancies = gridcells.get_attribute("vacant_residential_units") """since there were 5000 households already in gridcell #1, and gridcell #1 has 10000 residential units, there should be no more than 5000 vacant residential units in gridcell #1 after running this model""" self.assertEqual(vacancies[0] <= 5000, True, "Error: %d" % (vacancies[0], )) """there should be exactly 430000 vacant residential units after the model run, because there were originally 50 gridcells with 10000 residential units each, and a total of 7000 units are occupied after the run""" self.assertEqual( sum(vacancies) == 50 * 10000 - 7000, True, "Error: %d" % (sum(vacancies)))
def run_ALCM(niter): nhhs = 100 ngcs = 10 ngcs_attr = ngcs/2 ngcs_noattr = ngcs - ngcs_attr hh_grid_ids = array(nhhs*[-1]) storage = StorageFactory().get_storage('dict_storage') households_table_name = 'households' storage.write_table( table_name = households_table_name, table_data = { 'household_id': arange(nhhs)+1, 'grid_id': hh_grid_ids } ) gridcells_table_name = 'gridcells' storage.write_table( table_name = gridcells_table_name, table_data = { 'grid_id': arange(ngcs)+1, 'cost':array(ngcs_attr*[100]+ngcs_noattr*[1000]) } ) households = HouseholdDataset(in_storage=storage, in_table_name=households_table_name) gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name) # create coefficients and specification coefficients = Coefficients(names=('costcoef', ), values=(-0.001,)) specification = EquationSpecification(variables=('gridcell.cost', ), coefficients=('costcoef', )) logger.be_quiet() result = zeros((niter,ngcs)) for iter in range(niter): hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False, choices = 'opus_core.random_choices_from_index', sampler=None, #sample_size_locations = 30 ) hlcm.run(specification, coefficients, agent_set=households, debuglevel=1, chunk_specification={'nchunks':1}) # get results gridcells.compute_variables(['urbansim.gridcell.number_of_households'], resources=Resources({'household':households})) result_more_attractive = gridcells.get_attribute_by_id('number_of_households', arange(ngcs_attr)+1) result_less_attractive = gridcells.get_attribute_by_id('number_of_households', arange(ngcs_attr+1, ngcs+1)) households.set_values_of_one_attribute(attribute='grid_id', values=hh_grid_ids) gridcells.delete_one_attribute('number_of_households') result[iter,:] = concatenate((result_more_attractive, result_less_attractive)) #print result #, result_more_attractive.sum(), result_less_attractive.sum() return result
def run_model2(): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name = 'households', table_data = household_data) households = HouseholdDataset(in_storage=storage, in_table_name='households') storage.write_table(table_name = 'gridcells', table_data = gridcell_data) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False, choices = "opus_core.random_choices_from_index", sample_size_locations = 30) hlcm.run(specification, coefficients, agent_set = households, run_config=Resources({"demand_string":"gridcell.housing_demand"})) return gridcells.get_attribute("housing_demand")
def test_agents_do_not_go_to_inferior_locations(self): """100 gridcells - 99 with attractiveness 2000, 1 with attractiveness 1, no capacity restrictions 10,000 households We set the coefficient value for attracitiveness to 1. """ storage = StorageFactory().get_storage('dict_storage') #create households storage.write_table(table_name='households', table_data={ 'household_id': arange(10000) + 1, 'grid_id': array(10000 * [-1]) }) households = HouseholdDataset(in_storage=storage, in_table_name='households') # create gridcells storage.write_table(table_name='gridcells', table_data={ 'grid_id': arange(100) + 1, 'attractiveness': array(99 * [2000] + [1]) }) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') # create coefficients and specification coefficients = Coefficients(names=("attractcoef", ), values=(1, )) specification = EquationSpecification( variables=("gridcell.attractiveness", ), coefficients=("attractcoef", )) # run the model hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set=gridcells, compute_capacity_flag=False, choices="opus_core.random_choices_from_index", sample_size_locations=30) hlcm.run(specification, coefficients, agent_set=households, debuglevel=1) # get results gridcells.compute_variables(["urbansim.gridcell.number_of_households"], resources=Resources( {"household": households})) result = gridcells.get_attribute_by_id("number_of_households", 100) # nobody should choose gridcell 100 self.assertEqual(result, 0, "Error: %s is not equal to 0" % (result, ))
def test_unplaced_agents_decrease_available_space(self): """Using the household location choice model, create a set of available spaces and 2000 unplaced agents (along with 5000 placed agents). Run the model, and check that the unplaced agents were placed, and the number of available spaces has decreased""" storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='households', table_data = { 'grid_id': array(2000*[0] + 5000*[1]), 'household_id': arange(7000)+1 } ) storage.write_table(table_name='gridcells', table_data= { 'residential_units':array(50*[10000]), 'grid_id': arange(50)+1 } ) households = HouseholdDataset(in_storage=storage, in_table_name='households') gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') coefficients = Coefficients(names=("dummy",), values=(0.1,)) specification = EquationSpecification(variables=("gridcell.residential_units",), coefficients=("dummy",)) """need to specify to the household location choice model exactly which households are moving, because by default it assumes all current households want to move, but in this test, the 5000 households already in gridcell #1 shouldn't move. here, we specify that only the unplaced households should be moved.""" agents_index = where(households.get_attribute("grid_id") == 0)[0] hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, choices = "opus_core.random_choices_from_index", sample_size_locations = 30) hlcm.run(specification, coefficients, agent_set=households, agents_index=agents_index, debuglevel=1) gridcells.compute_variables(["urbansim.gridcell.vacant_residential_units"], resources=Resources({"household":households})) vacancies = gridcells.get_attribute("vacant_residential_units") """since there were 5000 households already in gridcell #1, and gridcell #1 has 10000 residential units, there should be no more than 5000 vacant residential units in gridcell #1 after running this model""" self.assertEqual(vacancies[0] <= 5000, True, "Error: %d" % (vacancies[0],)) """there should be exactly 430000 vacant residential units after the model run, because there were originally 50 gridcells with 10000 residential units each, and a total of 7000 units are occupied after the run""" self.assertEqual(sum(vacancies) == 50 * 10000 - 7000, True, "Error: %d" % (sum(vacancies)))
def run_model(): hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False, choices = "opus_core.random_choices_from_index", sample_size_locations = 8) hlcm.run(specification, coefficients, agent_set=households, debuglevel=1) # get results gridcells.compute_variables(["urbansim.gridcell.number_of_households"], resources=Resources({"household":households})) result_more_attractive = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr)+1) result_less_attractive = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr+1, ngcs+1)) households.set_values_of_one_attribute(attribute="grid_id", values=hh_grid_ids) gridcells.delete_one_attribute("number_of_households") result = concatenate((result_more_attractive, result_less_attractive)) return result
def run_model1(): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name = 'households', table_data = household_data) households = HouseholdDataset(in_storage=storage, in_table_name='households') storage.write_table(table_name = 'gridcells', table_data = gridcell_data) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False, choices = "opus_core.random_choices_from_index", sample_size_locations = 30) hlcm.run(specification, coefficients, agent_set = households) gridcells.compute_variables(["urbansim.gridcell.number_of_households"], resources=Resources({"household":households})) return gridcells.get_attribute("number_of_households")
def test_do_nothing_if_no_agents(self): storage = StorageFactory().get_storage('dict_storage') households_table_name = 'households' storage.write_table(table_name=households_table_name, table_data={ "household_id": arange(10000) + 1, "grid_id": array(10000 * [-1]) }) households = HouseholdDataset(in_storage=storage, in_table_name=households_table_name) gridcells_table_name = 'gridcells' storage.write_table(table_name=gridcells_table_name, table_data={ "grid_id": arange(100) + 1, "cost": array(50 * [100] + 50 * [1000]) }) gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name) # create coefficients and specification coefficients = Coefficients(names=("costcoef", ), values=(-0.001, )) specification = EquationSpecification(variables=("gridcell.cost", ), coefficients=("costcoef", )) # run the model hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set=gridcells, compute_capacity_flag=False, choices="opus_core.random_choices_from_index", sample_size_locations=30) hlcm.run(specification, coefficients, agent_set=households, agents_index=array([], dtype='int32'), debuglevel=1) # get results gridcells.compute_variables(["urbansim.gridcell.number_of_households"], resources=Resources( {"household": households})) result = gridcells.get_attribute("number_of_households") # check the individual gridcells self.assertEqual(ma.allclose(result, zeros((100, )), rtol=0), True)
def xtest_gracefully_handle_empty_choice_sets(self): storage = StorageFactory().get_storage('dict_storage') #create households storage.write_table(table_name='households', table_data={ 'household_id': arange(10000) + 1, 'grid_id': array(100 * range(100)) + 1 }) households = HouseholdDataset(in_storage=storage, in_table_name='households') # create gridcells storage.write_table(table_name='gridcells', table_data={ 'grid_id': arange(100) + 1, 'residential_units': array(100 * [100]) }) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') # create coefficients and specification coefficients = Coefficients(names=("dummy", ), values=(0, )) specification = EquationSpecification( variables=("gridcell.residential_units", ), coefficients=("dummy", )) # run the model hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set=gridcells, choices="opus_core.random_choices_from_index", sample_size_locations=30) hlcm.run(specification, coefficients, agent_set=households, debuglevel=1) # get results gridcells.compute_variables(["urbansim.gridcell.number_of_households"], resources=Resources( {"household": households})) result = gridcells.get_attribute_by_id("number_of_households", 100) # nobody should choose gridcell 100 self.assertEqual(ma.allclose(result.sum(), 0, rtol=0), True, "Error: %s is not equal to 0" % (result.sum(), ))
def run_model_2(): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name = 'households', table_data = household_data) households = HouseholdDataset(in_storage=storage, in_table_name='households') storage.write_table(table_name = 'gridcells', table_data = gridcell_data) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False, choices = "opus_core.random_choices_from_index", sample_size_locations = 8) hlcm.run(specification, coefficients, agent_set=households, debuglevel=1) # get results gridcells.compute_variables(["urbansim.gridcell.number_of_households"], resources=Resources({"household":households})) result_more_attractive = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr)+1) result_less_attractive = gridcells.get_attribute_by_id("number_of_households", arange(ngcs_attr+1, ngcs+1)) return array([result_more_attractive.sum(), result_less_attractive.sum()])
def test_agents_do_not_go_to_inferior_locations(self): """100 gridcells - 99 with attractiveness 2000, 1 with attractiveness 1, no capacity restrictions 10,000 households We set the coefficient value for attracitiveness to 1. """ storage = StorageFactory().get_storage('dict_storage') #create households storage.write_table(table_name='households', table_data = { 'household_id': arange(10000)+1, 'grid_id': array(10000*[-1]) } ) households = HouseholdDataset(in_storage=storage, in_table_name='households') # create gridcells storage.write_table(table_name='gridcells', table_data = { 'grid_id': arange(100)+1, 'attractiveness':array(99*[2000]+[1]) } ) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') # create coefficients and specification coefficients = Coefficients(names=("attractcoef", ), values=(1,)) specification = EquationSpecification(variables=("gridcell.attractiveness", ), coefficients=("attractcoef", )) # run the model hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False, choices = "opus_core.random_choices_from_index", sample_size_locations = 30) hlcm.run(specification, coefficients, agent_set = households, debuglevel=1) # get results gridcells.compute_variables(["urbansim.gridcell.number_of_households"], resources=Resources({"household":households})) result = gridcells.get_attribute_by_id("number_of_households", 100) # nobody should choose gridcell 100 self.assertEqual(result, 0, "Error: %s is not equal to 0" % (result,))
def test_do_nothing_if_no_agents(self): storage = StorageFactory().get_storage('dict_storage') households_table_name = 'households' storage.write_table( table_name = households_table_name, table_data = { "household_id": arange(10000)+1, "grid_id": array(10000*[-1]) } ) households = HouseholdDataset(in_storage=storage, in_table_name=households_table_name) gridcells_table_name = 'gridcells' storage.write_table( table_name = gridcells_table_name, table_data = { "grid_id": arange(100)+1, "cost":array(50*[100]+50*[1000]) } ) gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name) # create coefficients and specification coefficients = Coefficients(names=("costcoef", ), values=(-0.001,)) specification = EquationSpecification(variables=("gridcell.cost", ), coefficients=("costcoef", )) # run the model hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False, choices = "opus_core.random_choices_from_index", sample_size_locations = 30) hlcm.run(specification, coefficients, agent_set = households, agents_index=array([], dtype='int32'), debuglevel=1) # get results gridcells.compute_variables(["urbansim.gridcell.number_of_households"], resources=Resources({"household":households})) result = gridcells.get_attribute("number_of_households") # check the individual gridcells self.assertEqual(ma.allclose(result, zeros((100,)) , rtol=0), True)
def xtest_gracefully_handle_empty_choice_sets(self): storage = StorageFactory().get_storage('dict_storage') #create households storage.write_table(table_name='households', table_data = { 'household_id': arange(10000)+1, 'grid_id': array(100*range(100))+1 } ) households = HouseholdDataset(in_storage=storage, in_table_name='households') # create gridcells storage.write_table(table_name='gridcells', table_data = { 'grid_id': arange(100)+1, 'residential_units':array(100*[100]) } ) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') # create coefficients and specification coefficients = Coefficients(names=("dummy",), values=(0,)) specification = EquationSpecification(variables=("gridcell.residential_units",), coefficients=("dummy",)) # run the model hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set=gridcells, choices = "opus_core.random_choices_from_index", sample_size_locations = 30) hlcm.run(specification, coefficients, agent_set=households, debuglevel=1) # get results gridcells.compute_variables(["urbansim.gridcell.number_of_households"], resources=Resources({"household":households})) result = gridcells.get_attribute_by_id("number_of_households", 100) # nobody should choose gridcell 100 self.assertEqual(ma.allclose(result.sum(), 0 , rtol=0), True, "Error: %s is not equal to 0" % (result.sum(),))
def run_model1(): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='households', table_data=household_data) households = HouseholdDataset(in_storage=storage, in_table_name='households') storage.write_table(table_name='gridcells', table_data=gridcell_data) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set=gridcells, compute_capacity_flag=False, choices="opus_core.random_choices_from_index", sample_size_locations=30) hlcm.run(specification, coefficients, agent_set=households) gridcells.compute_variables( ["urbansim.gridcell.number_of_households"], resources=Resources({"household": households})) return gridcells.get_attribute("number_of_households")
def run_model2(): storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='households', table_data=household_data) households = HouseholdDataset(in_storage=storage, in_table_name='households') storage.write_table(table_name='gridcells', table_data=gridcell_data) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set=gridcells, compute_capacity_flag=False, choices="opus_core.random_choices_from_index", sample_size_locations=30) hlcm.run(specification, coefficients, agent_set=households, run_config=Resources( {"demand_string": "gridcell.housing_demand"})) return gridcells.get_attribute("housing_demand")
def estimate(self, spec_var=None, spec_py=None, movers_index = None, submodel_string = "", alt_sample_size=None, sampler = "opus_core.samplers.weighted_sampler", weight_string = "supply", aggregate_demand = False, submarket_definition = ('zone', 'building_type_id'), sample_size_from_each_stratum = 50 ): """ """ t1 = time() SimulationState().set_current_time(2000) dataset_pool=SessionConfiguration().get_dataset_pool() buildings = dataset_pool.get_dataset("building") agent_set = dataset_pool.get_dataset('household') #buildings.load_dataset() submarket_geography = dataset_pool.get_dataset(submarket_definition[0]) intermediates = '[]' if submarket_geography.dataset_name == 'zone': intermediates = '[parcel]' elif submarket_geography.dataset_name == 'faz': intermediates = '[zone, parcel]' elif submarket_geography.dataset_name == 'large_area': intermediates = '[faz, zone, parcel]' submarket_id_expression = 'building.disaggregate(%s.%s, intermediates=%s) * 100' % \ (submarket_geography.dataset_name, submarket_geography.get_id_name()[0], intermediates) submarket_variables = ['%s=numpy.ceil(submarket.submarket_id / 100)' % submarket_geography.get_id_name()[0]] if submarket_definition[1] == 'residential_building_type_id': set_residential_building_types(dataset_pool.get_dataset("building_type"), dataset_pool.get_dataset("building")) if submarket_definition[1] != '': submarket_id_expression = submarket_id_expression + ' + building.%s' % submarket_definition[1] submarket_variables.append(submarket_definition[1] + '=submarket.submarket_id % 100' ) submarkets = define_submarket(buildings, submarket_id_expression, #"urbansim_parcel.building.zone_id*100 + building.residential_building_type_id", #"building.disaggregate(faz.large_area_id, intermediates=[zone, parcel]) * 100 + building.residential_building_type_id", compute_variables=submarket_variables + [ "residential_units=submarket.aggregate(building.residential_units)", "number_of_buildings_with_non_zero_units=submarket.aggregate(building.residential_units > 0 )", "number_of_surveyed_households=submarket.aggregate(household.household_id > 5000000, intermediates=[building])", ], #filter = 'numpy.logical_and(submarket.number_of_surveyed_households > 0, submarket.residential_units>0)', #filter = 'submarket.supply > 0', #"psrc_parcel.building.large_area_id*100 + building.residential_building_type_id", #compute_variables=['residential_building_type_id=submarket.submarket_id % 100', #'large_area_id=numpy.ceil(submarket.submarket_id / 100)'] #"psrc_parcel.building.large_area_id", #compute_variables=[#'residential_building_type_id=submarket.submarket_id % 100', #'large_area_id=numpy.ceil(submarket.submarket_id)'] ) dataset_pool.add_datasets_if_not_included({'submarket':submarkets}) compute_lambda_and_supply(buildings, agent_set, movers_index, submarkets) submarket_filter = 'submarket.supply > 0' if submarket_filter is not None: from numpy import logical_not submarkets.remove_elements(index= where( logical_not(submarkets.compute_variables(submarket_filter)) )[0]) submarkets.touch_attribute(submarkets.get_id_name()[0]) buildings.touch_attribute(submarkets.get_id_name()[0]) if self.save_estimation_results: out_storage = StorageFactory().build_storage_for_dataset(type='sql_storage', storage_location=self.out_con) if spec_py is not None: reload(spec_py) spec_var = spec_py.specification if spec_var is not None: self.specification = load_specification_from_dictionary(spec_var) else: in_storage = StorageFactory().build_storage_for_dataset(type='sql_storage', storage_location=self.in_con) self.specification = EquationSpecification(in_storage=in_storage) self.specification.load(in_table_name="household_location_choice_model_specification") self.model_name = "household_location_choice_model" agent_set, agents_index_for_estimation = get_households_for_estimation(agent_set, AttributeCache(), "households_for_estimation", exclude_condition="household.disaggregate(submarket.submarket_id, intermediates=[building])<=0", ) agent_set.compute_variables("submarket_id=household.disaggregate(building.submarket_id)") agent_sample_rate = agents_index_for_estimation.size / float(movers_index.size) dataset_pool.add_datasets_if_not_included({'sample_rate': agent_sample_rate }) if aggregate_demand: location_set = buildings aggregate_dataset = 'submarket' #weight_string = 'inv_submarket_supply = 1.0 / (building.disaggregate(submarket.number_of_agents(building))).astype(float32) * (building.disaggregate(submarket.submarket_id) > 0)' #weight_string = 'submarket_supply = (building.disaggregate(submarket.supply) > 0).astype(int32)' #weight_string = 'submarket_supply = building.disaggregate(submarket.supply) * (building.disaggregate(submarket.submarket_id) > 0).astype(float32)' else: location_set = submarkets aggregate_dataset = None #weight_string = 'supply' model = HouseholdLocationChoiceModelCreator().get_model(location_set=location_set, #location_set=submarkets, #filter = 'building.disaggregate(submarket.submarket_id) > 0', #filter = 'numpy.logical_and(submarket.number_of_surveyed_households > 0, submarket.residential_units>0)', #filter = 'building.disaggregate(numpy.logical_and(submarket.number_of_buildings_with_non_zero_units > 5000, submarket.number_of_surveyed_households > 0))', submodel_string=submodel_string, sampler = sampler, #estimation_size_agents = agent_sample_rate * 100/20, # proportion of the agent set that should be used for the estimation sample_size_locations = alt_sample_size, #sample_proportion_locations = 1.0/1000, # choice set size (includes current location) compute_capacity_flag = True, probabilities = "opus_core.mnl_probabilities", choices = "urbansim.lottery_choices", #run_config = Resources({"capacity_string":"supply"}), estimate_config = Resources({"capacity_string":"supply", "weights_for_estimation_string":weight_string, "aggregate_to_dataset":aggregate_dataset, "stratum": "building.disaggregate(submarket.submarket_id)", "sample_size_from_each_stratum": sample_size_from_each_stratum, #"index2":where(submarkets.compute_variables('submarket.number_of_surveyed_households > 0'))[0], #"sample_rate": 1.0/5000, #"sample_size_from_chosen_stratum": 0, "include_chosen_choice": True })) # was dataset_pool.add_datasets_if_not_included({'sample_rate':agent_sample_rate}) self.result = model.estimate(self.specification, agent_set=agent_set, agents_index=agents_index_for_estimation, debuglevel=self.debuglevel, procedure="urbansim.constrain_estimation_bhhh_two_loops" ) #"urbansim.constrain_estimation_bhhh" #save estimation results if self.save_estimation_results: self.save_results(out_storage) logger.log_status("Estimation done. " + str(time()-t1) + " s")
def run_ALCM(niter): nhhs = 100 ngcs = 10 ngcs_attr = ngcs / 2 ngcs_noattr = ngcs - ngcs_attr hh_grid_ids = array(nhhs * [-1]) storage = StorageFactory().get_storage('dict_storage') households_table_name = 'households' storage.write_table(table_name=households_table_name, table_data={ 'household_id': arange(nhhs) + 1, 'grid_id': hh_grid_ids }) gridcells_table_name = 'gridcells' storage.write_table(table_name=gridcells_table_name, table_data={ 'grid_id': arange(ngcs) + 1, 'cost': array(ngcs_attr * [100] + ngcs_noattr * [1000]) }) households = HouseholdDataset(in_storage=storage, in_table_name=households_table_name) gridcells = GridcellDataset(in_storage=storage, in_table_name=gridcells_table_name) # create coefficients and specification coefficients = Coefficients(names=('costcoef', ), values=(-0.001, )) specification = EquationSpecification(variables=('gridcell.cost', ), coefficients=('costcoef', )) logger.be_quiet() result = zeros((niter, ngcs)) for iter in range(niter): hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set=gridcells, compute_capacity_flag=False, choices='opus_core.random_choices_from_index', sampler=None, #sample_size_locations = 30 ) hlcm.run(specification, coefficients, agent_set=households, debuglevel=1, chunk_specification={'nchunks': 1}) # get results gridcells.compute_variables(['urbansim.gridcell.number_of_households'], resources=Resources( {'household': households})) result_more_attractive = gridcells.get_attribute_by_id( 'number_of_households', arange(ngcs_attr) + 1) result_less_attractive = gridcells.get_attribute_by_id( 'number_of_households', arange(ngcs_attr + 1, ngcs + 1)) households.set_values_of_one_attribute(attribute='grid_id', values=hh_grid_ids) gridcells.delete_one_attribute('number_of_households') result[iter, :] = concatenate( (result_more_attractive, result_less_attractive)) #print result #, result_more_attractive.sum(), result_less_attractive.sum() return result
def estimate(self, spec_var=None, spec_py=None, submodel_string="workers", agent_sample_rate=0.005, alt_sample_size=None): """ """ CLOSE = 0.001 sampler = "opus_core.samplers.weighted_sampler" if alt_sample_size == None: sampler = None date_time_str = strftime("%Y_%m_%d__%H_%M", localtime()) agent_sample_rate_str = "__ASR_" + str(agent_sample_rate) alt_sample_size_str = "_ALT_" + str(alt_sample_size) info_file = date_time_str + agent_sample_rate_str + alt_sample_size_str + "__info.txt" logger.enable_file_logging(date_time_str + agent_sample_rate_str + alt_sample_size_str + "__run.txt") logger.enable_memory_logging() logger.log_status("Constrained Estimation with agent sample rate of %s and alternatvie sample size %s\n" % \ (agent_sample_rate, alt_sample_size)) t1 = time() SimulationState().set_current_time(2000) self.nbs = SessionConfiguration().get_dataset_from_pool("neighborhood") self.hhs = SessionConfiguration().get_dataset_from_pool('household') depts, lambda_value = compute_lambda(self.nbs) supply, vacancy_rate = compute_supply_and_vacancy_rate( self.nbs, depts, lambda_value) self.nbs.set_values_of_one_attribute("supply", supply) dataset_pool = SessionConfiguration().get_dataset_pool() dataset_pool.add_datasets_if_not_included({ 'vacancy_rate': vacancy_rate, 'sample_rate': agent_sample_rate }) SessionConfiguration()["CLOSE"] = CLOSE SessionConfiguration()['info_file'] = info_file if self.save_estimation_results: out_storage = StorageFactory().build_storage_for_dataset( type='sql_storage', storage_location=self.out_con) if spec_py is not None: reload(spec_py) spec_var = spec_py.specification if spec_var is not None: self.specification = load_specification_from_dictionary(spec_var) else: in_storage = StorageFactory().build_storage_for_dataset( type='sql_storage', storage_location=self.in_con) self.specification = EquationSpecification(in_storage=in_storage) self.specification.load( in_table_name="household_location_choice_model_specification") #submodel_string = "workers" seed(71) # was: seed(71,110) self.model_name = "household_location_choice_model" model = HouseholdLocationChoiceModelCreator().get_model( location_set=self.nbs, submodel_string=submodel_string, sampler=sampler, estimation_size_agents=agent_sample_rate * 100 / 20, # proportion of the agent set that should be used for the estimation, # sample_size_locations= alt_sample_size, # choice set size (includes current location) compute_capacity_flag=True, probabilities="opus_core.mnl_probabilities", choices="urbansim.lottery_choices", run_config=Resources({"capacity_string": "supply"}), estimate_config=Resources({ "capacity_string": "supply", "compute_capacity_flag": True })) #TODO: since households_for_estimation currently is the same as households, create_households_for_estimation #becomes unnecesarry #agent_set, agents_index_for_estimation = create_households_for_estimation(self.hhs, self.in_con) agent_set = self.hhs agents_index_for_estimation = arange(self.hhs.size()) self.result = model.estimate( self.specification, agent_set=agent_set, agents_index=agents_index_for_estimation, debuglevel=self.debuglevel, procedure="urbansim.constrain_estimation_bhhh_two_loops" ) #"urbansim.constrain_estimation_bhhh" #save estimation results if self.save_estimation_results: self.save_results(out_storage) logger.log_status("Estimation done. " + str(time() - t1) + " s")
# locations from gridcellset.tab locations= GridcellDataset(in_storage = StorageFactory().get_storage('tab_storage', storage_location = "."), in_table_name = "gridcellset", id_name="location") locations.summary() seed(1) coefficients = Coefficients(names=("costcoef", ), values=(-0.01,)) specification = EquationSpecification(variables=("gridcell.cost", ), coefficients=("costcoef", )) hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set = locations, sampler=None, utilities="opus_core.linear_utilities", probabilities="opus_core.mnl_probabilities", choices="opus_core.random_choices_from_index", compute_capacity_flag=False) agents.get_attribute("location") results = hlcm.run(specification, coefficients, agents) agents.get_attribute("location") hlcm.upc_sequence.plot_choice_histograms( capacity=locations.get_attribute("vacant_units")) hlcm.upc_sequence.show_plots() coef, results = hlcm.estimate(specification, agents) results = hlcm.run(specification, coef, agents) hlcm.upc_sequence.plot_choice_histograms(
'tab_storage', storage_location="."), in_table_name="gridcellset", id_name="location") locations.summary() seed(1) coefficients = Coefficients(names=("costcoef", ), values=(-0.01, )) specification = EquationSpecification(variables=("gridcell.cost", ), coefficients=("costcoef", )) hlcm = HouseholdLocationChoiceModelCreator().get_model( location_set=locations, sampler=None, utilities="opus_core.linear_utilities", probabilities="opus_core.mnl_probabilities", choices="opus_core.random_choices_from_index", compute_capacity_flag=False) agents.get_attribute("location") results = hlcm.run(specification, coefficients, agents) agents.get_attribute("location") hlcm.upc_sequence.plot_choice_histograms( capacity=locations.get_attribute("vacant_units")) hlcm.upc_sequence.show_plots() coef, results = hlcm.estimate(specification, agents) results = hlcm.run(specification, coef, agents) hlcm.upc_sequence.plot_choice_histograms(
def estimate(self, spec_var=None, spec_py=None, submodel_string = "workers", agent_sample_rate=0.005, alt_sample_size=None): """ """ CLOSE = 0.001 sampler = "opus_core.samplers.weighted_sampler" if alt_sample_size==None: sampler = None date_time_str=strftime("%Y_%m_%d__%H_%M", localtime()) agent_sample_rate_str = "__ASR_" + str(agent_sample_rate) alt_sample_size_str = "_ALT_" + str(alt_sample_size) info_file = date_time_str + agent_sample_rate_str + alt_sample_size_str + "__info.txt" logger.enable_file_logging(date_time_str + agent_sample_rate_str + alt_sample_size_str + "__run.txt") logger.enable_memory_logging() logger.log_status("Constrained Estimation with agent sample rate of %s and alternatvie sample size %s\n" % \ (agent_sample_rate, alt_sample_size)) t1 = time() SimulationState().set_current_time(2000) self.nbs = SessionConfiguration().get_dataset_from_pool("neighborhood") self.hhs = SessionConfiguration().get_dataset_from_pool('household') depts, lambda_value = compute_lambda(self.nbs) supply, vacancy_rate = compute_supply_and_vacancy_rate(self.nbs, depts, lambda_value) self.nbs.set_values_of_one_attribute("supply", supply) dataset_pool = SessionConfiguration().get_dataset_pool() dataset_pool.add_datasets_if_not_included({'vacancy_rate': vacancy_rate, 'sample_rate':agent_sample_rate }) SessionConfiguration()["CLOSE"] = CLOSE SessionConfiguration()['info_file'] = info_file if self.save_estimation_results: out_storage = StorageFactory().build_storage_for_dataset(type='sql_storage', storage_location=self.out_con) if spec_py is not None: reload(spec_py) spec_var = spec_py.specification if spec_var is not None: self.specification = load_specification_from_dictionary(spec_var) else: in_storage = StorageFactory().build_storage_for_dataset(type='sql_storage', storage_location=self.in_con) self.specification = EquationSpecification(in_storage=in_storage) self.specification.load(in_table_name="household_location_choice_model_specification") #submodel_string = "workers" seed(71) # was: seed(71,110) self.model_name = "household_location_choice_model" model = HouseholdLocationChoiceModelCreator().get_model(location_set=self.nbs, submodel_string=submodel_string, sampler = sampler, estimation_size_agents = agent_sample_rate * 100/20, # proportion of the agent set that should be used for the estimation, # sample_size_locations = alt_sample_size, # choice set size (includes current location) compute_capacity_flag = True, probabilities = "opus_core.mnl_probabilities", choices = "urbansim.lottery_choices", run_config = Resources({"capacity_string":"supply"}), estimate_config = Resources({"capacity_string":"supply","compute_capacity_flag":True})) #TODO: since households_for_estimation currently is the same as households, create_households_for_estimation #becomes unnecesarry #agent_set, agents_index_for_estimation = create_households_for_estimation(self.hhs, self.in_con) agent_set = self.hhs; agents_index_for_estimation = arange(self.hhs.size()) self.result = model.estimate(self.specification, agent_set=agent_set, agents_index=agents_index_for_estimation, debuglevel=self.debuglevel, procedure="urbansim.constrain_estimation_bhhh_two_loops" ) #"urbansim.constrain_estimation_bhhh" #save estimation results if self.save_estimation_results: self.save_results(out_storage) logger.log_status("Estimation done. " + str(time()-t1) + " s")
def estimate(self, spec_var=None, spec_py=None, movers_index=None, submodel_string="", alt_sample_size=None, sampler="opus_core.samplers.weighted_sampler", weight_string="supply", aggregate_demand=False, submarket_definition=('zone', 'building_type_id'), sample_size_from_each_stratum=50): """ """ t1 = time() SimulationState().set_current_time(2000) dataset_pool = SessionConfiguration().get_dataset_pool() buildings = dataset_pool.get_dataset("building") agent_set = dataset_pool.get_dataset('household') #buildings.load_dataset() submarket_geography = dataset_pool.get_dataset(submarket_definition[0]) intermediates = '[]' if submarket_geography.dataset_name == 'zone': intermediates = '[parcel]' elif submarket_geography.dataset_name == 'faz': intermediates = '[zone, parcel]' elif submarket_geography.dataset_name == 'large_area': intermediates = '[faz, zone, parcel]' submarket_id_expression = 'building.disaggregate(%s.%s, intermediates=%s) * 100' % \ (submarket_geography.dataset_name, submarket_geography.get_id_name()[0], intermediates) submarket_variables = [ '%s=numpy.ceil(submarket.submarket_id / 100)' % submarket_geography.get_id_name()[0] ] if submarket_definition[1] == 'residential_building_type_id': set_residential_building_types( dataset_pool.get_dataset("building_type"), dataset_pool.get_dataset("building")) if submarket_definition[1] != '': submarket_id_expression = submarket_id_expression + ' + building.%s' % submarket_definition[ 1] submarket_variables.append(submarket_definition[1] + '=submarket.submarket_id % 100') submarkets = define_submarket( buildings, submarket_id_expression, #"urbansim_parcel.building.zone_id*100 + building.residential_building_type_id", #"building.disaggregate(faz.large_area_id, intermediates=[zone, parcel]) * 100 + building.residential_building_type_id", compute_variables=submarket_variables + [ "residential_units=submarket.aggregate(building.residential_units)", "number_of_buildings_with_non_zero_units=submarket.aggregate(building.residential_units > 0 )", "number_of_surveyed_households=submarket.aggregate(household.household_id > 5000000, intermediates=[building])", ], #filter = 'numpy.logical_and(submarket.number_of_surveyed_households > 0, submarket.residential_units>0)', #filter = 'submarket.supply > 0', #"psrc_parcel.building.large_area_id*100 + building.residential_building_type_id", #compute_variables=['residential_building_type_id=submarket.submarket_id % 100', #'large_area_id=numpy.ceil(submarket.submarket_id / 100)'] #"psrc_parcel.building.large_area_id", #compute_variables=[#'residential_building_type_id=submarket.submarket_id % 100', #'large_area_id=numpy.ceil(submarket.submarket_id)'] ) dataset_pool.add_datasets_if_not_included({'submarket': submarkets}) compute_lambda_and_supply(buildings, agent_set, movers_index, submarkets) submarket_filter = 'submarket.supply > 0' if submarket_filter is not None: from numpy import logical_not submarkets.remove_elements(index=where( logical_not(submarkets.compute_variables(submarket_filter))) [0]) submarkets.touch_attribute(submarkets.get_id_name()[0]) buildings.touch_attribute(submarkets.get_id_name()[0]) if self.save_estimation_results: out_storage = StorageFactory().build_storage_for_dataset( type='sql_storage', storage_location=self.out_con) if spec_py is not None: reload(spec_py) spec_var = spec_py.specification if spec_var is not None: self.specification = load_specification_from_dictionary(spec_var) else: in_storage = StorageFactory().build_storage_for_dataset( type='sql_storage', storage_location=self.in_con) self.specification = EquationSpecification(in_storage=in_storage) self.specification.load( in_table_name="household_location_choice_model_specification") self.model_name = "household_location_choice_model" agent_set, agents_index_for_estimation = get_households_for_estimation( agent_set, AttributeCache(), "households_for_estimation", exclude_condition= "household.disaggregate(submarket.submarket_id, intermediates=[building])<=0", ) agent_set.compute_variables( "submarket_id=household.disaggregate(building.submarket_id)") agent_sample_rate = agents_index_for_estimation.size / float( movers_index.size) dataset_pool.add_datasets_if_not_included( {'sample_rate': agent_sample_rate}) if aggregate_demand: location_set = buildings aggregate_dataset = 'submarket' #weight_string = 'inv_submarket_supply = 1.0 / (building.disaggregate(submarket.number_of_agents(building))).astype(float32) * (building.disaggregate(submarket.submarket_id) > 0)' #weight_string = 'submarket_supply = (building.disaggregate(submarket.supply) > 0).astype(int32)' #weight_string = 'submarket_supply = building.disaggregate(submarket.supply) * (building.disaggregate(submarket.submarket_id) > 0).astype(float32)' else: location_set = submarkets aggregate_dataset = None #weight_string = 'supply' model = HouseholdLocationChoiceModelCreator().get_model( location_set=location_set, #location_set=submarkets, #filter = 'building.disaggregate(submarket.submarket_id) > 0', #filter = 'numpy.logical_and(submarket.number_of_surveyed_households > 0, submarket.residential_units>0)', #filter = 'building.disaggregate(numpy.logical_and(submarket.number_of_buildings_with_non_zero_units > 5000, submarket.number_of_surveyed_households > 0))', submodel_string=submodel_string, sampler=sampler, #estimation_size_agents = agent_sample_rate * 100/20, # proportion of the agent set that should be used for the estimation sample_size_locations=alt_sample_size, #sample_proportion_locations = 1.0/1000, # choice set size (includes current location) compute_capacity_flag=True, probabilities="opus_core.mnl_probabilities", choices="urbansim.lottery_choices", #run_config = Resources({"capacity_string":"supply"}), estimate_config=Resources({ "capacity_string": "supply", "weights_for_estimation_string": weight_string, "aggregate_to_dataset": aggregate_dataset, "stratum": "building.disaggregate(submarket.submarket_id)", "sample_size_from_each_stratum": sample_size_from_each_stratum, #"index2":where(submarkets.compute_variables('submarket.number_of_surveyed_households > 0'))[0], #"sample_rate": 1.0/5000, #"sample_size_from_chosen_stratum": 0, "include_chosen_choice": True })) # was dataset_pool.add_datasets_if_not_included({'sample_rate':agent_sample_rate}) self.result = model.estimate( self.specification, agent_set=agent_set, agents_index=agents_index_for_estimation, debuglevel=self.debuglevel, procedure="urbansim.constrain_estimation_bhhh_two_loops" ) #"urbansim.constrain_estimation_bhhh" #save estimation results if self.save_estimation_results: self.save_results(out_storage) logger.log_status("Estimation done. " + str(time() - t1) + " s")
def test_stochastic_test_case(self): """100 gridcells - 50 with cost 100, 50 with cost 1000, no capacity restrictions 10,000 households We set the coefficient value for cost -0.001. This leads to probability proportion 0.71 (less costly gridcells) to 0.29 (expensive gridcells) (derived from the logit formula) """ storage = StorageFactory().get_storage('dict_storage') nhouseholds = 10000 ngrids = 10 #create households storage.write_table(table_name='households', table_data={ 'household_id': arange(nhouseholds) + 1, 'grid_id': array(nhouseholds * [-1]) }) households = HouseholdDataset(in_storage=storage, in_table_name='households') # create gridcells storage.write_table(table_name='gridcells', table_data={ 'grid_id': arange(ngrids) + 1, 'cost': array(ngrids / 2 * [100] + ngrids / 2 * [1000]) }) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') # create coefficients and specification coefficients = Coefficients(names=("costcoef", ), values=(-0.001, )) specification = EquationSpecification(variables=("gridcell.cost", ), coefficients=("costcoef", )) # run the model hlcm = HouseholdLocationChoiceModelCreator().get_model(location_set=gridcells, compute_capacity_flag=False, \ choices = "opus_core.random_choices_from_index", sampler=None) #sample_size_locations = 30) # check the individual gridcells # This is a stochastic model, so it may legitimately fail occassionally. success = [] def inner_loop(): hlcm.run(specification, coefficients, agent_set=households, debuglevel=1, chunk_specification={'nchunks': 1}) # get results gridcells.compute_variables( ["urbansim.gridcell.number_of_households"], resources=Resources({"household": households})) result_more_attractive = gridcells.get_attribute_by_id( "number_of_households", arange(ngrids / 2) + 1) return result_more_attractive expected_results = array(ngrids / 2 * [nhouseholds * 0.71 / (ngrids / 2)]) self.run_stochastic_test(__file__, inner_loop, expected_results, 10, type="pearson", transformation=None) # Make sure it fails when expected distribution is different from actual. expected_results = array(ngrids / 2 * [nhouseholds * 0.61 / (ngrids / 2)]) try: self.run_stochastic_test(__file__, inner_loop, expected_results, 10, type="poisson", transformation=None) except AssertionError: pass