Compare commits

..

No commits in common. "e9c6f25a66e3edcb243fb0cb5cdbdf0e0104f3eb" and "4b3ba0e22c98f4788e68235f88e807417fe1b271" have entirely different histories.

5 changed files with 35 additions and 51 deletions

View File

@ -21,7 +21,7 @@ public class MainActivity extends AppCompatActivity implements Observer {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
this.track = new Track(20, 100); this.track = new Track(1, 100);
this.trackView = new TrackView(this, this.track); this.trackView = new TrackView(this, this.track);
this.track.addObserver(this); this.track.addObserver(this);
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout); ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout);
@ -43,7 +43,6 @@ public class MainActivity extends AppCompatActivity implements Observer {
+ "\n"; + "\n";
; ;
} }
s += "Average: " + trackRef.getLastAvg();
TextView view = (TextView) findViewById(R.id.debugTextView); TextView view = (TextView) findViewById(R.id.debugTextView);
view.setText(s); view.setText(s);
view.invalidate(); view.invalidate();
@ -55,7 +54,7 @@ public class MainActivity extends AppCompatActivity implements Observer {
public void onStepButtonClick(View view) { public void onStepButtonClick(View view) {
//for (int j = 0; j < 3000; j++) { //for (int j = 0; j < 3000; j++) {
this.track.timeElapse(); this.track.timeElapse(1f);
//} //}
} }

View File

@ -7,15 +7,8 @@ import java.util.Observable;
public class Track extends Observable { public class Track extends Observable {
protected List<Vehicle> vehicles; protected List<Vehicle> vehicles;
protected List<List<VehicleTimeRecord>> vtrList; protected List<List<VehicleTimeRecord>> vtr_list;
protected float trackLength; protected float trackLength;
protected float sumAvgMemory;
public float getLastAvg() {
return lastAvg;
}
protected float lastAvg;
public List<Vehicle> getVehicles() { public List<Vehicle> getVehicles() {
return vehicles; return vehicles;
@ -28,22 +21,20 @@ public class Track extends Observable {
public Track(int numberVehicles, float trackLength) { public Track(int numberVehicles, float trackLength) {
this.trackLength = trackLength; this.trackLength = trackLength;
this.vehicles = createVehiclesList(numberVehicles); this.vehicles = createVehiclesList(numberVehicles);
this.vtrList = new LinkedList<>(); this.vtr_list = new LinkedList<>();
this.sumAvgMemory = 0;
this.lastAvg = 0;
} }
protected List<Vehicle> createVehiclesList(int numberVehicles){ protected List<Vehicle> createVehiclesList(int numberVehicles){
List<Vehicle> result = new ArrayList<>(); List<Vehicle> result = new ArrayList<>();
for(int i=0;i<numberVehicles;i++){ for(int i=0;i<numberVehicles;i++){
Vehicle vehicle = new Vehicle(i, i, 5, 0.2f, this.trackLength); Vehicle vehicle = new Vehicle(i, i, 5, 0.1f, this.trackLength);
result.add(vehicle); result.add(vehicle);
} }
return result; return result;
} }
public void timeElapse() { public void timeElapse(float timeStep) {
for(int i=0; i<vehicles.size();i++) { for(int i=0; i<vehicles.size();i++) {
Vehicle v = vehicles.get(i); Vehicle v = vehicles.get(i);
int forerunnerIndex = i + 1; int forerunnerIndex = i + 1;
@ -55,40 +46,21 @@ public class Track extends Observable {
if(distanceForerunner < 0.0){ if(distanceForerunner < 0.0){
distanceForerunner += this.trackLength; distanceForerunner += this.trackLength;
} }
v.updateVelocity(distanceForerunner); v.updateVelocity(distanceForerunner, timeStep);
} }
List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size()); List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size());
this.vtrList.add(records); this.vtr_list.add(records);
for(Vehicle v: vehicles){ for(Vehicle v: vehicles){
v.timeElapse(); v.timeElapse(timeStep);
VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity); VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, timeStep);
records.add(vtr); records.add(vtr);
} }
update_avg();
this.setChanged(); this.setChanged();
this.notifyObservers(this); this.notifyObservers(this);
this.clearChanged(); this.clearChanged();
} }
public float avg_step(int step){ // Durchschnittsgeschwindigkeit, Anteil der Fahrzeuge unter einem bestimmten Threshold
float sum_step = 0; //
for (VehicleTimeRecord r : vtrList.get(step)) {
sum_step += r.velocity;
}
return sum_step / vehicles.size();
}
protected void update_avg(){
sumAvgMemory += avg_step(vtrList.size()-1);
lastAvg = 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);
}
} }

View File

@ -33,25 +33,27 @@ public class Vehicle {
return maxVelocity; return maxVelocity;
} }
public void updateVelocity(float distanceForerunner) { public void updateVelocity(float distanceForerunner, float timeStep) {
Random random = new Random(); Random random = new Random();
float r = random.nextFloat(); float r = random.nextFloat();
if (curVelocity < maxVelocity) { if (curVelocity < maxVelocity) {
curVelocity = curVelocity + 1; curVelocity = curVelocity + 1*timeStep;
} }
if (r < brakeProp && curVelocity > 0) { if (r < brakeProp && curVelocity > 0) {
curVelocity = getCurVelocity() - 1; curVelocity = getCurVelocity() - 1;
if (curVelocity < 0)
curVelocity = 0;
}
float distance = this.curVelocity * timeStep;
if (distance > distanceForerunner) {
curVelocity = distanceForerunner/timeStep;
} }
if (curVelocity > distanceForerunner) {
curVelocity = distanceForerunner;
} }
} public void timeElapse(float timeStep) {
position = (position + curVelocity*timeStep) % this.trackLength;
public void timeElapse() {
position = (position + curVelocity) % this.trackLength;
} }
} }

View File

@ -9,13 +9,19 @@ public class VehicleTimeRecord {
return velocity; return velocity;
} }
public float getTimestep() {
return timestep;
}
protected int id; protected int id;
protected float position; protected float position;
protected float velocity; protected float velocity;
protected float timestep;
public VehicleTimeRecord(int id, float position, float velocity) { public VehicleTimeRecord(int id, float position, float velocity, float timestep) {
this.id = id; this.id = id;
this.position = position; //needed??? this.position = position; //needed???
this.velocity = velocity; this.velocity = velocity;
this.timestep = timestep;
} }
} }

View File

@ -1,5 +1,7 @@
package de.hems.trafficsim; package de.hems.trafficsim;
import java.util.Date;
public class Worker extends Thread { public class Worker extends Thread {
protected Track track; protected Track track;
protected boolean stop; protected boolean stop;
@ -15,8 +17,11 @@ public class Worker extends Thread {
@Override @Override
public void run() { public void run() {
long lastStartTime = System.currentTimeMillis();
while (!stop) { while (!stop) {
this.track.timeElapse(); long startTime = System.currentTimeMillis();
this.track.timeElapse((startTime - lastStartTime));
lastStartTime = startTime;
} }
} }
} }