124 lines
3.6 KiB
Java
124 lines
3.6 KiB
Java
package de.hems.trafficsim;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.Observable;
|
|
|
|
public class Track extends Observable {
|
|
protected List<Vehicle> vehicles;
|
|
protected List<List<VehicleTimeRecord>> vtrList;
|
|
protected float trackLength;
|
|
protected float sumAvgMemory;
|
|
protected int historyLength;
|
|
protected float overallAvg;
|
|
protected float lastAvg;
|
|
protected int waitTime;
|
|
protected float maxVelocity;
|
|
protected float brakeProb;
|
|
|
|
public List<List<VehicleTimeRecord>> getVtrList() {
|
|
return vtrList;
|
|
}
|
|
public float getOverallAvg() {
|
|
return overallAvg;
|
|
}
|
|
public float getLastAvg() {
|
|
return lastAvg;
|
|
}
|
|
public List<Vehicle> getVehicles() {
|
|
return vehicles;
|
|
}
|
|
public float getTrackLength() {
|
|
return trackLength;
|
|
}
|
|
|
|
public Track(int numberVehicles, float trackLength, float brakeProb, float maxVelocity) {
|
|
this.trackLength = trackLength;
|
|
this.brakeProb = brakeProb;
|
|
this.maxVelocity = maxVelocity;
|
|
this.vehicles = createVehiclesList(numberVehicles);
|
|
this.vtrList = new LinkedList<>();
|
|
this.sumAvgMemory = 0;
|
|
this.overallAvg = 0;
|
|
this.lastAvg = 0;
|
|
this.historyLength = 25;
|
|
|
|
}
|
|
|
|
protected List<Vehicle> createVehiclesList(int numberVehicles){
|
|
List<Vehicle> result = new ArrayList<>();
|
|
for(int i=0;i<numberVehicles;i++){
|
|
Vehicle vehicle = new Vehicle(i, i, this.maxVelocity, this.brakeProb, this.trackLength);
|
|
result.add(vehicle);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public void setBrakeProb(float brakeProb) {
|
|
for (Vehicle v : this.vehicles) {
|
|
v.setBrakeProb(brakeProb);
|
|
}
|
|
}
|
|
|
|
public void setMaxVelocity(float maxVelocity) {
|
|
for (Vehicle v : this.vehicles) {
|
|
v.setMaxVelocity(maxVelocity);
|
|
}
|
|
}
|
|
|
|
public void timeElapse() {
|
|
for(int i=0; i<vehicles.size();i++) {
|
|
Vehicle v = vehicles.get(i);
|
|
int forerunnerIndex = i + 1;
|
|
if (forerunnerIndex >= vehicles.size()) {
|
|
forerunnerIndex -= vehicles.size();
|
|
}
|
|
Vehicle forerunner = vehicles.get(forerunnerIndex);
|
|
float distanceForerunner = forerunner.getPosition() - v.getPosition() - 1;
|
|
if(distanceForerunner < 0.0){
|
|
distanceForerunner += this.trackLength;
|
|
}
|
|
v.updateVelocity(distanceForerunner);
|
|
}
|
|
List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size());
|
|
this.vtrList.add(records);
|
|
for(Vehicle v: vehicles){
|
|
v.timeElapse();
|
|
VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, v.maxVelocity);
|
|
records.add(vtr);
|
|
}
|
|
update_avg();
|
|
this.setChanged();
|
|
this.notifyObservers();
|
|
this.clearChanged();
|
|
try {
|
|
Thread.sleep(waitTime);
|
|
} catch (InterruptedException ex) { }
|
|
}
|
|
|
|
public float avg_step(int step){
|
|
float sum_step = 0;
|
|
for (VehicleTimeRecord r : vtrList.get(step)) {
|
|
sum_step += r.velocity;
|
|
}
|
|
return sum_step / vehicles.size();
|
|
}
|
|
|
|
protected void update_avg(){
|
|
lastAvg = avg_step(vtrList.size()-1);
|
|
sumAvgMemory += lastAvg;
|
|
overallAvg = sumAvgMemory / vtrList.size();
|
|
}
|
|
|
|
public float avg_span(int start, int end){
|
|
float sum_span = 0;
|
|
for (int i=start; i <end;i++){
|
|
sum_span += avg_step(i);
|
|
}
|
|
return sum_span / (end-start+1);
|
|
}
|
|
|
|
}
|