def test_person_add_leg(): person = Person(1) act = Activity(1, 'home', 1) person.add(act) leg = Leg(1, 'car', start_area=1, end_area=2) person.add(leg) assert len(person.plan) == 2
def test_person_add_activity_activity_raise_error(): person = Person('1') act = Activity(1, 'home', 1) person.add(act) act = Activity(2, 'work', 1) with pytest.raises(PAMSequenceValidationError): person.add(act)
def test_person_add_activity_activity_raise_error(): person = Person(1) act = Activity(1, 'home', 1) person.add(act) act = Activity(2, 'work', 1) with pytest.raises(UserWarning): person.add(act)
def test_household_add_person(): household = Household(1) person = Person(1) person.add(Activity(1, 'home', 1, start_time=0)) household.add(person) assert len(household.people) == 1 assert list(household.people) == [1]
def Bobby(): Bobby = Person(4, attributes={'age': 6, 'job': 'education', 'gender': 'non-binary'}) Bobby.add(Activity(1, 'home', 'a', start_time=mtdt(0), end_time=mtdt(8 * 60))) Bobby.add(Leg(1, 'walk', 'a', 'b', start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 30))) Bobby.add(Activity(2, 'education', 'b', start_time=mtdt(8 * 60 + 30), end_time=mtdt(16 * 60))) Bobby.add(Leg(2, 'walk', 'b', 'c', start_time=mtdt(16 * 60), end_time=mtdt(16 * 60 + 30))) Bobby.add(Activity(5, 'home', 'a', start_time=mtdt(18 * 60 + 30), end_time=END_OF_DAY)) return Bobby
def test_person_not_closed_plan_different_areas(): person = Person(1) person.add(Activity(1, 'work', 1)) person.add(Leg(1, 'car', start_area=1, end_area=2)) person.add(Activity(2, 'home', 1)) person.add(Leg(2, 'car', start_area=2, end_area=3)) person.add(Activity(3, 'work', 3)) assert not person.closed_plan
def test_person_not_home_based(): person = Person(1) person.add(Activity(1, 'work', 1)) person.add(Leg(1, 'car', start_area=1, end_area=2)) person.add(Activity(2, 'home', 1)) person.add(Leg(2, 'car', start_area=2, end_area=1)) person.add(Activity(3, 'work', 1)) assert not person.home_based
def test_person_closed_plan(): person = Person(1) person.add(Activity(1, 'home', 1)) person.add(Leg(1, 'car', start_area=1, end_area=2)) person.add(Activity(2, 'work', 1)) person.add(Leg(2, 'car', start_area=2, end_area=1)) person.add(Activity(3, 'home', 1)) assert person.closed_plan
def home_education_home(): person = Person(1) person.add(Activity(1, 'home', 'a')) person.add(Leg(1, 'car', 'a', 'b')) person.add(Activity(2, 'education', 'b')) person.add(Leg(2, 'car', 'b', 'a')) person.add(Activity(3, 'home', 'a')) return person
def test_write_plans_xml_v12_assert_contents(tmp_path): population = Population() hh = Household('a') p = Person('a', attributes={'1':'1'}) p.add(Activity( act="home", loc=Point((0,0)), start_time=datetime(1900,1,1,0,0,0), end_time=datetime(1900,1,1,8,0,0) )) p.add(Leg( mode='car', start_loc=Point((0,0)), end_loc=Point((0,1000)), start_time=datetime(1900,1,1,8,0,0), end_time=datetime(1900,1,1,9,0,0) )) p.add(Activity( act="work", loc=Point((0,1000)), start_time=datetime(1900,1,1,9,0,0), end_time=datetime(1900,1,1,18,0,0) )) p.add(Leg( mode='car', start_loc=Point((0,1000)), end_loc=Point((0,0)), start_time=datetime(1900,1,1,18,0,0), end_time=datetime(1900,1,1,19,0,0) )) p.add(Activity( act="home", loc=Point((0,0)), start_time=datetime(1900,1,1,19,0,0), end_time=END_OF_DAY )) hh.add(p) population.add(hh) plans_location = str(tmp_path / "test_plans.xml") write_matsim( population, plans_path=plans_location, comment="test", version=12, household_key=None ) new = read_matsim( plans_location, version=12 ) assert new == population assert new['a']['a'].attributes == {'1':'1'} assert new['a']['a'].plan.day[1].distance == 1000
def person_crop_last_leg(): person = Person('1') person.add( Activity( seq=1, act='home', area='a', start_time=mtdt(0), end_time=mtdt(60) ) ) person.add( Leg( seq=1, mode='car', start_area='a', end_area='b', start_time=mtdt(60), end_time=mtdt(90) ) ) person.add( Activity( seq=2, act='education', area='b', start_time=mtdt(90), end_time=mtdt(120) ) ) person.add( Leg( seq=2, mode='car', start_area='b', end_area='a', start_time=mtdt(120), end_time=mtdt(2600) ) ) person.add( Activity( seq=3, act='home', area='a', start_time=mtdt(2600), end_time=mtdt(3000) ) ) return person
def person_work_home_work_not_closed(): person = Person('1') person.add( Activity( seq=1, act='work', area='a', start_time=mtdt(0), end_time=mtdt(60) ) ) person.add( Leg( seq=1, mode='car', start_area='a', end_area='b', start_time=mtdt(60), end_time=mtdt(90) ) ) person.add( Activity( seq=2, act='home', area='b', start_time=mtdt(90), end_time=mtdt(120) ) ) person.add( Leg( seq=2, mode='car', start_area='b', end_area='c', start_time=mtdt(120), end_time=mtdt(180) ) ) person.add( Activity( seq=3, act='work', area='c', start_time=mtdt(180), end_time=END_OF_DAY ) ) return person
def person_crop_last_act(): person = Person('1', attributes={'old': True}) person.add( Activity( seq=1, act='home', area='a', start_time=mtdt(0), end_time=mtdt(60) ) ) person.add( Leg( seq=1, mode='car', start_area='a', end_area='b', start_time=mtdt(60), end_time=mtdt(90) ) ) person.add( Activity( seq=2, act='education', area='b', start_time=mtdt(90), end_time=mtdt(120) ) ) person.add( Leg( seq=2, mode='car', start_area='b', end_area='a', start_time=mtdt(120), end_time=mtdt(180) ) ) person.add( Activity( seq=3, act='home', area='a', start_time=mtdt(180), end_time=END_OF_DAY ) ) return person
def person_home_education_home(): person = Person(1) person.add( Activity( seq=1, act='home', area='a', start_time=mtdt(0), end_time=mtdt(60) ) ) person.add( Leg( seq=1, mode='car', start_area='a', end_area='b', start_time=mtdt(60), end_time=mtdt(90) ) ) person.add( Activity( seq=2, act='education', area='b', start_time=mtdt(90), end_time=mtdt(120) ) ) person.add( Leg( seq=2, mode='car', start_area='b', end_area='a', start_time=mtdt(120), end_time=mtdt(180) ) ) person.add( Activity( seq=3, act='home', area='a', start_time=mtdt(180), end_time=END_OF_DAY ) ) return person
def population_heh(): home_loc = Point(0, 0) education_loc = Point(110, 110) attributes = {'hid': 0, 'hh_size': 3, 'inc': "high"} person = Person('1', attributes=attributes) person.add( Activity(seq=1, act='home', area='a', loc=home_loc, start_time=mtdt(0), end_time=mtdt(60))) person.add( Leg(seq=1, mode='car', start_area='a', end_area='b', start_time=mtdt(60), end_time=mtdt(90))) person.add( Activity(seq=2, act='education', area='b', loc=education_loc, start_time=mtdt(90), end_time=mtdt(120))) person.add( Leg(seq=2, mode='car', start_area='b', end_area='a', start_time=mtdt(120), end_time=mtdt(180))) person.add( Activity(seq=3, act='home', area='a', loc=home_loc, start_time=mtdt(180), end_time=END_OF_DAY)) person.plan.autocomplete_matsim() household = Household('0') household.add(person) population = Population() population.add(household) return population
def test_MoveActivityToHomeLocation_moves_shop_to_home_location(): Hilda = Person(1, attributes={ 'age': 45, 'job': 'influencer', 'gender': 'female' }) Hilda.add( Activity(1, 'home', 'a', start_time=mtdt(0), end_time=mtdt(8 * 60))) Hilda.add( Leg(1, 'car', 'a', 'b', start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 30))) Hilda.add( Activity(2, 'shop', 'b', start_time=mtdt(8 * 60 + 30), end_time=mtdt(16 * 60 + 30))) Hilda.add( Leg(1, 'car', 'b', 'a', start_time=mtdt(16 * 60 + 30), end_time=mtdt(17 * 60))) Hilda.add( Activity(5, 'home', 'a', start_time=mtdt(17 * 60), end_time=END_OF_DAY)) hhld = instantiate_household_with([Hilda]) policy = modify.PersonPolicy( modify.MoveActivityTourToHomeLocation(['shop']), modify.PersonProbability(1)) policy.apply_to(hhld) assert Hilda.plan[2].location == Hilda.home assert Hilda.plan[2].is_exact( Activity(2, 'shop', 'a', start_time=mtdt(8 * 60 + 30), end_time=mtdt(16 * 60 + 30)))
def home_education_home_university_student(): person = Person(1, attributes={'age': 18, 'job': 'education'}) person.add(Activity(1, 'home', 'a', start_time=mtdt(0), end_time=mtdt(60))) person.add(Leg(1, 'bike', 'a', 'b', start_time=mtdt(60), end_time=mtdt(2*60))) person.add(Activity(2, 'education', 'b', start_time=mtdt(2*60), end_time=mtdt(3*60))) person.add(Leg(2, 'bike', 'b', 'a', start_time=mtdt(3*60), end_time=mtdt(4*60))) person.add(Activity(3, 'home', 'a', start_time=mtdt(4*60), end_time=END_OF_DAY)) return person
def test_home_education_home_removal_of_education_act(): household = HouseHold(1) person = Person(1) person.add( Activity(seq=1, act='home', area='a', start_time=minutes_to_datetime(0), end_time=minutes_to_datetime(60))) person.add( Leg(seq=1, mode='car', start_loc='a', end_loc='b', start_time=minutes_to_datetime(60), end_time=minutes_to_datetime(90))) person.add( Activity(seq=2, act='education', area='b', start_time=minutes_to_datetime(90), end_time=minutes_to_datetime(120))) person.add( Leg(seq=2, mode='car', start_loc='b', end_loc='a', start_time=minutes_to_datetime(120), end_time=minutes_to_datetime(180))) person.add( Activity(seq=3, act='home', area='a', start_time=minutes_to_datetime(180), end_time=minutes_to_datetime(24 * 60 - 1))) household.add(person) policy = policies.RemoveEducationActivity(1) policy.apply_to(household) assert [p.act for p in household.people[1].activities] == ['home'] assert household.people[1].plan[0].start_time == minutes_to_datetime(0) assert household.people[1].plan[0].end_time == minutes_to_datetime(24 * 60 - 1)
def test_MoveActivityToHomeLocation_performs_mode_shift_to_walk_due_to_lack_of_driving_licence( ): Hilda = Person(1, attributes={ 'age': 45, 'job': 'influencer', 'gender': 'female', 'driving_licence': False }) Hilda.add( Activity(1, 'home', 'a', start_time=mtdt(0), end_time=mtdt(8 * 60))) Hilda.add( Leg(1, 'pt', 'a', 'b', start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 30))) Hilda.add( Activity(2, 'shop', 'b', start_time=mtdt(8 * 60 + 30), end_time=mtdt(16 * 60 + 30))) Hilda.add( Leg(1, 'pt', 'b', 'a', start_time=mtdt(16 * 60 + 30), end_time=mtdt(17 * 60))) Hilda.add( Activity(5, 'home', 'a', start_time=mtdt(17 * 60), end_time=END_OF_DAY)) hhld = instantiate_household_with([Hilda]) policy = modify.PersonPolicy( modify.MoveActivityTourToHomeLocation(['shop']), modify.PersonProbability(1)) policy.apply_to(hhld) assert Hilda.plan[1].mode == 'walk' assert Hilda.plan[3].mode == 'walk'
def test_writes_od_matrix_to_expected_file(tmpdir): population = Population() for hid in range(1, 11): household = Household(hid) population.add(household) for pid in range (2): person = Person(pid) person.add(Activity(1,'home', 'Barnet')) person.add(Leg(1,'car', start_area='Barnet', end_area='Southwark')) person.add(Activity(2,'work', 'Southwark')) person.add(Leg(2,'car', start_area='Southwark', end_area='Barnet')) person.add(Activity(3,'work', 'Barnet')) household.add(person) od_matrix_file = os.path.join(tmpdir, "od_matrix_test.csv") extract_od(population, od_matrix_file) od_matrix_csv_string = open(od_matrix_file).read() assert od_matrix_csv_string == 'ozone,Barnet,Southwark\nBarnet,0,20\nSouthwark,20,0\n'
def test_extract_household_mode_classes(): household = Household(hid='1') for i, (act, mode) in enumerate(zip(['work', 'school'], ['car', 'pt'])): person = Person(pid=str(i)) person.add( Activity(seq=1, act='home', area='A', start_time=mtdt(0), end_time=mtdt(600))) person.add( Leg(seq=2, mode=mode, start_area='A', end_area='B', start_time=mtdt(600), end_time=mtdt(620))) person.add( Activity(seq=3, act=act, area='B', start_time=mtdt(620), end_time=mtdt(1200))) household.add(person) assert household.mode_classes == set(['car', 'pt'])
def test_extract_population_mode_classes(): population = Population() for hid, (_act, _mode) in enumerate(zip(['work', 'shop'], ['pt', 'walk'])): household = Household(hid=str(hid)) population.add(household) for i, (act, mode) in enumerate(zip(['work', _act], ['car', _mode])): person = Person(pid=str(i)) person.add( Activity(seq=1, act='home', area='A', start_time=mtdt(0), end_time=mtdt(600))) person.add( Leg(seq=2, mode=mode, start_area='A', end_area='B', start_time=mtdt(600), end_time=mtdt(620))) person.add( Activity(seq=3, act=act, area='B', start_time=mtdt(620), end_time=mtdt(1200))) household.add(person) assert population.mode_classes == set(['car', 'pt', 'walk'])
def person_home_education_home(): person = Person(1) person.add( Activity(seq=1, act='home', area='a', start_time=minutes_to_datetime(0), end_time=minutes_to_datetime(60))) person.add( Leg(seq=1, mode='car', start_area='a', end_area='b', start_time=minutes_to_datetime(60), end_time=minutes_to_datetime(90))) person.add( Activity(seq=2, act='education', area='b', start_time=minutes_to_datetime(90), end_time=minutes_to_datetime(120))) person.add( Leg(seq=2, mode='car', start_area='b', end_area='a', start_time=minutes_to_datetime(120), end_time=minutes_to_datetime(180))) person.add( Activity(seq=3, act='home', area='a', start_time=minutes_to_datetime(180), end_time=minutes_to_datetime(24 * 60 - 1))) return person
def test_person_add_leg_leg_raise_error(): person = Person(1) act = Activity(1, 'home', 1) person.add(act) leg = Leg(1, 'car', start_area=1, end_area=2) person.add(leg) leg = Leg(2, 'car', start_area=2, end_area=1) with pytest.raises(UserWarning): person.add(leg)
def test_person_add_leg_leg_raise_error(): person = Person('1') act = Activity(1, 'home', 1) person.add(act) leg = Leg(1, 'car', start_area=1, end_area=2) person.add(leg) leg = Leg(2, 'car', start_area=2, end_area=1) with pytest.raises(PAMSequenceValidationError): person.add(leg)
def test_extract_person_mode_classes(): person = Person(pid=str(1)) person.add( Activity(seq=1, act='home', area='A', start_time=mtdt(0), end_time=mtdt(600))) person.add( Leg(seq=2, mode='bike', start_area='A', end_area='B', start_time=mtdt(600), end_time=mtdt(620))) person.add( Activity(seq=3, act='work', area='B', start_time=mtdt(620), end_time=mtdt(1200))) person.add( Leg(seq=2, mode='pt', start_area='B', end_area='A', start_time=mtdt(1200), end_time=mtdt(1220))) person.add( Activity(seq=3, act='home', area='A', start_time=mtdt(1220), end_time=mtdt(1500))) assert person.mode_classes == set(['bike', 'pt'])
def Timmy(): Timmy = Person(3, attributes={'age': 18, 'job': 'education', 'gender': 'male'}) Timmy.add(Activity(1, 'home', 'a', start_time=mtdt(0), end_time=mtdt(10 * 60))) Timmy.add(Leg(1, 'bike', 'a', 'b', start_time=mtdt(10 * 60), end_time=mtdt(11 * 60))) Timmy.add(Activity(2, 'education', 'b', start_time=mtdt(11 * 60), end_time=mtdt(13 * 60))) Timmy.add(Leg(2, 'bike', 'b', 'c', start_time=mtdt(13 * 60), end_time=mtdt(13 * 60 + 5))) Timmy.add(Activity(3, 'shop', 'c', start_time=mtdt(13 * 60 + 5), end_time=mtdt(13 * 60 + 30))) Timmy.add(Leg(3, 'bike', 'c', 'b', start_time=mtdt(13 * 60 + 30), end_time=mtdt(13 * 60 + 35))) Timmy.add(Activity(4, 'education', 'b', start_time=mtdt(13 * 60 + 35), end_time=mtdt(15 * 60))) Timmy.add(Leg(4, 'bike', 'b', 'd', start_time=mtdt(15 * 60), end_time=mtdt(15 * 60 + 10))) Timmy.add(Activity(5, 'leisure', 'd', start_time=mtdt(15 * 60 + 10), end_time=mtdt(18 * 60))) Timmy.add(Leg(5, 'bike', 'd', 'a', start_time=mtdt(18 * 60), end_time=mtdt(18 * 60 + 20))) Timmy.add(Activity(6, 'home', 'a', start_time=mtdt(18 * 60 + 20), end_time=END_OF_DAY)) return Timmy
def Steve(): Steve = Person(1, attributes={'age': 50, 'job': 'work', 'gender': 'male'}) Steve.add(Activity(1, 'home', 'a', start_time=mtdt(0), end_time=mtdt(5 * 60))) Steve.add(Leg(1, 'car', 'a', 'b', start_time=mtdt(5 * 60), end_time=mtdt(6 * 60))) Steve.add(Activity(2, 'work', 'b', start_time=mtdt(6 * 60), end_time=mtdt(12 * 60))) Steve.add(Leg(2, 'walk', 'b', 'c', start_time=mtdt(12 * 60), end_time=mtdt(12 * 60 + 10))) Steve.add(Activity(3, 'leisure', 'c', start_time=mtdt(12 * 60 + 10), end_time=mtdt(13 * 60 - 10))) Steve.add(Leg(3, 'walk', 'c', 'b', start_time=mtdt(13 * 60 - 10), end_time=mtdt(13 * 60))) Steve.add(Activity(4, 'work', 'b', start_time=mtdt(13 * 60), end_time=mtdt(18 * 60))) Steve.add(Leg(4, 'car', 'b', 'a', start_time=mtdt(18 * 60), end_time=mtdt(19 * 60))) Steve.add(Activity(5, 'home', 'a', start_time=mtdt(19 * 60), end_time=END_OF_DAY)) return Steve
def Hilda(): Hilda = Person(2, attributes={'age': 45, 'job': 'influencer', 'gender': 'female'}) Hilda.add(Activity(1, 'home', 'a', start_time=mtdt(0), end_time=mtdt(8 * 60))) Hilda.add(Leg(1, 'walk', 'a', 'b', start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 5))) Hilda.add(Activity(2, 'escort', 'b', start_time=mtdt(8 * 60 + 5), end_time=mtdt(8 * 60 + 30))) Hilda.add(Leg(1, 'pt', 'a', 'b', start_time=mtdt(8 * 60), end_time=mtdt(8 * 60 + 30))) Hilda.add(Activity(2, 'shop', 'b', start_time=mtdt(8 * 60 + 30), end_time=mtdt(14 * 60))) Hilda.add(Leg(2, 'pt', 'b', 'c', start_time=mtdt(14 * 60), end_time=mtdt(14 * 60 + 20))) Hilda.add(Activity(3, 'leisure', 'c', start_time=mtdt(14 * 60 + 20), end_time=mtdt(16 * 60 - 20))) Hilda.add(Leg(3, 'pt', 'c', 'b', start_time=mtdt(16 * 60 - 20), end_time=mtdt(16 * 60))) Hilda.add(Activity(2, 'escort', 'b', start_time=mtdt(16 * 60), end_time=mtdt(16 * 60 + 30))) Hilda.add(Leg(1, 'walk', 'a', 'b', start_time=mtdt(16 * 60 + 30), end_time=mtdt(16 * 60 + 5))) Hilda.add(Activity(5, 'home', 'a', start_time=mtdt(16 * 60 + 5), end_time=END_OF_DAY)) return Hilda
def test_home_work_home_education_home_removal_of_education_act(): person = Person(1) person.add( Activity( seq=1, act='home', area='a', start_time=mtdt(0), end_time=mtdt(60) ) ) person.add( Leg( seq=1, mode='car', start_area='a', end_area='b', start_time=mtdt(60), end_time=mtdt(90) ) ) person.add( Activity( seq=2, act='work', area='b', start_time=mtdt(90), end_time=mtdt(120) ) ) person.add( Leg( seq=2, mode='car', start_area='b', end_area='a', start_time=mtdt(120), end_time=mtdt(180) ) ) person.add( Activity( seq=3, act='home', area='a', start_time=mtdt(180), end_time=mtdt(300) ) ) person.add( Leg( seq=3, mode='car', start_area='a', end_area='b', start_time=mtdt(300), end_time=mtdt(390) ) ) person.add( Activity( seq=2, act='education', area='b', start_time=mtdt(390), end_time=mtdt(520) ) ) person.add( Leg( seq=2, mode='car', start_area='b', end_area='a', start_time=mtdt(520), end_time=mtdt(580) ) ) person.add( Activity( seq=3, act='home', area='a', start_time=mtdt(680), end_time=END_OF_DAY ) ) household = instantiate_household_with([person]) assert_correct_activities(person=household.people['1'], ordered_activities_list=['home', 'work', 'home', 'education', 'home']) policy = modify.RemoveActivity(activities=['education'], probability=1) policy.apply_to(household) assert_correct_activities(person=household.people['1'], ordered_activities_list=['home', 'work', 'home'])