Skip to content

Commit

Permalink
Bugfixes in the simulation.
Browse files Browse the repository at this point in the history
  • Loading branch information
bures committed Jun 20, 2014
1 parent 7bd7859 commit f6ab06a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,16 @@ public SchedulerEvent(Task task, Trigger trigger) {

@Override
public int compareTo(SchedulerEvent o) {
if( this.nextExecutionTime < o.nextExecutionTime ) return -1;
else if( this.nextExecutionTime > o.nextExecutionTime ) return 1;
else if (this == o) return 0;
if (this == o) return 0;
else if (this.nextExecutionTime < o.nextExecutionTime) return -1;
else if (this.nextExecutionTime > o.nextExecutionTime) return 1;
else {
if (this.trigger instanceof TimeTrigger && o.trigger instanceof TimeTrigger) {
TimeTrigger thisTimeTrigger = (TimeTrigger) this.trigger;
TimeTrigger otherTimeTrigger = (TimeTrigger) o.trigger;
if (thisTimeTrigger.getOrder() < otherTimeTrigger.getOrder()) return -1;
else if (thisTimeTrigger.getOrder() < otherTimeTrigger.getOrder()) return 1;
}
return this.hashCode() < o.hashCode() ? 1 : -1;
int thisOrder = this.trigger instanceof TimeTrigger ? ((TimeTrigger)this.trigger).getOrder() : 0;
int thatOrder = o.trigger instanceof TimeTrigger ? ((TimeTrigger)o.trigger).getOrder() : 0;

if (thisOrder < thatOrder) return -1;
else if (thisOrder > thatOrder) return 1;
else return Integer.compare(this.hashCode(), o.hashCode());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ 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()
Expand All @@ -149,6 +152,8 @@ public Coord estimatePosition(double 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.util.Map;
import java.util.Set;

import javax.management.RuntimeErrorException;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Leg;
Expand All @@ -22,8 +20,6 @@
import org.matsim.vehicles.VehicleType;
import org.matsim.vehicles.VehicleUtils;

import cz.cuni.mff.d3s.deeco.simulation.matsim.AdditionAwareAgentSource;

public class MATSimPopulationAgentSource implements
AdditionAwareAgentSource {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class MATSimSimulation extends Simulation implements SimulationStepListen
private long currentMilliseconds;
private final TreeSet<Callback> callbacks;
private final Map<String, Callback> hostIdToCallback;
private final MATSimPreloadingControler controler;
private final Controler controler;
private final JDEECoWithinDayMobsimListener listener;
private final MATSimDataProvider matSimProvider;
private final MATSimDataReceiver matSimReceiver;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cz.cuni.mff.d3s.deeco.simulation.scheduler;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import cz.cuni.mff.d3s.deeco.executor.Executor;
Expand Down Expand Up @@ -35,7 +35,7 @@ public class SimulationScheduler implements Scheduler,
private final SimulationHost host;
private final CallbackProvider callbackProvider;

private final SortedSet<SchedulerEvent> queue;
private final TreeSet<SchedulerEvent> queue;
private final Set<Task> allTasks;
private final Map<Task, SchedulerEvent> periodicEvents;

Expand All @@ -57,7 +57,7 @@ public SimulationScheduler(SimulationHost host,

@Override
public void start() {
registerNextExecution();
registerNextExecution(true);
}

@Override
Expand Down Expand Up @@ -136,7 +136,7 @@ public void executionFailed(Task task, Trigger trigger, Exception e) {
@Override
public void executionCompleted(Task task, Trigger trigger) {
onTriggerSchedules.remove(trigger);
registerNextExecution();
registerNextExecution(false);
}

@Override
Expand All @@ -153,9 +153,7 @@ public void invokeAndWait(Runnable doRun) throws InterruptedException {

@Override
public void at(long time) {
// System.out.println("Scheduler "
// +host.getId()+" at: "+time+" called with queue: " +
// Arrays.toString(queue.toArray()));
Log.d("Scheduler " + host.getHostId()+" at: "+time+" called with queue: " + Arrays.toString(queue.toArray()));
SchedulerEvent event;
while ((event = queue.first()) != null) {
// if notified too early (or already processed all events scheduled
Expand All @@ -171,8 +169,7 @@ public void at(long time) {
// period)
TimeTrigger timeTrigger = event.executable.getTimeTrigger();
event.nextPeriodStart += timeTrigger.getPeriod();
event.nextExecutionTime = event.nextPeriodStart
+ timeTrigger.getOffset();
event.nextExecutionTime = event.nextPeriodStart;
push(event);
}
if (executor != null) {
Expand Down Expand Up @@ -209,13 +206,12 @@ void scheduleAfter(SchedulerEvent event, long delay) {
event.nextExecutionTime = host.getCurrentMilliseconds() + delay;
event.nextPeriodStart = host.getCurrentMilliseconds() + delay;
push(event);

}

private void registerNextExecution() {
private void registerNextExecution(boolean firstExecution) {
if (!queue.isEmpty()) {
long nextExecutionTime = queue.first().nextExecutionTime;
if (nextExecutionTime <= host.getCurrentMilliseconds()) {
if (!firstExecution && nextExecutionTime <= host.getCurrentMilliseconds()) {
return; // nextExecutionTime = host.getSimulationTime() + 1;
}
callbackProvider.callAt(nextExecutionTime, host.getHostId());
Expand All @@ -225,20 +221,16 @@ private void registerNextExecution() {
}

private SchedulerEvent pop() {
if (queue.isEmpty())
return null;
else {
SchedulerEvent result = queue.first();
queue.remove(result);
return result;
}
return queue.pollFirst();
}

private void push(SchedulerEvent event) {
// Log.d("Adding: " + event);

queue.add(event);
// Log.d("Queue: " + Arrays.toString(queue.toArray()));


// Log.d("Queue: " + queue);

// TODO take into account different scheduling policies and WCET of
// tasks. According to those the tasks need to be rescheduled.
Expand Down

0 comments on commit f6ab06a

Please sign in to comment.