Compare commits
2 Commits
4b3ba0e22c
...
e9c6f25a66
Author | SHA1 | Date | |
---|---|---|---|
|
e9c6f25a66 | ||
|
ee79997e3e |
@ -21,7 +21,7 @@ public class MainActivity extends AppCompatActivity implements Observer {
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
this.track = new Track(1, 100);
|
||||
this.track = new Track(20, 100);
|
||||
this.trackView = new TrackView(this, this.track);
|
||||
this.track.addObserver(this);
|
||||
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout);
|
||||
@ -43,6 +43,7 @@ public class MainActivity extends AppCompatActivity implements Observer {
|
||||
+ "\n";
|
||||
;
|
||||
}
|
||||
s += "Average: " + trackRef.getLastAvg();
|
||||
TextView view = (TextView) findViewById(R.id.debugTextView);
|
||||
view.setText(s);
|
||||
view.invalidate();
|
||||
@ -54,7 +55,7 @@ public class MainActivity extends AppCompatActivity implements Observer {
|
||||
|
||||
public void onStepButtonClick(View view) {
|
||||
//for (int j = 0; j < 3000; j++) {
|
||||
this.track.timeElapse(1f);
|
||||
this.track.timeElapse();
|
||||
//}
|
||||
}
|
||||
|
||||
|
@ -7,8 +7,15 @@ import java.util.Observable;
|
||||
|
||||
public class Track extends Observable {
|
||||
protected List<Vehicle> vehicles;
|
||||
protected List<List<VehicleTimeRecord>> vtr_list;
|
||||
protected List<List<VehicleTimeRecord>> vtrList;
|
||||
protected float trackLength;
|
||||
protected float sumAvgMemory;
|
||||
|
||||
public float getLastAvg() {
|
||||
return lastAvg;
|
||||
}
|
||||
|
||||
protected float lastAvg;
|
||||
|
||||
public List<Vehicle> getVehicles() {
|
||||
return vehicles;
|
||||
@ -21,20 +28,22 @@ public class Track extends Observable {
|
||||
public Track(int numberVehicles, float trackLength) {
|
||||
this.trackLength = trackLength;
|
||||
this.vehicles = createVehiclesList(numberVehicles);
|
||||
this.vtr_list = new LinkedList<>();
|
||||
this.vtrList = new LinkedList<>();
|
||||
this.sumAvgMemory = 0;
|
||||
this.lastAvg = 0;
|
||||
}
|
||||
|
||||
protected List<Vehicle> createVehiclesList(int numberVehicles){
|
||||
List<Vehicle> result = new ArrayList<>();
|
||||
for(int i=0;i<numberVehicles;i++){
|
||||
Vehicle vehicle = new Vehicle(i, i, 5, 0.1f, this.trackLength);
|
||||
Vehicle vehicle = new Vehicle(i, i, 5, 0.2f, this.trackLength);
|
||||
result.add(vehicle);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void timeElapse(float timeStep) {
|
||||
public void timeElapse() {
|
||||
for(int i=0; i<vehicles.size();i++) {
|
||||
Vehicle v = vehicles.get(i);
|
||||
int forerunnerIndex = i + 1;
|
||||
@ -46,21 +55,40 @@ public class Track extends Observable {
|
||||
if(distanceForerunner < 0.0){
|
||||
distanceForerunner += this.trackLength;
|
||||
}
|
||||
v.updateVelocity(distanceForerunner, timeStep);
|
||||
v.updateVelocity(distanceForerunner);
|
||||
}
|
||||
List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size());
|
||||
this.vtr_list.add(records);
|
||||
this.vtrList.add(records);
|
||||
for(Vehicle v: vehicles){
|
||||
v.timeElapse(timeStep);
|
||||
VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, timeStep);
|
||||
v.timeElapse();
|
||||
VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity);
|
||||
records.add(vtr);
|
||||
}
|
||||
|
||||
update_avg();
|
||||
this.setChanged();
|
||||
this.notifyObservers(this);
|
||||
this.clearChanged();
|
||||
}
|
||||
|
||||
// Durchschnittsgeschwindigkeit, Anteil der Fahrzeuge unter einem bestimmten Threshold
|
||||
//
|
||||
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(){
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -33,27 +33,25 @@ public class Vehicle {
|
||||
return maxVelocity;
|
||||
}
|
||||
|
||||
public void updateVelocity(float distanceForerunner, float timeStep) {
|
||||
public void updateVelocity(float distanceForerunner) {
|
||||
Random random = new Random();
|
||||
|
||||
float r = random.nextFloat();
|
||||
|
||||
if (curVelocity < maxVelocity) {
|
||||
curVelocity = curVelocity + 1*timeStep;
|
||||
curVelocity = curVelocity + 1;
|
||||
}
|
||||
if (r < brakeProp && curVelocity > 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,19 +9,13 @@ public class VehicleTimeRecord {
|
||||
return velocity;
|
||||
}
|
||||
|
||||
public float getTimestep() {
|
||||
return timestep;
|
||||
}
|
||||
|
||||
protected int id;
|
||||
protected float position;
|
||||
protected float velocity;
|
||||
protected float timestep;
|
||||
|
||||
public VehicleTimeRecord(int id, float position, float velocity, float timestep) {
|
||||
public VehicleTimeRecord(int id, float position, float velocity) {
|
||||
this.id = id;
|
||||
this.position = position; //needed???
|
||||
this.velocity = velocity;
|
||||
this.timestep = timestep;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package de.hems.trafficsim;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Worker extends Thread {
|
||||
protected Track track;
|
||||
protected boolean stop;
|
||||
@ -17,11 +15,8 @@ public class Worker extends Thread {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long lastStartTime = System.currentTimeMillis();
|
||||
while (!stop) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
this.track.timeElapse((startTime - lastStartTime));
|
||||
lastStartTime = startTime;
|
||||
this.track.timeElapse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user