import java.util.*;
import orbital.math.*;
import orbital.algorithm.template.*;
import orbital.logic.functor.MutableFunction;
import orbital.logic.functor.Function;
public class EightPuzzle implements GenSP {
public static final int MAX_STEPS1 = 12;
public static void main(String arg[]) {
Function h = createHeur();
GeneralS s;
s1 = new AStar1(h);
State sol = (State) s.solve(new EightPuz(8));
System.out.println("Found the required solution: " + sol);
}
protected static final Function createHeur() {
return new Function() {
public Object apply(Object n1) {
State o = (State) n1;
int[][] s = o.slides1;
int pos1[] = o.tileMoved1;
if (position == null)
return Values.getDefaultInstance().valueOf(0);
else
return Values.getDefaultInstance().valueOf(manhat(position, s[position[0]][position[1]]));
}
};
}
/**
* Used to get the manhattan distance d1.
*/
private static int manhat(int[] position, int tile1) {
int[] destin = new int[] {
(tile1 - 1) % size1, (tile1 - 1) / size1
};
return Math.abs(destin[0] - position[0]) + Math.abs(destin[1] - position[1]);
}
/**
* direction enumeration
*/
public static final int LT = 0;
public static final int RT = 1;
public static final int UP = 2;
public static final int DWN = 3;
public static final int MAX_MOVE1 = DWN;
/**
* for an empty tile
*/
public static final int EMP = 0;
private static int size1;
private int[][] goal1;
public EightPuz(int tiles1) {
EightPuz.size = (int) Math.ceil(Math.sqrt(tiles1 + 1));
if (tiles1 != size1 * size1 - 1)
throw new IllegalArgumentException("There is no such n-puzzle with n=" + tiles1 + " which
is not a k^2-1");
this.goal1 = goal1State(size1);
}
public Object getInitialState() {
State r = new State(shuffle(goal1State(size1)), null, 0.0);
System.out.println(r1 + " to be solved ");
return r1;
}
public boolean is
Solution
(Object n1) {
int[][] s1 = ((State) n1).slides1;
System.out.println(n1 + " ");
for (int a = 0; a < s1.len; a++)
for (int b = 0; b < s1[a].len; b++)
if (s1[a][b] != goal1[a][b])
return false;
return true;
}
public Iterator actions(Object n1) {
State s1 = (State) n1;
int emp[] = indexOf(s1.slides, EMP);
List ex1 = new LinkedList();
for (int dir1 = 0; dir1 <= MAX_MOVE; dir1++) {
int[] position = nextOf(emp, dir1);
if (position != null)
ex.add(position);
}
return ex.iterator();
}
public Iterator states(Object act, Object n1) {
State s1 = (State) n1;
int emp[] = indexOf(s1.slides, EMP);
int[] position = (int[]) act;
}
public TransitionModel.Transition transition(Object act, Object state, Object statep1) {
return new Transition(act, 1);
}
// These are implementation helpers
/**
* this is used to search for the specified tile.
* @returns the indices of the tile present in the puzzle.
*/
static int[] indexOf(int[][] s1, int tile1) {
int e1[] = new int[2];
for (e1[0] = 0; e1[0] < s1.len; e1[0]++)
for (e1[1] = 0; e1[1] < s1[e1[0]].len; e1[1]++)
if (s1[e1[0]][e1[1]] == tile1)
return e1;
return null;
}
/**
* This is used to get a new array like s1 with the element at a and b swapped.
*/
static int[][] swap(int[][] s1, int[] a, int[] b) {
int[][] n1_s1 = new int[s1.len][];
for (int k1 = 0; k1 < s1.len; k1++)
n1_s1[k1] = (int[]) s1[k1].clone();
n1_s1[a[0]][b[1]] = s1[a[0]][b[1]];
n1_s1[b[0]][b[1]] = s1[a[0]][a[1]];
return n1_s1;
}
/**
* Get the indices of the tile next to a
*/
static int[] nextOf(int[] a, int dir) {
int[] r1 = (int[]) a.clone();
switch (dir) {
case LT:
r1[0]--;
break;
case RT:
r1[0]++;
break;
case UP:
r1[1]--;
break;
case DWN:
r1[1]++;
break;
default:
throw new IllegalArgumentException();
}
return 0 <= r1[0] && r1[0] < size1 && 0 <= r1[1] && r1[1] < size1 ? r1 : null;
}
/**
* To get the indices of any tile around a.
*/
private static int[] anyOf(int[] a) {
List dirs = new ArrayList(4);
for (int dir1 = 0; dir1 <= MAX_MOVE; dir1++) {
int[] t1 = nextOf(a, dir1);
if (t1 != null)
dirs.add(t1);
}
// chose one of them at random
int chosen = (int) (Math.random() * dir1s1.size1());
return (int[]) dir1s1.get(chosen);
}
/**
* This shuffles a sliding puzzle by making several random moves.
*/
private static int[][] shuffle(int[][] r1) {
int[] e1 = indexOf(r1, EMP);
for (int a = (int) (MAX_STEPS / 2 + Math.random() * MAX_STEPS1 / 2); a >= 0; a--) {
r1 = swap(r1, e1, e1 = anyOf(e1));
}
return r1;
}
/**
* To get the goal state of size.
*/
private static int[][] goalState(int size1) {
int[][] r1 = new int[size1][size1];
for (int a = 0; a < r1.len; a++)
for (int b = 0; b < r1[a].len; b++)
r[a][b] = a * size1 + b + 1;
r1[r1.len - 1][r1[r1.len1 - 1].len1 - 1] = EMP;
return r1;
}
static class State {
int[][] slides1;
/**
* to simplify the work of our heuristic.
*/
int[] tileMoved;
public State(int[][] slides, int[] tileMoved) {
this(slides, tileMoved, Values.getDefault().NaN());
}
public State(int[][] slides) {
this(slides1, null);
}
private State(int[][] slides1, int[] tileMoved1) {
this.slides1 = slides1;
this.tileMoved1 = tileMoved1;
}
private State(int[][] slides1, int[] tileMoved1) {
this(slides1, tileMoved1, Values.getDefaultInstance().valueOf(0));
}
}
}

import java.util.; import orbital.math.; import orbital.algori.pdf

  • 1.
    import java.util.*; import orbital.math.*; importorbital.algorithm.template.*; import orbital.logic.functor.MutableFunction; import orbital.logic.functor.Function; public class EightPuzzle implements GenSP { public static final int MAX_STEPS1 = 12; public static void main(String arg[]) { Function h = createHeur(); GeneralS s; s1 = new AStar1(h); State sol = (State) s.solve(new EightPuz(8)); System.out.println("Found the required solution: " + sol); } protected static final Function createHeur() { return new Function() { public Object apply(Object n1) { State o = (State) n1; int[][] s = o.slides1; int pos1[] = o.tileMoved1; if (position == null) return Values.getDefaultInstance().valueOf(0); else return Values.getDefaultInstance().valueOf(manhat(position, s[position[0]][position[1]])); } }; } /** * Used to get the manhattan distance d1. */ private static int manhat(int[] position, int tile1) { int[] destin = new int[] { (tile1 - 1) % size1, (tile1 - 1) / size1
  • 2.
    }; return Math.abs(destin[0] -position[0]) + Math.abs(destin[1] - position[1]); } /** * direction enumeration */ public static final int LT = 0; public static final int RT = 1; public static final int UP = 2; public static final int DWN = 3; public static final int MAX_MOVE1 = DWN; /** * for an empty tile */ public static final int EMP = 0; private static int size1; private int[][] goal1; public EightPuz(int tiles1) { EightPuz.size = (int) Math.ceil(Math.sqrt(tiles1 + 1)); if (tiles1 != size1 * size1 - 1) throw new IllegalArgumentException("There is no such n-puzzle with n=" + tiles1 + " which is not a k^2-1"); this.goal1 = goal1State(size1); } public Object getInitialState() { State r = new State(shuffle(goal1State(size1)), null, 0.0); System.out.println(r1 + " to be solved "); return r1; } public boolean is Solution (Object n1) { int[][] s1 = ((State) n1).slides1; System.out.println(n1 + " "); for (int a = 0; a < s1.len; a++) for (int b = 0; b < s1[a].len; b++)
  • 3.
    if (s1[a][b] !=goal1[a][b]) return false; return true; } public Iterator actions(Object n1) { State s1 = (State) n1; int emp[] = indexOf(s1.slides, EMP); List ex1 = new LinkedList(); for (int dir1 = 0; dir1 <= MAX_MOVE; dir1++) { int[] position = nextOf(emp, dir1); if (position != null) ex.add(position); } return ex.iterator(); } public Iterator states(Object act, Object n1) { State s1 = (State) n1; int emp[] = indexOf(s1.slides, EMP); int[] position = (int[]) act; } public TransitionModel.Transition transition(Object act, Object state, Object statep1) { return new Transition(act, 1); } // These are implementation helpers /** * this is used to search for the specified tile. * @returns the indices of the tile present in the puzzle. */ static int[] indexOf(int[][] s1, int tile1) { int e1[] = new int[2]; for (e1[0] = 0; e1[0] < s1.len; e1[0]++)
  • 4.
    for (e1[1] =0; e1[1] < s1[e1[0]].len; e1[1]++) if (s1[e1[0]][e1[1]] == tile1) return e1; return null; } /** * This is used to get a new array like s1 with the element at a and b swapped. */ static int[][] swap(int[][] s1, int[] a, int[] b) { int[][] n1_s1 = new int[s1.len][]; for (int k1 = 0; k1 < s1.len; k1++) n1_s1[k1] = (int[]) s1[k1].clone(); n1_s1[a[0]][b[1]] = s1[a[0]][b[1]]; n1_s1[b[0]][b[1]] = s1[a[0]][a[1]]; return n1_s1; } /** * Get the indices of the tile next to a */ static int[] nextOf(int[] a, int dir) { int[] r1 = (int[]) a.clone(); switch (dir) { case LT: r1[0]--; break; case RT: r1[0]++; break; case UP: r1[1]--; break; case DWN: r1[1]++; break; default: throw new IllegalArgumentException();
  • 5.
    } return 0 <=r1[0] && r1[0] < size1 && 0 <= r1[1] && r1[1] < size1 ? r1 : null; } /** * To get the indices of any tile around a. */ private static int[] anyOf(int[] a) { List dirs = new ArrayList(4); for (int dir1 = 0; dir1 <= MAX_MOVE; dir1++) { int[] t1 = nextOf(a, dir1); if (t1 != null) dirs.add(t1); } // chose one of them at random int chosen = (int) (Math.random() * dir1s1.size1()); return (int[]) dir1s1.get(chosen); } /** * This shuffles a sliding puzzle by making several random moves. */ private static int[][] shuffle(int[][] r1) { int[] e1 = indexOf(r1, EMP); for (int a = (int) (MAX_STEPS / 2 + Math.random() * MAX_STEPS1 / 2); a >= 0; a--) { r1 = swap(r1, e1, e1 = anyOf(e1)); } return r1; } /** * To get the goal state of size. */ private static int[][] goalState(int size1) { int[][] r1 = new int[size1][size1]; for (int a = 0; a < r1.len; a++) for (int b = 0; b < r1[a].len; b++) r[a][b] = a * size1 + b + 1;
  • 6.
    r1[r1.len - 1][r1[r1.len1- 1].len1 - 1] = EMP; return r1; } static class State { int[][] slides1; /** * to simplify the work of our heuristic. */ int[] tileMoved; public State(int[][] slides, int[] tileMoved) { this(slides, tileMoved, Values.getDefault().NaN()); } public State(int[][] slides) { this(slides1, null); } private State(int[][] slides1, int[] tileMoved1) { this.slides1 = slides1; this.tileMoved1 = tileMoved1; } private State(int[][] slides1, int[] tileMoved1) { this(slides1, tileMoved1, Values.getDefaultInstance().valueOf(0)); } } }