update Vehicle and Campaign

- Added movment in vehicle.
   - Now Campaign sets Routes for
   it's Vehicles.
   - Fixed Array size of stdRoutes
   - Added getShortestRoute(Campaign)
   and getRoutesToDistrict(District)
   in MakkahCity

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2020-11-11 21:02:59 +03:00
parent f4bb5e6099
commit 16ff81a44c
3 changed files with 91 additions and 26 deletions

View File

@ -45,6 +45,9 @@ public class Campaign {
public void setDestToHousingRoute(Route destToHousingRoute) { public void setDestToHousingRoute(Route destToHousingRoute) {
this.destToHousingRoute = destToHousingRoute; this.destToHousingRoute = destToHousingRoute;
for(Vehicle vehicle : this.getVehicles()){
vehicle.setRoute(destToHousingRoute);
}
} }
public District getHotelDistrict(){ return this.hotelDistrict; } public District getHotelDistrict(){ return this.hotelDistrict; }

View File

@ -5,7 +5,7 @@ public class MakkahCity {
private static final ArrayList<Campaign> listOfCampaigns = new ArrayList<>(); private static final ArrayList<Campaign> listOfCampaigns = new ArrayList<>();
private static final ArrayList<Vehicle> listOfVehicles = new ArrayList<>(); private static final ArrayList<Vehicle> listOfVehicles = new ArrayList<>();
private static final Route[] stdRoutes = new Route[9]; private static final Route[] stdRoutes = new Route[6];
private static final Street[] stdStreet = new Street[8]; private static final Street[] stdStreet = new Street[8];
private static final PDate timeManager = new PDate( private static final PDate timeManager = new PDate(
@ -31,37 +31,33 @@ public class MakkahCity {
//Set Routes for Campaigns //Set Routes for Campaigns
setRoutesForCampaigns(); setRoutesForCampaigns();
//TODO: Set Schedule for Campaigns //TODO: [1]Set Schedule for Campaigns
while(!timeManager.isEnded()) { while(!timeManager.isEnded()) {
timeManager.step(Calendar.MINUTE, 1); timeManager.step(Calendar.MINUTE, 1);
System.out.println(timeManager.getCurrentTime()); System.out.println(timeManager.getCurrentTime());
//TODO: add civil cars in loop iterations. (noise) //TODO: [2]add civil cars in loop iterations. (noise)
//noise based on time of day (From PDate) //noise based on time of day (From PDate)
//TODO: Move busses and vehicles. //TODO: [3]Move busses and vehicles.
//TODO: Update streets. //TODO: [4]Update streets.
//TODO: Streets move forward. //TODO: [5]Streets move forward.
//TODO: update vehicles on street. //TODO: [6]update vehicles on street.
} }
} }
private static void setRoutesForCampaigns() { private static void setRoutesForCampaigns() {
for (Campaign camp : listOfCampaigns){ for (Campaign camp : listOfCampaigns){
if (camp.getHotelDistrict() == District.ALAZIZIYA) { camp.setDestToHousingRoute(getShortestRoute(camp));
setUpCampaginRoute(camp, RouteName.mashierToAlAzizi1);
}
else if (camp.getHotelDistrict() == District.ALMANSOOR){
setUpCampaginRoute(camp, RouteName.mashierToAlMansoor2);
}
else {
setUpCampaginRoute(camp, RouteName.mashierToAlHijra1);
}
} }
} }
/*
This is not used. The campaign object sets the routes for the busses
*/
@Deprecated
private static void setUpCampaginRoute(Campaign camp, int routeName) { private static void setUpCampaginRoute(Campaign camp, int routeName) {
Route route = stdRoutes[routeName]; Route route = stdRoutes[routeName];
camp.setDestToHousingRoute(route); camp.setDestToHousingRoute(route);
@ -90,7 +86,7 @@ public class MakkahCity {
stdStreet[StreetNames.STREET2] = new Street(7000,2); stdStreet[StreetNames.STREET2] = new Street(7000,2);
stdStreet[StreetNames.STREET3] = new Street(400,2); stdStreet[StreetNames.STREET3] = new Street(400,2);
stdStreet[StreetNames.STREET4] = new Street(8200,2); stdStreet[StreetNames.STREET4] = new Street(8200,2);
stdStreet[StreetNames.IBRAHIM_ALKHALIL] = new Street(100,2); //TODO: Change numbers stdStreet[StreetNames.IBRAHIM_ALKHALIL] = new Street(100,2); //TODO: [7]Change numbers
} }
private static void makeRoutes() { private static void makeRoutes() {
@ -106,7 +102,7 @@ public class MakkahCity {
stdStreet[StreetNames.STREET1], stdStreet[StreetNames.STREET1],
stdStreet[StreetNames.FOURTH_HIGHWAY], stdStreet[StreetNames.FOURTH_HIGHWAY],
stdStreet[StreetNames.STREET4] stdStreet[StreetNames.STREET4]
},District.ALAZIZIYA, Mashier.ARAFAT); },District.ALHIJRA, Mashier.ARAFAT);
stdRoutes[RouteName.mashierToAlMansoor1] = new Route( stdRoutes[RouteName.mashierToAlMansoor1] = new Route(
new Street[]{ new Street[]{
@ -121,7 +117,7 @@ public class MakkahCity {
stdStreet[StreetNames.STREET1], stdStreet[StreetNames.STREET1],
stdStreet[StreetNames.STREET2], stdStreet[StreetNames.STREET2],
stdStreet[StreetNames.KUDAY], stdStreet[StreetNames.KUDAY],
stdStreet[StreetNames.IBRAHIM_ALKHALIL]//TODO: is actually half of ibrahim khalil. stdStreet[StreetNames.IBRAHIM_ALKHALIL]//TODO: [8]is actually half of ibrahim khalil.
},District.ALMANSOOR, Mashier.ARAFAT); },District.ALMANSOOR, Mashier.ARAFAT);
//Optimal for Almansoor //Optimal for Almansoor
@ -176,4 +172,39 @@ public class MakkahCity {
public static PDate getTimeManager() { public static PDate getTimeManager() {
return timeManager; return timeManager;
} }
/**
* Find shortest path without respect to traffic
* @param campaign
* @return
*/
private static Route getShortestRoute(Campaign campaign) {
Route[] routes = getRoutesToDistrict(campaign.getHotelDistrict());
Route route = null;
double min = Double.MAX_VALUE;
for (Route r : routes) {
if (r.getTotalLength() < min) {
min = r.getTotalLength();
route = r;
}
}
return route;
}
/**
* Find routes that connect to a certain district.
* @param district
* @return Array of routes that connect to 'district'
*/
private static Route[] getRoutesToDistrict(District district) {
ArrayList<Route> routes = new ArrayList<>();
for (Route route : stdRoutes) {
if (route.getHotelArea() == district) {
routes.add(route);
}
}
Route[] routesArray = new Route[routes.size()];
return routes.toArray(routesArray);
}
} }

View File

@ -1,9 +1,15 @@
import java.util.Date;
public abstract class Vehicle { public abstract class Vehicle {
private double vehicleSize; private double vehicleSize;
private Route route; private Route route;
private Street currentStreet; private Street currentStreet;
private double currentLocation; private double currentLocation;
private int currentStreetIndex;
private boolean arrivedToDest;
private boolean moving;
private Date timeStartedMoving;
public Vehicle(double vehicleSize){ public Vehicle(double vehicleSize){
setVehicleSize(vehicleSize); setVehicleSize(vehicleSize);
@ -18,6 +24,8 @@ public abstract class Vehicle {
public void setRoute(Route route) { public void setRoute(Route route) {
this.route = route; this.route = route;
this.currentStreetIndex = 0;
this.route.getStreets()[0].addVehicle(this);//TODO: [9]street might not add if capacity is low
} }
private void setVehicleSize(double vehicleSize) { private void setVehicleSize(double vehicleSize) {
@ -33,11 +41,30 @@ public abstract class Vehicle {
return currentLocation; return currentLocation;
} }
//TODO: Manage movement and setStreet and location ons street.
public void moveForward(double distance) { public void moveForward(double distance) {
//TODO: Check if at end of street move to next street in Route. if (moving && !arrivedToDest){
this.currentLocation += distance; if (isAtEndOfCurrentStreet()) {
moveToNextStreet();
//this.currentLocation += distance;
}
else this.currentLocation += distance;
}
}
private boolean isAtEndOfCurrentStreet() {
//At last meter of current street
return (this.currentStreet.getLength() - this.currentLocation) < 1;
}
private void moveToNextStreet() {
this.currentStreetIndex++;
Street nextStreet;
try { nextStreet = this.getRoute().getStreets()[currentStreetIndex]; }
catch (IndexOutOfBoundsException e) { this.arrivedToDest = true; return;}
if (nextStreet.canTakeVehicles(this)){
this.currentStreet = nextStreet;
this.currentLocation = 0;
}
} }
/** /**
@ -45,9 +72,13 @@ public abstract class Vehicle {
*/ */
public double getDistanceToNextVehicle() { public double getDistanceToNextVehicle() {
if (getCurrentStreet() != null) { if (getCurrentStreet() != null) {
int indexOfNxt = getCurrentStreet().getVehicles().indexOf(this) + 1; //next int indexOfNxt = getCurrentStreet().getVehicles().indexOf(this) - 1; //next
Vehicle nextVehicle = getCurrentStreet().getVehicles().get(indexOfNxt); //Index may be out of bound if this car is the only or first (end if street)
return nextVehicle.getCurrentLocation() - this.getCurrentLocation(); try {
Vehicle nextVehicle = getCurrentStreet().getVehicles().get(indexOfNxt);
return nextVehicle.getCurrentLocation() - this.getCurrentLocation();
}
catch (IndexOutOfBoundsException e) {return -1;}
} }
return -1;//open or unlimited return -1;//open or unlimited
} }