Browse Source

implement traffic algorithms

tags/Release_1
Waetschker Daniel (uib17511) 3 years ago
parent
commit
e149a5c342
3 changed files with 66 additions and 23 deletions
  1. +10
    -4
      app/src/main/java/de/hems/trafficsim/MainActivity.java
  2. +27
    -7
      app/src/main/java/de/hems/trafficsim/Track.java
  3. +29
    -12
      app/src/main/java/de/hems/trafficsim/Vehicle.java

+ 10
- 4
app/src/main/java/de/hems/trafficsim/MainActivity.java View File

@@ -17,7 +17,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();
this.track = new Track(20);
this.track.addObserver(this);

}
@@ -26,14 +26,20 @@ public class MainActivity extends AppCompatActivity implements Observer {
public void update(Observable observable, Object o) {
String s = "";
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);
view.setText(s);
view.invalidate();
}

public void onStepButtonClick (View view){
this.track.timeElapse(50);
public void onStepButtonClick (View view) {
for (int j = 0; j < 3000; j++) {
this.track.timeElapse(50);
}
}
}

+ 27
- 7
app/src/main/java/de/hems/trafficsim/Track.java View File

@@ -6,26 +6,46 @@ import java.util.Observable;

public class Track extends Observable {
protected List<Vehicle> vehicles;


public List<Vehicle> getVehicles() {

return vehicles;
}



public Track() {
this.vehicles = createVehiclesList();
public Track(int numberVehicles) {
this.vehicles = createVehiclesList(numberVehicles);
}

protected List<Vehicle> createVehiclesList(){
protected List<Vehicle> createVehiclesList(int numberVehicles){
List<Vehicle> result = new ArrayList<>();
for(int i=0;i<10;i++){
Vehicle vehicle = new Vehicle(i, 5, 0.2f);
for(int i=0;i<numberVehicles;i++){
Vehicle vehicle = new Vehicle(i, i, 5, 0.2f);
result.add(vehicle);
}

return result;
}

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.notifyObservers(this);
this.clearChanged();


+ 29
- 12
app/src/main/java/de/hems/trafficsim/Vehicle.java View File

@@ -1,15 +1,23 @@
package de.hems.trafficsim;

import java.util.Random;

public class Vehicle {
protected int id;
protected float position;
protected float curVelocity;
protected float maxVelocity;
protected float brakeProp;

public void setForerunner(Vehicle forerunner) {
this.forerunner = forerunner;
}

protected Vehicle forerunner;
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 float getPosition() {
return position;
@@ -19,17 +27,26 @@ public class Vehicle {
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) {

position = (position + curVelocity) % 50;
}

}


Loading…
Cancel
Save