Skip to content

Commit

Permalink
Refactorized simulation random due to an error on simulations
Browse files Browse the repository at this point in the history
  • Loading branch information
cruizba committed Oct 9, 2018
1 parent 537b706 commit db5da84
Show file tree
Hide file tree
Showing 29 changed files with 29 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,34 @@
public class SimulationRandom {

private static SimulationRandom generalInstance = null;
private static SimulationRandom userCreationInstance = null;
private static List<GeoPoint> validRandomPositions = new ArrayList<>();
private List<GeoPoint> validRandomPositions;

public static void init() {
if (generalInstance != null || userCreationInstance != null) {
if (generalInstance != null) {
throw new IllegalStateException("Instances have already been initialized.");
}

generalInstance = new SimulationRandom();
userCreationInstance = new SimulationRandom();
}

public static void init(long seed) {
if (generalInstance != null || userCreationInstance != null) {
if (generalInstance != null) {
throw new IllegalStateException("Instances have already been initialized.");
}

generalInstance = new SimulationRandom(seed);
userCreationInstance = new SimulationRandom(seed);
}

public static SimulationRandom getGeneralInstance() {
if (generalInstance != null) return generalInstance;
throw new IllegalStateException("You should first call init(seed)");
}

public static SimulationRandom getUserCreationInstance() {
if (userCreationInstance != null) return userCreationInstance;
throw new IllegalStateException("You should first call init(seed)");
}

public static boolean addRandomUsedPoint(GeoPoint point) {
public boolean addRandomUsedPoint(GeoPoint point) {
return validRandomPositions.add(point);
}

public static GeoPoint getRandomUsedPoint() throws IllegalAccessException {
public GeoPoint getRandomUsedPoint() throws IllegalAccessException {
int index = generalInstance.nextInt(0, validRandomPositions.size() - 1);
if(validRandomPositions.size() == 0) {
throw new IllegalAccessException("No valid random points yet");
Expand All @@ -64,10 +56,12 @@ public static GeoPoint getRandomUsedPoint() throws IllegalAccessException {

private SimulationRandom() {
this.random = new Random();
this.validRandomPositions = new ArrayList<>();
}

private SimulationRandom(long seed) {
this.random = new Random(seed);
this.validRandomPositions = new ArrayList<>();
}

public int nextInt(int min, int max) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public double getLambda() {
*/
public int randomInterarrivalDelay() {
double lambdaSeconds = lambda / 60;
SimulationRandom random = SimulationRandom.getUserCreationInstance();
SimulationRandom random = SimulationRandom.getGeneralInstance();
double randomValue = Math.log(1.0 - random.nextDouble(Double.MIN_VALUE, 1));
Double result = -randomValue / lambdaSeconds;
Long longResult = Math.round(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public List<SingleUser> generateUsers() {
//If not radius is specified, user just appears in the position submitted.
if (radius > 0) {
BoundingCircle boundingCircle = new BoundingCircle(position, radius);
userPosition = boundingCircle.randomPointInCircle(SimulationRandom.getUserCreationInstance());
userPosition = boundingCircle.randomPointInCircle(SimulationRandom.getGeneralInstance());
} else {
userPosition = position;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public User(SimulationServices services) {
this.bike = null;

// random velocity between 3km/h and 7km/h in m/s
this.walkingVelocity = SimulationRandom.getUserCreationInstance().nextInt(3, 8) / 3.6;
this.walkingVelocity = SimulationRandom.getGeneralInstance().nextInt(3, 8) / 3.6;
// random velocity between 10km/h and 20km/h in m/s
this.cyclingVelocity = SimulationRandom.getUserCreationInstance().nextInt(10, 21) / 3.6;
this.cyclingVelocity = SimulationRandom.getGeneralInstance().nextInt(10, 21) / 3.6;

this.reservedBike = false;
this.reservedSlot = false;
Expand All @@ -163,9 +163,9 @@ public User(SimulationServices services, GeoPoint finalDestination) {
this.bike = null;

// random velocity between 3km/h and 7km/h in m/s
this.walkingVelocity = SimulationRandom.getUserCreationInstance().nextInt(3, 8) / 3.6;
this.walkingVelocity = SimulationRandom.getGeneralInstance().nextInt(3, 8) / 3.6;
// random velocity between 10km/h and 20km/h in m/s
this.cyclingVelocity = SimulationRandom.getUserCreationInstance().nextInt(10, 21) / 3.6;
this.cyclingVelocity = SimulationRandom.getGeneralInstance().nextInt(10, 21) / 3.6;

this.reservedBike = false;
this.reservedSlot = false;
Expand Down Expand Up @@ -388,24 +388,13 @@ public void returnBikeWithReservationTo(Station station) {
this.reservation = null;
}

private List<GeoRoute> createRouteFromStationToSame(List<GeoRoute> geoRoute, String vehicle, boolean calcNewRandom) throws Exception {
private List<GeoRoute> createRouteFromStationToSame(List<GeoRoute> geoRoute, String vehicle) throws Exception {

GeoPoint auxiliarPoint = null;
GeoPoint auxiliarPoint = null;
double RADIO = 1000;

if(calcNewRandom) {
auxiliarPoint = services.getInfrastructureManager().generateRandomPointInCircle(this.position, RADIO);
}
else {
auxiliarPoint = SimulationRandom.getRandomUsedPoint();
}

auxiliarPoint = services.getInfrastructureManager().generateRandomPointInCircle(this.position, RADIO);
GeoRoute initRoute = graph.obtainShortestRouteBetween(this.position, auxiliarPoint, vehicle);
GeoRoute returnRoute = graph.obtainShortestRouteBetween(auxiliarPoint, destinationPoint, vehicle);

if(calcNewRandom) {
SimulationRandom.addRandomUsedPoint(auxiliarPoint);
}
geoRoute.add(initRoute.concatRoute(returnRoute));
return geoRoute;
}
Expand All @@ -418,26 +407,22 @@ public List<GeoRoute> calculateRoutes(GeoPoint destinationPoint) throws GeoRoute
ArrayList<GeoRoute> newRoutes = new ArrayList<>();
// First try. If valid random point, program still running
try {
createRouteFromStationToSame(newRoutes, vehicle, true);
createRouteFromStationToSame(newRoutes, vehicle);
}
// If not valid route, we use a point used before
// If no points are at first runtime this method should be executed
catch(Exception e1) {
try {
createRouteFromStationToSame(newRoutes, vehicle, false);
}
// If no points are at first runtime this method should be executed
catch(Exception e2) {
boolean isValidPoint = false;
while(!isValidPoint) {
try {
createRouteFromStationToSame(newRoutes, vehicle, true);
isValidPoint = true;
}
catch(Exception e3) {
System.out.println("Trying new point");
}
boolean isValidPoint = false;
int tryCounter = 1;
while(!isValidPoint) {
try {
createRouteFromStationToSame(newRoutes, vehicle);
isValidPoint = true;
}
catch(Exception e3) {
System.out.println("Trying new point route" + tryCounter);
tryCounter++;
}
}
}
}
return newRoutes;
}
Expand Down
3 changes: 0 additions & 3 deletions experiment_scripts/.directory

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit db5da84

Please sign in to comment.