Hallo! Habe in der Uni eine Programmieraufgabe, aber ich verstehe nicht genau, was ich machen soll. Es geht um das WaterPouring Problem, aber wir müssen das Problem generell machen, sodass es nicht nur für Wasser anwendbar ist, sondern allgemein.
Das Problem1 ist das originale WaterPouring Problem, der Code funktioniert, aber wir müssen hier ein interface implementieren. Im interface gibt es eine Methode solve(.....) mit mehreren Parametern (daher anders als die vorhandene solve(int goal) methode in der Klasse). Ich verstehe jetzt nicht, was ich in der neuen solve methode machen muss. Kann mir bitte jemand helfen???
Hier ist das interface:
Und hier ist die Klasse:
Das Problem1 ist das originale WaterPouring Problem, der Code funktioniert, aber wir müssen hier ein interface implementieren. Im interface gibt es eine Methode solve(.....) mit mehreren Parametern (daher anders als die vorhandene solve(int goal) methode in der Klasse). Ich verstehe jetzt nicht, was ich in der neuen solve methode machen muss. Kann mir bitte jemand helfen???
Hier ist das interface:
Java:
import java.io.PrintStream;
/** Interface for Problem 1 */
public interface P1 {
/**
* Solves problem 1 (two empty canisters of given capacity)
*
* Example: System.out.println(p1.solve(5, 9, 8, System.out)); --> 6
*
* @param xCapacity
* the capacity of canister X
* @param yCapacity
* the capacity of canister Y
* @param goal
* the goal we want to achieve
* @param out
* a printstream to write the path to the goal (should be a single line)
* @return the number of actions in the shortest path to the goal
*/
public int solve(int xCapacity, int yCapacity, int goal, PrintStream out);
}
Und hier ist die Klasse:
Java:
import assignment8_int.P1;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/** water pouring problem. based on the python code of peter norvig (see below) */
public class PureWaterPouring implements P1 {
private final int xCapacity;
private final int yCapacity;
private int[] initState = new int[] { 0, 0 };
public PureWaterPouring() {
this.xCapacity=0;
this.yCapacity=0;
}
public PureWaterPouring(int xCapacity, int yCapacity) {
this.xCapacity = xCapacity;
this.yCapacity = yCapacity;
}
private boolean containsGoal(int[] state, int goal) {
return state[0] == goal || state[1] == goal;
}
private boolean isAlreadyExplored(int[] state, Set<int[]> explored) {
for (int[] entry : explored) {
if (entry[0] == state[0] && entry[1] == state[1]) {
return true;
}
}
return false;
}
public Collection<?> solve(int goal) {
if (containsGoal(initState, goal)) {
return Arrays.asList(initState);
}
Set<int[]> explored = new HashSet<>();
LinkedList<LinkedList<?>> frontier = new LinkedList<>(); /* note: not only the states are relevant, we also want to know, how we got there -> consider Move */
frontier.push(new LinkedList<>(Arrays.asList(initState)));
while (!frontier.isEmpty()) {
LinkedList<?> path = frontier.pop();
int[] state = (int[]) path.get(path.size() - 1); // state of last path
for (Entry<int[], String> e : getSuccessors(state[0], state[1]).entrySet()) {
int[] succState = e.getKey();
String succAction = e.getValue();
if (!isAlreadyExplored(succState, explored)) {
explored.add(succState);
LinkedList<Object> path2 = new LinkedList<>(path); // copy path
path2.addLast(succAction);
path2.addLast(succState);
if (containsGoal(succState, goal)) {
return path2;
} else {
frontier.add(path2);
}
}
}
}
return Collections.emptyList();
}
private int[] toState(int x, int y) {
return new int[] { x, y };
}
private Map<int[], String> getSuccessors(int x, int y) {
assert (x <= xCapacity && y <= yCapacity);
Map<int[], String> successors = new HashMap<>();
successors.put((y + x <= yCapacity) ? toState(0, y + x) : toState(x - (yCapacity - y), y + (yCapacity - y)), "from_X_to_Y");
successors.put((x + y <= xCapacity) ? toState(x + y, 0) : toState(x + (xCapacity - x), y - (xCapacity - x)), "from_Y_to_X");
successors.put(toState(xCapacity, y), "fill_X");
successors.put(toState(x, yCapacity), "fill_Y");
successors.put(toState(0, y), "empty_X");
successors.put(toState(x, 0), "empty_Y");
return successors;
}
public static void main(String[] args) {
PureWaterPouring wp = new PureWaterPouring(4, 9);
Collection<?> solution = wp.solve(6);
if (solution.size() > 0) {
for (Object o : solution) {
if (o instanceof int[]) {
int[] state = (int[]) o;
System.out.print(state[0] + "," + state[1] + " ");
} else {
System.out.print(o + " ");
}
}
System.out.println();
} else {
System.out.println("no solution");
}
}
@Override
public int solve(int xCapacity, int yCapacity, int goal, PrintStream out) {
// TODO Auto-generated method stub
return 0;
}
}