Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mainpackage;
- import java.util.*;
- public class Water_jug_algorithm {
- /*
- * Setting up data structure to use through out the program.
- * 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.
- * Using an array allow you to call the required element. This would be useful in the operations.
- * 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
- */
- 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}.
- public static int[] max= new int[3]; //This integer array will hold the max capacity values input by user.
- 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
- public static LinkedList <int[]> inStates = new LinkedList<int[]>(); //This linked list will hold the states ready for operations to be applied on
- 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.
- /*
- * This method uses a scanner to take in 3 integer values from the user. Which determines the capacity for jug A, B, C respectively
- * INPUT: User inputs of 3 integers, read in by scanner
- * 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
- */
- public static void getCapacity() {
- Scanner reader = new Scanner(System.in);
- System.out.println("Please enter the capacity of jug A:");
- max[0] = reader.nextInt(); //max capacity for jug A assigned
- System.out.println("Please enter the capacity of jug B:");
- max[1] = reader.nextInt(); //max capacity for jug B assigned
- System.out.println("Please enter the capacity of jug C:");
- max[2] = reader.nextInt(); //max capacity for jug C assigned
- reader.close();
- }
- /*
- * 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
- * INPUT: Index value of jug that is being filled
- * OUTPUT: New state is returned after filling the appropriate jug
- */
- public static int[] fillJug(int fillJug,int[]hold) {
- int []fillers = null;
- if(fillJug == 0) {
- fillers = new int[] {max[0],hold[1],hold[2]}; //filling jug A to max capacity
- }
- else if(fillJug == 1) {
- fillers = new int[] {hold[0],max[1],hold[2]}; //filling jug B to max capacity
- }
- else {
- fillers = new int[] {hold[0],hold[1],max[2]}; //filling jug C to max capacity
- }
- return fillers; //return new state
- }
- /*
- * Method for the empty operation. Takes input and based on that input the appropriate jug is emptied by reassigning the value to equal zero
- * INPUT: Index value of jug that needs to be emptied
- * OUTPUT: New state is returned after emptying the appropriate jug
- */
- public static int[] emptyJug(int emptyJug,int[] hold) {
- int[]empty=null;
- if(emptyJug == 0) {
- empty = new int[] {0,hold[1],hold[2]}; //emptying jug A
- }
- else if(emptyJug == 1) {
- empty = new int[] {hold[0],0,hold[2]}; //emptying jug B
- }
- else {
- empty = new int[] {hold[0],hold[1],0}; //emptying jug C
- }
- return empty; //return new state
- }
- /*
- * Method to transfer water between two jugs. Takes two input, and based on the input the transfer is completed. Subject to relevant conditions
- * INPUT: index value of jug pouring form, index value of the jug pouring into
- * OUTPUT:New state is returned with the water transfer completed
- */
- public static int[] transferWater(int jugX, int jugY, int[] hold) {
- int[]transfers=hold;
- int jugFrom=hold[jugX];
- int jugTo=hold[jugY];
- if(jugFrom > max[jugY]) {
- transfers[jugX] = (jugFrom - (max[jugY] - jugTo));
- transfers[jugY] = max[jugY];
- } else {
- int sum = jugFrom+jugTo;
- if(sum>max[jugY]) {
- transfers[jugX] = jugFrom;
- transfers[jugY] = jugTo;
- } else {
- transfers[jugX] = 0;
- transfers[jugY] = jugFrom + jugTo;
- }
- }
- return transfers;
- }
- //loops through operations
- public static void loopOperations(int[] hold) {
- //Fill jugs
- resultStates.add(fillJug(0,hold.clone()));
- resultStates.add(fillJug(1,hold.clone()));
- resultStates.add(fillJug(2,hold.clone()));
- //Empty jugs
- resultStates.add(emptyJug(0,hold.clone()));
- resultStates.add(emptyJug(1,hold.clone()));
- resultStates.add(emptyJug(2,hold.clone()));
- //Water transfers
- resultStates.add(transferWater(0,1,hold.clone()));
- resultStates.add(transferWater(0,2,hold.clone()));
- resultStates.add(transferWater(1,0,hold.clone()));
- resultStates.add(transferWater(1,2,hold.clone()));
- resultStates.add(transferWater(2,0,hold.clone()));
- resultStates.add(transferWater(2,1,hold.clone()));
- }
- //Method to check if output state exists already
- public static boolean exists(int[]states) {
- boolean does_exist=false;
- for(int i=0; i<allStates.size(); i++) { //iterate through allstate
- int count=0; //holds
- for(int k=0; k<3;k++) {
- if(states[k]==allStates.get(i)[k]) {
- count++;
- }
- }
- if(count==3) {
- does_exist=true;
- i=allStates.size();
- }
- }
- return does_exist;
- }
- //Prints Results
- private static void printResults(LinkedList<int[]> aList) {
- for(int i=0; i<aList.size(); i++) { //iterate through allstate
- String ex ="(";
- for(int k=0; k<3;k++) {
- ex+=aList.get(i)[k];
- if(k!=2) {ex+=",";}
- }
- ex+=")";
- System.out.println(ex);
- }
- System.out.println("Total Number of States= " + aList.size());
- }
- //Main class
- public static void main(String[] args) {
- System.out.println("AI Methods CW - The Water Jug Problem | Christo Philip | B626170");
- System.out.println("----------------------------------------------------------------");
- getCapacity(); //gets user input for A, B and C
- System.out.println("All possible states from given capacity and an initial start state of (0,0,0):");
- int[] nullState = new int[] {0,0,0};
- inStates.add(nullState);
- allStates.add(nullState);
- boolean cycle = true;
- while (cycle == true) {
- int[] x =null;
- while(!inStates.isEmpty()){
- x = inStates.remove();
- loopOperations(x);
- }
- int counter = 0;
- int[]y=null;
- while(!resultStates.isEmpty()){
- y = resultStates.remove();
- if(!exists(y)) {
- allStates.add(y);
- inStates.add(y);
- counter++;
- }
- }
- if(counter==0) { //if nothing new found
- cycle=false;
- }
- }
- printResults(allStates);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement