Skip to content

Commit

Permalink
add update and create car ownership models for takamatsu/kagawa
Browse files Browse the repository at this point in the history
fixes #216
  • Loading branch information
nkuehnel committed Oct 9, 2019
1 parent c359721 commit 6cb8310
Show file tree
Hide file tree
Showing 51 changed files with 377 additions and 95 deletions.
4 changes: 2 additions & 2 deletions analysis/src/main/java/sdg/SDGCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public static void calculateSdgIndicators(DataContainer dataContainer, String ou


Map<Integer,List<Household>> hhBySize = households.parallelStream().collect(Collectors.groupingBy(hh -> hh.getPersons().size()));
hhBySize.entrySet().forEach(entry -> logger.info(entry.getKey() + " " + entry.getValue().stream().filter(hh -> (double) HouseholdUtil.getHhIncome(hh) < 10000).count() / (double) entry.getValue().size()));
hhBySize.entrySet().forEach(entry -> logger.info(entry.getKey() + " " + entry.getValue().stream().filter(hh -> (double) HouseholdUtil.getAnnualHhIncome(hh) < 10000).count() / (double) entry.getValue().size()));





Map<Integer,List<Household>> hhByRegion = households.parallelStream().collect(Collectors.groupingBy(hh -> hh.getDwellingId()));
hhBySize.entrySet().forEach(entry -> logger.info(entry.getKey() + " " + entry.getValue().stream().filter(hh -> (double) HouseholdUtil.getHhIncome(hh) < 10000).count() / (double) entry.getValue().size()));
hhBySize.entrySet().forEach(entry -> logger.info(entry.getKey() + " " + entry.getValue().stream().filter(hh -> (double) HouseholdUtil.getAnnualHhIncome(hh) < 10000).count() / (double) entry.getValue().size()));


// logger.info(hhUnder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void convertHhs(DataSet dataSet, DataContainer dataContainer) {

MitoHousehold household = new MitoHousehold(
siloHousehold.getId(),
HouseholdUtil.getHhIncome(siloHousehold) / 12,
HouseholdUtil.getAnnualHhIncome(siloHousehold) / 12,
siloHousehold.getAutos());
household.setHomeZone(zone);

Expand Down
6 changes: 3 additions & 3 deletions siloCore/src/main/java/de/tum/bgu/msm/data/SummarizeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public static void scaleMicroDataToExogenousForecast(int year, DataContainer dat
pwp.print(",0,");
pwp.print(pp.getJobId());
pwp.print(",");
pwp.println(pp.getIncome());
pwp.println(pp.getAnnualIncome());
}
// duplicate household if selected
if (selectedHH[i] > 0) { // household to be repeated for this output file
Expand All @@ -260,7 +260,7 @@ public static void scaleMicroDataToExogenousForecast(int year, DataContainer dat
pwp.print(",0,");
pwp.print(pp.getJobId());
pwp.print(",");
pwp.println(pp.getIncome());
pwp.println(pp.getAnnualIncome());
artificialPpId++;
}
artificialHhId++;
Expand Down Expand Up @@ -290,7 +290,7 @@ public static void scaleMicroDataToExogenousForecast(int year, DataContainer dat
pwp.print(",0,");
pwp.print(pp.getJobId());
pwp.print(",");
pwp.println(pp.getIncome());
pwp.println(pp.getAnnualIncome());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private void setHighestVariablesAndCalculateRentShareByIncome() {
largestNoBedrooms = Math.max(largestNoBedrooms, dd.getBedrooms());
int hhId = dd.getResidentId();
if (hhId > 0) {
int hhinc = HouseholdUtil.getHhIncome(householdData.getHousehold(hhId));
int hhinc = HouseholdUtil.getAnnualHhIncome(householdData.getHousehold(hhId));
IncomeCategory incomeCategory = HouseholdUtil.getIncomeCategoryForIncome(hhinc);
int rentCategory = (int) ((dd.getPrice() * 1.) / 200.); // rent category defined as <rent/200>
rentCategory = Math.min(rentCategory, RENT_CATEGORIES); // ensure that rent categories do not exceed max
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private float[][][] calculateIncomeDistribution() {
if (pp.getOccupation() == Occupation.EMPLOYED) {
occupation = 1;
}
averageIncome[pp.getGender().ordinal()][age][occupation] += pp.getIncome();
averageIncome[pp.getGender().ordinal()][age][occupation] += pp.getAnnualIncome();
count[pp.getGender().ordinal()][age][occupation]++;
}
for (int i = 0; i < averageIncome.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,29 @@ public static int getNumberOfWorkers(Household household) {
}

public static int getNumberOfChildren(Household household) {
return (int) household.getPersons().values().stream().filter(p -> ((Person) p).getRole() == PersonRole.CHILD).count();
return (int) household.getPersons().values().stream().filter(p -> p.getRole() == PersonRole.CHILD).count();
}

public static int getHHLicenseHolders(Household household) {
return (int) household.getPersons().values().stream().filter(Person::hasDriverLicense).count();
}

public static int getHhIncome(Household household) {
int hhInc = 0;
for (Person i : household.getPersons().values()) {
hhInc += i.getIncome();
}
return hhInc;
public static int getAnnualHhIncome(Household household) {
return household.getPersons().values().stream().mapToInt(Person::getAnnualIncome).sum();
}

public static boolean checkIfOnlyChildrenRemaining(Household household) {
public static boolean checkIfAdultsPresent(Household household) {
if (household.getPersons().isEmpty()) {
return false;
}
for (Person pp : household.getPersons().values()) {
if (pp.getAge() >= 16) {
return false;
}
}
return true;
return household.getPersons().values().stream().anyMatch(pp -> pp.getAge() >= 16);
}

public static HouseholdType defineHouseholdType(Household household) {
// define household type based on size and income

int hhSize = household.getHhSize();
IncomeCategory incomeCategory = getIncomeCategoryForIncome(HouseholdUtil.getHhIncome(household));
IncomeCategory incomeCategory = getIncomeCategoryForIncome(HouseholdUtil.getAnnualHhIncome(household));

HouseholdType ht = null;
if (hhSize == 1) {
Expand Down Expand Up @@ -117,7 +108,8 @@ public static Person findMostLikelyUnmarriedPartner (Person per, Household hh) {
tempUtil = 1f / (float) ageDiff;
}
if (tempUtil > highestUtil) {
selectedPartner = partner; // find most likely partner
// find most likely partner
selectedPartner = partner;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private int selectNewIncome () {
}
prob[smallestAbsValuePos] = prob[smallestAbsValuePos] * 10; // make no change most likely
int sel = SiloUtil.select(prob, random);
return Math.max((person.getIncome() + lowerBound + (upperBound - lowerBound) / prob.length * sel), 0);
return Math.max((person.getAnnualIncome() + lowerBound + (upperBound - lowerBound) / prob.length * sel), 0);
}

private float getDesiredShift() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public void quitJob(boolean makeJobAvailableToOthers, Person person) {
jb.setWorkerID(-1);
person.setWorkplace(-1);
person.setOccupation(Occupation.UNEMPLOYED);
person.setIncome((int) (person.getIncome() * 0.6 + 0.5));
person.setIncome((int) (person.getAnnualIncome() * 0.6 + 0.5));
//todo: think about smarter retirement/social welfare algorithm to adjust income after employee leaves work.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public interface Person extends Id {

Occupation getOccupation();

int getIncome();
int getAnnualIncome();

PersonType getType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public PersonImpl duplicate(Person originalPerson, int id) {
originalPerson.getOccupation(),
originalPerson.getRole(),
-1,
originalPerson.getIncome());
originalPerson.getAnnualIncome());
duplicate.setDriverLicense(originalPerson.hasDriverLicense());
return duplicate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Occupation getOccupation() {
}

@Override
public int getIncome() {
public int getAnnualIncome() {
return income;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void writePersons(String path) {
pwp.print(",");
pwp.print(pp.getJobId());
pwp.print(",");
pwp.print(pp.getIncome());
pwp.print(pp.getAnnualIncome());
pwp.println();

if (pp.getId() == SiloUtil.trackPp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void summarizeAverageHouseholdSize() {
}

private void summarizeHouseholdIncome() {
List<Integer> incomes = dataContainer.getHouseholdDataManager().getHouseholds().stream().map(h -> h.getPersons().values().stream().mapToInt(p -> p.getIncome()).sum()).collect(Collectors.toList());
List<Integer> incomes = dataContainer.getHouseholdDataManager().getHouseholds().stream().map(h -> h.getPersons().values().stream().mapToInt(p -> p.getAnnualIncome()).sum()).collect(Collectors.toList());
double aveHHincome = incomes.stream().mapToDouble(i -> i).average().getAsDouble();
double medianHhIncome = Quantiles.median().compute(incomes);
String row = "AveHHInc," + aveHHincome + ",MedianHHInc," + medianHhIncome;
Expand Down Expand Up @@ -279,7 +279,7 @@ private void summarizeHousingCostsByIncomeGroup() {
int[][] rentByIncome = new int[10][10];
int[] rents = new int[10];
for (Household hh : dataContainer.getHouseholdDataManager().getHouseholds()) {
int hhInc = HouseholdUtil.getHhIncome(hh);
int hhInc = HouseholdUtil.getAnnualHhIncome(hh);
int rent = dataContainer.getRealEstateDataManager().getDwelling(hh.getDwellingId()).getPrice();
int incCat = Math.min((hhInc / 10000), 9);
int rentCat = Math.min((rent / 250), 9);
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.checkIfOnlyChildrenRemaining(hhOfPersonToDie);
final boolean onlyChildrenLeft = HouseholdUtil.checkIfAdultsPresent(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 @@ -363,7 +363,7 @@ private void movePerson(Person person1, Household moveTo) {
if (!moveTo.equals(household1)) {
householdDataManager.removePersonFromHousehold(person1);
householdDataManager.addPersonToHousehold(person1, moveTo);
if (HouseholdUtil.checkIfOnlyChildrenRemaining(household1)) {
if (HouseholdUtil.checkIfAdultsPresent(household1)) {
moveRemainingChildren(household1, moveTo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void finishOverwriteTracer () {
Household hh = householdDataManager.getHouseholdFromId(dd.getResidentId());
householdId[row-1] = hh.getId();
householdSize[row-1] = hh.getHhSize();
householdInc[row-1] = HouseholdUtil.getHhIncome(hh);
householdInc[row-1] = HouseholdUtil.getAnnualHhIncome(hh);
householdAuto[row-1] = hh.getAutos();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public Occupation getOccupation() {
}

@Override
public int getIncome() {
return delegate.getIncome();
public int getAnnualIncome() {
return delegate.getAnnualIncome();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Person giveBirth(Person parent, int id, Gender gender) {
public Person duplicate(Person originalPerson, int nextPersonId) {
PersonCapeTown duplicate = new PersonCapeTown(nextPersonId, originalPerson.getAge(),
originalPerson.getGender(), originalPerson.getOccupation(),
originalPerson.getRole(), originalPerson.getJobId(), originalPerson.getIncome());
originalPerson.getRole(), originalPerson.getJobId(), originalPerson.getAnnualIncome());
duplicate.setRace(((PersonCapeTown) originalPerson).getRace());
duplicate.setDriverLicense(originalPerson.hasDriverLicense());
return duplicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void writePersons(String path) {
pwp.print(",");
pwp.print(pp.getJobId());
pwp.print(",");
pwp.print(pp.getIncome());
pwp.print(pp.getAnnualIncome());
pwp.print(",");
pwp.print(((PersonCapeTown)pp).getRace());
pwp.println();
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.checkIfOnlyChildrenRemaining(household1)) {
if (HouseholdUtil.checkIfAdultsPresent(household1)) {
moveRemainingChildren(household1, moveTo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public PersonTak duplicate(Person originalPerson, int id) {
originalPerson.getOccupation(),
originalPerson.getRole(),
-1,
originalPerson.getIncome());
originalPerson.getAnnualIncome());
duplicate.setDriverLicense(originalPerson.hasDriverLicense());
duplicate.setSchoolId(((PersonTak) originalPerson).getSchoolId());
return duplicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public Occupation getOccupation() {
}

@Override
public int getIncome() {
return delegate.getIncome();
public int getAnnualIncome() {
return delegate.getAnnualIncome();
}

@Override
Expand Down Expand Up @@ -152,7 +152,7 @@ public PersonTak duplicate(Person originalPerson, int id) {
originalPerson.getOccupation(),
originalPerson.getRole(),
-1,
originalPerson.getIncome());
originalPerson.getAnnualIncome());
duplicate.setDriverLicense(originalPerson.hasDriverLicense());
duplicate.setSchoolId(((PersonTak) originalPerson).getSchoolId());
return duplicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void writePersons(String path) {
pwp.print(",");
pwp.print(pp.getJobId());
pwp.print(",");
pwp.print(pp.getIncome());
pwp.print(pp.getAnnualIncome());
pwp.print(",");
pwp.print(((PersonTak) pp).getSchoolId());
pwp.println();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.tum.bgu.msm.models.carOwnership;

import de.tum.bgu.msm.utils.Sampler;

interface CreateCarOwnershipStrategyImpl {
Sampler<Integer> getSampler(int license, int workers, int income, double logDistanceToTransit, int areaType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package de.tum.bgu.msm.models.carOwnership;

import de.tum.bgu.msm.utils.Sampler;
import de.tum.bgu.msm.utils.SiloUtil;

import java.util.Random;

public class CreateCarOwnershipStrategyTakImpl implements CreateCarOwnershipStrategyImpl {

private final static Integer[] options = {0,1,2,3};
private final static double[] betaLicense = {3.11410, 4.65460, 5.71850};
private final static double[] betaWorkers = {0.22840, 0.76420, 1.13260};
private final static double[] betaIncome = {0.00070, 0.00100, 0.00120};
private final static double[] betaDistance = {0.15230, 0.25180, 0.25930};
private final static double[][] betasAreaType = {
{0., 0.88210, 0.99270, 1.34420},
{0., 1.43410, 1.60730, 2.25490},
{0., 1.69830, 1.91330, 2.93080}
};

private final static double[] intercept = {-4.69730, -10.98800, -17.00200};

private final Random random;

public CreateCarOwnershipStrategyTakImpl() {
this.random = SiloUtil.getRandomObject();
}

@Override
public Sampler<Integer> getSampler(int license, int workers, int income, double logDistanceToTransit, int areaType) {

double[] probs = new double[options.length];
double sum = 0;
for(int i = 0; i < options.length-1; i++) {
double utility = intercept[i] + (betaLicense[i] * license) + (betaWorkers[i] * workers) + (betaIncome[i] * income) + (betaDistance[i] * logDistanceToTransit) + betasAreaType[i][areaType/10 -1];
double result = Math.exp(utility);
sum += result;
probs[i+1] = result;
}

double prob0cars = 1.0 / (sum + 1.0);

sum = 0;
for(int i = 0; i < options.length-1; i++) {
probs[i+1] = probs[i+1] * prob0cars;
sum += probs[i+1];
}

probs[0] = 1 - sum;

return new Sampler<>(options, probs, random);
}
}
Loading

0 comments on commit 6cb8310

Please sign in to comment.