implement traffic algorithms

This commit is contained in:
Waetschker Daniel (uib17511) 2020-10-12 18:26:52 +02:00
parent 23ac354bb6
commit e149a5c342
3 changed files with 66 additions and 23 deletions

View File

@ -17,7 +17,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(); this.track = new Track(20);
this.track.addObserver(this); this.track.addObserver(this);
} }
@ -26,7 +26,11 @@ public class MainActivity extends AppCompatActivity implements Observer {
public void update(Observable observable, Object o) { public void update(Observable observable, Object o) {
String s = ""; String s = "";
for (Vehicle v: this.track.getVehicles()){ for (Vehicle v: this.track.getVehicles()){
s+="Pos = " + v.getPosition() +"\n"; s+= "Vehicle " + v.id + " | "
+ "Pos = " + v.getPosition() + " | "
+ "Vel = " + v.getCurVelocity()+ " | "
+ "\n";
;
} }
TextView view = (TextView) findViewById(R.id.debugTextView); TextView view = (TextView) findViewById(R.id.debugTextView);
view.setText(s); view.setText(s);
@ -34,6 +38,8 @@ public class MainActivity extends AppCompatActivity implements Observer {
} }
public void onStepButtonClick (View view) { public void onStepButtonClick (View view) {
for (int j = 0; j < 3000; j++) {
this.track.timeElapse(50); this.track.timeElapse(50);
} }
} }
}

View File

@ -6,26 +6,46 @@ import java.util.Observable;
public class Track extends Observable { public class Track extends Observable {
protected List<Vehicle> vehicles; protected List<Vehicle> vehicles;
public List<Vehicle> getVehicles() { public List<Vehicle> getVehicles() {
return vehicles; return vehicles;
} }
public Track(int numberVehicles) {
this.vehicles = createVehiclesList(numberVehicles);
public Track() {
this.vehicles = createVehiclesList();
} }
protected List<Vehicle> createVehiclesList(){ protected List<Vehicle> createVehiclesList(int numberVehicles){
List<Vehicle> result = new ArrayList<>(); List<Vehicle> result = new ArrayList<>();
for(int i=0;i<10;i++){ for(int i=0;i<numberVehicles;i++){
Vehicle vehicle = new Vehicle(i, 5, 0.2f); Vehicle vehicle = new Vehicle(i, i, 5, 0.2f);
result.add(vehicle); result.add(vehicle);
} }
return result; return result;
} }
public void timeElapse(float timeStep) { public void timeElapse(float timeStep) {
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 += 50;
}
v.updateVelocity(distanceForerunner);
}
for(Vehicle v: vehicles){
v.timeElapse(50);
}
this.setChanged(); this.setChanged();
this.notifyObservers(this); this.notifyObservers(this);
this.clearChanged(); this.clearChanged();

View File

@ -1,16 +1,24 @@
package de.hems.trafficsim; package de.hems.trafficsim;
import java.util.Random;
public class Vehicle { public class Vehicle {
protected int id;
protected float position; protected float position;
protected float curVelocity; protected float curVelocity;
protected float maxVelocity; protected float maxVelocity;
protected float brakeProp;
public Vehicle(int id, int position, float maxVelocity, float brakeProp) {
this.id = id;
this.position = position;
this.maxVelocity = maxVelocity;
this.brakeProp = brakeProp;
this.curVelocity = 0;
public void setForerunner(Vehicle forerunner) {
this.forerunner = forerunner;
} }
protected Vehicle forerunner;
public float getPosition() { public float getPosition() {
return position; return position;
} }
@ -19,17 +27,26 @@ public class Vehicle {
return curVelocity; return curVelocity;
} }
protected float brakeProp; public void updateVelocity(float distanceForerunner) {
Random random = new Random();
float r = random.nextFloat();
if (curVelocity < maxVelocity) {
curVelocity = curVelocity + 1;
}
if (r < brakeProp && curVelocity > 0) {
curVelocity = getCurVelocity() - 1;
}
if (curVelocity > distanceForerunner) {
curVelocity = distanceForerunner;
}
public Vehicle(float position, float maxVelocity, float brakeProp) {
this.position = position;
this.maxVelocity = maxVelocity;
this.brakeProp = brakeProp;
this.curVelocity = 0;
this.forerunner = null;
} }
public void timeElapse(float timeStep) { public void timeElapse(float timeStep) {
position = (position + curVelocity) % 50;
}
} }
}