|
7 | 7 | import de.tum.bgu.msm.data.travelTimes.TravelTimes; |
8 | 8 | import de.tum.bgu.msm.modules.modeChoice.ModeChoiceCalibrationData; |
9 | 9 | import org.matsim.api.core.v01.population.Population; |
| 10 | +import org.renjin.primitives.vector.RowNamesVector; |
| 11 | +import org.renjin.sexp.*; |
10 | 12 |
|
11 | 13 | import java.util.*; |
| 14 | +import java.util.function.Predicate; |
12 | 15 |
|
13 | 16 | public class DataSet { |
14 | 17 |
|
@@ -186,6 +189,22 @@ public static int getStudentsForHousehold(MitoHousehold household) { |
186 | 189 |
|
187 | 190 | } |
188 | 191 |
|
| 192 | + public static int countMembersByAttribute(MitoHousehold household,int minAge,int maxAge) { |
| 193 | + return (int) household.getPersons().values().stream().filter(person -> |
| 194 | + person.getAge() >= minAge & person.getAge() <= maxAge).count(); |
| 195 | + } |
| 196 | + |
| 197 | + public static int countMembersByAttribute(MitoHousehold household,int minAge,int maxAge, MitoOccupationStatus occupation) { |
| 198 | + return (int) household.getPersons().values().stream().filter(person -> |
| 199 | + person.getAge() >= minAge & person.getAge() <= maxAge & |
| 200 | + person.getMitoOccupationStatus().equals(occupation)).count(); |
| 201 | + } |
| 202 | + |
| 203 | + public static int countMembersByFilter(MitoHousehold household, Predicate<MitoPerson> filter) { |
| 204 | + return (int) household.getPersons().values().stream().filter(filter).count(); |
| 205 | + } |
| 206 | + |
| 207 | + |
189 | 208 | public static int getLicenseHoldersForHousehold(MitoHousehold household) { |
190 | 209 | return (int) household.getPersons().values().stream().filter(MitoPerson::hasDriversLicense).count(); |
191 | 210 | } |
@@ -242,6 +261,226 @@ public ModeChoiceCalibrationData getModeChoiceCalibrationData() { |
242 | 261 | return modeChoiceCalibrationData; |
243 | 262 | } |
244 | 263 |
|
| 264 | + private int determineAreaType(MitoHousehold hh) { |
| 265 | + int areaType = -1; |
| 266 | + if (hh.getHomeZone() != null) { |
| 267 | + areaType = hh.getHomeZone().getAreaTypeSG().code() / 10; |
| 268 | + } else { |
| 269 | + System.out.println("Home MitoZone for Household " + hh.getId() + " is null!"); |
| 270 | + } |
| 271 | + return areaType; |
| 272 | + } |
| 273 | + |
| 274 | + private ListVector RDataFrame; |
| 275 | + public ListVector getRdataFrame() {return this.RDataFrame; } |
| 276 | + |
| 277 | + // Method to create R data frame |
| 278 | + public void buildRdataFrame() { |
| 279 | + |
| 280 | + //Create Builders for each Variable |
| 281 | + IntArrayVector.Builder hhId = new IntArrayVector.Builder(); |
| 282 | + |
| 283 | + IntArrayVector.Builder hhSize = new IntArrayVector.Builder(); |
| 284 | + IntArrayVector.Builder hhSize1 = new IntArrayVector.Builder(); |
| 285 | + IntArrayVector.Builder hhSize2 = new IntArrayVector.Builder(); |
| 286 | + IntArrayVector.Builder hhSize3 = new IntArrayVector.Builder(); |
| 287 | + IntArrayVector.Builder hhSize4 = new IntArrayVector.Builder(); |
| 288 | + IntArrayVector.Builder hhSize5 = new IntArrayVector.Builder(); |
| 289 | + IntArrayVector.Builder hhSize345 = new IntArrayVector.Builder(); |
| 290 | + IntArrayVector.Builder hhSize45 = new IntArrayVector.Builder(); |
| 291 | + |
| 292 | + DoubleArrayVector.Builder hhTravelTimeBudgetHBW = new DoubleArrayVector.Builder(); |
| 293 | + DoubleArrayVector.Builder hhTravelTimeBudgetHBE = new DoubleArrayVector.Builder(); |
| 294 | + DoubleArrayVector.Builder hhTravelTimeBudgetHBS = new DoubleArrayVector.Builder(); |
| 295 | + DoubleArrayVector.Builder hhTravelTimeBudgetHBR = new DoubleArrayVector.Builder(); |
| 296 | + DoubleArrayVector.Builder hhTravelTimeBudgetHBO = new DoubleArrayVector.Builder(); |
| 297 | + DoubleArrayVector.Builder hhTravelTimeBudgetNHBW = new DoubleArrayVector.Builder(); |
| 298 | + DoubleArrayVector.Builder hhTravelTimeBudgetNHBO = new DoubleArrayVector.Builder(); |
| 299 | + |
| 300 | + DoubleArrayVector.Builder hhTravelTimeBudgetHBO_Size1 = new DoubleArrayVector.Builder(); |
| 301 | + DoubleArrayVector.Builder hhTravelTimeBudgetHBO_Size2 = new DoubleArrayVector.Builder(); |
| 302 | + DoubleArrayVector.Builder hhTravelTimeBudgetHBO_Size345 = new DoubleArrayVector.Builder(); |
| 303 | + DoubleArrayVector.Builder hhTravelTimeBudgetNHBO_Size1 = new DoubleArrayVector.Builder(); |
| 304 | + DoubleArrayVector.Builder hhTravelTimeBudgetNHBO_Size2 = new DoubleArrayVector.Builder(); |
| 305 | + DoubleArrayVector.Builder hhTravelTimeBudgetNHBO_Size345 = new DoubleArrayVector.Builder(); |
| 306 | + |
| 307 | + IntArrayVector.Builder hhPersons0to6 = new IntArrayVector.Builder(); |
| 308 | + IntArrayVector.Builder hhPersons6to17 = new IntArrayVector.Builder(); |
| 309 | + IntArrayVector.Builder hhPersons18to29_worker = new IntArrayVector.Builder(); |
| 310 | + IntArrayVector.Builder hhPersons18to29_student = new IntArrayVector.Builder(); |
| 311 | + IntArrayVector.Builder hhPersons18to29_unemployed = new IntArrayVector.Builder(); |
| 312 | + IntArrayVector.Builder hhPersons30to64_worker = new IntArrayVector.Builder(); |
| 313 | + IntArrayVector.Builder hhPersons30to64_student = new IntArrayVector.Builder(); |
| 314 | + IntArrayVector.Builder hhPersons30to64_unemployed = new IntArrayVector.Builder(); |
| 315 | + IntArrayVector.Builder hhPersons65up = new IntArrayVector.Builder(); |
| 316 | + |
| 317 | + IntArrayVector.Builder hhPersonsFemale = new IntArrayVector.Builder(); |
| 318 | + IntArrayVector.Builder hhPersonsWithMobilityRestriction = new IntArrayVector.Builder(); |
| 319 | + |
| 320 | + IntArrayVector.Builder hhEconomicStatus = new IntArrayVector.Builder(); |
| 321 | + IntArrayVector.Builder hhEconomicStatus2 = new IntArrayVector.Builder(); |
| 322 | + IntArrayVector.Builder hhEconomicStatus3 = new IntArrayVector.Builder(); |
| 323 | + IntArrayVector.Builder hhEconomicStatus4 = new IntArrayVector.Builder(); |
| 324 | + IntArrayVector.Builder hhEconomicStatus5 = new IntArrayVector.Builder(); |
| 325 | + IntArrayVector.Builder hhEconomicStatus45 = new IntArrayVector.Builder(); |
| 326 | + |
| 327 | + IntArrayVector.Builder hhAutos = new IntArrayVector.Builder(); |
| 328 | + DoubleArrayVector.Builder hhPropAutos = new DoubleArrayVector.Builder(); |
| 329 | + |
| 330 | + IntArrayVector.Builder hhRegionType = new IntArrayVector.Builder(); |
| 331 | + IntArrayVector.Builder hhRegionType2 = new IntArrayVector.Builder(); |
| 332 | + IntArrayVector.Builder hhRegionType3 = new IntArrayVector.Builder(); |
| 333 | + IntArrayVector.Builder hhRegionType4 = new IntArrayVector.Builder(); |
| 334 | + |
| 335 | + // Loop through dataSet and assign values |
| 336 | + final Iterator<MitoHousehold> iterator = this.getHouseholds().values().iterator(); |
| 337 | + for (; iterator.hasNext(); ) { |
| 338 | + MitoHousehold hh = iterator.next(); |
| 339 | + |
| 340 | + hhId.add(hh.getId()); |
| 341 | + int householdSize = hh.getHhSize(); |
| 342 | + hhSize.add(householdSize); |
| 343 | + hhSize1.add(householdSize == 1 ? 1 : 0); |
| 344 | + hhSize2.add(householdSize == 2 ? 1 : 0); |
| 345 | + hhSize3.add(householdSize == 3 ? 1 : 0); |
| 346 | + hhSize4.add(householdSize == 4 ? 1 : 0); |
| 347 | + hhSize5.add(householdSize >= 5 ? 1 : 0); |
| 348 | + hhSize345.add(householdSize >= 3 ? 1 : 0); |
| 349 | + hhSize45.add(householdSize >= 4 ? 1 : 0); |
| 350 | + |
| 351 | + // todo: include predicted TTBs (once TTB is moved before TripGen). The following numbers are just averages. |
| 352 | + hhTravelTimeBudgetHBW.add(26); |
| 353 | + hhTravelTimeBudgetHBE.add(7); |
| 354 | + hhTravelTimeBudgetHBS.add(14); |
| 355 | + hhTravelTimeBudgetHBR.add(31); |
| 356 | + hhTravelTimeBudgetHBO.add(30); |
| 357 | + hhTravelTimeBudgetNHBW.add(6); |
| 358 | + hhTravelTimeBudgetNHBO.add(23); |
| 359 | + |
| 360 | + hhTravelTimeBudgetHBO_Size1.add(30); |
| 361 | + hhTravelTimeBudgetHBO_Size2.add(30); |
| 362 | + hhTravelTimeBudgetHBO_Size345.add(30); |
| 363 | + hhTravelTimeBudgetNHBO_Size1.add(23); |
| 364 | + hhTravelTimeBudgetNHBO_Size2.add(23); |
| 365 | + hhTravelTimeBudgetNHBO_Size345.add(23); |
| 366 | + |
| 367 | + hhPersons0to6.add(DataSet.countMembersByFilter(hh, mitoPerson -> |
| 368 | + mitoPerson.getAge() < 6)); |
| 369 | + hhPersons6to17.add(DataSet.countMembersByFilter(hh, mitoPerson -> |
| 370 | + mitoPerson.getAge() >= 6 & mitoPerson.getAge() <= 17)); |
| 371 | + hhPersons18to29_worker.add(DataSet.countMembersByFilter(hh, mitoPerson -> |
| 372 | + mitoPerson.getAge() >= 18 & mitoPerson.getAge() <= 29 & |
| 373 | + mitoPerson.getMitoOccupationStatus().equals(MitoOccupationStatus.WORKER))); |
| 374 | + hhPersons18to29_student.add(DataSet.countMembersByFilter(hh, mitoPerson -> |
| 375 | + mitoPerson.getAge() >= 18 & mitoPerson.getAge() <= 29 & |
| 376 | + mitoPerson.getMitoOccupationStatus().equals(MitoOccupationStatus.STUDENT))); |
| 377 | + hhPersons18to29_unemployed.add(DataSet.countMembersByFilter(hh, mitoPerson -> |
| 378 | + mitoPerson.getAge() >= 18 & mitoPerson.getAge() <= 29 & |
| 379 | + mitoPerson.getMitoOccupationStatus().equals(MitoOccupationStatus.UNEMPLOYED))); |
| 380 | + hhPersons30to64_worker.add(DataSet.countMembersByFilter(hh, mitoPerson -> |
| 381 | + mitoPerson.getAge() >= 30 & mitoPerson.getAge() <= 64 & |
| 382 | + mitoPerson.getMitoOccupationStatus().equals(MitoOccupationStatus.WORKER))); |
| 383 | + hhPersons30to64_student.add(DataSet.countMembersByFilter(hh, mitoPerson -> |
| 384 | + mitoPerson.getAge() >= 30 & mitoPerson.getAge() <= 64 & |
| 385 | + mitoPerson.getMitoOccupationStatus().equals(MitoOccupationStatus.STUDENT))); |
| 386 | + hhPersons30to64_unemployed.add(DataSet.countMembersByFilter(hh, mitoPerson -> |
| 387 | + mitoPerson.getAge() >= 30 & mitoPerson.getAge() <= 64 & |
| 388 | + mitoPerson.getMitoOccupationStatus().equals(MitoOccupationStatus.UNEMPLOYED))); |
| 389 | + hhPersons65up.add(DataSet.countMembersByFilter(hh, mitoPerson -> |
| 390 | + mitoPerson.getAge() >= 65)); |
| 391 | + |
| 392 | + |
| 393 | + hhPersonsFemale.add(DataSet.countMembersByFilter(hh, mitoPerson -> |
| 394 | + mitoPerson.getMitoGender().equals(MitoGender.FEMALE))); |
| 395 | + hhPersonsWithMobilityRestriction.add(0); //todo: include actual disability status (currently 0) |
| 396 | + |
| 397 | + int economicStatus = hh.getEconomicStatus(); |
| 398 | + hhEconomicStatus.add(economicStatus); |
| 399 | + hhEconomicStatus2.add(economicStatus == 2 ? 1 : 0); |
| 400 | + hhEconomicStatus3.add(economicStatus == 3 ? 1 : 0); |
| 401 | + hhEconomicStatus4.add(economicStatus == 4 ? 1 : 0); |
| 402 | + hhEconomicStatus5.add(economicStatus == 5 ? 1 : 0); |
| 403 | + hhEconomicStatus45.add(economicStatus >= 4 ? 1 : 0); |
| 404 | + |
| 405 | + hhAutos.add(hh.getAutos()); |
| 406 | + hhPropAutos.add(Math.min(1.0,((double) hh.getAutos() / DataSet.countMembersByFilter(hh, mitoPerson -> |
| 407 | + mitoPerson.getAge() >= 15)))); |
| 408 | + |
| 409 | + int hhAreaType = determineAreaType(hh); |
| 410 | + hhRegionType.add(hhAreaType); |
| 411 | + hhRegionType2.add(hhAreaType == 2 ? 1 : 0); |
| 412 | + hhRegionType3.add(hhAreaType == 3 ? 1 : 0); |
| 413 | + hhRegionType4.add(hhAreaType == 4 ? 1 : 0); |
| 414 | + } |
| 415 | + |
| 416 | + // Add all vectors to a R data frame |
| 417 | + ListVector.NamedBuilder RModelDataBuilder = new ListVector.NamedBuilder(); |
| 418 | + RModelDataBuilder.setAttribute(Symbols.CLASS, StringVector.valueOf("data.frame")); |
| 419 | + RModelDataBuilder.setAttribute(Symbols.ROW_NAMES, new RowNamesVector(this.getHouseholds().size())); |
| 420 | + RModelDataBuilder.add("hh.id",hhId.build()); |
| 421 | + RModelDataBuilder.add("hh.size",hhSize.build()); |
| 422 | + RModelDataBuilder.add("hh.size.1",hhSize1.build()); |
| 423 | + RModelDataBuilder.add("hh.size.2",hhSize2.build()); |
| 424 | + RModelDataBuilder.add("hh.size.3",hhSize3.build()); |
| 425 | + RModelDataBuilder.add("hh.size.4",hhSize4.build()); |
| 426 | + RModelDataBuilder.add("hh.size.5",hhSize5.build()); |
| 427 | + RModelDataBuilder.add("hh.size.345",hhSize345.build()); |
| 428 | + RModelDataBuilder.add("hh.size.45",hhSize45.build()); |
| 429 | + |
| 430 | + RModelDataBuilder.add("hh.TTB.HBW",hhTravelTimeBudgetHBW.build()); |
| 431 | + RModelDataBuilder.add("hh.TTB.HBE",hhTravelTimeBudgetHBE.build()); |
| 432 | + RModelDataBuilder.add("hh.TTB.HBS",hhTravelTimeBudgetHBS.build()); |
| 433 | + RModelDataBuilder.add("hh.TTB.HBR",hhTravelTimeBudgetHBR.build()); |
| 434 | + RModelDataBuilder.add("hh.TTB.HBO",hhTravelTimeBudgetHBO.build()); |
| 435 | + RModelDataBuilder.add("hh.TTB.NHBW",hhTravelTimeBudgetNHBW.build()); |
| 436 | + RModelDataBuilder.add("hh.TTB.NHBO",hhTravelTimeBudgetNHBO.build()); |
| 437 | + |
| 438 | + RModelDataBuilder.add("hh.TTB.HBO.size.1",hhTravelTimeBudgetHBO_Size1.build()); |
| 439 | + RModelDataBuilder.add("hh.TTB.HBO.size.2",hhTravelTimeBudgetHBO_Size2.build()); |
| 440 | + RModelDataBuilder.add("hh.TTB.HBO.size.345",hhTravelTimeBudgetHBO_Size345.build()); |
| 441 | + |
| 442 | + RModelDataBuilder.add("hh.TTB.NHBO.size.1",hhTravelTimeBudgetNHBO_Size1.build()); |
| 443 | + RModelDataBuilder.add("hh.TTB.NHBO.size.2",hhTravelTimeBudgetNHBO_Size2.build()); |
| 444 | + RModelDataBuilder.add("hh.TTB.NHBO.size.345",hhTravelTimeBudgetNHBO_Size345.build()); |
| 445 | + |
| 446 | + RModelDataBuilder.add("hh.pers_under6",hhPersons0to6.build()); |
| 447 | + RModelDataBuilder.add("hh.pers_6to17",hhPersons6to17.build()); |
| 448 | + RModelDataBuilder.add("hh.pers_18to29_w",hhPersons18to29_worker.build()); |
| 449 | + RModelDataBuilder.add("hh.pers_18to29_s",hhPersons18to29_student.build()); |
| 450 | + RModelDataBuilder.add("hh.pers_18to29_u",hhPersons18to29_unemployed.build()); |
| 451 | + RModelDataBuilder.add("hh.pers_30to64_w",hhPersons30to64_worker.build()); |
| 452 | + RModelDataBuilder.add("hh.pers_30to64_s",hhPersons30to64_student.build()); |
| 453 | + RModelDataBuilder.add("hh.pers_30to64_u",hhPersons30to64_unemployed.build()); |
| 454 | + RModelDataBuilder.add("hh.pers_65up",hhPersons65up.build()); |
| 455 | + |
| 456 | + RModelDataBuilder.add("hh.pers_female",hhPersonsFemale.build()); |
| 457 | + RModelDataBuilder.add("hh.pers_mobility_restriction",hhPersonsWithMobilityRestriction.build()); |
| 458 | + |
| 459 | + RModelDataBuilder.add("hh.economic_status",hhEconomicStatus.build()); |
| 460 | + RModelDataBuilder.add("hh.economic_status.2",hhEconomicStatus2.build()); |
| 461 | + RModelDataBuilder.add("hh.economic_status.3",hhEconomicStatus3.build()); |
| 462 | + RModelDataBuilder.add("hh.economic_status.4",hhEconomicStatus4.build()); |
| 463 | + RModelDataBuilder.add("hh.economic_status.5",hhEconomicStatus5.build()); |
| 464 | + |
| 465 | + RModelDataBuilder.add("hh.autos",hhAutos.build()); |
| 466 | + RModelDataBuilder.add("hh.prop_autos",hhPropAutos.build()); |
| 467 | + |
| 468 | + RModelDataBuilder.add("hh.BBSR",hhRegionType.build()); |
| 469 | + RModelDataBuilder.add("hh.BBSR.2",hhRegionType2.build()); |
| 470 | + RModelDataBuilder.add("hh.BBSR.3",hhRegionType3.build()); |
| 471 | + RModelDataBuilder.add("hh.BBSR.4",hhRegionType4.build()); |
| 472 | + |
| 473 | + this.RDataFrame = RModelDataBuilder.build(); |
| 474 | + |
| 475 | + } |
| 476 | + |
| 477 | + |
| 478 | + |
| 479 | + |
| 480 | + |
| 481 | + |
| 482 | + |
| 483 | + |
245 | 484 |
|
246 | 485 |
|
247 | 486 |
|
|
0 commit comments