implement average method

remove time step
rename vtrList
This commit is contained in:
Waetschker Daniel (uib17511) 2020-11-02 17:19:07 +01:00
parent 58054d6eea
commit ee79997e3e
5 changed files with 44 additions and 18 deletions

View File

@ -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(25, 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(50);
this.track.timeElapse();
//}
}

View File

@ -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,7 +28,9 @@ 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){
@ -34,7 +43,7 @@ public class Track extends Observable {
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;
@ -49,15 +58,37 @@ public class Track extends Observable {
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(50);
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();
}
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);
}
}

View File

@ -50,7 +50,7 @@ public class Vehicle {
}
public void timeElapse(float timeStep) {
public void timeElapse() {
position = (position + curVelocity) % this.trackLength;
}

View File

@ -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;
}
}

View File

@ -16,7 +16,7 @@ public class Worker extends Thread {
@Override
public void run() {
while (!stop) {
this.track.timeElapse(50);
this.track.timeElapse();
}
}
}