implement average method
remove time step rename vtrList
This commit is contained in:
parent
58054d6eea
commit
ee79997e3e
@ -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(25, 100);
|
this.track = new Track(20, 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,6 +43,7 @@ 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();
|
||||||
@ -54,7 +55,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(50);
|
this.track.timeElapse();
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,8 +7,15 @@ 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>> vtr_list;
|
protected List<List<VehicleTimeRecord>> vtrList;
|
||||||
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;
|
||||||
@ -21,7 +28,9 @@ 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.vtr_list = new LinkedList<>();
|
this.vtrList = new LinkedList<>();
|
||||||
|
this.sumAvgMemory = 0;
|
||||||
|
this.lastAvg = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<Vehicle> createVehiclesList(int numberVehicles){
|
protected List<Vehicle> createVehiclesList(int numberVehicles){
|
||||||
@ -34,7 +43,7 @@ public class Track extends Observable {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void timeElapse(float timeStep) {
|
public void timeElapse() {
|
||||||
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;
|
||||||
@ -49,15 +58,37 @@ public class Track extends Observable {
|
|||||||
v.updateVelocity(distanceForerunner);
|
v.updateVelocity(distanceForerunner);
|
||||||
}
|
}
|
||||||
List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size());
|
List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size());
|
||||||
this.vtr_list.add(records);
|
this.vtrList.add(records);
|
||||||
for(Vehicle v: vehicles){
|
for(Vehicle v: vehicles){
|
||||||
v.timeElapse(50);
|
v.timeElapse();
|
||||||
VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, timeStep);
|
VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity);
|
||||||
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){
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ public class Vehicle {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void timeElapse(float timeStep) {
|
public void timeElapse() {
|
||||||
position = (position + curVelocity) % this.trackLength;
|
position = (position + curVelocity) % this.trackLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,19 +9,13 @@ 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, float timestep) {
|
public VehicleTimeRecord(int id, float position, float velocity) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.position = position; //needed???
|
this.position = position; //needed???
|
||||||
this.velocity = velocity;
|
this.velocity = velocity;
|
||||||
this.timestep = timestep;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ public class Worker extends Thread {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (!stop) {
|
while (!stop) {
|
||||||
this.track.timeElapse(50);
|
this.track.timeElapse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user