daily pastebin goal
86%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package mainpackage;
  2. import java.util.*;
  3.  
  4. public class Water_jug_algorithm {
  5.  
  6.    
  7.     /*
  8.      * Setting up data structure to use through out the program.
  9.      * Integer array was used to contain each state, because an integer can have a maximum specified size. The states will only ever have 3 elements, so an array helps keep the structure.
  10.      * Using an array allow you to call the required element. This would be useful in the operations.
  11.      * LinkedLists were also used hold all the states throughout the program. They are much better for data manipulation than arraylists and therefore appropriate for this program
  12.      */
  13.     public static int[] hold = new int[3];  //this integer array acts as a structure for each state to be held in the format {x,y,z}.  
  14.     public static int[] max= new int[3]; //This integer array will hold the max capacity values input by user.
  15.     public static LinkedList <int[]> allStates = new LinkedList<int[]>(); //This Linked List will hold the states produced, ready to print at the end of the program
  16.     public static LinkedList <int[]> inStates = new LinkedList<int[]>();    //This linked list will hold the states ready for operations to be applied on
  17.     public static LinkedList <int[]> resultStates = new LinkedList<int[]>(); //This Linked List will hold the results of the operations applied. Checks for duplicates will also be done, before being pushed to allStates to be printed.
  18.    
  19.    
  20.     /*
  21.      * This method uses a scanner to take in 3 integer values from the user. Which determines the capacity for jug A, B, C respectively
  22.      * INPUT: User inputs of 3 integers, read in by scanner
  23.      * OUTPUT: Method assigns each integer to the correct index in the integer array max[] created earlier, this can be used later to reference the capacity of the jugs
  24.      */
  25.     public static void getCapacity() {
  26.             Scanner reader = new Scanner(System.in);
  27.             System.out.println("Please enter the capacity of jug A:");
  28.             max[0] = reader.nextInt(); //max capacity for jug A assigned
  29.             System.out.println("Please enter the capacity of jug B:");
  30.             max[1] = reader.nextInt(); //max capacity for jug B assigned
  31.             System.out.println("Please enter the capacity of jug C:");
  32.             max[2] = reader.nextInt(); //max capacity for jug C assigned
  33.             reader.close();
  34.     }
  35.    
  36.    
  37.     /*
  38.      * A method for the fill operation. Takes input and based on input the appropriate jug is filled to the max capacity value from the max integer array
  39.      * INPUT: Index value of jug that is being filled
  40.      * OUTPUT: New state is returned after filling the appropriate jug
  41.      */
  42.     public static int[] fillJug(int fillJug,int[]hold) {
  43.         int []fillers = null;
  44.         if(fillJug == 0) {
  45.             fillers = new int[] {max[0],hold[1],hold[2]}; //filling jug A to max capacity
  46.         }
  47.         else if(fillJug == 1) {
  48.             fillers = new int[] {hold[0],max[1],hold[2]}; //filling jug B to max capacity
  49.         }
  50.         else {
  51.             fillers = new int[] {hold[0],hold[1],max[2]}; //filling jug C to max capacity
  52.         }
  53.         return fillers; //return new state
  54.     }
  55.    
  56.     /*
  57.      * Method for the empty operation. Takes input and based on that input the appropriate jug is emptied by reassigning the value to equal zero
  58.      * INPUT: Index value of jug that needs to be emptied
  59.      * OUTPUT: New state is returned after emptying the appropriate jug
  60.      */
  61.     public static int[] emptyJug(int emptyJug,int[] hold) {
  62.         int[]empty=null;
  63.         if(emptyJug == 0) {
  64.             empty = new int[] {0,hold[1],hold[2]}; //emptying jug A
  65.         }
  66.         else if(emptyJug == 1) {
  67.             empty = new int[] {hold[0],0,hold[2]}; //emptying jug B
  68.         }
  69.         else {
  70.             empty = new int[] {hold[0],hold[1],0}; //emptying jug C
  71.         }
  72.         return empty; //return new state
  73.     }
  74.    
  75.     /*
  76.      * Method to transfer water between two jugs. Takes two input, and based on the input the transfer is completed. Subject to relevant conditions
  77.      * INPUT: index value of jug pouring form, index value of the jug pouring into
  78.      * OUTPUT:New state is returned with the water transfer completed
  79.      */
  80.     public static int[] transferWater(int jugX, int jugY, int[] hold) {
  81.         int[]transfers=hold;
  82.         int jugFrom=hold[jugX];
  83.         int jugTo=hold[jugY];
  84.         if(jugFrom > max[jugY]) {
  85.             transfers[jugX] = (jugFrom - (max[jugY] - jugTo));
  86.             transfers[jugY] = max[jugY];
  87.         } else {
  88.             int sum = jugFrom+jugTo;
  89.             if(sum>max[jugY]) {
  90.                 transfers[jugX] = jugFrom;
  91.                 transfers[jugY] = jugTo;
  92.             } else {
  93.                 transfers[jugX] = 0;
  94.                 transfers[jugY] = jugFrom + jugTo;
  95.             }
  96.         }
  97.         return transfers;
  98.     }
  99.    
  100.     //loops through operations
  101.     public static void loopOperations(int[] hold) {
  102.         //Fill jugs
  103.         resultStates.add(fillJug(0,hold.clone()));
  104.         resultStates.add(fillJug(1,hold.clone()));
  105.         resultStates.add(fillJug(2,hold.clone()));
  106.         //Empty jugs
  107.         resultStates.add(emptyJug(0,hold.clone()));
  108.         resultStates.add(emptyJug(1,hold.clone()));
  109.         resultStates.add(emptyJug(2,hold.clone()));
  110.         //Water transfers
  111.         resultStates.add(transferWater(0,1,hold.clone()));
  112.         resultStates.add(transferWater(0,2,hold.clone()));
  113.         resultStates.add(transferWater(1,0,hold.clone()));
  114.         resultStates.add(transferWater(1,2,hold.clone()));
  115.         resultStates.add(transferWater(2,0,hold.clone()));
  116.         resultStates.add(transferWater(2,1,hold.clone()));
  117.     }
  118.  
  119.     //Method to check if output state exists already
  120.     public static boolean exists(int[]states) {
  121.         boolean does_exist=false;
  122.         for(int i=0; i<allStates.size(); i++) { //iterate through allstate
  123.             int count=0; //holds
  124.             for(int k=0; k<3;k++) {
  125.                 if(states[k]==allStates.get(i)[k]) {
  126.                     count++;
  127.                 }
  128.             }
  129.             if(count==3) {
  130.                 does_exist=true;
  131.                 i=allStates.size();
  132.             }
  133.         }
  134.         return does_exist;
  135.     }
  136.    
  137.     //Prints Results
  138.     private static void printResults(LinkedList<int[]> aList) {
  139.         for(int i=0; i<aList.size(); i++) { //iterate through allstate
  140.             String ex ="(";
  141.             for(int k=0; k<3;k++) {
  142.                 ex+=aList.get(i)[k];
  143.                 if(k!=2) {ex+=",";}
  144.             }
  145.             ex+=")";
  146.             System.out.println(ex);
  147.         }
  148.         System.out.println("Total Number of States= " + aList.size());
  149.     }
  150.    
  151.     //Main class
  152.     public static void main(String[] args) {
  153.         System.out.println("AI Methods CW - The Water Jug Problem | Christo Philip | B626170");
  154.         System.out.println("----------------------------------------------------------------");
  155.         getCapacity(); //gets user input for A, B and C
  156.         System.out.println("All possible states from given capacity and an initial start state of (0,0,0):");
  157.        
  158.         int[] nullState = new int[] {0,0,0};
  159.         inStates.add(nullState);
  160.         allStates.add(nullState);
  161.        
  162.         boolean cycle = true;
  163.         while (cycle == true) {
  164.             int[] x =null;
  165.             while(!inStates.isEmpty()){
  166.                 x = inStates.remove();
  167.                 loopOperations(x);
  168.             }
  169.        
  170.             int counter = 0;
  171.             int[]y=null;
  172.             while(!resultStates.isEmpty()){
  173.                 y = resultStates.remove();
  174.                 if(!exists(y)) {
  175.                     allStates.add(y);
  176.                     inStates.add(y);
  177.                     counter++;
  178.                 }
  179.             }
  180.             if(counter==0) { //if nothing new found
  181.                 cycle=false;
  182.             }
  183.         }
  184.         printResults(allStates);
  185.     }
  186. }
RAW Paste Data
Top