public FreespeedTravelTimeAndDisutility(PlanCalcScoreConfigGroup cnScoringGroup) {
   this(
       cnScoringGroup.getModes().get(TransportMode.car).getMarginalUtilityOfTraveling() / 3600.0,
       cnScoringGroup.getPerforming_utils_hr() / 3600.0,
       //				cnScoringGroup.getMarginalUtlOfDistanceCar());
       cnScoringGroup.getModes().get(TransportMode.car).getMonetaryDistanceRate()
           * cnScoringGroup.getMarginalUtilityOfMoney());
 }
 public SocialCostTravelDisutility(
     TravelTime travelTime,
     SocialCostCalculatorV2 scc,
     PlanCalcScoreConfigGroup cnScoringGroup) {
   this.travelTime = travelTime;
   this.scc = scc;
   this.marginalCostOfTime =
       (-cnScoringGroup.getModes().get(TransportMode.car).getMarginalUtilityOfTraveling()
               / 3600.0)
           + (cnScoringGroup.getPerforming_utils_hr() / 3600.0);
   this.marginalCostOfDistance =
       -cnScoringGroup.getModes().get(TransportMode.car).getMonetaryDistanceRate()
           * cnScoringGroup.getMarginalUtilityOfMoney();
 }
Example #3
0
  /** reads all activity utility values from the config-file */
  private static final void readUtilityValues(PlanCalcScoreConfigGroup config) {
    for (ActivityParams params : config.getActivityParams()) {
      //			String type = params.getType();
      //			double priority = params.getPriority();
      //			double typDurationSecs = params.getTypicalDuration();
      //			ActivityUtilityParameters actParams = new ActivityUtilityParameters(type, priority,
      // typDurationSecs);
      //			if (params.getMinimalDuration() >= 0) {
      //				actParams.setMinimalDuration(params.getMinimalDuration());
      //			}
      //			if (params.getOpeningTime() >= 0) {
      //				actParams.setOpeningTime(params.getOpeningTime());
      //			}
      //			if (params.getLatestStartTime() >= 0) {
      //				actParams.setLatestStartTime(params.getLatestStartTime());
      //			}
      //			if (params.getEarliestEndTime() >= 0) {
      //				actParams.setEarliestEndTime(params.getEarliestEndTime());
      //			}
      //			if (params.getClosingTime() >= 0) {
      //				actParams.setClosingTime(params.getClosingTime());
      //			}
      //			utilParams.put(type, actParams);

      ActivityUtilityParameters.Builder factory = new ActivityUtilityParameters.Builder(params);
      if (params.getActivityType().equals(PtConstants.TRANSIT_ACTIVITY_TYPE)) {
        factory.setScoreAtAll(false);
      }
      utilParams.put(params.getActivityType(), factory.build());
    }
  }
  // test overnight activities with first and last activity of different types
  @Test
  public final void test0c() {

    PlanCalcScoreConfigGroup plansCalcScoreConfigGroup = new PlanCalcScoreConfigGroup();

    ActivityParams activityParams1 = new ActivityParams("firstActivityType");
    activityParams1.setTypicalDuration(12 * 3600.);

    ActivityParams activityParams2 = new ActivityParams("lastActivityType");
    activityParams2.setTypicalDuration(12 * 3600.);

    plansCalcScoreConfigGroup.addActivityParams(activityParams1);
    plansCalcScoreConfigGroup.addActivityParams(activityParams2);

    plansCalcScoreConfigGroup.setEarlyDeparture_utils_hr(0.);
    plansCalcScoreConfigGroup.setLateArrival_utils_hr(0.);
    plansCalcScoreConfigGroup.setMarginalUtlOfWaiting_utils_hr(0.);
    plansCalcScoreConfigGroup.setPerforming_utils_hr(6.);

    ScenarioConfigGroup scenarioConfig = new ScenarioConfigGroup();
    CharyparNagelScoringParameters params =
        new CharyparNagelScoringParameters.Builder(
                plansCalcScoreConfigGroup,
                plansCalcScoreConfigGroup.getScoringParameters(null),
                scenarioConfig)
            .build();

    MarginalSumScoringFunction marginaSumScoringFunction = new MarginalSumScoringFunction(params);

    Id<Link> linkId = null;

    ActivityImpl activity1 = new ActivityImpl("firstActivityType", linkId);
    activity1.setEndTime(7 * 3600.);
    ActivityImpl activity2 = new ActivityImpl("lastActivityType", linkId);
    activity2.setStartTime(18 * 3600.);

    // test if zero delay results in zero activity delay disutility
    double delay1 = 0 * 3600.;
    double activityDelayDisutility1 =
        marginaSumScoringFunction.getOvernightActivityDelayDisutility(activity1, activity2, delay1);
    // 6 --> 10.0934029996839 utils + 7 --> 21.1922519472465 utils = 31.285654946930 utils
    // 6 --> 10.0934029996839 utils + 7 --> 21.1922519472465 utils = 31.285654946930 utils
    Assert.assertEquals(
        "Wrong disutility from starting an activity with a delay (arriving later at the activity location).",
        0.,
        activityDelayDisutility1,
        MatsimTestUtils.EPSILON);

    // test if a delay results in the right activity delay disutility
    double delay2 = 1 * 3600.;
    double activityDelayDisutility2 =
        marginaSumScoringFunction.getOvernightActivityDelayDisutility(activity1, activity2, delay2);
    // 6 --> 10.0934029996839 utils + 7 --> 21.1922519472465 utils = 31.285654946930 utils
    // 7 --> 21.1922519472465 utils + 7 --> 21.1922519472465 utils = 42.3845038944931 utils
    Assert.assertEquals(
        "Wrong disutility from starting an activity with a delay (arriving later at the activity location).",
        11.0988489475631,
        activityDelayDisutility2,
        MatsimTestUtils.EPSILON);
  }
 public EmissionScoringFunctionFactory(MatsimServices controler) {
   this.controler = controler;
   this.configGroup = controler.getConfig().planCalcScore();
   this.params =
       new CharyparNagelScoringParameters.Builder(
               configGroup,
               configGroup.getScoringParameters(null),
               controler.getConfig().scenario())
           .build();
   this.network = controler.getScenario().getNetwork();
   this.scoringFromEmissions = new ScoringFromEmissions(params);
 }
  public IncomeTravelCostCalculator(
      final TravelTime timeCalculator,
      PlanCalcScoreConfigGroup charyparNagelScoring,
      PersonHouseholdMapping personHouseholdMapping) {
    this.timeCalculator = timeCalculator;
    this.personHouseholdMapping = personHouseholdMapping;

    /* Usually, utility from traveling should be negative (it's a disutility)
     * but the cost should be positive. Thus negate the utility.
     * Distance dependent routing is only impelemted for car since pt is only pseudo transit*/

    //		this.distanceCostFactor = - charyparNagelScoring.getMarginalUtlOfDistanceCar();
    this.distanceCostFactor =
        -charyparNagelScoring.getModes().get(TransportMode.car).getMonetaryDistanceRate()
            * charyparNagelScoring.getMarginalUtilityOfMoney();

    // also opportunity costs of time have to be considered at this point (second summand)!
    this.betaTravelTime =
        (-charyparNagelScoring.getModes().get(TransportMode.car).getMarginalUtilityOfTraveling()
                / 3600.0)
            + (charyparNagelScoring.getPerforming_utils_hr() / 3600.0);
  }
  @Override
  public ScoringFunction createNewScoringFunction(Person person) {

    PlanCalcScoreConfigGroup configGroup = controler.getConfig().planCalcScore();
    CharyparNagelScoringParameters params =
        new CharyparNagelScoringParameters.Builder(
                configGroup,
                configGroup.getScoringParameters(null),
                controler.getConfig().scenario())
            .build();
    Network network = controler.getScenario().getNetwork();

    ScoringFunctionAccumulator accumulator = new ScoringFunctionAccumulator();

    accumulator.addScoringFunction(new CharyparNagelActivityScoring(params));
    accumulator.addScoringFunction(new CharyparNagelLegScoring(params, network));
    accumulator.addScoringFunction(new CharyparNagelAgentStuckScoring(params));

    accumulator.addScoringFunction(this.scoringFromEmissions);

    return accumulator;
  }
Example #8
0
  @Override
  public TravelDisutility createTravelDisutility(
      TravelTime timeCalculator, PlanCalcScoreConfigGroup cnScoringGroup) {
    double marginalutilityOfMoney = cnScoringGroup.getMarginalUtilityOfMoney();
    final ResDisCalculator resdiscal =
        new ResDisCalculator(
            tdf.createTravelDisutility(timeCalculator, cnScoringGroup),
            ecl,
            marginalutilityOfMoney,
            this.emissionModule,
            this.emissionCostModule);

    return resdiscal;
  }
Example #9
0
  protected static void init() {
    if (initialized) return;

    utilParams.clear();
    marginalUtilityOfWaiting = configGroup.getMarginalUtlOfWaiting_utils_hr() / 3600.0;
    marginalUtilityOfLateArrival = configGroup.getLateArrival_utils_hr() / 3600.0;
    marginalUtilityOfEarlyDeparture = configGroup.getEarlyDeparture_utils_hr() / 3600.0;
    marginalUtilityOfTraveling =
        configGroup.getModes().get(TransportMode.car).getMarginalUtilityOfTraveling() / 3600.0;
    marginalUtilityOfTravelingPT =
        configGroup.getModes().get(TransportMode.pt).getMarginalUtilityOfTraveling() / 3600.0;
    marginalUtilityOfTravelingWalk =
        configGroup.getModes().get(TransportMode.walk).getMarginalUtilityOfTraveling() / 3600.0;
    marginalUtilityOfPerforming = configGroup.getPerforming_utils_hr() / 3600.0;

    //		marginalUtilityOfPtFare = marginalUtilityOfPtFare;

    //		marginalUtilityOfFuel = configGroup.getMarginalUtlOfDistanceCar();
    marginalUtilityOfFuel =
        configGroup.getModes().get(TransportMode.car).getMonetaryDistanceRate()
            * configGroup.getMarginalUtilityOfMoney();

    abortedPlanScore =
        Math.min(
                Math.min(marginalUtilityOfLateArrival, marginalUtilityOfEarlyDeparture),
                Math.min(marginalUtilityOfTraveling, marginalUtilityOfWaiting))
            * 3600.0
            * 24.0; // SCENARIO_DURATION
    // TODO 24 has to be replaced by a variable like scenario_dur (see also other places below)

    readUtilityValues(configGroup);
    scoreActs =
        ((marginalUtilityOfPerforming != 0)
            || (marginalUtilityOfWaiting != 0)
            || (marginalUtilityOfLateArrival != 0)
            || (marginalUtilityOfEarlyDeparture != 0));
    initialized = true;
  }
    public Fixture() {
      firstLegStartTime = 7 * 3600;
      firstLegTravelTime = 30 * 60;
      thirdLegTravelTime = 30 * 60;
      secondLegStartTime = 10 * 3600;
      secondLegTravelTime = 15 * 60;
      thirdLegStartTime = 13 * 3600;
      fourthLegStartTime = 16 * 3600;
      fourthLegTravelTime = 15 * 60;
      // home act end 7am
      // work 7:30 to 10:00
      // work 10:15 to 13:00
      // work 13:30 to 16:00
      // home 15:15 to ...

      this.config = ConfigUtils.createConfig();
      PlanCalcScoreConfigGroup scoring = this.config.planCalcScore();
      scoring.setBrainExpBeta(2.0);

      scoring.setConstantCar(0.0);
      scoring.setConstantPt(0.0);
      scoring.setConstantWalk(0.0);
      scoring.setConstantBike(0.0);

      scoring.setEarlyDeparture_utils_hr(0.0);
      scoring.setLateArrival_utils_hr(0.0);
      scoring.setMarginalUtlOfWaiting_utils_hr(0.0);
      scoring.setPerforming_utils_hr(0.0);
      scoring.setTraveling_utils_hr(0.0);
      scoring.setTravelingPt_utils_hr(0.0);
      scoring.setTravelingWalk_utils_hr(0.0);
      scoring.setTravelingBike_utils_hr(0.0);

      scoring.setMarginalUtilityOfMoney(1.);
      scoring.setMonetaryDistanceCostRateCar(0.0);
      scoring.setMonetaryDistanceCostRatePt(0.0);

      // setup activity types h and w for scoring
      PlanCalcScoreConfigGroup.ActivityParams params =
          new PlanCalcScoreConfigGroup.ActivityParams("h");
      params.setTypicalDuration(15 * 3600);
      scoring.addActivityParams(params);

      params = new PlanCalcScoreConfigGroup.ActivityParams("w");
      params.setTypicalDuration(3 * 3600);
      scoring.addActivityParams(params);

      this.scenario = ScenarioUtils.createScenario(config);
      this.network = (NetworkImpl) this.scenario.getNetwork();
      Node node1 =
          this.network.createAndAddNode(Id.create("1", Node.class), new CoordImpl(0.0, 0.0));
      Node node2 =
          this.network.createAndAddNode(Id.create("2", Node.class), new CoordImpl(500.0, 0.0));
      Node node3 =
          this.network.createAndAddNode(Id.create("3", Node.class), new CoordImpl(5500.0, 0.0));
      Node node4 =
          this.network.createAndAddNode(Id.create("4", Node.class), new CoordImpl(6000.0, 0.0));
      Node node5 =
          this.network.createAndAddNode(Id.create("5", Node.class), new CoordImpl(11000.0, 0.0));
      Node node6 =
          this.network.createAndAddNode(Id.create("6", Node.class), new CoordImpl(11500.0, 0.0));
      Node node7 =
          this.network.createAndAddNode(Id.create("7", Node.class), new CoordImpl(16500.0, 0.0));
      Node node8 =
          this.network.createAndAddNode(Id.create("8", Node.class), new CoordImpl(17000.0, 0.0));
      Node node9 =
          this.network.createAndAddNode(Id.create("9", Node.class), new CoordImpl(22000.0, 0.0));
      Node node10 =
          this.network.createAndAddNode(Id.create("10", Node.class), new CoordImpl(22500.0, 0.0));

      Link link1 =
          this.network.createAndAddLink(Id.create("1", Link.class), node1, node2, 500, 25, 3600, 1);
      Link link2 =
          this.network.createAndAddLink(
              Id.create("2", Link.class), node2, node3, 25000, 50, 3600, 1);
      Link link3 =
          this.network.createAndAddLink(Id.create("3", Link.class), node3, node4, 500, 25, 3600, 1);
      this.network.createAndAddLink(Id.create("4", Link.class), node4, node5, 5000, 50, 3600, 1);
      Link link5 =
          this.network.createAndAddLink(Id.create("5", Link.class), node5, node6, 500, 25, 3600, 1);
      this.network.createAndAddLink(Id.create("6", Link.class), node6, node7, 5000, 50, 3600, 1);
      Link link7 =
          this.network.createAndAddLink(Id.create("7", Link.class), node7, node8, 500, 25, 3600, 1);
      this.network.createAndAddLink(Id.create("8", Link.class), node8, node9, 5000, 50, 3600, 1);
      Link link9 =
          this.network.createAndAddLink(
              Id.create("9", Link.class), node9, node10, 500, 25, 3600, 1);

      this.person = new PersonImpl(Id.create("1", Person.class));
      this.plan = this.person.createAndAddPlan(true);

      ActivityImpl firstActivity = this.plan.createAndAddActivity("h", link1.getId());
      firstActivity.setEndTime(firstLegStartTime);

      Leg leg = this.plan.createAndAddLeg(TransportMode.car);
      leg.setDepartureTime(firstLegStartTime);
      leg.setTravelTime(firstLegTravelTime);
      NetworkRoute route1 = new LinkNetworkRouteImpl(link1.getId(), link3.getId());
      route1.setLinkIds(link1.getId(), Arrays.asList(link2.getId()), link3.getId());
      route1.setTravelTime(firstLegTravelTime);
      route1.setDistance(RouteUtils.calcDistance(route1, this.network));
      leg.setRoute(route1);

      ActivityImpl secondActivity = this.plan.createAndAddActivity("w", link3.getId());
      secondActivity.setStartTime(firstLegStartTime + firstLegTravelTime);
      secondActivity.setEndTime(secondLegStartTime);
      leg = this.plan.createAndAddLeg(TransportMode.pt);
      leg.setDepartureTime(secondLegStartTime);
      leg.setTravelTime(secondLegTravelTime);
      Route route2 = new GenericRouteImpl(link3.getId(), link5.getId());
      route2.setTravelTime(secondLegTravelTime);
      route2.setDistance(20000.0);
      leg.setRoute(route2);

      ActivityImpl thirdActivity = this.plan.createAndAddActivity("w", link5.getId());
      thirdActivity.setStartTime(secondLegStartTime + secondLegTravelTime);
      thirdActivity.setEndTime(thirdLegStartTime);
      leg = this.plan.createAndAddLeg(TransportMode.walk);
      leg.setDepartureTime(thirdLegStartTime);
      leg.setTravelTime(thirdLegTravelTime);
      Route route3 = new GenericRouteImpl(link5.getId(), link7.getId());
      route3.setTravelTime(thirdLegTravelTime);
      route3.setDistance(CoordUtils.calcDistance(link5.getCoord(), link7.getCoord()));
      leg.setRoute(route3);

      ActivityImpl fourthActivity = this.plan.createAndAddActivity("w", link7.getId());
      fourthActivity.setStartTime(thirdLegStartTime + thirdLegTravelTime);
      fourthActivity.setEndTime(fourthLegStartTime);
      leg = this.plan.createAndAddLeg(TransportMode.bike);
      leg.setDepartureTime(fourthLegStartTime);
      leg.setTravelTime(fourthLegTravelTime);
      Route route4 = new GenericRouteImpl(link7.getId(), link9.getId());
      route4.setTravelTime(fourthLegTravelTime);
      route4.setDistance(CoordUtils.calcDistance(link7.getCoord(), link9.getCoord()));
      leg.setRoute(route4);

      ActivityImpl fifthActivity = this.plan.createAndAddActivity("h", link9.getId());
      fifthActivity.setStartTime(fourthLegStartTime + fourthLegTravelTime);
      this.scenario.getPopulation().addPerson(this.person);
    }
  public static void main(String[] args) {
    Config config = new Config();
    config.addCoreModules();
    Controler controler = new Controler(config);

    // controler settings
    controler
        .getConfig()
        .controler()
        .setOverwriteFileSetting(
            true
                ? OutputDirectoryHierarchy.OverwriteFileSetting.overwriteExistingFiles
                : OutputDirectoryHierarchy.OverwriteFileSetting.failIfDirectoryExists);
    controler.getConfig().controler().setCreateGraphs(false);

    // controlerConfigGroup
    ControlerConfigGroup ccg = controler.getConfig().controler();
    ccg.setOutputDirectory(outputPath);
    ccg.setFirstIteration(0);
    ccg.setLastIteration(0);
    ccg.setMobsim("qsim");
    Set set = new HashSet();
    set.add(EventsFileFormat.xml);
    ccg.setEventsFileFormats(set);
    //		ccg.setRunId("321");

    // qsimConfigGroup
    QSimConfigGroup qcg = controler.getConfig().qsim();
    qcg.setStartTime(0 * 3600.);
    qcg.setEndTime(30 * 3600.);
    qcg.setFlowCapFactor(0.1);
    qcg.setStorageCapFactor(0.3);
    //		qcg.setFlowCapFactor(0.01);
    //		qcg.setStorageCapFactor(0.03);
    qcg.setNumberOfThreads(1);
    qcg.setRemoveStuckVehicles(false);
    qcg.setStuckTime(10.0);

    // planCalcScoreConfigGroup
    PlanCalcScoreConfigGroup pcs = controler.getConfig().planCalcScore();
    Set<String> activities = new HashSet<String>();
    activities.add("unknown");
    activities.add("work");
    activities.add("pickup");
    activities.add("with adult");
    activities.add("other");
    activities.add("pvWork");
    activities.add("pvHome");
    activities.add("gvHome");
    activities.add("education");
    activities.add("business");
    activities.add("shopping");
    activities.add("private");
    activities.add("leisure");
    activities.add("sports");
    activities.add("home");
    activities.add("friends");

    for (String activity : activities) {
      ActivityParams params = new ActivityParams(activity);
      params.setTypicalDuration(30 * 3600);
      pcs.addActivityParams(params);
    }

    // strategy
    StrategyConfigGroup scg = controler.getConfig().strategy();
    StrategySettings strategySettings =
        new StrategySettings(Id.create("1", StrategySettings.class));
    strategySettings.setStrategyName("ChangeExpBeta");
    strategySettings.setWeight(1.0);
    scg.addStrategySettings(strategySettings);

    // network
    NetworkConfigGroup ncg = controler.getConfig().network();
    ncg.setInputFile(networkFile);

    // plans
    PlansConfigGroup pcg = controler.getConfig().plans();
    pcg.setInputFile(plansFile);

    // define emission tool input files

    EmissionsConfigGroup ecg = new EmissionsConfigGroup();
    controler.getConfig().addModule(ecg);
    ecg.setEmissionRoadTypeMappingFile(roadTypeMappingFile);
    ecg.setEmissionVehicleFile(emissionVehicleFile);

    ecg.setAverageWarmEmissionFactorsFile(averageFleetWarmEmissionFactorsFile);
    ecg.setAverageColdEmissionFactorsFile(averageFleetColdEmissionFactorsFile);

    ecg.setUsingDetailedEmissionCalculation(isUsingDetailedEmissionCalculation);
    ecg.setDetailedWarmEmissionFactorsFile(detailedWarmEmissionFactorsFile);
    ecg.setDetailedColdEmissionFactorsFile(detailedColdEmissionFactorsFile);

    // TODO: the following does not work yet. Need to force controler to always write events in the
    // last iteration.
    VspExperimentalConfigGroup vcg = controler.getConfig().vspExperimental();
    vcg.setWritingOutputEvents(false);

    controler.addControlerListener(new EmissionControlerListener());
    controler.run();
  }
 @Override
 public PathSizeLogitSelector get() {
   return new PathSizeLogitSelector(
       config.getPathSizeLogitBeta(), -config.getBrainExpBeta(), network);
 }
 @Override
 public ExpBetaPlanChanger<Plan, Person> get() {
   return new ExpBetaPlanChanger<>(-config.getBrainExpBeta());
 }
  public void run() {

    new MatsimNetworkReader(scenario).readFile(scenario.getConfig().network().getInputFile());
    new TransitScheduleReaderV1(scenario)
        .readFile(this.scenario.getConfig().transit().getTransitScheduleFile());
    Logger.getLogger(this.getClass())
        .fatal(
            "cannot say if the following should be vehicles or transit vehicles; aborting ... .  kai, feb'15");
    System.exit(-1);
    new VehicleReaderV1((scenario).getTransitVehicles())
        .readFile(this.scenario.getConfig().transit().getVehiclesFile());

    Controler controler = new Controler(this.scenario);
    controler
        .getConfig()
        .controler()
        .setOverwriteFileSetting(
            OutputDirectoryHierarchy.OverwriteFileSetting.overwriteExistingFiles);
    controler.addOverridingModule(new OTFVisFileWriterModule());
    controler.addControlerListener(
        new OptControlerListener(
            this.fare,
            this.scenario,
            this.calculate_inVehicleTimeDelayEffects,
            this.calculate_waitingTimeDelayEffects,
            this.calculate_capacityDelayEffects,
            this.marginalCostPricingPt,
            this.calculate_carCongestionEffects,
            this.marginalCostPricingCar));

    ControlerConfigGroup controlerConfGroup = controler.getConfig().controler();
    if (controlerConfGroup.getLastIteration() == 0) {
      controlerConfGroup.setWriteEventsInterval(1);
      controlerConfGroup.setWritePlansInterval(1);
    } else {
      controlerConfGroup.setWriteEventsInterval(controlerConfGroup.getLastIteration());
      controlerConfGroup.setWritePlansInterval(controlerConfGroup.getLastIteration());
    }

    PlanCalcScoreConfigGroup planCalcScoreConfigGroup = controler.getConfig().planCalcScore();
    planCalcScoreConfigGroup.setMarginalUtilityOfMoney(MARGINAL_UTILITY_OF_MONEY);
    planCalcScoreConfigGroup.setPerforming_utils_hr(PERFORMING);

    planCalcScoreConfigGroup.getModes().get(TransportMode.car).setConstant(CONSTANT_CAR);
    planCalcScoreConfigGroup
        .getModes()
        .get(TransportMode.car)
        .setMarginalUtilityOfTraveling(TRAVEL_CAR);
    planCalcScoreConfigGroup
        .getModes()
        .get(TransportMode.car)
        .setMonetaryDistanceRate(MONETARY_DISTANCE_COST_RATE_CAR);

    planCalcScoreConfigGroup.getModes().get(TransportMode.walk).setConstant(CONSTANT_WALK);
    planCalcScoreConfigGroup
        .getModes()
        .get(TransportMode.walk)
        .setMarginalUtilityOfTraveling(TRAVEL_WALK);
    planCalcScoreConfigGroup
        .getModes()
        .get(TransportMode.pt)
        .setMonetaryDistanceRate(MONETARY_DISTANCE_COST_RATE_PT);

    planCalcScoreConfigGroup.getModes().get(TransportMode.pt).setConstant(CONSTANT_PT);
    planCalcScoreConfigGroup
        .getModes()
        .get(TransportMode.pt)
        .setMarginalUtilityOfTraveling(TRAVEL_PT_IN_VEHICLE);
    planCalcScoreConfigGroup.setMarginalUtlOfWaitingPt_utils_hr(TRAVEL_PT_WAITING);
    planCalcScoreConfigGroup.setUtilityOfLineSwitch(LINE_SWITCH);

    planCalcScoreConfigGroup.setLateArrival_utils_hr(LATE_ARRIVAL);
    planCalcScoreConfigGroup.setEarlyDeparture_utils_hr(EARLY_DEPARTURE);
    planCalcScoreConfigGroup.setMarginalUtlOfWaiting_utils_hr(WAITING);

    OptimizationScoringFunctionFactory scoringfactory =
        new OptimizationScoringFunctionFactory(scenario, STUCK_SCORE);

    controler.setScoringFunctionFactory(scoringfactory);

    controler.getConfig().controler().setCreateGraphs(false);
    controler.run();
  }
  // test normal activities
  @Test
  public final void test0a() {

    PlanCalcScoreConfigGroup plansCalcScoreConfigGroup = new PlanCalcScoreConfigGroup();
    ActivityParams activityParams = new ActivityParams("work");
    activityParams.setTypicalDuration(6 * 3600.);
    activityParams.setOpeningTime(7 * 3600.);
    activityParams.setClosingTime(18 * 3600.);

    plansCalcScoreConfigGroup.addActivityParams(activityParams);
    plansCalcScoreConfigGroup.setEarlyDeparture_utils_hr(0.);
    plansCalcScoreConfigGroup.setLateArrival_utils_hr(0.);
    plansCalcScoreConfigGroup.setMarginalUtlOfWaiting_utils_hr(0.);
    plansCalcScoreConfigGroup.setPerforming_utils_hr(6.);

    ScenarioConfigGroup scenarioConfig = new ScenarioConfigGroup();

    CharyparNagelScoringParameters params =
        new CharyparNagelScoringParameters.Builder(
                plansCalcScoreConfigGroup,
                plansCalcScoreConfigGroup.getScoringParameters(null),
                scenarioConfig)
            .build();

    MarginalSumScoringFunction marginaSumScoringFunction = new MarginalSumScoringFunction(params);

    Id<Link> linkId = null;

    // test if zero delay results in zero activity delay disutility
    ActivityImpl activity1 = new ActivityImpl("work", linkId);
    activity1.setStartTime(10 * 3600.);
    activity1.setEndTime(16 * 3600.);
    double delay1 = 0 * 3600.;
    double activityDelayDisutility1 =
        marginaSumScoringFunction.getNormalActivityDelayDisutility(activity1, delay1);
    Assert.assertEquals(
        "Wrong disutility from starting an activity with a delay (arriving later at the activity location).",
        0.,
        activityDelayDisutility1,
        MatsimTestUtils.EPSILON);

    // test if a delay results in zero activity delay disutility if the agent would have arrived to
    // late at the activity location anyway
    ActivityImpl activity2 = new ActivityImpl("work", linkId);
    activity2.setStartTime(19 * 3600.);
    activity2.setEndTime(20 * 3600.);
    double delay2 = 0.5 * 3600.;
    double activityDelayDisutility2 =
        marginaSumScoringFunction.getNormalActivityDelayDisutility(activity2, delay2);
    Assert.assertEquals(
        "Wrong disutility from starting an activity with a delay (arriving later at the activity location).",
        0.,
        activityDelayDisutility2,
        MatsimTestUtils.EPSILON);

    // test if a delay results in zero activity delay disutility if the agent would have arrived to
    // early at the activity location anyway
    ActivityImpl activity3 = new ActivityImpl("work", linkId);
    activity3.setStartTime(4 * 3600.);
    activity3.setEndTime(5 * 3600.);
    double delay3 = 0.5 * 3600.;
    double activityDelayDisutility3 =
        marginaSumScoringFunction.getNormalActivityDelayDisutility(activity3, delay3);
    Assert.assertEquals(
        "Wrong disutility from starting an activity with a delay (arriving later at the activity location).",
        0.,
        activityDelayDisutility3,
        MatsimTestUtils.EPSILON);

    // test if a delay results in the right activity delay disutility if the agent would have had
    // more time to perform the activity
    ActivityImpl activity4 = new ActivityImpl("work", linkId);
    activity4.setStartTime(10 * 3600.);
    activity4.setEndTime(16 * 3600.);
    double delay4 = 1 * 3600.;
    double activityDelayDisutility4 =
        marginaSumScoringFunction.getNormalActivityDelayDisutility(activity4, delay4);
    // 6 hours --> 65.549424473781 utils
    // 5 hours --> 60 utils
    Assert.assertEquals(
        "Wrong disutility from starting an activity with a delay (arriving later at the activity location).",
        5.549424473781310,
        activityDelayDisutility4,
        MatsimTestUtils.EPSILON);

    // repeat the previous test: test if a delay results in the right activity delay disutility if
    // the agent would have had more time to perform the activity
    double activityDelayDisutility4b =
        marginaSumScoringFunction.getNormalActivityDelayDisutility(activity4, delay4);
    // 6 hours --> 65.549424473781 utils
    // 5 hours --> 60 utils
    Assert.assertEquals(
        "Wrong disutility from starting an activity with a delay (arriving later at the activity location).",
        5.549424473781310,
        activityDelayDisutility4b,
        MatsimTestUtils.EPSILON);
  }
  public PersonalScoringParameters(final PlanCalcScoreConfigGroup config) {
    this.usingOldScoringBelowZeroUtilityDuration =
        config.isUsingOldScoringBelowZeroUtilityDuration();

    marginalUtilityOfWaiting_s = config.getMarginalUtlOfWaiting_utils_hr() / 3600.0;
    marginalUtilityOfLateArrival_s = config.getLateArrival_utils_hr() / 3600.0;
    marginalUtilityOfEarlyDeparture_s = config.getEarlyDeparture_utils_hr() / 3600.0;
    marginalUtilityOfWaitingPt_s = config.getMarginalUtlOfWaitingPt_utils_hr() / 3600.0;
    marginalUtilityOfPerforming_s = config.getPerforming_utils_hr() / 3600.0;
    utilityOfLineSwitch = config.getUtilityOfLineSwitch();
    marginalUtilityOfMoney = config.getMarginalUtilityOfMoney();
    scoreActs =
        marginalUtilityOfPerforming_s != 0
            || marginalUtilityOfWaiting_s != 0
            || marginalUtilityOfLateArrival_s != 0
            || marginalUtilityOfEarlyDeparture_s != 0;

    SortedMap<String, ActivityUtilityParameters> tmpUtlParams =
        new TreeMap<String, ActivityUtilityParameters>();
    for (ActivityParams params : config.getActivityParams()) {
      ActivityUtilityParameters.Builder factory = new ActivityUtilityParameters.Builder(params);
      // the following was introduced in nov'12.  Also see setupTransitSimulation in Controler.
      // kai, nov'12
      if (params.getActivityType().equals(PtConstants.TRANSIT_ACTIVITY_TYPE)) {
        factory.setScoreAtAll(false);
      }
      tmpUtlParams.put(params.getActivityType(), factory.create());
    }
    utilParams = Collections.unmodifiableMap(tmpUtlParams);

    SortedMap<String, Mode> tmpModeParams = new TreeMap<String, Mode>();
    Map<String, ModeParams> modes = config.getModes();
    double worstMarginalUtilityOfTraveling_s = 0.0;
    for (Entry<String, ModeParams> mode : modes.entrySet()) {
      String modeName = mode.getKey();
      ModeParams modeParams = mode.getValue();
      double marginalUtilityOfTraveling_s = modeParams.getMarginalUtilityOfTraveling() / 3600.0;
      worstMarginalUtilityOfTraveling_s =
          Math.min(worstMarginalUtilityOfTraveling_s, marginalUtilityOfTraveling_s);
      double marginalUtilityOfDistance_m = modeParams.getMarginalUtilityOfDistance();
      double monetaryDistanceCostRate = modeParams.getMonetaryDistanceCostRate();
      double constant = modeParams.getConstant();
      Mode newModeParams =
          new Mode(
              marginalUtilityOfTraveling_s,
              marginalUtilityOfDistance_m,
              monetaryDistanceCostRate,
              constant);
      tmpModeParams.put(modeName, newModeParams);
    }
    modeParams = Collections.unmodifiableMap(tmpModeParams);

    abortedPlanScore =
        Math.min(
                Math.min(marginalUtilityOfLateArrival_s, marginalUtilityOfEarlyDeparture_s),
                Math.min(
                    worstMarginalUtilityOfTraveling_s - marginalUtilityOfPerforming_s,
                    marginalUtilityOfWaiting_s - marginalUtilityOfPerforming_s))
            * 3600.0
            * 24.0; // SCENARIO_DURATION
    // TODO 24 has to be replaced by a variable like scenario_dur (see also other places below)
    // This rather complicated definition has to do with the fact that exp(some_large_number)
    // relatively quickly becomes Inf.
    // In consequence, the abortedPlanScore needs to be more strongly negative than anything else,
    // but not much more.
    // kai, feb'12
  }
  // test overnight activities with first and last activity of the same type
  @Test
  public final void test0b() {

    PlanCalcScoreConfigGroup plansCalcScoreConfigGroup = new PlanCalcScoreConfigGroup();
    ActivityParams activityParams = new ActivityParams("overnightActivity");
    activityParams.setTypicalDuration(12 * 3600.);

    plansCalcScoreConfigGroup.addActivityParams(activityParams);
    plansCalcScoreConfigGroup.setEarlyDeparture_utils_hr(0.);
    plansCalcScoreConfigGroup.setLateArrival_utils_hr(0.);
    plansCalcScoreConfigGroup.setMarginalUtlOfWaiting_utils_hr(0.);
    plansCalcScoreConfigGroup.setPerforming_utils_hr(6.);

    ScenarioConfigGroup scenarioConfig = new ScenarioConfigGroup();

    CharyparNagelScoringParameters params =
        new CharyparNagelScoringParameters.Builder(
                plansCalcScoreConfigGroup,
                plansCalcScoreConfigGroup.getScoringParameters(null),
                scenarioConfig)
            .build();

    MarginalSumScoringFunction marginaSumScoringFunction = new MarginalSumScoringFunction(params);

    Id<Link> linkId = null;

    ActivityImpl activity1 = new ActivityImpl("overnightActivity", linkId);
    activity1.setEndTime(7 * 3600.);
    ActivityImpl activity2 = new ActivityImpl("overnightActivity", linkId);
    activity2.setStartTime(18 * 3600.);

    // test if zero delay results in zero activity delay disutility
    double delay1 = 0 * 3600.;
    double activityDelayDisutility1 =
        marginaSumScoringFunction.getOvernightActivityDelayDisutility(activity1, activity2, delay1);
    // 6 + 7 hours --> 65.763074952494600 utils
    // 6 + 7 hours --> 65.763074952494600 utils
    Assert.assertEquals(
        "Wrong disutility from starting an activity with a delay (arriving later at the activity location).",
        0.,
        activityDelayDisutility1,
        MatsimTestUtils.EPSILON);

    // test if a delay results in the right activity delay disutility
    double delay2 = 1 * 3600.;
    double activityDelayDisutility2 =
        marginaSumScoringFunction.getOvernightActivityDelayDisutility(activity1, activity2, delay2);
    // 6 + 7 hours --> 65.763074952494600 utils
    // 7 + 7 hours --> 71.098848947562600 utils
    Assert.assertEquals(
        "Wrong disutility from starting an activity with a delay (arriving later at the activity location).",
        5.335773995067980,
        activityDelayDisutility2,
        MatsimTestUtils.EPSILON);

    // repeat the previous test: test if a delay results in the right activity delay disutility
    double activityDelayDisutility2b =
        marginaSumScoringFunction.getOvernightActivityDelayDisutility(activity1, activity2, delay2);
    // 6 + 7 hours --> 65.763074952494600 utils
    // 7 + 7 hours --> 71.098848947562600 utils
    Assert.assertEquals(
        "Wrong disutility from starting an activity with a delay (arriving later at the activity location).",
        5.335773995067980,
        activityDelayDisutility2b,
        MatsimTestUtils.EPSILON);
  }