Ifi6057strategies
Allikas: Lambda
8-Puzzle: vali ülesanne (testprob) ja soovitav otsingustrateegia. Hoiatus: kõik strateegiad ei pruugi mõistliku aja jooksul valmis saada, eriti Pythonis.
Python (pane aima-python-master kataloogi):
#eight.py
import search
badmoves = {4 : 3, 7: 6, 3 : 4, 6 : 7} # "teleporting" across board
class EightPuzzle(search.Problem):
def actions(self, state):
al = []
tyhik = state[0]
for offset in [-3, -1, 1, 3]:
koht = tyhik + offset
if koht >= 1 and koht <= 9 and badmoves.get(tyhik) != koht:
al.append(koht)
return al
def result(self, state, action):
newstate = [action]
for el in state[1:]:
if el == action:
newstate.append(state[0])
else:
newstate.append(el)
return tuple(newstate)
def goal_test(self, state):
return self.goal == state
GOAL = (9, 1, 2, 3 ,4 ,5, 6, 7, 8)
def test(initial, searchfunc):
p = EightPuzzle(initial, GOAL)
ip = search.InstrumentedProblem(p)
n=searchfunc(ip)
# expanded/goal tests/leaves/(solution)
print (ip)
if hasattr(n, "path"):
print (n.path())
else:
print (n)
if __name__ == "__main__":
a = (7, 2, 5, 3 ,1 ,8, 6, 4, 9)
b = (5, 2, 6, 3, 4, 9, 8, 7, 1)
c = (4, 2, 3, 8, 1, 9, 7, 6, 5)
testprob = a
test(testprob, search.breadth_first_tree_search)
#test(testprob, search.breadth_first_search)
#test(testprob, search.depth_first_tree_search)
#test(testprob, search.depth_first_graph_search)
def dls(param):
return search.depth_limited_search(param, 7)
#test(testprob, dls)
#test(testprob, search.iterative_deepening_search)
Java (pane aima-gui/src/main/java/aima/gui/demo/search kataloogi):
//eight.java
package aima.gui.demo.search;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import aima.core.agent.Action;
import aima.core.environment.eightpuzzle.EightPuzzleBoard;
import aima.core.environment.eightpuzzle.EightPuzzleFunctionFactory;
import aima.core.search.framework.GoalTest;
import aima.core.search.framework.GraphSearch;
import aima.core.search.framework.Problem;
import aima.core.search.framework.Search;
import aima.core.search.framework.SearchAgent;
import aima.core.search.framework.TreeSearch;
import aima.core.search.uninformed.BreadthFirstSearch;
import aima.core.search.uninformed.DepthFirstSearch;
import aima.core.search.uninformed.DepthLimitedSearch;
import aima.core.search.uninformed.IterativeDeepeningSearch;
class MyGoalTest implements GoalTest {
EightPuzzleBoard goal = new EightPuzzleBoard(new int[] { 1, 2, 3, 4, 5,
6, 7, 8, 0 });
public boolean isGoalState(Object state) {
EightPuzzleBoard board = (EightPuzzleBoard) state;
return board.equals(goal);
}
}
public class eight {
static void test(EightPuzzleBoard initial, Search searchfunc) {
try {
Problem problem = new Problem(initial, EightPuzzleFunctionFactory
.getActionsFunction(), EightPuzzleFunctionFactory
.getResultFunction(), new MyGoalTest());
SearchAgent agent = new SearchAgent(problem, searchfunc);
printInstrumentation(agent.getInstrumentation());
printActions(agent.getActions());
} catch (Exception e) {
e.printStackTrace();
}
}
static EightPuzzleBoard a = new EightPuzzleBoard(
new int[] { 4, 1, 3, 7, 2, 6, 0, 5, 8 });
static EightPuzzleBoard b = new EightPuzzleBoard(
new int[] { 8, 1, 3, 4, 0, 2, 7, 6, 5 });
static EightPuzzleBoard c = new EightPuzzleBoard(
new int[] { 4, 1, 2, 0, 8, 7, 6, 3, 5 });
public static void main(String[] args) {
EightPuzzleBoard testprob = a;
test(testprob, new BreadthFirstSearch(new TreeSearch()));
// test(testprob, new BreadthFirstSearch(new GraphSearch()));
// test(testprob, new DepthFirstSearch(new TreeSearch()));
// test(testprob, new DepthFirstSearch(new GraphSearch()));
// test(testprob, new DepthLimitedSearch(7));
// test(testprob, new IterativeDeepeningSearch());
}
private static void printInstrumentation(Properties properties) {
Iterator<Object> 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<Action> actions) {
for (int i = 0; i < actions.size(); i++) {
String action = actions.get(i).toString();
System.out.println(action);
}
}
}