Skip to content

Commit

Permalink
rename PlanMutateTimeAllocationSimplified to MutateActivityTimeAlloca…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
jfbischoff committed Oct 13, 2023
1 parent 62d1c20 commit 8e71d4a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,21 @@

package org.matsim.core.population.algorithms;

import java.util.Map;
import java.util.Random;

import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.router.TripStructureUtils;
import org.matsim.core.router.TripStructureUtils.StageActivityHandling;

/**
* Mutates the duration of activities randomly within a specified range.
* <br>
* Other than the "full" version, this version just mutates activity end time and activity duration if they are defined,
* without thinking any further. If this produces invalid plans, they will eventually be removed through the selection
* process. kai, jun'12
*
* @author knagel
* @author knagel, jbischoff
*/
public final class PlanMutateTimeAllocationSimplified implements PlanAlgorithm {
public final class MutateActivityTimeAllocation implements PlanAlgorithm {


public static final String INITIAL_END_TIME_ATTRIBUTE = "initialEndTime";
Expand All @@ -52,7 +46,7 @@ public final class PlanMutateTimeAllocationSimplified implements PlanAlgorithm {
private final double mutationRangeStep;


public PlanMutateTimeAllocationSimplified(final double mutationRange, boolean affectingDuration, final Random random, double latestActivityEndTime, boolean mutateAroundInitialEndTimeOnly, double mutationRangeStep) {
public MutateActivityTimeAllocation(final double mutationRange, boolean affectingDuration, final Random random, double latestActivityEndTime, boolean mutateAroundInitialEndTimeOnly, double mutationRangeStep) {
this.mutationRange = mutationRange;
this.affectingDuration = affectingDuration;
this.random = random;
Expand All @@ -76,11 +70,8 @@ public void run(final Plan plan) {
}
}
double newEndTime = Math.min(mutateTime(endTime, mutationRange),this.latestActivityEndTime);
double shift = endTime -newEndTime;
act.setEndTime(newEndTime);
if (act.getStartTime().isDefined()){
act.setStartTime(act.getStartTime().seconds()-shift);
}
act.setStartTimeUndefined();
}
else if ( affectingDuration ) {
if ( act.getMaximumDuration().isDefined()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,13 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.population.Population;
import org.matsim.api.core.v01.replanning.PlanStrategyModule;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.groups.GlobalConfigGroup;
import org.matsim.core.config.groups.PlansConfigGroup;
import org.matsim.core.config.groups.TimeAllocationMutatorConfigGroup;
import org.matsim.core.gbl.MatsimRandom;
import org.matsim.core.population.algorithms.PlanAlgorithm;
import org.matsim.core.population.algorithms.PlanMutateTimeAllocationSimplified;
import org.matsim.core.population.algorithms.TripPlanMutateTimeAllocation;
import org.matsim.core.population.algorithms.MutateActivityTimeAllocation;
import org.matsim.core.replanning.modules.AbstractMultithreadedModule;
import org.matsim.core.router.TripRouter;
import org.matsim.core.router.TripStructureUtils.StageActivityHandling;

import jakarta.inject.Provider;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Wraps the {@link org.matsim.core.population.algorithms.TripPlanMutateTimeAllocation}-
Expand Down Expand Up @@ -70,7 +57,7 @@ class TimeAllocationMutatorModule extends AbstractMultithreadedModule{

@Override
public PlanAlgorithm getPlanAlgoInstance() {
PlanAlgorithm pmta = new PlanMutateTimeAllocationSimplified
PlanAlgorithm pmta = new MutateActivityTimeAllocation
(this.mutationRange, this.affectingDuration, MatsimRandom.getLocalInstance(),
timeAllocationMutatorConfigGroup.getLatestActivityEndTime(), timeAllocationMutatorConfigGroup.isMutateAroundInitialEndTimeOnly(),
timeAllocationMutatorConfigGroup.getMutationRangeStep());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,29 @@

import static org.junit.Assert.assertTrue;

import jakarta.inject.Provider;

import org.junit.Rule;
import org.junit.Test;
import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.network.Node;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.gbl.MatsimRandom;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.population.PersonUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.algorithms.PlanAlgorithm;
import org.matsim.core.population.algorithms.PlanMutateTimeAllocationSimplified;
import org.matsim.core.router.TripRouter;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.population.algorithms.MutateActivityTimeAllocation;
import org.matsim.core.utils.misc.Time;
import org.matsim.testcases.MatsimTestUtils;

/**
* Tests the functionality of {@link TimeAllocationMutatorModule}, mainly that the
* correct mutation range is handed over to the underlying {@link PlanMutateTimeAllocationSimplified}.
* correct mutation range is handed over to the underlying {@link MutateActivityTimeAllocation}.
*
* @author mrieser
*/
Expand All @@ -64,8 +57,8 @@ public class TimeAllocationMutatorModuleTest {
@Test public void testSimplifiedMutation() {
boolean affectingDuration = true ;

runSimplifiedMutationRangeTest(new PlanMutateTimeAllocationSimplified( 750, affectingDuration, MatsimRandom.getLocalInstance(),24*3600,false,1), 750);
runSimplifiedMutationRangeTest(new PlanMutateTimeAllocationSimplified( 7200, affectingDuration, MatsimRandom.getLocalInstance(),24*3600,false,1), 7200);
runSimplifiedMutationRangeTest(new MutateActivityTimeAllocation( 750, affectingDuration, MatsimRandom.getLocalInstance(),24*3600,false,1), 750);
runSimplifiedMutationRangeTest(new MutateActivityTimeAllocation( 7200, affectingDuration, MatsimRandom.getLocalInstance(),24*3600,false,1), 7200);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.core.population.PersonUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.algorithms.PlanMutateTimeAllocationSimplified;
import org.matsim.core.population.algorithms.MutateActivityTimeAllocation;
import org.matsim.pt.PtConstants;

import java.util.Random;
Expand Down Expand Up @@ -66,8 +66,8 @@ public void testRun() {
PopulationUtils.createAndAddActivityFromCoord(plan, "work", new Coord(0, 500));
boolean affectingDuration = true ;

PlanMutateTimeAllocationSimplified mutator =
new PlanMutateTimeAllocationSimplified(
MutateActivityTimeAllocation mutator =
new MutateActivityTimeAllocation(
3600.,
affectingDuration, new Random(2011),24*3600,false,1);
mutator.run(plan);
Expand Down Expand Up @@ -110,8 +110,8 @@ public void testRunLatestEndTime() {
boolean affectingDuration = true;
final double latestEndTime = 30. * 3600;

PlanMutateTimeAllocationSimplified mutator =
new PlanMutateTimeAllocationSimplified(
MutateActivityTimeAllocation mutator =
new MutateActivityTimeAllocation(
3600.,
affectingDuration, new Random(2011),latestEndTime,false,1);

Expand Down

0 comments on commit 8e71d4a

Please sign in to comment.