-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2151 from LBNL-UCB-STI/inm/fix-not-updating-in-re…
…al-time-config-values-2 Inm/fix not updating in real time config values 2
- Loading branch information
Showing
22 changed files
with
274 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package beam.matsim; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Singleton; | ||
import org.apache.log4j.Logger; | ||
import org.matsim.analysis.IterationStopWatch; | ||
import org.matsim.api.core.v01.network.Network; | ||
import org.matsim.api.core.v01.population.Population; | ||
import org.matsim.api.core.v01.population.PopulationWriter; | ||
import org.matsim.core.config.Config; | ||
import org.matsim.core.config.groups.ControlerConfigGroup; | ||
import org.matsim.core.controler.OutputDirectoryHierarchy; | ||
import org.matsim.core.controler.corelisteners.PlansDumping; | ||
import org.matsim.core.controler.events.BeforeMobsimEvent; | ||
import org.matsim.core.controler.listener.BeforeMobsimListener; | ||
import org.matsim.core.utils.geometry.CoordinateTransformation; | ||
import org.matsim.core.utils.geometry.transformations.TransformationFactory; | ||
|
||
@Singleton | ||
public class CustomPlansDumpingImpl implements PlansDumping, BeforeMobsimListener { | ||
static final private Logger log = Logger.getLogger(CustomPlansDumpingImpl.class); | ||
|
||
@Inject | ||
private Config config; | ||
@Inject | ||
private Network network; | ||
@Inject | ||
private Population population; | ||
@Inject | ||
private IterationStopWatch stopwatch; | ||
@Inject | ||
private OutputDirectoryHierarchy controlerIO; | ||
@Inject | ||
private ControlerConfigGroup controlerConfigGroup; | ||
|
||
private int writePlansInterval() { | ||
return controlerConfigGroup.getWritePlansInterval(); | ||
} | ||
|
||
private int writeMoreUntilIteration() { | ||
return controlerConfigGroup.getWritePlansUntilIteration(); | ||
} | ||
|
||
@Inject | ||
CustomPlansDumpingImpl() { | ||
} | ||
|
||
@Override | ||
public void notifyBeforeMobsim(final BeforeMobsimEvent event) { | ||
final boolean writingPlansAtAll = writePlansInterval() > 0; | ||
final boolean regularWritePlans = writePlansInterval() > 0 && (event.getIteration() > 0 && event.getIteration() % writePlansInterval() == 0); | ||
final boolean earlyIteration = event.getIteration() <= writeMoreUntilIteration(); | ||
if (writingPlansAtAll && (regularWritePlans || earlyIteration)) { | ||
stopwatch.beginOperation("dump all plans"); | ||
log.info("dumping plans..."); | ||
final String inputCRS = config.plans().getInputCRS(); | ||
final String internalCRS = config.global().getCoordinateSystem(); | ||
|
||
if (inputCRS == null) { | ||
new PopulationWriter(population, network).write(controlerIO.getIterationFilename(event.getIteration(), "plans.xml.gz")); | ||
} else { | ||
log.info("re-projecting population from " + internalCRS + " back to " + inputCRS + " for export"); | ||
|
||
final CoordinateTransformation transformation = | ||
TransformationFactory.getCoordinateTransformation( | ||
internalCRS, | ||
inputCRS); | ||
|
||
new PopulationWriter(transformation, population, network).write(controlerIO.getIterationFilename(event.getIteration(), "plans.xml.gz")); | ||
} | ||
log.info("finished plans dump."); | ||
stopwatch.endOperation("dump all plans"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package beam.matsim; | ||
|
||
import beam.sim.BeamConfigChangesObservable; | ||
import beam.sim.config.BeamConfig; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Singleton; | ||
import org.matsim.core.config.groups.ControlerConfigGroup; | ||
import org.matsim.core.config.groups.LinkStatsConfigGroup; | ||
import scala.Tuple2; | ||
|
||
import java.util.Observable; | ||
import java.util.Observer; | ||
|
||
@Singleton | ||
public class MatsimConfigUpdater implements Observer { | ||
private ControlerConfigGroup controlerConfigGroup; | ||
private LinkStatsConfigGroup linkStatsConfigGroup; | ||
|
||
@Inject | ||
MatsimConfigUpdater(BeamConfigChangesObservable beamConfigChangesObservable, LinkStatsConfigGroup linkStatsConfigGroup, ControlerConfigGroup controlerConfigGroup) { | ||
this.controlerConfigGroup = controlerConfigGroup; | ||
this.linkStatsConfigGroup = linkStatsConfigGroup; | ||
|
||
beamConfigChangesObservable.addObserver(this); | ||
} | ||
|
||
@Override | ||
public void update(Observable observable, Object o) { | ||
if (o instanceof Tuple2) { | ||
Tuple2 t = (Tuple2) o; | ||
if (t._2 instanceof BeamConfig) { | ||
BeamConfig beamConfig = (BeamConfig) t._2; | ||
|
||
controlerConfigGroup.setWritePlansInterval(beamConfig.beam().physsim().writePlansInterval()); | ||
linkStatsConfigGroup.setWriteLinkStatsInterval(beamConfig.matsim().modules().linkStats().writeLinkStatsInterval()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/scala/beam/sim/CustomTerminateAtFixedIterationNumber.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package beam.sim | ||
|
||
import beam.sim.config.BeamConfigHolder | ||
import javax.inject.Inject | ||
import org.matsim.core.config.groups.ControlerConfigGroup | ||
import org.matsim.core.controler.TerminationCriterion | ||
|
||
class CustomTerminateAtFixedIterationNumber extends TerminationCriterion { | ||
private var lastIteration = 0 | ||
private var beamConfigHolder: BeamConfigHolder = _ | ||
|
||
@Inject | ||
def this(controlerConfigGroup: ControlerConfigGroup, beamConfigHolder: BeamConfigHolder) { | ||
this() | ||
this.beamConfigHolder = beamConfigHolder | ||
this.lastIteration = controlerConfigGroup.getLastIteration | ||
} | ||
|
||
override def continueIterations(iteration: Int): Boolean = { | ||
lastIteration = beamConfigHolder.beamConfig.beam.agentsim.lastIteration | ||
iteration <= lastIteration | ||
} | ||
|
||
} |
Oops, something went wrong.