Advertisement
Tal_Rofe

yy

Jan 15th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.50 KB | None | 0 0
  1. package algorithm;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5.  
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9.  
  10. public class Algorithm{
  11.    
  12.     private static class ArrayListCar extends ArrayList<Car>{
  13.        
  14.     }
  15.    
  16.     private int spotsInRow; // as an input from server. possible value: 4,5,6,7,8
  17.     private ArrayListCar[][][] park; // each spot has a list of all the cars belong to the spot
  18.     private char[][][] statusPark; // real time 3-dimensions array that has status for each spot
  19.     // e - EMPTY, f - FULL, i - INVALID, s - SAVE, o - ordered
  20.    
  21.     Algorithm(){ // CONSTRUCTOR
  22.         spotsInRow = 4; // spotsInRow = 4 as an initial value
  23.         statusPark = new char[3][3][4];
  24.         park = new ArrayListCar[3][3][4];
  25.        
  26.         int i, j, k;
  27.         for (i = 0; i < 3; i++)
  28.             for (j = 0; j < 3; j++)
  29.                 for(k = 0; k < 4; k++) {
  30.                     park[i][j][k] = new ArrayListCar(); // each cell has an empty list as an initial state
  31.                     statusPark[i][j][k] = 'e';
  32.                 }
  33.         }
  34.    
  35.     Algorithm(int numSpotsInRow){ // 2nd CONSTRUCTOR
  36.         statusPark = new char[3][3][numSpotsInRow];
  37.         park = new ArrayListCar[3][3][numSpotsInRow];
  38.         spotsInRow = numSpotsInRow;
  39.        
  40.         int i, j, k;
  41.         for (i = 0; i < 3; i++)
  42.             for (j = 0; j < 3; j++)
  43.                 for (k = 0; k < numSpotsInRow; k++) {
  44.                     park[i][j][k] = new ArrayListCar();
  45.                     statusPark[i][j][k] = 'e';
  46.                 }
  47.     }
  48.    
  49.     public Algorithm(int numSpotsInRow, String db, String si){ // 3rd CONSTRUCTOR
  50.         this.spotsInRow = numSpotsInRow;
  51.         this.park = generateParkFromString(db);
  52.         this.statusPark = generateStatusPark(si);
  53.        
  54.         int i, j, k;
  55.         for (i = 0; i < 3; i++)
  56.             for (j = 0; j < 3; j++)
  57.                 for (k = 0; k < numSpotsInRow; k++)
  58.                     // sort by exit time. It also means it is sorted by entry time.
  59.                     Collections.sort(park[i][j][k]);
  60.     }
  61.    
  62.     private char[][][] generateStatusPark(String si){
  63.         //TODO: make row width dynamic
  64.         char[][][] tmpStatuses = new char[3][3][4];
  65.         try {
  66.             JSONArray statuses = new JSONArray(si);
  67.             for(int index = 0; index < statuses.length(); index++) {
  68.                 JSONObject current = statuses.getJSONObject(index);
  69.                 int i = current.getInt("i");
  70.                 int j = current.getInt("j");
  71.                 int k = current.getInt("k");
  72.                 tmpStatuses[i][j][k] = current.getString("status").charAt(0);
  73.                
  74.             }
  75.         } catch (JSONException e) {
  76.             // TODO Auto-generated catch block
  77.             e.printStackTrace();
  78.         }
  79.         return tmpStatuses;
  80.     }
  81.    
  82.     private ArrayListCar[][][] generateParkFromString(String st){
  83.         ArrayListCar[][][] cars = new ArrayListCar[3][3][4];
  84.         for (int i = 0; i < 3; i++)
  85.             for (int j = 0; j < 3; j++)
  86.                 for(int k = 0; k < 4; k++)
  87.                     cars[i][j][k] = new ArrayListCar();
  88.         try {
  89.             JSONArray arr = new JSONArray(st);
  90.             for(int index = 0; index < arr.length(); index++) {
  91.                 JSONObject current = arr.getJSONObject(index);
  92.                 int i = current.getInt("i");
  93.                 int j = current.getInt("j");
  94.                 int k = current.getInt("k");
  95.                 String carID = current.getString("carID");
  96.                 long entryTime = current.getLong("entryTime");
  97.                 long exitTime = current.getLong("exitTime");
  98.                 if(cars[i][j][k] == null) {
  99.                     cars[i][j][k] = new ArrayListCar();
  100.                 }
  101.                 cars[i][j][k].add(new Car(carID, exitTime, entryTime));
  102.             }
  103.         } catch (JSONException e) {
  104.             // TODO Auto-generated catch block
  105.             e.printStackTrace();
  106.         }
  107.        
  108.         return null;
  109.     }
  110.    
  111.     //------------------------//
  112.    
  113.     // function checks whether specific spot is empty in real time or not
  114.         private boolean isEmptySpotRealTime(int i, int j, int k){
  115.             if (statusPark[i][j][k] == 's' || statusPark[i][j][k] == 'i')
  116.                 return false;
  117.            
  118.             long realTime = System.currentTimeMillis();
  119.             int z;
  120.             for (z = 0; z < park[i][j][k].size(); z++){
  121.                 if (realTime <= park[i][j][k].get(z).getExitTime() &&
  122.                         realTime >= park[i][j][k].get(z).getEntryTime())
  123.                     return false;
  124.             }
  125.             return true;
  126.         }
  127.  
  128.         // function checks whether specific spot is empty but has ordered cars belong to it
  129.         private boolean isEmptyButOrderedRealTime(int i, int j, int k){
  130.             if (!park[i][j][k].isEmpty() && isEmptySpotRealTime(i, j, k))
  131.                 return true;
  132.             return false;
  133.         }
  134.        
  135.         // function gets specific list of cars that have any connection with a specific spot
  136.         // returns true if the specific spot is empty in the given times
  137.         private boolean checkAvailableSpot(long entryTime, long exitTime, ArrayListCar list){
  138.             // NO NEED TO CHECK 's' , 'i' CASES
  139.            
  140.             if (list.isEmpty()){
  141.                 return true;
  142.             }
  143.             else if (exitTime <= list.get(0).getEntryTime())
  144.                 return true;
  145.             else if (entryTime >= list.get(list.size() - 1).getExitTime())
  146.                 return true;
  147.             else{
  148.                 int z;
  149.                 for (z = 0; z < list.size() - 1; z++){
  150.                     if (entryTime >= list.get(z).getExitTime() && exitTime <= list.get(z+1).getEntryTime())
  151.                         return true;
  152.                 }
  153.                 return false;
  154.             }
  155.         }
  156.        
  157.         // function checks whether the park is available to insertion command in the given times
  158.         private boolean checkAvailablePark(long entryTime, long exitTime){
  159.             int i, j, k;
  160.             for (i = 0; i < 3; i++)
  161.                 for (j = 0; j < 3; j++)
  162.                     for (k = 0; k < spotsInRow; k++)
  163.                         if ((statusPark[i][j][k] != 's' && statusPark[i][j][k] != 'i') &&
  164.                                 checkAvailableSpot(entryTime, exitTime, park[i][j][k]))
  165.                             return true;
  166.             return false;
  167.         }
  168.        
  169.         // function returns spot's coordinates where a given car is located at
  170.         private int[] locateCarSpot(String carID){
  171.             int[] a = {-1, -1, -1}; // an array holds the coordinates of the car
  172.             int i, j, k, z;
  173.            
  174.             for (i = 0; i < 3; i++){
  175.                 for (j = 0; j < 3; j++){
  176.                     for (k = 0; k < spotsInRow; k++){
  177.                         for(z = 0; z < park[i][j][k].size(); z++){
  178.                             if (park[i][j][k].get(z).getCarID().equals(carID)){
  179.                                 a[0] = i; a[1] = j; a[2] = k;
  180.                                 return a;
  181.                             }
  182.                         }
  183.                     }
  184.                 }
  185.             }
  186.             return a;
  187.         }
  188.        
  189.         // get the cars out of the park from a specific depth
  190.         // list is a list which keeps the ejected cars
  191.         private void ejectInDepth(ArrayListCar list, int i, int j, int k){
  192.             int t, z;
  193.             for (t = 0; t <= i; t++){
  194.                 if (statusPark[t][j][k] != 'i' && statusPark[t][j][k] != 's'){
  195.                     statusPark[t][j][k] = 'e'; // update the status because cars were ejected
  196.                    
  197.                     for (z = park[t][j][k].size() - 1; z >= 0; z--){
  198.                         list.add(park[t][j][k].get(z));
  199.                         park[t][j][k].remove(z);
  200.                     }
  201.                 }
  202.             }
  203.         }
  204.        
  205.         // get the cars out of the park from a specific floor
  206.         // list is a list which keeps the ejected cars
  207.         private void ejectInFloor(ArrayList<Car> list, int j, int k){
  208.             int t, z;
  209.             for (t = 0; t < j; t++){
  210.                 if (statusPark[0][t][k] != 'i' && statusPark[0][t][k] != 's'){
  211.                     statusPark[0][t][k] = 'e'; // update the status because cars we ejected
  212.                    
  213.                     for (z = park[0][t][k].size() - 1; z >= 0; z--){
  214.                         list.add(park[0][t][k].get(z));
  215.                         park[0][t][k].remove(z);
  216.                     }
  217.                 }
  218.             }
  219.         }
  220.        
  221.         // find the optimal position for an entry command of a car
  222.         private int[] locateOptimalSpot(long entryTime, long exitTime){
  223.             int i, j, k, z, moves, minMoves = 6;
  224.             int[] optimal = {-1, -1, -1};
  225.             boolean st;
  226.            
  227.             for (k = 0; k < spotsInRow; k++){ // first loop on width
  228.                 for (j = 0; j < 3; j++){ // then loop on height
  229.                    
  230.                     moves = 0; // will be calculated with each iteration
  231.                    
  232.                     for (z = 0; z < j; z++){
  233.                         if (statusPark[0][z][k] != 'i' && statusPark[0][z][k] != 's'){
  234.                             st = checkAvailableSpot(entryTime, exitTime, park[0][z][k]);
  235.                             if (!st) // if the spot won't be empty in the given times we need to move another car
  236.                                 moves++;
  237.                         }
  238.                     }
  239.                    
  240.                     for (i = 0; i < 3; i++){           
  241.                         if (statusPark[i][j][k] != 'i' && statusPark[i][j][k] != 's'){
  242.                             st = checkAvailableSpot(entryTime, exitTime, park[i][j][k]);
  243.                             if (st)
  244.                                 break;
  245.                             moves++;
  246.                         }
  247.                     }
  248.                    
  249.                     if (moves < minMoves){ // when we find lower number of moves we update
  250.                         minMoves = moves;
  251.                         optimal[0] = i; optimal[1] = j; optimal[2] = k;
  252.                     }
  253.                 }
  254.             }
  255.             return optimal;
  256.         }
  257.        
  258.         // function plots back the ejected cars (given in list) into the park
  259.         // assume list is sorted by exit time
  260.         private void reEnterCars(ArrayListCar list, int i, int j, int k){
  261.             long lastCTime;
  262.            
  263.             if (!list.isEmpty()){ // as long as we need to re-insert the ejected car
  264.                 int t = 0;
  265.                 for(t = 0; t < j; t++){
  266.                     if (statusPark[0][t][k] != 's' && statusPark[0][t][k] != 'i' && !list.isEmpty()){
  267.                         park[0][t][k].add(list.get(0));
  268.                         list.remove(0);
  269.                        
  270.                         while (!list.isEmpty()){
  271.                             lastCTime = park[0][t][k].get(park[0][t][k].size() - 1).getExitTime();
  272.                             if (list.get(0).getEntryTime() >= lastCTime){
  273.                                  park[0][t][k].add(list.get(0));
  274.                                  list.remove(0);
  275.                             }
  276.                             else
  277.                                 break;
  278.                         }
  279.                        
  280.                         if (!isEmptySpotRealTime(0, t, k))
  281.                             statusPark[0][t][k] = 'f';
  282.                        
  283.                         if (isEmptyButOrderedRealTime(0, t, k))
  284.                             statusPark[0][t][k] = 'o';
  285.                     }
  286.                 }
  287.  
  288.                
  289.                 t = 0;
  290.                 for(t = 0; t <= i; t++){
  291.                     if (statusPark[t][j][k] != 's' && statusPark[t][j][k] != 'i' && !list.isEmpty()){
  292.                         park[t][j][k].add(list.get(0));
  293.                         list.remove(0);
  294.                        
  295.                         while (!list.isEmpty()){
  296.                             lastCTime = park[t][j][k].get(park[t][j][k].size() - 1).getExitTime();
  297.                             if (list.get(0).getEntryTime() >= lastCTime){
  298.                                  park[t][j][k].add(list.get(0));
  299.                                  list.remove(0);
  300.                             }
  301.                             else
  302.                                 break;
  303.                         }
  304.                     }
  305.                 }
  306.             }
  307.         }
  308.        
  309.         public void insertOrderedCar(Car car, long entryTime, long exitTime){
  310.             boolean fullPark = !this.checkAvailablePark(entryTime, exitTime);
  311.             if(fullPark){
  312.                 // Send message to server: insertion command failed
  313.             }
  314.            
  315.             else{
  316.                 int[] a = locateOptimalSpot(entryTime, exitTime);
  317.                 park[a[0]][a[1]][a[2]].add(car);
  318.                
  319.                 if(isEmptyButOrderedRealTime(a[0], a[1], a[2]))
  320.                     statusPark[a[0]][a[1]][a[2]] = 'o';
  321.             }
  322.         }
  323.        
  324.         public void insertCar(Car car){
  325.             long entryTime = car.getEntryTime(), exitTime = car.getExitTime();
  326.            
  327.             boolean fullPark = !this.checkAvailablePark((int)entryTime, exitTime);
  328.             if(fullPark){
  329.                 // Send message to server: insertion command failed
  330.             }
  331.            
  332.             else{
  333.                 int[] a = locateOptimalSpot(entryTime, exitTime);
  334.                 park[a[0]][a[1]][a[2]].add(car);
  335.                 Collections.sort(park[a[0]][a[1]][a[2]]);
  336.                 statusPark[a[0]][a[1]][a[2]] = 'f';
  337.                
  338.                 ArrayListCar list = new ArrayListCar();
  339.                 ejectInDepth(list, a[0] - 1, a[1], a[2]);
  340.                 ejectInFloor(list, a[1], a[2]);
  341.                 Collections.sort(list);
  342.                
  343.                 reEnterCars(list, a[0] - 1, a[1], a[2]);
  344.             }
  345.         }
  346.        
  347.         public void ejectCar(Car car){
  348.             int[] a = locateCarSpot(car.getCarID());
  349.             ArrayListCar list = new ArrayListCar();
  350.             ejectInDepth(list, a[0] - 1, a[1], a[2]);
  351.             ejectInFloor(list, a[1], a[2]);
  352.             Collections.sort(list);
  353.             int i;
  354.             for (i = 0; i < park[a[0]][a[1]][a[2]].size(); i++){
  355.                 if (park[a[0]][a[1]][a[2]].get(i).getCarID().equals(car.getCarID())){
  356.                     park[a[0]][a[1]][a[2]].remove(i);
  357.                     break;
  358.                 }
  359.             }
  360.             Collections.sort(park[a[0]][a[1]][a[2]]);
  361.            
  362.             if (isEmptySpotRealTime(a[0], a[1], a[2]))
  363.                 statusPark[a[0]][a[1]][a[2]] = 'e';
  364.            
  365.             if (isEmptyButOrderedRealTime(a[0], a[1], a[2]))
  366.                 statusPark[a[0]][a[1]][a[2]] = 'o';
  367.            
  368.                
  369.             reEnterCars(list, a[0] - 1, a[1], a[2]);
  370.         }
  371.    
  372.     //------------------------//
  373.    
  374.    
  375.     // FORMAT: <Calculation of converted convenient indexes> with <status in the spot>
  376.         public String generateStatusStringOld(){
  377.             String statusString = "";
  378.             for(int i = 0 ; i < 3; i++){
  379.                 for(int j = 0; j < 3; j++){
  380.                     for(int k = 0; k < this.spotsInRow; k++){
  381.                         int value = i * 3 * this.spotsInRow + j * this.spotsInRow + k;
  382.                         statusString += Integer.toString(value);
  383.                         statusString += this.statusPark[i][j][k];
  384.                     }
  385.                 }
  386.             }
  387.             return statusString;
  388.         }
  389.         public String generateStatusString(){
  390.             JSONArray statuses = new JSONArray();
  391.             //String statusString = "";
  392.             for(int i = 0 ; i < 3; i++){
  393.                 for(int j = 0; j < 3; j++){
  394.                     for(int k = 0; k < this.spotsInRow; k++){
  395.                         JSONObject current = new JSONObject();
  396.                         //System.out.println(i + "," + j + "," + k);
  397.                         try {
  398.                             //current.put("pos", i * 3 * this.spotsInRow + j * this.spotsInRow + k);
  399.                             current.put("i", i);
  400.                             current.put("j", j);
  401.                             current.put("k", k);
  402.                             current.put("status", String.valueOf(this.statusPark[i][j][k]));
  403.                         } catch (JSONException e) {
  404.                             // TODO Auto-generated catch block
  405.                             e.printStackTrace();
  406.                         }
  407.                         statuses.put(current);
  408.                        
  409.                     }
  410.                 }
  411.             }
  412.             //return statusString;
  413.             return statuses.toString();
  414.         }
  415.        
  416.         // FORMAT: <depth index,> <height index,> <width index,> <carID> <entry time of car,>
  417.         // <exit time of car,> <#> <\n> ((EACH LINE DESCRIBES ONE SPOT))
  418.         public String generateDBString(){
  419.             JSONArray spots = new JSONArray();
  420.             int i, j, k, z;
  421.             //String DBString = "";
  422.             for (i = 0; i < 3; i++){
  423.                 for(j = 0; j < 3; j++){
  424.                     for(k = 0; k < this.spotsInRow; k++){
  425.                         for (z = 0; z < park[i][j][k].size(); z++){
  426.                            
  427.                             JSONObject current = new JSONObject();
  428.                             try {
  429.                                 current.put("i", i);
  430.                                 current.put("j", j);
  431.                                 current.put("k", k);
  432.                                 current.put("carID", park[i][j][k].get(z).getCarID());
  433.                                 current.put("entryTime", park[i][j][k].get(z).getEntryTime());
  434.                                 current.put("exitTime", park[i][j][k].get(z).getExitTime());
  435.                                 spots.put(current);
  436.                             }catch(Exception e) {
  437.                                 e.printStackTrace();
  438.                             }
  439.  
  440.                         }
  441.                     }
  442.                 }
  443.             }
  444.             //return DBString;
  445.             return spots.toString();
  446.         }
  447.        
  448.    
  449.    
  450.    
  451.     public static void main(String args[]) {
  452.        
  453.        
  454.         Algorithm alg = new Algorithm(4);
  455.        
  456.         alg.statusPark[0][0][0] = 'i';
  457.        
  458.         alg.insertCar(new Car("cesc", 2l, 9l));
  459.         alg.insertCar(new Car("roare", 9l, 11l));
  460.         alg.insertCar(new Car("roland", 11l, 12l));
  461.         alg.insertCar(new Car("obo", 13l, 16l));
  462.         alg.insertCar(new Car("sakho", 7l, 11l));
  463.         alg.insertCar(new Car("miki", 66, 67));
  464.        
  465.         int[] a = alg.locateCarSpot("cesc");
  466.         System.out.println("car: " + a[0] + "," + a[1] + ","+a[2]);
  467.        
  468.         a = alg.locateCarSpot("roare");
  469.         System.out.println("car: " + a[0] + "," + a[1] + ","+a[2]);
  470.        
  471.          System.out.println(alg.generateStatusString());
  472.          System.out.println(alg.generateDBString());
  473.          
  474.          System.out.println("nnn: " + alg.statusPark[0][2][2]);
  475.          
  476.          //System.out.println(alg.park[0][0][0].get(0).getCarID());
  477.          //System.out.println(alg.park[0][0][0].get(1).getCarID());
  478.          
  479.     }
  480.    
  481. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement