Guest User

Untitled

a guest
Jul 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.62 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.BitSet;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.PriorityQueue;
  6.  
  7.  
  8. public class BF{
  9.  
  10.     ArrayList<ArrayList<BitSet>> statesList;
  11.     Comparator<Node> comparator;
  12.     PriorityQueue<Node> nodeQueue;
  13.     ArrayList<BitSet> finalState;
  14.     String order;
  15.     char[] strategy;
  16.     String solution="";
  17.     boolean isSolved=false;
  18.     int depth=0;
  19.     int size;
  20.  
  21.  
  22.  
  23.  
  24.     public ArrayList<ArrayList<BitSet>> getStatesList() {
  25.         return statesList;
  26.     }
  27.  
  28.     public void setStatesList(ArrayList<ArrayList<BitSet>> statesList) {
  29.         this.statesList = statesList;
  30.     }
  31.  
  32.     public PriorityQueue<Node> getNodeQueue() {
  33.         return nodeQueue;
  34.     }
  35.  
  36.     public void setNodeQueue(PriorityQueue<Node> nodeQueue) {
  37.         this.nodeQueue = nodeQueue;
  38.     }
  39.  
  40.     public ArrayList<BitSet> getFinalState() {
  41.         return finalState;
  42.     }
  43.  
  44.     public void setFinalState(ArrayList<BitSet> finalState) {
  45.         this.finalState = finalState;
  46.     }
  47.  
  48.     public String getOrder() {
  49.         return order;
  50.     }
  51.  
  52.     public void setOrder(String order) {
  53.         this.order = order;
  54.     }
  55.  
  56.     public char[] getStrategy() {
  57.         return strategy;
  58.     }
  59.  
  60.     public void setStrategy(char[] strategy) {
  61.         this.strategy = strategy;
  62.     }
  63.  
  64.     public String getSolution() {
  65.         return solution;
  66.     }
  67.  
  68.     public void setSolution(String solution) {
  69.         this.solution = solution;
  70.     }
  71.  
  72.     public boolean isSolved() {
  73.         return isSolved;
  74.     }
  75.  
  76.     public void setSolved(boolean isSolved) {
  77.         this.isSolved = isSolved;
  78.     }
  79.  
  80.     public int getDepth() {
  81.         return depth;
  82.     }
  83.  
  84.     public void setDepth(int depth) {
  85.         this.depth = depth;
  86.     }
  87.  
  88.     public int getSize() {
  89.         return size;
  90.     }
  91.  
  92.     public void setSize(int size) {
  93.         this.size = size;
  94.     }
  95.  
  96.     public BF(int size) {
  97.         this.statesList = new ArrayList<ArrayList<BitSet>>();
  98.         this.finalState = new ArrayList<BitSet>();
  99.         this.comparator = new Sort();
  100.         this.nodeQueue = new PriorityQueue<Node>(3000,comparator);
  101.         this.size = size;
  102.     }
  103.  
  104.     public void addToStatesList(ArrayList<BitSet> state)
  105.     {
  106.         statesList.add(state);
  107.  
  108.     }
  109.  
  110.     public void createFinalState()
  111.     {
  112.         if(this.size==4)
  113.         {
  114.             ArrayList<Integer> tmp = new ArrayList<Integer>();
  115.             tmp.add(1);tmp.add(2);tmp.add(3);tmp.add(4);
  116.             tmp.add(5);tmp.add(6);tmp.add(7);tmp.add(8);
  117.             tmp.add(9);tmp.add(10);tmp.add(11);tmp.add(12);
  118.             tmp.add(13);tmp.add(14);tmp.add(15);tmp.add(0);
  119.             Converter con = new Converter(tmp);
  120.             this.finalState = con.covertToBitSet();
  121.         }
  122.         else if (this.size==3)
  123.         {
  124.             ArrayList<Integer> tmp = new ArrayList<Integer>();
  125.             tmp.add(1);tmp.add(2);tmp.add(3);
  126.             tmp.add(4);tmp.add(5);tmp.add(6);
  127.             tmp.add(7);tmp.add(8);tmp.add(0);
  128.             Converter con = new Converter(tmp);
  129.             this.finalState = con.covertToBitSet();
  130.         }
  131.     }
  132.  
  133.     public boolean isFinished(ArrayList<BitSet> st)
  134.     {
  135.         ArrayList<BitSet> tmp = new ArrayList<BitSet>(st);
  136.         int comp=0;
  137.         for(int i=0;i<tmp.size();i++)
  138.         {
  139.             if(tmp.get(i).toString().compareTo(finalState.get(i).toString())==0) comp++;
  140.             else return false;
  141.  
  142.         }
  143.  
  144.         if(comp==tmp.size()) return true;
  145.         else
  146.             return false;
  147.     }
  148.  
  149.     public int heuristic1(ArrayList<BitSet> st){
  150.         ArrayList<BitSet> tmp = new ArrayList<BitSet>(st);
  151.         int comp=0;
  152.         for(int i=0;i<tmp.size();i++)
  153.         {
  154.             if(!(tmp.get(i).toString().compareTo(finalState.get(i).toString())==0)) comp++;
  155.         }
  156.  
  157.         return comp;
  158.  
  159.     }
  160.  
  161.     public boolean isStateUnique(ArrayList<BitSet> tmp)
  162.     {
  163.  
  164.         int comp;
  165.  
  166.         for(int j=0;j<statesList.size();j++)
  167.         {
  168.             comp=0;
  169.  
  170.             ArrayList<BitSet> tmp2 = new ArrayList<BitSet>(statesList.get(j));
  171.  
  172.             for(int i=0;i<tmp.size();i++)
  173.             {
  174.                 if(tmp.get(i).toString().compareTo(tmp2.get(i).toString())==0) comp++;
  175.             }
  176.  
  177.             if(comp==tmp.size())
  178.             {
  179.                 return false;
  180.             }
  181.         }
  182.  
  183.         return true;
  184.     }
  185.  
  186.     public int findEmpty(ArrayList<BitSet> st)
  187.     {
  188.         int zero=0;
  189.  
  190.         ArrayList<BitSet> tmp = st;
  191.  
  192.         for(int i=0;i<tmp.size();i++)
  193.         {
  194.             if(tmp.get(i).toString().compareTo("{}")==0) zero=i;
  195.  
  196.         }
  197.  
  198.         return zero;
  199.     }
  200.  
  201.  
  202.     public void searchBF(Node n){
  203.  
  204.         System.out.println("Glebokosc: "+(++depth));
  205.  
  206.         if(isSolved==false)
  207.         {
  208.  
  209.             ArrayList<BitSet> newState;
  210.             int zero;
  211.             zero = findEmpty(n.nodeState);
  212.             System.out.println("zero"+zero);
  213.  
  214.             if(zero%size!=0 &&isSolved==false)
  215.             {
  216.                 System.out.println("L");
  217.                 newState = new ArrayList<BitSet>(n.nodeState);
  218.                 Collections.swap(newState, zero, zero-1);
  219.  
  220.                 if(isStateUnique(newState)==true && isFinished(newState)==false)
  221.                 {
  222.                     Node tmpNode = new Node(n.path+'L',n.path,statesList.get(statesList.size()-1), heuristic1(newState));
  223.                     nodeQueue.add(tmpNode);
  224.  
  225.                 }
  226.                 if(isFinished(newState)==true)
  227.                 {
  228.                     isSolved=true;
  229.                     solution=(n.path)+'L';
  230.                     return;
  231.                 }
  232.             }
  233.  
  234.             if((zero%size)!=(size-1) &&isSolved==false )
  235.             {
  236.                 System.out.println("P");
  237.                 newState = new ArrayList<BitSet>(n.nodeState);
  238.                 Collections.swap(newState, zero, zero+1);
  239.  
  240.                 if(isStateUnique(newState)==true && isFinished(newState)==false)
  241.                 {
  242.                     System.out.println("PPPPP");
  243.                     System.out.println(newState.toString());
  244.                     Node tmpNode = new Node(n.path+'P',n.path,statesList.get(statesList.size()-1), heuristic1(newState));
  245.                     System.out.println(heuristic1(newState));
  246.                     nodeQueue.add(tmpNode);
  247.  
  248.                 }
  249.                 if(isFinished(newState)==true)
  250.                 {
  251.                     isSolved=true;
  252.                     solution=(n.path)+'P';
  253.                     return;
  254.                 }
  255.             }
  256.  
  257.             if(zero >= size &&isSolved==false)
  258.             {
  259.                 System.out.println("G");
  260.                 newState = new ArrayList<BitSet>(n.nodeState);
  261.                 Collections.swap(newState, zero, zero-size);
  262.  
  263.                 if(isStateUnique(newState)==true && isFinished(newState)==false)
  264.                 {
  265.                     System.out.println("GGGG");
  266.                     System.out.println(heuristic1(newState));
  267.                     Node tmpNode = new Node(n.path+'G',n.path,statesList.get(statesList.size()-1), heuristic1(newState));
  268.                     nodeQueue.add(tmpNode);
  269.  
  270.                 }
  271.                 if(isFinished(newState)==true)
  272.                 {
  273.                     isSolved=true;
  274.                     solution=(n.path)+'G';
  275.                     return;
  276.                 }
  277.             }
  278.  
  279.  
  280.             if(zero < size*size-size &&isSolved==false)
  281.             {
  282.  
  283.                 System.out.println("D");
  284.                 newState = new ArrayList<BitSet>(n.nodeState);
  285.                 Collections.swap(newState, zero, zero+size);
  286.  
  287.                 if(isStateUnique(newState)==true && isFinished(newState)==false)
  288.                 {
  289.                     Node tmpNode = new Node(n.path+'D',n.path,statesList.get(statesList.size()-1), heuristic1(newState));
  290.                     nodeQueue.add(tmpNode);
  291.  
  292.                 }
  293.                 if(isFinished(newState)==true)
  294.                 {
  295.                     isSolved=true;
  296.                     solution=(n.path)+'D';
  297.                     return;
  298.                 }
  299.             }
  300.  
  301.             addToStatesList(n.nodeState);
  302.  
  303.             if(!nodeQueue.isEmpty()&& isSolved==false ){
  304.                 System.out.println("wracaj");
  305.                 searchBF(nodeQueue.poll());
  306.             }
  307.         }
  308.         else return;
  309.     }
  310.  
  311.  
  312. }
Add Comment
Please, Sign In to add comment