MATSIM
PlanMutateTimeAllocation.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.List;
24 import java.util.Random;
25 
31 
42 public final class PlanMutateTimeAllocation implements PlanAlgorithm {
43 
44  private final double mutationRange;
45  private final Random random;
46 
47  public PlanMutateTimeAllocation(final double mutationRange, final Random random) {
48  this.mutationRange = mutationRange;
49  this.random = random;
50  }
51 
52  @Override
53  public void run(final Plan plan) {
54  mutatePlan(plan);
55  }
56 
57  private void mutatePlan(final Plan plan) {
58 
59  List<PlanElement> planElements = plan.getPlanElements();
60  int max = planElements.size();
61 
62  double now = 0;
63 
64  // apply mutation to all activities except the last home activity
65  for (int i = 0; i < max; i++) {
66 
67  PlanElement pe = planElements.get(i);
68 
69  if (pe instanceof Activity) {
70  Activity act = (Activity) pe;
71 
72  // handle first activity
73  if (i == 0) {
74  // set start to midnight
75  act.setStartTime(now);
76  // mutate the end time of the first activity
77  act.setEndTime(mutateTime(act.getEndTime()));
78  // calculate resulting duration
80  // move now pointer
81  now += act.getEndTime().seconds();
82 
83  // handle middle activities
84  } else if (i < (max - 1)) {
85 
86  // assume that there will be no delay between arrival time and activity start time
87  act.setStartTime(now);
88  if (act.getMaximumDuration().isDefined()) {
89  // mutate the durations of all 'middle' activities
91  now += act.getMaximumDuration().seconds();
92  // set end time accordingly
93  act.setEndTime(now);
94  } else {
95  double newEndTime = mutateTime(act.getEndTime());
96  if (newEndTime < now) {
97  newEndTime = now;
98  }
99  act.setEndTime(newEndTime);
100  now = newEndTime;
101  }
102  // handle last activity
103  } else {
104 
105  // assume that there will be no delay between arrival time and activity start time
106  act.setStartTime(now);
107  // invalidate duration and end time because the plan will be interpreted 24 hour wrap-around
109  act.setEndTimeUndefined();
110 
111  }
112 
113  } else if (pe instanceof Leg) {
114 
115  Leg leg = (Leg) pe;
116 
117  // assume that there will be no delay between end time of previous activity and departure time
118  leg.setDepartureTime(now);
119  // let duration untouched. if defined add it to now
120  if (leg.getTravelTime().isDefined()) {
121  now += leg.getTravelTime().seconds();
122  }
123  final double arrTime = now;
124  // set planned arrival time accordingly
125  leg.setTravelTime( arrTime - leg.getDepartureTime().seconds());
126 
127  }
128  }
129  }
130 
131  private double mutateTime(final OptionalTime time) {
132  if (time.isDefined()) {
133  double t = time.seconds() + (int)((this.random.nextDouble() * 2.0 - 1.0) * this.mutationRange);
134  if (t < 0) t = 0;
135  if (t > 24*3600) t = 24*3600;
136  return t;
137  } else {
138  return this.random.nextInt(24*3600);
139  }
140  }
141 
142 }
abstract List< PlanElement > getPlanElements()
void setDepartureTime(final double seconds)
void setTravelTime(final double seconds)
PlanMutateTimeAllocation(final double mutationRange, final Random random)
void setEndTime(final double seconds)