Hey everybody,
i wanna use the array "intArray" in the class eightPuzzleAStarDemo() as initial state but i get a cannot use it.
Does anybody has a hint for me?
Is it because it is not in the same package?
Thank you
package aaa.mayerp.projectone;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Intro {
public static void main(String[] args) throws IOException {
String[] temp;
InputStreamReader inputStream = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(inputStream);
System.out.println("Enter eight numbers from 0 to 9");
String initialstate = keyboard.readLine();
String delimiter = ",";
temp = initialstate.split(delimiter);
int[] intArray = new int[temp.length];
for (int i = 0; i < temp.length; i++) {
for (int x = 0; x < temp.length; x++) {
try {
intArray[x] = Integer.parseInt(temp[x]);
} catch(NumberFormatException e) {
System.out.println("Invalid int encountered:- " + temp[x]);
}
}
}
System.out.println("Numbers array contents: ");
for (int num: intArray) {
// System.out.println("Number: [" + num + "]");
System.out.println(num);
}
}
}
package aima.search.demos;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import aima.search.eightpuzzle.EightPuzzleBoard;
import aima.search.eightpuzzle.EightPuzzleGoalTest;
import aima.search.eightpuzzle.EightPuzzleSuccessorFunction;
import aima.search.eightpuzzle.ManhattanHeuristicFunction;
import aima.search.eightpuzzle.MisplacedTilleHeuristicFunction;
import aima.search.framework.GraphSearch;
import aima.search.framework.Problem;
import aima.search.framework.Search;
import aima.search.framework.SearchAgent;
import aima.search.informed.AStarSearch;
import aima.search.informed.GreedyBestFirstSearch;
import aima.search.informed.SimulatedAnnealingSearch;
import aima.search.uninformed.DepthLimitedSearch;
import aima.search.uninformed.IterativeDeepeningSearch;
/**
* @author Ravi Mohan
*
*/
public class EightPuzzleDemo {
static EightPuzzleBoard boardWithThreeMoveSolution = new EightPuzzleBoard(
new int[] { 1, 2, 5, 3, 4, 0, 6, 7, 8 });;
static EightPuzzleBoard random1 = new EightPuzzleBoard(new int[] { 1, 4, 2,
7, 5, 8, 3, 0, 6 });
static EightPuzzleBoard extreme = new EightPuzzleBoard(new int[] { 0, 8, 7,
6, 5, 4, 3, 2, 1 });
public static void main(String[] args) {
// eightPuzzleDLSDemo();
// eightPuzzleIDLSDemo();
// eightPuzzleGreedyBestFirstDemo();
// eightPuzzleGreedyBestFirstManhattanDemo();
eightPuzzleAStarDemo();
// eightPuzzleAStarManhattanDemo();
// eightPuzzleSimulatedAnnealingDemo();
}
private static void eightPuzzleDLSDemo() {
System.out.println("\nEightPuzzleDemo recursive DLS -->");
try {
Problem problem = new Problem(random1,
new EightPuzzleSuccessorFunction(),
new EightPuzzleGoalTest());
Search search = new DepthLimitedSearch(9);
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleIDLSDemo() {
System.out.println("\nEightPuzzleDemo Iterative DLS -->");
try {
Problem problem = new Problem(random1,
new EightPuzzleSuccessorFunction(),
new EightPuzzleGoalTest());
Search search = new IterativeDeepeningSearch();
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleGreedyBestFirstDemo() {
System.out
.println("\nEightPuzzleDemo Greedy Best First Search (MisplacedTileHeursitic)-->");
try {
Problem problem = new Problem(boardWithThreeMoveSolution,
new EightPuzzleSuccessorFunction(),
new EightPuzzleGoalTest(),
new MisplacedTilleHeuristicFunction());
Search search = new GreedyBestFirstSearch(new GraphSearch());
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleGreedyBestFirstManhattanDemo() {
System.out
.println("\nEightPuzzleDemo Greedy Best First Search (ManhattanHeursitic)-->");
try {
Problem problem = new Problem(boardWithThreeMoveSolution,
new EightPuzzleSuccessorFunction(),
new EightPuzzleGoalTest(), new ManhattanHeuristicFunction());
Search search = new GreedyBestFirstSearch(new GraphSearch());
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleAStarDemo() {
System.out
.println("\nEightPuzzleDemo AStar Search (MisplacedTileHeursitic)-->");
try {
Problem problem = new Problem(intArray,
new EightPuzzleSuccessorFunction(),
new EightPuzzleGoalTest(),
new MisplacedTilleHeuristicFunction());
Search search = new AStarSearch(new GraphSearch());
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleSimulatedAnnealingDemo() {
System.out.println("\nEightPuzzleDemo Simulated Annealing Search -->");
try {
Problem problem = new Problem(random1,
new EightPuzzleSuccessorFunction(),
new EightPuzzleGoalTest(), new ManhattanHeuristicFunction());
SimulatedAnnealingSearch search = new SimulatedAnnealingSearch();
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
System.out.println("Search Outcome=" + search.getOutcome());
System.out.println("Final State=\n" + search.getLastSearchState());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleAStarManhattanDemo() {
System.out
.println("\nEightPuzzleDemo AStar Search (ManhattanHeursitic)-->");
try {
Problem problem = new Problem(random1,
new EightPuzzleSuccessorFunction(),
new EightPuzzleGoalTest(), new ManhattanHeuristicFunction());
Search search = new AStarSearch(new GraphSearch());
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void printInstrumentation(Properties properties) {
Iterator keys = properties.keySet().iterator();
while (keys.hasNext()) {
String key = (String) keys.next();
String property = properties.getProperty(key);
System.out.println(key + " : " + property);
}
}
private static void printActions(List actions) {
for (int i = 0; i < actions.size(); i++) {
String action = (String) actions.get(i);
System.out.println(action);
}
}
}