diff --git a/src/Bus.java b/src/Bus.java index 929c929..271a7ed 100644 --- a/src/Bus.java +++ b/src/Bus.java @@ -4,7 +4,7 @@ public class Bus extends CivilVehicle { private String UID; private static int numeberOfBuses; private final int TIME_TO_FIX = 20; //in minutes - public final int MAX_FORWARD = 900; //Meter/Min + public static final int MAX_FORWARD = 900; //Meter/Min public static final double STD_BUS_SIZE = 10; diff --git a/src/Campaign.java b/src/Campaign.java index b4c163b..bc0ac24 100644 --- a/src/Campaign.java +++ b/src/Campaign.java @@ -13,10 +13,11 @@ public class Campaign { private ArrayList vehicles = new ArrayList<>(); - //Will be of type PDate after extention private Date timeToLeaveToDest; private Date timeToLeaveToHousing; + private boolean startedMoving; + private static int numeberOfCampains; public Campaign(District hotelDistrict, int numberofBusses) { @@ -89,6 +90,10 @@ public class Campaign { this.vehicles = vehicles; } + public void setStartedMoving(){ + this.startedMoving = true; + } + private void generateBusses(int number){ for (int i = 1; i <= number; i++) { vehicles.add(new Bus()); diff --git a/src/MakkahCity.java b/src/MakkahCity.java index 98c3a2d..70a6792 100644 --- a/src/MakkahCity.java +++ b/src/MakkahCity.java @@ -10,64 +10,97 @@ public class MakkahCity { private static final PDate timeManager = new PDate( new GregorianCalendar(2020, Calendar.JANUARY, 1, 4, 0, 0), - new GregorianCalendar(2020, Calendar.JANUARY, 1, 20, 0, 0) + new GregorianCalendar(2020, Calendar.JANUARY, 1, 17, 0, 0) ); public static void main(String[] args) { //Gen Camp - generateCamps(District.ALAZIZIYA, getRandom(70, 100)); - generateCamps(District.ALMANSOOR, getRandom(110, 160)); - generateCamps(District.ALHIJRA, getRandom(80, 110)); + generateCamps(District.ALAZIZIYA, (int)getRandom(70, 100)); + generateCamps(District.ALMANSOOR, (int)getRandom(110, 160)); + generateCamps(District.ALHIJRA, (int)getRandom(80, 110)); - fillBusesToList(); + //fillBusesToList(); //Make Streets makeStreets(); + addCivilVehicleNoise(); + //Make Routes makeRoutes(); //Set Routes for Campaigns setRoutesForCampaigns(); - - + //TODO: use Queues or Wating area for each street? while(!timeManager.isEnded()) { - timeManager.step(Calendar.MINUTE, 1); - //System.out.println(timeManager.getCurrentTime()); - Vehicle v = listOfVehicles.get(0); - Vehicle v2 = listOfVehicles.get(1); //Start of Every hour if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) == 0){ } //Start of Every half-hour - if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) == 30){ + if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) % 30 == 0){ } + //Start of every 10min + if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) % 10 == 0){ + addCivilVehicleNoise(); + } if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) == getRandom(0,59) && timeManager.getCurrentCalendar().get(Calendar.SECOND) == getRandom(0,59)){ } - System.out.println("v1 "+ v.getCurrentLocation()+ " " + v.getCurrentStreet().getName().name()); - System.out.println("v2 "+ v2.getCurrentLocation()+ " " + v.getCurrentStreet().getName().name()); - //TODO: [2]add civil cars in loop iterations. (noise) - //noise based on time of day (From PDate) - //TODO: [3]Move busses and vehicles. - //TODO: [4]Update streets. + for (Vehicle vehicle : listOfVehicles) { + Route route = vehicle.getRoute(); + double currentLocation = vehicle.getCurrentLocation(); + if (vehicle.getCurrentStreet() == null && + route.getStreets()[0].capcityPoint(0,1500) < 1) { + vehicle.setCurrentStreet(route.getStreets()[0]); + } + else if (vehicle.getCurrentStreet() != null && vehicle.getCurrentStreet().capcityPoint(currentLocation+1500, + currentLocation+1500*2) < 1 ) { //May test diff values. - //TODO: [5]Streets move forward. - - for (Campaign campaign : listOfCampaigns){ - for (Vehicle vehicle : campaign.getVehicles()){ - vehicle.moveForward(((Bus)vehicle).MAX_FORWARD); + if (currentLocation >= vehicle.getCurrentStreet().getLength()) { + //Move to next street + vehicle.setCurrentLocation(0); + int nxtIndex = route.indexOf(vehicle.getCurrentStreet()) + 1; + if (nxtIndex <= route.getStreets().length - 1) + vehicle.setCurrentStreet(route.getStreets()[nxtIndex]); + else + vehicle.arrive(); + } + if (!vehicle.isArrivedToDest()) { + if (vehicle instanceof Bus) vehicle.move(Bus.MAX_FORWARD); + else if (vehicle instanceof Sedan) vehicle.move(Sedan.MAX_FORWARD); + else if (vehicle instanceof SUV) vehicle.move(SUV.MAX_FORWARD); + else if (vehicle instanceof Truck) vehicle.move(Bus.MAX_FORWARD); + } } } + Vehicle v = listOfVehicles.get(10); + if (v.getCurrentStreet() != null) { + System.out.printf("St: %s distance: %f total: %f %s\n", + v.getCurrentStreet().getName(), + v.getCurrentLocation(), + v.getTotalDistanceTraveled(), + timeManager.getCurrentTime()); + } + //noise based on time of day (From PDate) - //TODO: [6]update vehicles on street. + //TODO: [5]Streets move forward. + //TODO: Get real car values. + //TODO: Get real street lengths + //TODO: UID For all vehicles + + /* + Output: + Street stats + Campaigns avg (at end) + */ + timeManager.step(Calendar.MINUTE, 1); } } @@ -90,13 +123,13 @@ public class MakkahCity { } } - private static int getRandom(int min, int max) { - return (int)(Math.random() * (max - min) + min); + private static double getRandom(double min, double max) { + return (Math.random() * (max - min) + min); } private static void generateCamps(District area, int count) { for (int i = 0; i < count; i++){ - Campaign camp = new Campaign(area, getRandom(10, 15)); + Campaign camp = new Campaign(area, (int)getRandom(10, 15)); listOfCampaigns.add(camp); } } @@ -166,6 +199,56 @@ public class MakkahCity { } } + private static void addCivilVehicleNoise() { + + for (Street street: stdStreet) { + int numOfSedan = (int)getRandom(7, 15); + int numOfSUV = (int)getRandom(5, 10); + int numOfTruck = (int)getRandom(0, 2); + + if (street.getName() == StreetNames.FOURTH_HIGHWAY) numOfSedan = (int) (numOfSedan * 0.5); + if (street.getName() == StreetNames.STREET1) numOfSedan = (int) (numOfSedan * 1.5); //add more streets + for (int x = 0; x < numOfSedan; x++) { + Sedan car = new Sedan(getRandom(4, 5)); + double pointOfEntry = getRandom(0, street.getLength()); + if (street.capcityPoint(pointOfEntry, pointOfEntry+1500) < 1){ + listOfVehicles.add(car); + car.setCurrentLocation(pointOfEntry); + car.setRoute(new Route(street)); + car.setCurrentStreet(street); + } + + } + + if (street.getName() == StreetNames.FOURTH_HIGHWAY) numOfTruck = (int) (numOfTruck * 0.5); + if (street.getName() == StreetNames.STREET1) numOfTruck = (int) (numOfTruck * 1.5); //add more streets + for (int x = 0; x < numOfTruck; x++) { + Truck car = new Truck(getRandom(4, 5)); + double pointOfEntry = getRandom(0, street.getLength()); + if (street.capcityPoint(pointOfEntry, pointOfEntry+1500) < 1){ + listOfVehicles.add(car); + car.setCurrentLocation(pointOfEntry); + car.setRoute(new Route(street)); + car.setCurrentStreet(street); + } + } + + if (street.getName() == StreetNames.FOURTH_HIGHWAY) numOfSUV = (int) (numOfSUV * 0.5); + if (street.getName() == StreetNames.STREET1) numOfSUV = (int) (numOfSUV * 1.5); //add more streets + for (int x = 0; x < numOfSUV; x++) { + SUV car = new SUV(getRandom(4, 5)); + double pointOfEntry = getRandom(0, street.getLength()); + if (street.capcityPoint(pointOfEntry, pointOfEntry+1500) < 1){ + listOfVehicles.add(car); + car.setCurrentLocation(pointOfEntry); + car.setRoute(new Route(street)); + car.setCurrentStreet(street); + } + } + + } + } + /** * Generates 'noise' cars to be used in streets * @param numberOfCars diff --git a/src/Route.java b/src/Route.java index e7edb29..5c143b1 100644 --- a/src/Route.java +++ b/src/Route.java @@ -10,6 +10,18 @@ public class Route { setMashier(mashier); } + public Route(Street street){ + this.streets = new Street[1]; + this.streets[0] = street; + } + + public int indexOf(Street street){ + for (int i = 0; i < streets.length; i++) { + if (street == streets[i]) return i; + } + return -1; + } + public Street[] getStreets() { return this.streets; } diff --git a/src/SUV.java b/src/SUV.java index 4bd690d..2da980e 100644 --- a/src/SUV.java +++ b/src/SUV.java @@ -2,7 +2,7 @@ public class SUV extends CivilVehicle { private final int TIME_TO_FIX = 15; //in minutes - public final int MAX_FORWARD = 1300; + public static final int MAX_FORWARD = 1300; public SUV(double vehicleSize){ super(vehicleSize); diff --git a/src/Sedan.java b/src/Sedan.java index e6ee0ac..3f9de04 100644 --- a/src/Sedan.java +++ b/src/Sedan.java @@ -2,7 +2,7 @@ public class Sedan extends CivilVehicle { private final int TIME_TO_FIX = 15; //in minutes - public final int MAX_FORWARD = 1500; // Meter/Min + public static final int MAX_FORWARD = 1500; // Meter/Min public Sedan(double vehicleSize){ super(vehicleSize); diff --git a/src/Street.java b/src/Street.java index a482972..78edd6f 100644 --- a/src/Street.java +++ b/src/Street.java @@ -71,9 +71,20 @@ public class Street { } public void addVehicle(Vehicle vehicle) { - if(capcity() > vehicle.getVehicleSize() + 0.5) { + //if(capcity() > vehicle.getVehicleSize() + 0.5) { //adds incoming vehicle in last. vehicles.add(vehicle); + //} + } + + public double capcityPoint(double min, double max) { + double totalLength = (max - min) * numberOfLanes; + double totalLenthofCar=0; + for(int i=0;i= min && + vehicles.get(i).getCurrentLocation() <= max) + totalLenthofCar+=vehicles.get(i).getVehicleSize(); } + return totalLenthofCar / totalLength; } } diff --git a/src/Vehicle.java b/src/Vehicle.java index 3e69ad4..ca2705f 100644 --- a/src/Vehicle.java +++ b/src/Vehicle.java @@ -6,14 +6,16 @@ public abstract class Vehicle { private Route route; private Street currentStreet; private double currentLocation; - private int currentStreetIndex; + private double totalDistanceTraveled; private boolean arrivedToDest; private boolean moving; private Date timeStartedMoving; + private Date timeOfArrival; public Vehicle(double vehicleSize){ setVehicleSize(vehicleSize); } + public double getVehicleSize() { return vehicleSize; } @@ -22,11 +24,26 @@ public abstract class Vehicle { return route; } + public void arrive() { + setArrivedToDest(true); + setMoving(false); + setTimeOfArrival(MakkahCity.getTimeManager().getCurrentTime()); + getCurrentStreet().getVehicles().remove(this); + } + + public void move(double distance) { + if (!isMoving()) { + setMoving(true); + setTimeStartedMoving(MakkahCity.getTimeManager().getCurrentTime()); + } + setCurrentLocation(getCurrentLocation() + distance); + setTotalDistanceTraveled(getTotalDistanceTraveled() + distance); + } + public void setRoute(Route route) { this.route = route; - this.currentStreetIndex = 0; - this.currentStreet = route.getStreets()[0]; - this.route.getStreets()[0].addVehicle(this);//TODO: [9]street might not add if capacity is low + //this.currentStreet = route.getStreets()[0]; + //this.route.getStreets()[0].addVehicle(this); } private void setVehicleSize(double vehicleSize) { @@ -42,42 +59,6 @@ public abstract class Vehicle { return currentLocation; } - public void moveForward(double distance) { - if (!moving && !arrivedToDest) moving = true; - if (!arrivedToDest){ - if (isAtEndOfCurrentStreet()) { - moveToNextStreet(); - //this.currentLocation += distance; - } - else { - double dToNext = getDistanceToNextVehicle(); - double trafficFactor = getCurrentStreet().getPercentRemainingCapacity() / 100.0; - if (dToNext == -1 ) { - this.currentLocation += distance * trafficFactor; - } else if (dToNext < distance){//TODO after I wake up - 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; this.moving = false; return;} - if (nextStreet.canTakeVehicles(this)){ - this.currentStreet = nextStreet; - this.currentLocation = 0; - } - } - /** * @return Distance in meters to next car on same street. -1 if Can not calculate. */ @@ -93,4 +74,56 @@ public abstract class Vehicle { } return -1;//open or unlimited } + + public void setCurrentStreet(Street currentStreet) { + if (this.currentStreet != null) { + this.currentStreet.getVehicles().remove(this); + } + this.currentStreet = currentStreet; + this.currentStreet.addVehicle(this); + } + + public void setCurrentLocation(double currentLocation) { + this.currentLocation = currentLocation; + } + + public double getTotalDistanceTraveled() { + return totalDistanceTraveled; + } + + public void setTotalDistanceTraveled(double totalDistanceTraveled) { + this.totalDistanceTraveled = totalDistanceTraveled; + } + + public boolean isArrivedToDest() { + return arrivedToDest; + } + + public void setArrivedToDest(boolean arrivedToDest) { + this.arrivedToDest = arrivedToDest; + } + + public boolean isMoving() { + return moving; + } + + public void setMoving(boolean moving) { + this.moving = moving; + } + + public Date getTimeStartedMoving() { + return timeStartedMoving; + } + + public void setTimeStartedMoving(Date timeStartedMoving) { + this.timeStartedMoving = timeStartedMoving; + } + + public Date getTimeOfArrival() { + return timeOfArrival; + } + + public void setTimeOfArrival(Date timeOfArrival) { + this.timeOfArrival = timeOfArrival; + } }