Skip to content

Commit 9bc26a2

Browse files
little bit of cleanup
1 parent 541d403 commit 9bc26a2

File tree

5 files changed

+46
-42
lines changed

5 files changed

+46
-42
lines changed

src/main/java/ch/sebastianhaeni/pancake/ParallelSolver.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,30 @@
99
import mpi.MPI;
1010

1111

12-
public class ParallelSolver {
12+
public final class ParallelSolver {
1313
private static final Logger LOG = LogManager.getLogger("ParallelSolver");
1414

1515
public static final int CONTROLLER_RANK = 0;
1616
public static final int SLEEP_MILLIS = 3;
1717
public static final int[] EMPTY_BUFFER = new int[0];
1818

19-
// private static int[] INITIAL_STATE = new int[]{3,4,2,5,1}; // 5
20-
// private static int[] INITIAL_STATE = new int[]{5,6,4,10,7,9,3,1,2,8}; // 10
21-
// private static int[] INITIAL_STATE = new int[]{13,12,2,7,8,3,11,15,5,1,6,9,14,10,4}; // 15
22-
// private static int[] INITIAL_STATE = new int[]{1,10,3,15,5,9,17,16,14,6,8,12,11,20,13,19,7,4,18,2}; // 20
23-
// private static int[] INITIAL_STATE = new int[]{4,25,15,7,28,17,6,5,23,29,19,27,13,21,9,1,26,22,11,18,16,12,10,30,2,3,8,14,24,20}; // 30
24-
// private static int[] INITIAL_STATE = new int[]{7,13,21,4,16,31,30,35,40,23,8,19,33,24,38,28,29,14,26,6,25,32,1,3,5,22,37,9,20,2,10,34,18,17,11,27,12,15,39,36}; // 40
25-
// private static int[] INITIAL_STATE = new int[]{13,23,24,28,39,17,38,31,9,5,30,40,26,32,8,36,19,16,33,6,20,12,14,25,27,22,34,11,21,4,10,18,15,7,2,37,1,29,35,3}; // 40
26-
// private static int[] INITIAL_STATE = new int[]{39,2,22,20,23,30,31,46,49,8,21,5,29,11,19,33,38,17,6,35,42,34,15,7,48,3,9,37,28,41,40,24,26,50,4,43,12,18,13,10,36,1,25,32,45,47,16,14,27,44}; // 50
27-
// private static int[] INITIAL_STATE = new int[] { 21, 33, 30, 58, 17, 6, 41, 3, 12, 22, 56, 16, 20, 35, 27, 44, 36, 24, 10, 19, 53, 54, 9, 42, 59, 34, 13, 39, 37, 29, 15, 25, 60, 50, 31, 46, 38, 51, 1, 49, 47, 8, 52, 57, 48, 18, 5, 55, 43, 7, 26, 32, 40, 28, 23, 2, 11, 45, 14, 4 }; // 60
28-
// private static int[] INITIAL_STATE = new int[] { 5, 10, 13, 8, 1, 14, 6, 2, 15, 9, 7, 12, 11, 4, 3 }; // 60
29-
private static int[] INITIAL_STATE = new int[] { 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11 }; // 60
30-
// private static int[] INITIAL_STATE = new int[]{14,34,9,13,27,46,41,25,5,44,47,22,35,7,17,49,54,2,32,42,39,50,12,52,66,31,24,36,6,18,45,60,28,37,65,43,48,38,58,33,8,10,55,61,53,62,19,26,59,16,57,15,56,20,11,29,4,40,1,51,63,30,64,23,3,21}; // 66
31-
// private static int[] INITIAL_STATE = new int[]{51,52,3,40,19,8,62,69,27,48,31,23,50,26,10,38,28,60,68,53,9,64,2,56,30,6,4,58,14,17,66,41,45,49,59,42,5,61,65,13,67,55,7,70,18,33,25,46,57,1,47,37,43,34,16,32,39,21,29,35,11,24,63,20,22,44,12,54,15,36}; // 70
19+
// private static static int[] INITIAL_STATE = {3,4,2,5,1}; // 5
20+
// private static static int[] INITIAL_STATE = {5,6,4,10,7,9,3,1,2,8}; // 10
21+
// private static static int[] INITIAL_STATE = {13,12,2,7,8,3,11,15,5,1,6,9,14,10,4}; // 15
22+
// private static static int[] INITIAL_STATE = {1,10,3,15,5,9,17,16,14,6,8,12,11,20,13,19,7,4,18,2}; // 20
23+
// private static static int[] INITIAL_STATE = {4,25,15,7,28,17,6,5,23,29,19,27,13,21,9,1,26,22,11,18,16,12,10,30,2,3,8,14,24,20}; // 30
24+
// private static static int[] INITIAL_STATE = {7,13,21,4,16,31,30,35,40,23,8,19,33,24,38,28,29,14,26,6,25,32,1,3,5,22,37,9,20,2,10,34,18,17,11,27,12,15,39,36}; // 40
25+
// private static static int[] INITIAL_STATE = {13,23,24,28,39,17,38,31,9,5,30,40,26,32,8,36,19,16,33,6,20,12,14,25,27,22,34,11,21,4,10,18,15,7,2,37,1,29,35,3}; // 40
26+
// private static static int[] INITIAL_STATE = {39,2,22,20,23,30,31,46,49,8,21,5,29,11,19,33,38,17,6,35,42,34,15,7,48,3,9,37,28,41,40,24,26,50,4,43,12,18,13,10,36,1,25,32,45,47,16,14,27,44}; // 50
27+
// private static static int[] INITIAL_STATE = { 21, 33, 30, 58, 17, 6, 41, 3, 12, 22, 56, 16, 20, 35, 27, 44, 36, 24, 10, 19, 53, 54, 9, 42, 59, 34, 13, 39, 37, 29, 15, 25, 60, 50, 31, 46, 38, 51, 1, 49, 47, 8, 52, 57, 48, 18, 5, 55, 43, 7, 26, 32, 40, 28, 23, 2, 11, 45, 14, 4 }; // 60
28+
// private static static int[] INITIAL_STATE = { 5, 10, 13, 8, 1, 14, 6, 2, 15, 9, 7, 12, 11, 4, 3 }; // 60
29+
private static final int[] INITIAL_STATE = { 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11 };
30+
// private static static int[] INITIAL_STATE = {14,34,9,13,27,46,41,25,5,44,47,22,35,7,17,49,54,2,32,42,39,50,12,52,66,31,24,36,6,18,45,60,28,37,65,43,48,38,58,33,8,10,55,61,53,62,19,26,59,16,57,15,56,20,11,29,4,40,1,51,63,30,64,23,3,21}; // 66
31+
// private static static int[] INITIAL_STATE = {51,52,3,40,19,8,62,69,27,48,31,23,50,26,10,38,28,60,68,53,9,64,2,56,30,6,4,58,14,17,66,41,45,49,59,42,5,61,65,13,67,55,7,70,18,33,25,46,57,1,47,37,43,34,16,32,39,21,29,35,11,24,63,20,22,44,12,54,15,36}; // 70
32+
33+
private ParallelSolver() {
34+
// nop
35+
}
3236

3337
public static void main(String[] args) {
3438
MPI.Init(args);
Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
package ch.sebastianhaeni.pancake;
22

3-
import ch.sebastianhaeni.pancake.dto.Node;
4-
53
import java.util.Arrays;
64
import java.util.Stack;
75

8-
public class RecursiveSolver {
6+
import ch.sebastianhaeni.pancake.dto.Node;
97

10-
private static Stack<Node> stack = new Stack<>();
8+
public final class RecursiveSolver {
9+
10+
private static final Stack<Node> NODES = new Stack<>();
1111
private static int candidateBound = Integer.MAX_VALUE;
1212

1313
private RecursiveSolver() {
1414
}
1515

1616
public static void main(String[] args) {
1717

18-
Node root = new Node(new int[]{46, 53, 18, 4, 56, 11, 57, 59, 55, 25, 6, 34, 41, 13, 39, 52, 7, 33, 43, 21, 32, 51, 5, 1, 58, 49, 54, 9, 24, 17, 19, 27, 50, 42, 22, 30, 20, 3, 14, 28, 40, 10, 12, 29, 37, 8, 36, 2, 26, 38, 16, 60, 15, 48, 35, 45, 31, 44, 23, 47});
18+
// Node root = new Node(new int[]{46, 53, 18, 4, 56, 11, 57, 59, 55, 25, 6, 34, 41, 13, 39, 52, 7, 33, 43, 21, 32, 51, 5, 1, 58, 49, 54, 9, 24, 17, 19, 27, 50, 42, 22, 30, 20, 3, 14, 28, 40, 10, 12, 29, 37, 8, 36, 2, 26, 38, 16, 60, 15, 48, 35, 45, 31, 44, 23, 47});
1919
// Node root = new Node(new int[]{2, 6, 10, 5, 1, 3, 8, 4, 7, 9});
2020
// Node root = new Node(new int[]{3, 1, 2});
2121
// Node root = new Node(new int[]{11, 7, 15, 6, 17, 3, 19, 16, 4, 10, 8, 12, 20, 14, 2, 5, 1, 9, 13, 18});
2222
// Node root = new Node(new int[]{5, 2, 7, 10, 13, 16, 14, 6, 8, 18, 15, 11, 1, 12, 3, 4, 9, 17});
23-
// Node root = new Node(new int[] { 4, 25, 15, 7, 28, 17, 6, 5, 23, 29, 19, 27, 13, 21, 9, 1, 26, 22, 11, 18, 16, 12, 10, 30, 2, 3, 8, 14, 24, 20 });
24-
// Node root = new Node(new int[]{2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11});
23+
Node root = new Node(new int[] { 4, 25, 15, 7, 28, 17, 6, 5, 23, 29, 19, 27, 13, 21, 9, 1, 26, 22, 11, 18, 16, 12, 10, 30, 2, 3, 8, 14, 24, 20 });
24+
// Node root = new Node(new int[]{2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11});
2525

2626
System.out.format("Solving a pancake pile of height %d.\n", root.getState().length);
2727

@@ -34,46 +34,45 @@ public static void main(String[] args) {
3434

3535
private static void showSolution(long start, long end) {
3636
System.out.format("%dms passed\n", (end - start));
37-
System.out.format("Found solution after %d flips.\n", stack.size());
37+
System.out.format("Found solution after %d flips.\n", NODES.size());
3838

3939
StringBuilder sb = new StringBuilder();
4040

41-
while (!stack.isEmpty()) {
42-
sb.insert(0, Arrays.toString(stack.pop().getState()) + '\n');
41+
while (!NODES.isEmpty()) {
42+
sb.insert(0, Arrays.toString(NODES.pop().getState()) + '\n');
4343
}
4444

4545
System.out.println(sb.toString());
4646
}
4747

4848
private static void solve(Node root) {
4949
root = root.augment();
50-
stack.push(root);
51-
stack.peek().calcDistance();
52-
stack.peek().nextNodes();
50+
NODES.push(root);
51+
NODES.peek().calcDistance();
52+
NODES.peek().nextNodes();
5353
int bound = root.getDistance();
5454

5555
int stateBound;
5656

57-
while (stack.peek().getDistance() != 0) {
58-
if (stack.peek().getDistance() + stack.peek().getDepth() > bound) {
59-
stateBound = stack.peek().getDepth() + stack.peek().getDistance();
57+
while (NODES.peek().getDistance() != 0) {
58+
if (NODES.peek().getDistance() + NODES.peek().getDepth() > bound) {
59+
stateBound = NODES.peek().getDepth() + NODES.peek().getDistance();
6060
candidateBound = stateBound < candidateBound ? stateBound : candidateBound;
61-
stack.pop();
62-
} else if (stack.peek().getChildren().empty()) {
63-
if (stack.peek().getDepth() == 0) {
61+
NODES.pop();
62+
} else if (NODES.peek().getChildren().empty()) {
63+
if (NODES.peek().getDepth() == 0) {
6464
bound = candidateBound;
6565
System.out.println("Searching with bound " + candidateBound);
6666
candidateBound = Integer.MAX_VALUE;
67-
stack.peek().nextNodes();
67+
NODES.peek().nextNodes();
6868
} else {
69-
stack.pop();
69+
NODES.pop();
7070
}
7171
} else {
72-
stack.push(stack.peek().getChildren().pop());
73-
stack.peek().nextNodes();
72+
NODES.push(NODES.peek().getChildren().pop());
73+
NODES.peek().nextNodes();
7474
}
7575
}
7676
}
7777

78-
7978
}

src/main/java/ch/sebastianhaeni/pancake/dto/Node.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public class Node implements Serializable {
2929
*/
3030
private final int flipPosition;
3131

32-
private Stack<Node> children = new Stack<>();
32+
/**
33+
* Stack of child nodes generated from this node.
34+
*/
35+
private final Stack<Node> children = new Stack<>();
3336

3437
/**
3538
* Pancake pile size.

src/main/java/ch/sebastianhaeni/pancake/processor/Controller.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package ch.sebastianhaeni.pancake.processor;
22

33
import java.util.ArrayList;
4-
import java.util.List;
54
import java.util.concurrent.LinkedBlockingQueue;
65
import java.util.concurrent.TimeUnit;
7-
import java.util.stream.Collectors;
86

97
import org.apache.logging.log4j.LogManager;
108
import org.apache.logging.log4j.Logger;

src/main/java/ch/sebastianhaeni/pancake/processor/Worker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
public class Worker implements IProcessor {
2121

22-
private Logger log;
22+
private final Logger log;
2323
private Request splitCommand = null;
2424
private long idleTime = 0;
2525
private Request killCommand = null;

0 commit comments

Comments
 (0)