-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
executable file
·146 lines (131 loc) · 4.79 KB
/
Main.java
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
import Algorithms.*;
import java.util.Scanner;
/**
* Main class.
*/
public class Main {
// Seed -8191564060360249868 lets the heuristic search solve n = 150 in 20 secs.
public static void main(String[] args) {
if (args.length != 1) {
printMenu();
return;
}
int arg;
try {
arg = Integer.parseInt(args[0]);
} catch (Exception ex) {
printMenu();
return;
}
handleParameters(arg);
}
/**
* Deals with the command line parameters. Determines the algorithm to run.
* @param arg the value that the user inputted
*/
private static void handleParameters(int arg) {
if (arg == 0) {
int n = getIntInput("Queens: ");
int seed = getIntInput("Seed: ");
boolean debug = getBooleanInput();
CompletelyRandom.search(n, seed, debug);
} else if (arg == 1) {
int n = getIntInput("Queens: ");
int seed = getIntInput("Seed: ");
boolean debug = getBooleanInput();
RandomWithPropagation.search(n, seed, debug);
} else if (arg == 2) {
int n = getIntInput("Queens: ");
int timeLimit = getIntInput("Time Limit (secs): ");
boolean debug = getBooleanInput();
BlindSearchNaive.search(n, timeLimit, debug);
} else if (arg == 3) {
int n = getIntInput("Queens: ");
int timeLimit = getIntInput("Time Limit (secs): ");
boolean debug = getBooleanInput();
BlindSearchIntermediate.search(n, timeLimit, debug);
} else if (arg == 4) {
int n = getIntInput("Queens: ");
int timeLimit = getIntInput("Time Limit (secs): ");
boolean debug = getBooleanInput();
BlindSearchAdvanced.search(n, timeLimit, debug);
} else if (arg == 5) {
int n = getIntInput("Queens: ");
int seed = getIntInput("Seed: ");
int timeLimit = getIntInput("Time Limit (secs): ");
boolean debug = getBooleanInput();
PopulationBased.search(n, seed, timeLimit, debug);
} else if (arg == 6) {
int n = getIntInput("Queens: ");
int seed = getIntInput("Seed: ");
int timeLimit = getIntInput("Time Limit (secs): ");
boolean debug = getBooleanInput();
HeuristicSearch.search(n, seed, timeLimit, debug);
} else if (arg == 7) {
int n = getIntInput("Queens: ");
ExplicitSolution.run(n);
} else {
System.out.println("Parameter must be between 0 and 7 inclusive.");
}
}
/**
* Menu outlining the usage of the program.
*/
private static void printMenu() {
System.out.println();
System.out.println("Usage: ");
System.out.println("\tjava Main [integer from 0 to 7]");
System.out.println();
System.out.println("Parameters: ");
System.out.println("\t0 - random search");
System.out.println("\t1 - semi-random search");
System.out.println("\t2 - naive blind search");
System.out.println("\t3 - intermediate blind search");
System.out.println("\t4 - advanced blind search");
System.out.println("\t5 - iterative repair");
System.out.println("\t6 - population based search");
System.out.println("\t7 - explicit solution");
System.out.println();
System.out.println("Examples: ");
System.out.println("\tjava Main 1");
System.out.println("\tjava Main 6");
System.out.println();
}
/**
* Get boolean value from the user. Exit if invalid input.
* @return the user input
*/
private static boolean getBooleanInput() {
System.out.print("Debug Mode: ");
Scanner scanner = new Scanner(System.in);
boolean input = false;
try {
input = scanner.nextBoolean();
} catch (Exception ex) {
System.out.println("Input must be a boolean (true/false).");
System.exit(-1);
}
return input;
}
/**
* Get integer value from the user. Exit if invalid input.
* @param msg the message to prompt the user
* @return the user input
*/
private static int getIntInput(String msg) {
System.out.print(msg);
Scanner scanner = new Scanner(System.in);
int input = -1;
try {
input = scanner.nextInt();
} catch (Exception ex) {
System.out.println("Input must be an integer.");
System.exit(-1);
}
if (msg.equals("Queens: ") && input < 4) {
System.out.println("Number of Queens should be at least 4.");
System.exit(-1);
}
return input;
}
}