kr-ist - lecture 3a: problem solving in javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf ·...
TRANSCRIPT
![Page 1: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/1.jpg)
KR-IST - Lecture 3a:
Problem solving in Java
Chris Thornton
October 30, 2014
![Page 2: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/2.jpg)
Introduction
This lecture will look at a java program for solving the water-jugsproblem.
We have two jugs X and Y.
X can hold 4 pints.
Y can hold 3.
Permissable moves are to fill X from Y, to fill Y from X, and toempty or fill either jug completely.
The aim is to find a sequence of actions, starting from empty jugs,which achieves a state in which Y contains exactly two pints.
![Page 3: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/3.jpg)
Preliminaries
Initial questions
![Page 4: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/4.jpg)
Preliminaries
Initial questions
◮ What are the essential features of a state?
![Page 5: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/5.jpg)
Preliminaries
Initial questions
◮ What are the essential features of a state?
◮ What is the best way to represent a state?
![Page 6: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/6.jpg)
Preliminaries
Initial questions
◮ What are the essential features of a state?
◮ What is the best way to represent a state?
◮ Given a chosen state representation, how easy will it be togenerate successors?
![Page 7: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/7.jpg)
Preliminaries
Initial questions
◮ What are the essential features of a state?
◮ What is the best way to represent a state?
◮ Given a chosen state representation, how easy will it be togenerate successors?
![Page 8: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/8.jpg)
Produce a sketch of the successor method
Represent jug contents by numbers.
State representation is a data structure containing two numbers.
Successor generation will involve looking at two numbers andfinding out which actions are possible.
Is it possible to transfer from one jug to another?
Is it possible to empty a jug?
Is it possible to fill a jug?
How will these actions be applied within the chosen staterepresentation?
![Page 9: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/9.jpg)
Node class
import java.util.*;
class Node {
int x = 0, y = 0; /* state variables */
Node parent = null; /* parent link */
Node (int x, int y, Node parent) {
this.x = x;
this.y = y;
this.parent = parent;
}
public String toString() {
return(x + " " + y);
}
public boolean equals(Object node) { /* argument has to be an Object
return(((Node)node).x == x && ((Node)node).y == y);
}
![Page 10: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/10.jpg)
Node class cont.
NB. Use Vector instead of ArrayList because they allow ‘insert’mutations.
Vector<Node> getPath(Vector<Node> v) {
v.insertElementAt(this, 0);
if (parent != null) v = parent.getPath(v);
return(v);
}
Vector<Node> getPath() { return(getPath(new Vector<Node>()));
}
![Page 11: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/11.jpg)
WaterJugsSearch class (successor function)
public class WaterJugsSearch {
boolean isGoal(Node node) {
return(node.y == 2);
}
![Page 12: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/12.jpg)
WaterJugsSearch class cont.
Vector<Node> getSuccessors(Node parent) {
int x = parent.x, y = parent.y;
Vector<Node> successors = new Vector<Node>();
if (x < 4 && y > 0) { /* transfer amount z from y to x */
int z = Math.min(y, 4-x);
successors.add(new Node(x+z, y-z, parent)); }
if (y < 3 && x > 0) { /* transfer amount z from x to y */
int z = Math.min(x, 3-y);
successors.add(new Node(x-z, y+z, parent)); }
if (x > 0) { /* empty x */
successors.add(new Node(0, y, parent)); }
if (y > 0) { /* empty y */
successors.add(new Node(x, 0, parent)); }
if (x < 4) { /* fill x from tap */
successors.add(new Node(4, y, parent)); }
if (y < 3) { /* fill y from tap */
successors.add(new Node(x, 3, parent)); }
return(successors);
}
![Page 13: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/13.jpg)
Main loop
void run() {
Vector<Node> open = new Vector<Node>();
open.add(new Node(0, 0, null));
while (open.size() > 0) {
Node node = open.remove(0);
if (isGoal(node)) {
System.out.println("Solution: " + node.getPath()); }
else {
Vector<Node> successors = getSuccessors(node);
for (int i = 0; i < successors.size(); i++) {
Node child = successors.get(i);
if (!node.getPath().contains((Object)child)) {
open.add(child); }
}
}
}
}
![Page 14: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/14.jpg)
main method
public static void main(String args[]) { // do the search
new WaterJugsSearch().run();
}
}
![Page 15: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/15.jpg)
Output generated
There are 12 distinct solutions:
Solution: [0 0, 0 3, 3 0, 3 3, 4 2]
Solution: [0 0, 4 0, 1 3, 0 3, 3 0, 3 3, 4 2]
Solution: [0 0, 4 0, 4 3, 0 3, 3 0, 3 3, 4 2]
Solution: [0 0, 4 0, 1 3, 4 3, 0 3, 3 0, 3 3, 4 2]
Solution: [0 0, 4 0, 1 3, 1 0, 0 1, 4 1, 2 3, 2 0, 0 2]
Solution: [0 0, 4 0, 1 3, 1 0, 0 1, 0 3, 3 0, 3 3, 4 2]
Solution: [0 0, 4 0, 1 3, 1 0, 0 1, 4 1, 2 3, 0 3, 3 0, 3 3, 4 2]
Solution: [0 0, 4 0, 1 3, 1 0, 0 1, 4 1, 4 3, 0 3, 3 0, 3 3, 4 2]
Solution: [0 0, 0 3, 3 0, 4 0, 1 3, 1 0, 0 1, 4 1, 2 3, 2 0, 0 2]
Solution: [0 0, 0 3, 4 3, 4 0, 1 3, 1 0, 0 1, 4 1, 2 3, 2 0, 0 2]
Solution: [0 0, 4 0, 1 3, 1 0, 0 1, 4 1, 2 3, 4 3, 0 3, 3 0, 3 3, 4 2]
Solution: [0 0, 0 3, 3 0, 3 3, 4 3, 4 0, 1 3, 1 0, 0 1, 4 1, 2 3, 2 0,
.
![Page 16: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/16.jpg)
Summary
![Page 17: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/17.jpg)
Summary
◮ Always start by choosing a state representation
![Page 18: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/18.jpg)
Summary
◮ Always start by choosing a state representation
◮ Node class
![Page 19: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/19.jpg)
Summary
◮ Always start by choosing a state representation
◮ Node class
◮ WaterJugsSearch class with successor function
![Page 20: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/20.jpg)
Summary
◮ Always start by choosing a state representation
◮ Node class
◮ WaterJugsSearch class with successor function
◮ Main loop
![Page 21: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/21.jpg)
Summary
◮ Always start by choosing a state representation
◮ Node class
◮ WaterJugsSearch class with successor function
◮ Main loop
◮ main method
![Page 22: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/22.jpg)
Summary
◮ Always start by choosing a state representation
◮ Node class
◮ WaterJugsSearch class with successor function
◮ Main loop
◮ main method
◮ Output generated
![Page 23: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/23.jpg)
Summary
◮ Always start by choosing a state representation
◮ Node class
◮ WaterJugsSearch class with successor function
◮ Main loop
◮ main method
◮ Output generated
![Page 24: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/24.jpg)
Questions
![Page 25: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/25.jpg)
Questions
◮ What is the first task when implementating a search program?
![Page 26: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/26.jpg)
Questions
◮ What is the first task when implementating a search program?
◮ How soon should you start writing code?
![Page 27: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/27.jpg)
Questions
◮ What is the first task when implementating a search program?
◮ How soon should you start writing code?
◮ How can we resolve the main drawback with the Vector datastructure?
![Page 28: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/28.jpg)
Questions
◮ What is the first task when implementating a search program?
◮ How soon should you start writing code?
◮ How can we resolve the main drawback with the Vector datastructure?
◮ Writing the code for successor generation is the key task inimplementing a search program? What is the best way to goabout this task?
![Page 29: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/29.jpg)
Questions
◮ What is the first task when implementating a search program?
◮ How soon should you start writing code?
◮ How can we resolve the main drawback with the Vector datastructure?
◮ Writing the code for successor generation is the key task inimplementing a search program? What is the best way to goabout this task?
◮ What strategy does the WaterJugsSearch program use forgenerating solution paths?
![Page 30: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/30.jpg)
Questions
◮ What is the first task when implementating a search program?
◮ How soon should you start writing code?
◮ How can we resolve the main drawback with the Vector datastructure?
◮ Writing the code for successor generation is the key task inimplementing a search program? What is the best way to goabout this task?
◮ What strategy does the WaterJugsSearch program use forgenerating solution paths?
◮ What other strategies might be used for keeping track of andprinting out solution paths?
![Page 31: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/31.jpg)
Questions
◮ What is the first task when implementating a search program?
◮ How soon should you start writing code?
◮ How can we resolve the main drawback with the Vector datastructure?
◮ Writing the code for successor generation is the key task inimplementing a search program? What is the best way to goabout this task?
◮ What strategy does the WaterJugsSearch program use forgenerating solution paths?
◮ What other strategies might be used for keeping track of andprinting out solution paths?
◮ Modify the program so that it implements aniterative-deepening search strategy.
![Page 32: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/32.jpg)
Questions
◮ What is the first task when implementating a search program?
◮ How soon should you start writing code?
◮ How can we resolve the main drawback with the Vector datastructure?
◮ Writing the code for successor generation is the key task inimplementing a search program? What is the best way to goabout this task?
◮ What strategy does the WaterJugsSearch program use forgenerating solution paths?
◮ What other strategies might be used for keeping track of andprinting out solution paths?
◮ Modify the program so that it implements aniterative-deepening search strategy.
![Page 33: KR-IST - Lecture 3a: Problem solving in Javausers.sussex.ac.uk/~christ/crs/kr-ist/lec03a.pdf · This lecture will look at a java program for solving the water-jugs problem. We have](https://reader035.vdocuments.us/reader035/viewer/2022070911/5fb2c317a371923d1e0081ca/html5/thumbnails/33.jpg)
Exercises
Implement a version of the WaterJugsSearch program whichsearches for a debt-minimising sequence of card transfers (asdetailed in the previous lecture).