Skip to content

Commit

Permalink
fix householdutil method
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuehnel committed Oct 9, 2019
1 parent 6cb8310 commit cc0855b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public static int getAnnualHhIncome(Household household) {
return household.getPersons().values().stream().mapToInt(Person::getAnnualIncome).sum();
}

public static boolean checkIfAdultsPresent(Household household) {
public static boolean checkIfNoAdultsPresent(Household household) {
if (household.getPersons().isEmpty()) {
return false;
}
return household.getPersons().values().stream().anyMatch(pp -> pp.getAge() >= 16);
return household.getPersons().values().stream().allMatch(pp -> pp.getAge() < 16);
}

public static HouseholdType defineHouseholdType(Household household) {
Expand Down Expand Up @@ -79,6 +79,7 @@ public static HouseholdType defineHouseholdType(Household household) {

/**
* return income category defined exogenously
*
* @param hhInc
* @return
*/
Expand All @@ -93,13 +94,13 @@ public static IncomeCategory getIncomeCategoryForIncome(int hhInc) {
return IncomeCategory.values()[IncomeCategory.values().length - 1];
}

public static Person findMostLikelyUnmarriedPartner (Person per, Household hh) {
public static Person findMostLikelyUnmarriedPartner(Person per, Household hh) {
// when assigning roles to persons, look for likely partner in household that is not married yet

Person selectedPartner = null;
double highestUtil = Double.NEGATIVE_INFINITY;
double tempUtil;
for (Person partner: hh.getPersons().values()) {
for (Person partner : hh.getPersons().values()) {
if (partner.getGender() != per.getGender() && partner.getRole() != PersonRole.MARRIED) {
int ageDiff = Math.abs(per.getAge() - partner.getAge());
if (ageDiff == 0) {
Expand All @@ -121,12 +122,12 @@ public static Person findMostLikelyPartner(Person per, Household hh) {
double highestUtil = Double.NEGATIVE_INFINITY;
double tempUtil;
Person selectedPartner = null;
for(Person partner: hh.getPersons().values()) {
for (Person partner : hh.getPersons().values()) {
if (!partner.equals(per) && partner.getGender() != per.getGender() && partner.getRole() == PersonRole.MARRIED) {
final int ageDiff = Math.abs(per.getAge() - partner.getAge());
if (ageDiff == 0) {
tempUtil = 2.;
} else {
} else {
tempUtil = 1. / ageDiff;
}
if (tempUtil > highestUtil) {
Expand All @@ -137,23 +138,23 @@ public static Person findMostLikelyPartner(Person per, Household hh) {
}
if (selectedPartner == null) {
logger.error("Could not find spouse of person " + per.getId() + " in household " + hh.getId());
for (Person person: hh.getPersons().values()) {
for (Person person : hh.getPersons().values()) {
logger.error("Houshold member " + person.getId() + " (gender: " + person.getGender() + ") is " +
person.getRole());
}
}
return selectedPartner;
}

public static void defineUnmarriedPersons (Household hh) {
public static void defineUnmarriedPersons(Household hh) {
// For those that did not become the married couple define role in household (child or single)
for (Person pp: hh.getPersons().values()) {
for (Person pp : hh.getPersons().values()) {
if (pp.getRole() == PersonRole.MARRIED) {
continue;
}
boolean someone15to40yearsOlder = false; // assumption that this person is a parent
final int ageMain = pp.getAge();
for (Person per: hh.getPersons().values()) {
for (Person per : hh.getPersons().values()) {
if (pp.equals(per)) {
continue;
}
Expand All @@ -177,7 +178,7 @@ public static void defineUnmarriedPersons (Household hh) {
public static void findMarriedCouple(Household hh) {
List<Person> personsCopy = hh.getPersons().values().stream().sorted(new PersonUtils.PersonByAgeComparator()).collect(Collectors.toList());

for (Person person: personsCopy) {
for (Person person : personsCopy) {
Person partner = HouseholdUtil.findMostLikelyUnmarriedPartner(person, hh);
if (partner != null) {
partner.setRole(PersonRole.MARRIED);
Expand All @@ -196,12 +197,12 @@ public static void findMarriedCouple(Household hh) {
}
}
}

public static Map<Integer, Integer> getPopulationByZoneAsMap(DataContainer dataContainer) {
Map<Integer, Integer> zonePopulationMap = new HashMap<>();
for (int zone : dataContainer.getGeoData().getZones().keySet()) {
zonePopulationMap.put(zone, 0);
}
Map<Integer, Integer> zonePopulationMap = new HashMap<>();
for (int zone : dataContainer.getGeoData().getZones().keySet()) {
zonePopulationMap.put(zone, 0);
}

for (Household hh : dataContainer.getHouseholdDataManager().getHouseholds()) {
final int zone = dataContainer.getRealEstateDataManager().getDwelling(hh.getDwellingId()).getZoneId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import de.tum.bgu.msm.data.household.Household;
import de.tum.bgu.msm.data.household.HouseholdDataManager;
import de.tum.bgu.msm.utils.SiloUtil;
import org.jboss.logging.Logger;
import org.apache.log4j.Logger;

import java.io.PrintWriter;

public class DefaultHouseholdWriter implements HouseholdWriter {

private final HouseholdDataManager householdData;
private final static Logger logger = Logger.getLogger(DefaultHouseholdWriter.class);
private final static Logger logger = Logger.getLogger(DefaultHouseholdWriter.class);

public DefaultHouseholdWriter(HouseholdDataManager householdData) {
this.householdData = householdData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ boolean die(Person person) {
}
householdDataManager.removePerson(person.getId());

final boolean onlyChildrenLeft = HouseholdUtil.checkIfAdultsPresent(hhOfPersonToDie);
final boolean onlyChildrenLeft = HouseholdUtil.checkIfNoAdultsPresent(hhOfPersonToDie);
if (onlyChildrenLeft) {
for (Person pp : hhOfPersonToDie.getPersons().values()) {
if (pp.getId() == SiloUtil.trackPp || hhOfPersonToDie.getId() == SiloUtil.trackHh) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
package de.tum.bgu.msm.models.demography.leaveParentalHousehold;

import de.tum.bgu.msm.container.DataContainer;
import de.tum.bgu.msm.data.Region;
import de.tum.bgu.msm.data.dwelling.RealEstateDataManager;
import de.tum.bgu.msm.data.geo.GeoData;
import de.tum.bgu.msm.data.household.Household;
import de.tum.bgu.msm.data.household.HouseholdDataManager;
import de.tum.bgu.msm.data.household.HouseholdFactory;
Expand Down Expand Up @@ -67,17 +64,6 @@ public LeaveParentHhModelImpl(DataContainer dataContainer, MovesModelImpl move,

@Override
public void setup() {
// switch (properties.main.implementation) {
// case MARYLAND:
// case AUSTIN:
// reader = new InputStreamReader(this.getClass().getResourceAsStream("LeaveParentHhCalcMstm"));
// break;
// case MUNICH:
// case PERTH:
// case KAGAWA:
// case CAPE_TOWN:
// reader = new InputStreamReader(this.getClass().getResourceAsStream("LeaveParentHhCalc"));
// }
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ private void movePerson(Person person1, Household moveTo) {
if (!moveTo.equals(household1)) {
householdDataManager.removePersonFromHousehold(person1);
householdDataManager.addPersonToHousehold(person1, moveTo);
if (HouseholdUtil.checkIfAdultsPresent(household1)) {
if (HouseholdUtil.checkIfNoAdultsPresent(household1)) {
moveRemainingChildren(household1, moveTo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private void movePerson(Person person1, Household moveTo) {
if (!moveTo.equals(household1)) {
householdDataManager.removePersonFromHousehold(person1);
householdDataManager.addPersonToHousehold(person1, moveTo);
if (HouseholdUtil.checkIfAdultsPresent(household1)) {
if (HouseholdUtil.checkIfNoAdultsPresent(household1)) {
moveRemainingChildren(household1, moveTo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ private void movePerson(Person person1, Household moveTo) {
if (!moveTo.equals(household1)) {
householdDataManager.removePersonFromHousehold(person1);
householdDataManager.addPersonToHousehold(person1, moveTo);
if (HouseholdUtil.checkIfAdultsPresent(household1)) {
if (HouseholdUtil.checkIfNoAdultsPresent(household1)) {
moveRemainingChildren(household1, moveTo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private void movePerson(Person person1, Household moveTo) {
if (!moveTo.equals(household1)) {
householdDataManager.removePersonFromHousehold(person1);
householdDataManager.addPersonToHousehold(person1, moveTo);
if (HouseholdUtil.checkIfAdultsPresent(household1)) {
if (HouseholdUtil.checkIfNoAdultsPresent(household1)) {
moveRemainingChildren(household1, moveTo);
}
}
Expand Down

0 comments on commit cc0855b

Please sign in to comment.