-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIda.java
More file actions
150 lines (122 loc) · 3.31 KB
/
Ida.java
File metadata and controls
150 lines (122 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package ida.ipl;
final class Ida {
/**
* expands this board into all possible positions, and returns the number of
* solutions. Will cut off at the bound set in the board.
*/
static int solutions(Board board, BoardCache cache) {
if (board.distance() == 0) {
return 1;
}
if (board.distance() > board.bound()) {
return 0;
}
Board[] children = board.makeMoves(cache);
int result = 0;
for (int i = 0; i < children.length; i++) {
if (children[i] != null) {
result += solutions(children[i], cache);
}
}
cache.put(children);
return result;
}
/**
* expands this board into all possible positions, and returns the number of
* solutions. Will cut off at the bound set in the board.
*/
static int solutions(Board board) {
if (board.distance() == 0) {
return 1;
}
if (board.distance() > board.bound()) {
return 0;
}
Board[] children = board.makeMoves();
int result = 0;
for (int i = 0; i < children.length; i++) {
if (children[i] != null) {
result += solutions(children[i]);
}
}
return result;
}
private static void solve(Board board, boolean useCache) {
BoardCache cache = null;
if (useCache) {
cache = new BoardCache();
}
int bound = board.distance();
int solutions;
System.out.print("Try bound ");
System.out.flush();
do {
board.setBound(bound);
System.out.print(bound + " ");
System.out.flush();
if (useCache) {
solutions = solutions(board, cache);
} else {
solutions = solutions(board);
}
bound += 2;
} while (solutions == 0);
System.out.println("\nresult is " + solutions + " solutions of "
+ board.bound() + " steps");
}
public static void main(String[] args) {
String fileName = null;
boolean cache = true;
int threads = 1;
/* Use suitable default value. */
int length = 58;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("--file")) {
fileName = args[++i];
} else if (args[i].equals("--nocache")) {
cache = false;
} else if (args[i].equals("--threads")) {
i++;
threads = Integer.parseInt(args[i]);
}else if (args[i].equals("--length")) {
i++;
length = Integer.parseInt(args[i]);
} else {
System.err.println("No such option: " + args[i]);
System.exit(1);
}
}
Board initialBoard = null;
if (fileName == null) {
initialBoard = new Board(length);
} else {
try {
initialBoard = new Board(fileName);
} catch (Exception e) {
System.err
.println("could not initialize board from file: " + e);
}
}
System.out.println("Running IDA*, initial board:");
System.out.println(initialBoard);
// switching off the sequential block
/*long start = System.currentTimeMillis();
solve(initialBoard, cache);
long end = System.currentTimeMillis();*/
// Rather calling ipl block
// NOTE: this is printed to standard error! The rest of the output
// is
// constant for each set of parameters. Printing this to standard
// error
// makes the output of standard out comparable with "diff"
//System.err.println("ida took " + (end - start) + " milliseconds");
/* calling the IPL function */
try {
IdaNode solve = new IdaNode (threads, initialBoard, cache);
solve.run(threads);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}