Guest User

Untitled

a guest
Dec 9th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. package heuristicSearch;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5.  
  6.  
  7. public class FractionPuzzleNode extends Node{
  8.  
  9.     private String numerators;
  10.     private String denominator;
  11.     double value;
  12.     FractionPuzzleNode parent;
  13.     private FractionPuzzleNode goal;
  14.     ArrayList<String> stringList = new ArrayList <String>();
  15.    
  16.     /*
  17.      * Constructor. Init the numerator, denominator and the goal.
  18.      */
  19.    
  20.     public FractionPuzzleNode (String numerator, String denominator, FractionPuzzleNode goal){
  21.         super();
  22.         this.denominator = denominator;
  23.         this.numerators = numerator;
  24.         this.goal = goal;
  25.         calculateValue();
  26.     }
  27.    
  28.     /*
  29.      * Calculating the real value of the node
  30.      */
  31.    
  32.     public void calculateValue(){
  33.         double num = Integer.parseInt(numerators);
  34.         double den = Integer.parseInt(denominator);
  35.         value = num/den;
  36.     }
  37.    
  38.     public double getValue(){
  39.         return this.value;
  40.     }
  41.    
  42.     @Override
  43.     public int hashCode() {
  44.         // TODO Auto-generated method stub
  45.         return 0;
  46.     }
  47.  
  48.     @Override
  49.     public boolean equals(Object o) {
  50.         if (((FractionPuzzleNode) o).getValue() == this.value) {
  51.             return true;
  52.         }else {
  53.             return false;
  54.         }
  55.     }
  56.    
  57.     /*
  58.      * Returning the cost so far, based on how many parent node this node has.
  59.      */
  60.  
  61.     @Override
  62.     public int getCostSoFar() {
  63.         if (!this.lineage().isEmpty()){
  64.             return this.lineage().size()-1;
  65.         }
  66.         else
  67.             return 0;
  68.     }
  69.    
  70.     /*
  71.      * Constructing all permutations of a string, and adds it to the stringlist.
  72.      */
  73.    
  74.     public void showPattern(String st, String chars) {
  75.         if (chars.length() <= 1) {
  76.                 this.stringList.add(st + chars);
  77.                
  78.         }else{
  79.                 for (int i = 0; i < chars.length(); i++) {
  80.                         try {
  81.                                 String newString = chars.substring(0, i) + chars.substring(i + 1);
  82.                                 showPattern(st + chars.charAt(i), newString);
  83.                         } catch (Exception e) {
  84.                                 e.printStackTrace();
  85.                         }
  86.                 }
  87.         }
  88.      
  89.     }
  90.    
  91.     private FractionPuzzleNode createNewFractionPuzzleNode(String string){
  92.         FractionPuzzleNode node = new FractionPuzzleNode(string.substring(0, 4),string.substring(4, 9), this.goal);
  93.         node.parent = this;
  94.         return node;
  95.        
  96.     }
  97.  
  98.     @Override
  99.     public Collection<FractionPuzzleNode> children() {
  100.         Collection<FractionPuzzleNode> childrenList = new ArrayList<FractionPuzzleNode>();
  101.         showPattern("",(numerators+denominator));
  102.         for (int i = 0; i < stringList.size(); i++) {
  103.             childrenList.add(createNewFractionPuzzleNode(stringList.get(i)));
  104.         }
  105.         return childrenList;
  106.     }
  107.  
  108.     @Override
  109.     protected int heuristic() {
  110.         return (int) ((this.goal.getValue() - this.value));
  111.     }
  112.  
  113.     @Override
  114.     protected Node getParent() {
  115.         return parent;
  116.     }
  117.  
  118.     public String toString(){
  119.         String s = "";
  120.         s += this.value;
  121.         return s;
  122.     }
  123.  
  124.    
  125. }
Add Comment
Please, Sign In to add comment