MATSIM
MutateActivityTimeAllocation.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * PlanMutateTimeAllocation.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007, 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.population.algorithms;
22 
23 import java.util.Random;
24 
31 
37 public final class MutateActivityTimeAllocation implements PlanAlgorithm {
38 
39 
40  public static final String INITIAL_END_TIME_ATTRIBUTE = "initialEndTime";
41  private final double mutationRange;
42  private final Random random;
43  private final boolean affectingDuration;
44  private final double latestActivityEndTime;
45  private final boolean mutateAroundInitialEndTimeOnly;
46  private final double mutationRangeStep;
47 
48 
49  public MutateActivityTimeAllocation(final double mutationRange, boolean affectingDuration, final Random random, double latestActivityEndTime, boolean mutateAroundInitialEndTimeOnly, double mutationRangeStep) {
50  this.mutationRange = mutationRange;
51  this.affectingDuration = affectingDuration;
52  this.random = random;
53  this.latestActivityEndTime = latestActivityEndTime;
54  this.mutateAroundInitialEndTimeOnly = mutateAroundInitialEndTimeOnly;
55  this.mutationRangeStep = Math.max(1.0,mutationRangeStep);
56 
57  }
58 
59  @Override
60  public void run(final Plan plan) {
62  if (act.getEndTime().isDefined()) {
63  double endTime = act.getEndTime().seconds();
64  if (mutateAroundInitialEndTimeOnly){
65  Object initialEndtime = act.getAttributes().getAttribute(INITIAL_END_TIME_ATTRIBUTE);
66  if (initialEndtime!=null) {
67  endTime = (double) initialEndtime;
68  } else {
69  act.getAttributes().putAttribute(INITIAL_END_TIME_ATTRIBUTE,endTime);
70  }
71  }
72  double newEndTime = Math.min(mutateTime(endTime, mutationRange),this.latestActivityEndTime);
73  act.setEndTime(newEndTime);
74  act.setStartTimeUndefined();
75  }
76  else if ( affectingDuration ) {
77  if ( act.getMaximumDuration().isDefined()) {
78  act.setMaximumDuration(mutateTime(act.getMaximumDuration().seconds(), mutationRange));
79  }
80  }
81  }
83 
84  }
85 
86  private void setLegDepartureTimes(Plan plan) {
87  //setting leg departure times can only be an estimate and might be useful for certain dynamic modes.
88  //In general, it is best to trigger a reroute after mutating time.
89  double now = 0;
90  for (PlanElement planElement : plan.getPlanElements()){
91  if (planElement instanceof Activity activity){
92  if (activity.getEndTime().isDefined()){
93  now = activity.getEndTime().seconds();
94  }
95  else if (activity.getMaximumDuration().isDefined()){
96  now = now + activity.getMaximumDuration().seconds();
97  }
98  } else if (planElement instanceof Leg leg){
99  if (leg.getDepartureTime().isDefined()) {
100  leg.setDepartureTime(now);
101  if (leg.getTravelTime().isDefined()) {
102  now = now + leg.getTravelTime().seconds();
103  }
104  }
105  }
106  }
107  }
108 
109  private double mutateTime(final double time, double mutationRange) {
110  double t = time;
111  int mutationRangeBins = (int) Math.ceil( mutationRange/mutationRangeStep);
112  t = t - mutationRange + (2*this.random.nextInt(mutationRangeBins)*mutationRangeStep) ;
113  if (t < 0) {
114  t = 0;
115  }
116  // note that this also affects duration
117  return t;
118  }
119 
120 
121 
122 }
static List< Activity > getActivities(final Plan plan, final StageActivityHandling stageActivityHandling)
List< PlanElement > getPlanElements()
MutateActivityTimeAllocation(final double mutationRange, boolean affectingDuration, final Random random, double latestActivityEndTime, boolean mutateAroundInitialEndTimeOnly, double mutationRangeStep)