def test_do_nothing_if_no_agents(self):
        storage = StorageFactory().get_storage("dict_storage")

        storage.write_table(table_name="households", table_data={"household_id": array([], dtype="int32")})
        hh_set = HouseholdDataset(in_storage=storage, in_table_name="households")

        storage.write_table(table_name="rates", table_data=self.annual_relocation_rates_for_households_data)
        hh_rateset = HouseholdRelocationRateDataset(in_storage=storage, in_table_name="rates")

        hrm_resources = Resources({"annual_household_relocation_rate": hh_rateset})

        hrm = HouseholdRelocationModelCreator().get_model(debuglevel=2)
        hrm_relocation_results = hrm.run(hh_set, resources=hrm_resources)

        should_be = 0
        self.assertEqual(hrm_relocation_results.size, should_be, msg="Error ")
    def test_hrm_all_households_relocate_with_100_percent_probability(self):
        """Creates four groups of households as defined in the setUp method, 
        and assigns their probability of relocate as 100% (1 because it is a ratio).
        Ensure that all 15,000 households decide to relocate.
        """
        storage = StorageFactory().get_storage("dict_storage")

        storage.write_table(table_name="households", table_data=self.household_data)
        hh_set = HouseholdDataset(in_storage=storage, in_table_name="households")

        storage.write_table(table_name="rates", table_data=self.annual_relocation_rates_for_households_data)
        hh_rateset = HouseholdRelocationRateDataset(in_storage=storage, in_table_name="rates")

        hrm_resources = Resources({"annual_household_relocation_rate": hh_rateset})

        hrm = HouseholdRelocationModelCreator().get_model(debuglevel=1)
        hrm_relocation_results = hrm.run(hh_set, resources=hrm_resources)

        should_be = 1500
        self.assertEqual(hrm_relocation_results.size, should_be, msg="Error ")
    def test_hrm_no_households_relocate_with_0_percent_probability(self):
        """Creates four groups of households as defined in the setUp method, 
        and assigns their probability of relocate as 0%.
        Ensure that all 15,000 households do not decide to relocate.
        """
        storage = StorageFactory().get_storage('dict_storage')
        
        annual_relocation_rates_for_households_data = self.annual_relocation_rates_for_households_data
        annual_relocation_rates_for_households_data['probability_of_relocating'] = array(4*[0.0])
        
        storage.write_table(table_name = 'households', table_data = self.household_data)
        storage.write_table(table_name = 'rates', table_data = annual_relocation_rates_for_households_data)
      
        hh_set = HouseholdDataset(in_storage=storage, in_table_name='households')
        hh_rateset = HouseholdRelocationRateDataset(in_storage=storage, in_table_name='rates')

        hrm_resources = Resources({'rate_set':hh_rateset})

        hrm = HouseholdRelocationModelCreator().get_model(debuglevel=1)
        hrm_relocation_results = hrm.run(hh_set,resources=hrm_resources)

        should_be = 0
        self.assertEqual(hrm_relocation_results.size, should_be, msg = 'Error ')
    logger.log_status("submarket defined by %s x %s " % (options.submarket_geography, options.submarket_attribute) )

    estimator = HLCMEstimator(config=my_configuration,
                              save_estimation_results=False)
    estimator.simulation_state.set_current_time(2000)
    
    attribute_cache = AttributeCache()
    sc = SessionConfiguration()
    CLOSE = 0.005   #criterion for convergence
    sc.put_data({'CLOSE':CLOSE, 'info_file':info_file})
    
    seed(71) # was: seed(71,110)

    ## relocate movers
    from urbansim.models.household_relocation_model_creator import HouseholdRelocationModelCreator
    hrm = HouseholdRelocationModelCreator().get_model(probabilities='urbansim.household_relocation_probabilities',
                                                      location_id_name='building_id' )
    hrm_resources = hrm.prepare_for_run(rate_storage=attribute_cache,
                                        rate_table='annual_relocation_rates_for_households',
                                        what='households')
    hrm_index = hrm.run(agent_set=sc.get_dataset_from_pool('household'),
                        resources=hrm_resources)
    
    estimator.estimate(spec_py=hlcm_specification, 
                       movers_index=hrm_index, 
                       submarket_definition=(options.submarket_geography, options.submarket_attribute),
                       alt_sample_size=alt_sample_size,
                       sampler=sampler,
                       weight_string = options.weight_string,
                       aggregate_demand=options.aggregate_demand,
                       sample_size_from_each_stratum = int(options.sample_size_from_each_stratum),
                   )
    estimator = HLCMEstimator(config=my_configuration,
                              save_estimation_results=False)
    estimator.simulation_state.set_current_time(2000)

    attribute_cache = AttributeCache()
    sc = SessionConfiguration()
    CLOSE = 0.005  #criterion for convergence
    sc.put_data({'CLOSE': CLOSE, 'info_file': info_file})

    seed(71)  # was: seed(71,110)

    ## relocate movers
    from urbansim.models.household_relocation_model_creator import HouseholdRelocationModelCreator
    hrm = HouseholdRelocationModelCreator().get_model(
        probabilities='urbansim.household_relocation_probabilities',
        location_id_name='building_id')
    hrm_resources = hrm.prepare_for_run(
        rate_storage=attribute_cache,
        rate_table='annual_relocation_rates_for_households',
        what='households')
    hrm_index = hrm.run(agent_set=sc.get_dataset_from_pool('household'),
                        resources=hrm_resources)

    estimator.estimate(
        spec_py=hlcm_specification,
        movers_index=hrm_index,
        submarket_definition=(options.submarket_geography,
                              options.submarket_attribute),
        alt_sample_size=alt_sample_size,
        sampler=sampler,