public HouseholdBasedVehicleRessources( final Households hhs) { this.households = hhs; for (Household hh : hhs.getHouseholds().values()) { final Id hhId = hh.getId(); for (Id agentId : hh.getMemberIds()) { agent2hh.put( agentId , hhId ); } } }
@Override public Set<Id<Vehicle>> identifyVehiclesUsableForAgent(final Id<Person> person) { // we can't simply remember an agent->hh mapping at construction, // as the households container is mutable. It would not make any sense // to modify it, but one is never too prudent. final Id<Household> hhId = agent2hh.get( person ); if ( hhId == null ) throw new RuntimeException( "no household known for "+person ); final Household hh = households.getHouseholds().get( hhId ); if ( hh == null ) throw new RuntimeException( "household "+hhId+" vanished!" ); if ( !hh.getMemberIds().contains( person ) ) throw new RuntimeException( "household "+hhId+" does not contain "+person ); return new HashSet<Id<Vehicle>>( hh.getVehicleIds() ); } }
@Override public int compare(Household o1, Household o2) { if (o1.getIncome().getIncomePeriod() != o2.getIncome().getIncomePeriod()){ throw new IllegalArgumentException("Can only compare Households with incomes in " + " same income period"); } if (o1.getIncome().getIncome() < o2.getIncome().getIncome()){ return -1; } else if (o1.getIncome().getIncome() > o2.getIncome().getIncome()){ return 1; } return 0; }
/*package*/ void writeHousehold(Household h) throws UncheckedIOException { this.atts.clear(); atts.add(createTuple(HouseholdsSchemaV10Names.ID, h.getId().toString())); this.writeStartTag(HouseholdsSchemaV10Names.HOUSEHOLD, atts); if ((h.getMemberIds() != null) && !h.getMemberIds().isEmpty()){ this.writeMembers(h.getMemberIds()); } if ((h.getVehicleIds() != null) && !h.getVehicleIds().isEmpty()) { this.writeStartTag(HouseholdsSchemaV10Names.VEHICLES, null); for (Id<Vehicle> id : h.getVehicleIds()){ atts.clear(); atts.add(createTuple(HouseholdsSchemaV10Names.REFID, id.toString())); this.writeStartTag(HouseholdsSchemaV10Names.VEHICLEDEFINITIONID, atts, true); } this.writeEndTag(HouseholdsSchemaV10Names.VEHICLES); } if (h.getIncome() != null){ this.writeIncome(h.getIncome()); } AttributesXmlWriterDelegate attributesWriter = new AttributesXmlWriterDelegate(); attributesWriter.putAttributeConverters(this.attributeConverters); try { this.writer.write(NL); } catch (IOException e) { e.printStackTrace(); } attributesWriter.writeAttributes( "\t\t" , this.writer , h.getAttributes() ); this.writeEndTag(HouseholdsSchemaV10Names.HOUSEHOLD); }
/** * Adds the household to the container. If streaming is set, the household * is added, the algorithms are run on the household, and it is subsequently * removed. If not, the household is added incrementally to the container. * @param household * @throws IllegalArgumentException if the container already includes the * {@link Id} of the household being added. */ public final void addHousehold(final Household household){ /* Validate that a household with the same Id does not exist yet. */ if(this.getHouseholds().containsKey(household.getId())){ throw new IllegalArgumentException("Household with Id " + household.getId() + " already exisits."); } this.households.put(household.getId(), household); }
public void reinitialize(Households hhs) { this.phMap.clear(); for (Household h : hhs.getHouseholds().values()){ for (Id<Person> member : h.getMemberIds()){ this.phMap.put(member, h); } } }
currAttributes = this.currentHousehold.getAttributes(); attributesReader.startTag( name , atts , context, currAttributes );
private void fillHousehold() { ((HouseholdImpl) this.currentHousehold).setMemberIds(this.currentmembers); ((HouseholdImpl) this.currentHousehold).setVehicleIds(this.currentVehicleIds); this.currentHousehold.setIncome(this.currentincome); this.currentHhId = null; this.currentVehicleIds = null; this.currentincome = null; this.currentmembers = null; this.currentIncomePeriod = null; this.currentincomeCurrency = null; }
Household hh = households.getHouseholds().get(id23); assertNotNull(hh); assertEquals(id23, hh.getId()); assertEquals(3, hh.getMemberIds().size()); List<Id<Person>> hhmemberIds = new ArrayList<>(); hhmemberIds.addAll(hh.getMemberIds()); Collections.sort(hhmemberIds); assertEquals(pid23, hhmemberIds.get(0)); assertNotNull(hh.getVehicleIds()); List<Id<Vehicle>> vehIds = new ArrayList<>(); vehIds.addAll(hh.getVehicleIds()); Collections.sort(vehIds); assertEquals(2, vehIds.size()); assertNotNull(hh.getIncome()); assertNotNull(hh.getIncome().getIncomePeriod()); assertEquals(IncomePeriod.month, hh.getIncome().getIncomePeriod()); assertEquals("eur", hh.getIncome().getCurrency()); assertEquals(50000.0d, hh.getIncome().getIncome(), EPSILON); Attributes currentAttributes = hh.getAttributes(); assertNotNull("Custom attributes from household with id 23 should not be empty.", currentAttributes); String customAttributeName = "customAttribute1"; assertEquals(id24, hh.getId()); assertEquals(2, hh.getMemberIds().size()); assertEquals(pid44, hh.getMemberIds().get(0)); assertEquals(pid45, hh.getMemberIds().get(1));
public void runAlgorithms(Household h){ for(HouseholdAlgorithm algorithm : this.householdAlgorithms){ log.info("Running algorithm " + algorithm.getClass().getName() + " on household " + h.getId()); algorithm.run(h); } }
private static Collection<? extends Collection<Id<Person>>> extractGroups( final Households households) { final List<List<Id<Person>>> groups = new ArrayList<>(); for ( Household hh : households.getHouseholds().values() ) { groups.add( hh.getMemberIds() ); } return groups; }
/*package*/ Household createHousehold() { Household hh = this.builder.createHousehold(this.currentHhId); ((HouseholdImpl) hh).setMemberIds(this.currentmembers); ((HouseholdImpl) hh).setVehicleIds(this.currentVehicleIds); hh.setIncome(this.currentincome); this.currentHhId = null; this.currentVehicleIds = null; this.currentincome = null; this.currentmembers = null; this.currentIncomePeriod = null; this.currentincomeCurrency = null; return hh; }
/** * Test that households with the same {@link Id} are not accepted. */ public void testAddHousehold_DuplicateId(){ HouseholdsImpl hhs = new HouseholdsImpl(); Household hh1 = new HouseholdImpl(Id.create("1", Household.class)); Household hh2 = new HouseholdImpl(Id.create("1", Household.class)); assertEquals("Shouldn't have a household.", 0, hhs.getHouseholds().size()); hhs.addHousehold(hh1); assertEquals("Didn't add the household.", 1, hhs.getHouseholds().size()); assertEquals("Should have added the household.", hh1, hhs.getHouseholds().get(hh1.getId())); try{ hhs.addHousehold(hh2); fail("Should not have accepted household with similar Id."); } catch (IllegalArgumentException e){ } }