Skip to content

Commit

Permalink
MATSim data extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Kit committed Sep 3, 2014
1 parent 00f162a commit 117a688
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
package cz.cuni.mff.d3s.deeco.simulation;

import cz.cuni.mff.d3s.deeco.simulation.task.SimulationStepTask;

/**
* Listener interface for time events triggered by a simulation.
Expand All @@ -19,5 +18,5 @@ public interface SimulationStepListener {
* @param time
* current simulation time
*/
void at(long time, SimulationStepTask task);
void at(long time, Object trigerO);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cz.cuni.mff.d3s.deeco.simulation.matsim;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.matsim.api.core.v01.Id;
import org.matsim.core.mobsim.framework.Mobsim;

public class DefaultMATSimExtractor implements MATSimExtractor {

@Override
public Object extractFromMATSim(Collection<JDEECoAgent> agents,
Mobsim mobsim) {
Map<Id, MATSimOutput> map = new HashMap<Id, MATSimOutput>();
MATSimOutput matSimOutput;
for (JDEECoAgent agent : agents) {
matSimOutput = new MATSimOutput(agent.getCurrentLinkId(),
agent.getState());
map.put(agent.getId(), matSimOutput);
}
return map;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cz.cuni.mff.d3s.deeco.simulation.matsim;

import java.util.Collection;
import java.util.Map;

public class DefaultMATSimUpdater implements MATSimUpdater {

@SuppressWarnings("unchecked")
@Override
public void updateJDEECoAgents(Object input, Collection<JDEECoAgent> agents) {
if (input != null && input instanceof Map<?, ?>) {
Map<String, MATSimInput> map = (Map<String, MATSimInput>) input;
for (JDEECoAgent agent: agents) {
if (map.containsKey(agent.getId())) {
agent.setRoute(((MATSimInput) map.get(agent.getId())).route);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

import java.util.List;

import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Link;
import org.matsim.core.api.experimental.events.AgentArrivalEvent;
import org.matsim.core.basic.v01.IdImpl;
import org.matsim.core.mobsim.framework.MobsimDriverAgent;
import org.matsim.core.mobsim.qsim.interfaces.MobsimVehicle;
import org.matsim.core.mobsim.qsim.interfaces.Netsim;
import org.matsim.core.mobsim.qsim.qnetsimengine.QVehicle;
import org.matsim.core.utils.geometry.CoordImpl;

/**
* JDEECo agent implementation. The agent is used by the
Expand Down Expand Up @@ -98,7 +94,25 @@ public void notifyArrivalOnLinkByNonNetworkMode(Id linkId) {
}

public void setRoute(List<Id> route) {
this.route = route;
int index = route.indexOf(currentLinkId);
if (index < 0) {
this.route = route;
} else {
boolean isPartOf = true;
int nextIndex;
for (Id lId: this.route) {
nextIndex = route.indexOf(lId);
if (index + 1 == nextIndex) {
index = nextIndex;
} else {
isPartOf = false;
break;
}
}
if (!isPartOf) {
this.route = route;
}
}
}

public Id getCurrentLinkId() {
Expand All @@ -122,7 +136,16 @@ public Id chooseNextLinkId() {
if (route == null || route.isEmpty()) {
return null;
} else {
return route.remove(0);
Id result = route.remove(0);
if (result.equals(currentLinkId)) {
if (route.isEmpty()) {
return null;
} else {
return route.remove(0);
}
} else {
return result;
}
}
}

Expand All @@ -138,39 +161,6 @@ public MobsimVehicle getVehicle() {
return this.vehicle;
}

// This method should not be used because in case of traffic jam, it gives completely off results. It acts if the road was empty and
// then waits minutes at the end of the link
@Deprecated
public Coord estimatePosition(double now) {
if (state.equals(State.ACTIVITY) && currentLinkId != null) {
return simulation.getScenario().getNetwork().getLinks()
.get(currentLinkId).getToNode().getCoord();
}
if (vehicle != null) {
QVehicle qVehicle = (QVehicle) vehicle;
Link link = qVehicle.getCurrentLink();
double time = qVehicle.getEarliestLinkExitTime() - now;
if (time <= 0) {
return qVehicle.getCurrentLink().getToNode().getCoord();
}

// XXX: Since the time is used, it's not necessary to use the velocity, as the velocity is constant!!! This is an overkill!
double velocity = (link.getFreespeed() > qVehicle
.getMaximumVelocity()) ? qVehicle.getMaximumVelocity()
: link.getFreespeed();
double remainingDistance = velocity * time;
double distanceDriven = link.getLength() - remainingDistance;
Coord from = link.getFromNode().getCoord();
Coord to = link.getToNode().getCoord();
double estX = ((distanceDriven * (to.getX() - from.getX())) / link
.getLength()) + from.getX();
double estY = ((distanceDriven * (to.getY() - from.getY())) / link
.getLength()) + from.getY();
return new CoordImpl(estX, estY);
}
return null;
}

public void notifyMoveOverNode(Id newLinkId) {
this.currentLinkId = newLinkId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package cz.cuni.mff.d3s.deeco.simulation.matsim;

import java.util.HashMap;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Exchanger;

import org.matsim.api.core.v01.Id;
import org.matsim.core.mobsim.framework.events.MobsimBeforeSimStepEvent;
import org.matsim.core.mobsim.framework.listeners.MobsimBeforeSimStepListener;

Expand All @@ -26,78 +24,57 @@
public class JDEECoWithinDayMobsimListener implements
MobsimBeforeSimStepListener {

private final Exchanger<Map<Id, ?>> exchanger;
private final Exchanger<Object> exchanger;
private final List<JDEECoAgentProvider> agentProviders;
private final SimulationStepListener stepListener;
private final MATSimUpdater updater;
private final MATSimExtractor extractor;

protected JDEECoWithinDayMobsimListener(Exchanger<Map<Id, ?>> exchanger, SimulationStepListener stepListener) {
protected JDEECoWithinDayMobsimListener(Exchanger<Object> exchanger, SimulationStepListener stepListener, MATSimUpdater updater, MATSimExtractor extractor) {
this.exchanger = exchanger;
this.agentProviders = new LinkedList<JDEECoAgentProvider>();
this.stepListener = stepListener;
this.updater = updater;
this.extractor = extractor;
}

public JDEECoWithinDayMobsimListener(Exchanger<Map<Id, ?>> exchanger) {
this(exchanger, null);
public JDEECoWithinDayMobsimListener(Exchanger<Object> exchanger, MATSimUpdater updater, MATSimExtractor extractor) {
this(exchanger, null, updater, extractor);
}

public JDEECoWithinDayMobsimListener(SimulationStepListener stepListener) {
this(null, stepListener);
public JDEECoWithinDayMobsimListener(SimulationStepListener stepListener, MATSimUpdater updater, MATSimExtractor extractor) {
this(null, stepListener, updater, extractor);
}

public void registerAgentProvider(JDEECoAgentProvider agentProvider) {
if (!agentProviders.contains(agentProvider)) {
agentProviders.add(agentProvider);
}
}

public Map<Id, MATSimOutput> getOutputs(double currentSeconds) {
Map<Id, MATSimOutput> matSimOutputs = new HashMap<Id, MATSimOutput>();

// Get agents current positions and jDEECoAgents
MATSimOutput matSimOutput;
for (JDEECoAgentProvider agentProvider : agentProviders) {
for (JDEECoAgent agent : agentProvider.getAgents()) {
matSimOutput = new MATSimOutput(agent.getCurrentLinkId(),
agent.estimatePosition(currentSeconds), agent.getState());
matSimOutputs.put(agent.getId(), matSimOutput);
}
}
return matSimOutputs;

public void updateJDEECoAgents(Object input) {
updater.updateJDEECoAgents(input, getAllJDEECoAgents());
}

public void setInputs(Map<Id, ?> matSimInputs) {
if (matSimInputs != null && !matSimInputs.isEmpty()) {
MATSimInput mData;
for (JDEECoAgentProvider agentProvider : agentProviders) {
for (JDEECoAgent agent : agentProvider.getAgents()) {
if (matSimInputs.containsKey(agent.getId())) {
mData = (MATSimInput) matSimInputs.get(agent.getId());
agent.setRoute(mData.route);
}
}
}
public Collection<JDEECoAgent> getAllJDEECoAgents() {
List<JDEECoAgent> agents = new LinkedList<>();
for (JDEECoAgentProvider agentProvider : agentProviders) {
agents.addAll(agentProvider.getAgents());
}
return agents;
}

@SuppressWarnings("rawtypes")
public void notifyMobsimBeforeSimStep(MobsimBeforeSimStepEvent event) {
if (exchanger != null) {
try {
Map<Id, MATSimOutput> matSimOutputs = getOutputs(event
.getSimulationTime());
// Exchange data (Rendezvous)
Map<Id, ?> matSimInputs = exchanger.exchange(matSimOutputs);
// Log.w("MATSim After data exchange at " +
// event.getSimulationTime() + " " + remainingExchanges );

// Update jDEECo agents next link id
setInputs(matSimInputs);
updateJDEECoAgents(exchanger.exchange(extractor.extractFromMATSim(getAllJDEECoAgents(), event.getQueueSimulation())));
} catch (Exception e) {
Log.e("jDEECoWithinDayMobsimListener: ", e);
}
}
if (stepListener != null) {
stepListener.at(Math.round(event.getSimulationTime()), null);
stepListener.at(Math.round(event.getSimulationTime()), event.getQueueSimulation());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package cz.cuni.mff.d3s.deeco.simulation.matsim;

import java.util.Map;

import org.matsim.api.core.v01.Id;

/**
* Interface for MATSim data retrieval. This data comes from MATSim side.
Expand All @@ -11,5 +8,5 @@
*
*/
public interface MATSimDataReceiver {
public void setMATSimData(Map<Id, MATSimOutput> data);
public void setMATSimData(Object data);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cz.cuni.mff.d3s.deeco.simulation.matsim;

import java.util.Collection;

import org.matsim.core.mobsim.framework.Mobsim;

public interface MATSimExtractor {
public Object extractFromMATSim(Collection<JDEECoAgent> agents, Mobsim mobsim);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ public class MATSimInput {

public MATSimInput clone() {
MATSimInput result = new MATSimInput();
result.route = new LinkedList<Id>(route);
if (route == null) {
result.route = null;
} else {
result.route = new LinkedList<Id>(route);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Exchanger;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.core.controler.events.StartupEvent;
import org.matsim.core.controler.listener.StartupListener;
Expand All @@ -30,7 +28,7 @@ public class MATSimOMNetSimulation extends OMNetSimulation implements
SimulationStepListener, MATSimTimeProvider {
private static final int MILLIS_IN_SECOND = 1000;

private final Exchanger<Map<Id, ?>> exchanger;
private final Exchanger<Object> exchanger;
private final MATSimDataProvider matSimProvider;
private final MATSimDataReceiver matSimReceiver;
private final MATSimPreloadingControler controler;
Expand All @@ -44,7 +42,7 @@ public class MATSimOMNetSimulation extends OMNetSimulation implements

public MATSimOMNetSimulation(NetworkProvider np,
MATSimDataReceiver matSimReceiver,
MATSimDataProvider matSimProvider,
MATSimDataProvider matSimProvider, MATSimUpdater updater, MATSimExtractor extractor,
final Collection<? extends AdditionAwareAgentSource> agentSources,
String matSimConf) {
super(np);
Expand All @@ -64,8 +62,8 @@ public MATSimOMNetSimulation(NetworkProvider np,
+ " matsimEndTime: " + end);
this.remainingExchanges = Math.round((end - start) / step) + 1;

this.exchanger = new Exchanger<Map<Id, ?>>();
this.listener = new JDEECoWithinDayMobsimListener(exchanger);
this.exchanger = new Exchanger<Object>();
this.listener = new JDEECoWithinDayMobsimListener(exchanger, updater, extractor);
this.matSimProvider = matSimProvider;
this.matSimReceiver = matSimReceiver;

Expand Down Expand Up @@ -103,10 +101,10 @@ public void notifyStartup(StartupEvent event) {
}

public MATSimOMNetSimulation(MATSimDataReceiver matSimReceiver,
MATSimDataProvider matSimProvider,
MATSimDataProvider matSimProvider, MATSimUpdater updater, MATSimExtractor extractor,
final Collection<? extends AdditionAwareAgentSource> agentSources,
String matSimConf) {
this(null, matSimReceiver, matSimProvider, agentSources, matSimConf);
this(null, matSimReceiver, matSimProvider, updater, extractor, agentSources, matSimConf);
}

public double getDuration() {
Expand All @@ -117,7 +115,7 @@ public double getDuration() {
return end - start;
}

public void at(long time, SimulationStepTask task) {
public void at(long time, Object triger) {
try {
if (matSimThread == null) {
matSimThread = new Thread(new Runnable() {
Expand All @@ -131,13 +129,10 @@ public void run() {
matSimThread.start();
}
if (matSimThread.isAlive() && this.remainingExchanges > 0) {
// long currentTime = getCurrentTime();
// long matsimTime = getMATSimTime();
matSimReceiver.setMATSimData((Map<Id, MATSimOutput>)exchanger.exchange(matSimProvider
matSimReceiver.setMATSimData(exchanger.exchange(matSimProvider
.getMATSimData()));
this.remainingExchanges--;
// Log.w("jDEECo After data exchange at " + currentTime
// + " MATSim time: " + matsimTime + " " + remainingExchanges);
SimulationStepTask task = (SimulationStepTask) triger;
task.scheduleNextExecutionAfter(simulationStep);
}
} catch (InterruptedException e) {
Expand Down
Loading

0 comments on commit 117a688

Please sign in to comment.