Advertisement
Tal_Rofe

alg

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