Repository navigation
Expand file tree
/
Copy pathpsychicoptimal.java
More file actions
293 lines (274 loc) · 7.63 KB
/
Copy pathpsychicoptimal.java
File metadata and controls
293 lines (274 loc) · 7.63 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/** PA1 - psychicoptimal.java
* @author Chris Jung, Meelad Dawood
* @date October 9, 2017
* Dr. Glick, COMP 480
*/
import java.io.*;
import java.util.*;
/** Class "Ticket"
* Holds information about a ticket generated by the "k" parameter:
* ID for the number generated, HashSet "numbers" for the ticket numbers.
* Can be initialized with a specific ID (used for the starting "null" ticket).
**/
class Ticket
{
private static int IDCounter = 0;
private int ID;
private HashSet<Integer> numbers = new HashSet<Integer>();
public Ticket ()
{
this.ID = IDCounter++;
}
public Ticket (int id)
{
this.ID = id;
}
public void addNumber(int a)
{
numbers.add(a);
}
public int getID() { return this.ID; }
public HashSet<Integer> getNumbers()
{
return numbers;
}
public boolean contains (Combination b, int requiredToContain)
{
if (this.numbers.isEmpty()) return false;
int contains = 0;
for (Integer number: b.getNumbers())
{
if (this.numbers.contains(number))
contains++;
if (contains >= requiredToContain)
return true;
}
return false;
}
}
/** Class "Combination"
* Holds information about a combination of numbers, generated by the "j" parameter:
* ID for the number generated, HashSet "numbers" for the combination number.
*/
class Combination
{
private static int IDCounter = 0;
private int ID;
private HashSet<Integer> numbers = new HashSet<Integer>();
public Combination ()
{
this.ID = IDCounter++;
}
public void addNumber (int n)
{
numbers.add(n);
}
public int getID () { return this.ID; }
public HashSet<Integer> getNumbers()
{
return numbers;
}
}
/** Class "TicketComparator"
* Compares the Integer values of Strings, returns the comparison result.
* For use when sorting the final results in lex order.
*/
class TicketComparator<E> implements Comparator<E>
{
public int compare(Object a, Object b)
{
String aa = (String)a;
String bb = (String)b;
String[] aarr = aa.split(" ");
String[] barr = bb.split(" ");
for (int i = 0; i < aarr.length; i++)
{
int check1 = Integer.parseInt(aarr[i]);
int check2 = Integer.parseInt(barr[i]);
//System.out.println("");
int toReturn = check1 - check2;
if (toReturn < 0)
return -1;
else if (toReturn > 0)
return 1;
}
return 0;
}
}
public class psychicoptimal
{
static int smallest = Integer.MAX_VALUE;
static LinkedList<Ticket> bestPath = null;
static ArrayList<Ticket> everything = new ArrayList<Ticket>();
static ArrayList<Combination> possibilities = new ArrayList<Combination>();
public static void main (String [] args)
{
// Get input from file
if (args.length != 2)
{
System.out.println("Wrong number of arguments.");
System.exit(1);
}
int n = 0, k = 0, j = 0, l = 0;
String inputName = args[0], outputName = args[1];
try
{
File inFile = new File(inputName);
Scanner in = new Scanner(inFile);
n = in.nextInt();
j = in.nextInt();
k = in.nextInt();
l = in.nextInt();
in.close();
}
catch (FileNotFoundException d)
{
System.out.println("File not found!");
}
// Sanitize inputs
if (n < k || k < j || j < l || n == 0 || k == 0 || j == 0 || l == 0)
{
System.out.println("Bad values for n, j, k, l!");
System.exit(1);
}
// Generate Tickets and Combinations based on k, j
int[] elements = new int[n];
for (int index = 0; index < n; index++)
elements[index] = index + 1;
int [] data = new int [n];
generateRecursive(elements, data, 0, n-1, 0, k, true);
int[] givenData = new int[n];
generateRecursive(elements, givenData, 0, n-1, 0, j, false);
// Brute force search all the Combinations and Tickets for working sets.
// Store information in "bestPath"
runningSumBruteForce(new Ticket(-1), null, elements, l);
// Collect the results and convert to strings for printing
ArrayList<String> resultStrings = new ArrayList<String>();
Collections.sort(resultStrings);
for (Ticket d: bestPath)
{
String dd = d.getNumbers().toString();
dd = dd.replaceAll("([\\[,\\]])", "");
resultStrings.add(dd);
}
resultStrings.sort(new TicketComparator<String>());
// Print results to output file
try
{
File out = new File (outputName);
out.createNewFile();
PrintWriter print = new PrintWriter (out);
print.println(smallest);
for (String str : resultStrings)
{
print.println(str);
}
print.close();
}
catch (Exception exception)
{
System.out.println("Unable to print to file");
}
}
/** runningSumBruteForce
* Recursively compares combinations of Ticket objects to a set of Combination objects.
* Stores the best solutions (fewest number of tickets) in "bestPath" if a new best is encountered.
* Runs in (O(2^n)).
*
* @param root Ticket object from which to start
* @param decisions LinkedList of the included Tickets in this iteration
* @param data Data to check
* @param requiredToContain Number of Integers required to match to be covered by a Ticket
* @return LinkedList of the decisions (if all Combinations covered) or null
*/
static LinkedList<Ticket> runningSumBruteForce (Ticket root, LinkedList<Ticket> decisions, int[] data, int requiredToContain)
{
if (decisions == null) decisions = new LinkedList<Ticket>();
// Check the combinations on the current list of tickets
boolean allFound = true;
for (Combination each:possibilities)
{
if (!root.contains(each, requiredToContain))
{
boolean foundElsewhere = false;
for (Ticket checkDecisions:decisions)
{
if (checkDecisions.contains(each, requiredToContain))
{
foundElsewhere = true;
break;
}
}
if (!foundElsewhere) allFound = false;
}
}
// Not all combinations are covered, call recursively on larger sets
if (!allFound)
{
for (int index = root.getID() + 1; index < everything.size(); index++)
{
LinkedList<Ticket> newDecisions = new LinkedList<Ticket>(decisions);
if (root.getID() != -1)
newDecisions.add(root);
LinkedList<Ticket> found = runningSumBruteForce(everything.get(index), newDecisions, data, requiredToContain);
// Recursive call has returned, if !null and better than current
// best, set bestPath to new path and smallest to bestPath.size()
if (found != null)
{
if (found.size() < smallest)
{
bestPath = new LinkedList<Ticket>(found);
smallest = found.size();
}
}
}
return null;
}
else
{
// All combinations are covered, return the decisions list and root
decisions.add(root);
return decisions;
}
}
/** generateRecursive
* Recursively generates a set of Ticket objects or Combination objects based on values for input.
* Stores values in "possibilities" or "everything"
* @param arr Placeholder array for integers
* @param data Data to cover
* @param start Starting point for generation
* @param end Ending point for generation
* @param index Current index
* @param r Number until the end (calculated later)
* @param ticket true if generating Ticket objects, false if Combination objects
*/
static void generateRecursive(int arr[], int data[], int start, int end, int index, int r, boolean ticket)
{
if (index == r)
{
if (ticket)
{
Ticket toAdd = new Ticket();
for (int j=0; j<r; j++)
{
toAdd.addNumber(data[j]);
}
everything.add(toAdd);
}
else
{
Combination toAdd = new Combination();
for (int j=0; j<r; j++)
{
toAdd.addNumber(data[j]);
}
possibilities.add(toAdd);
}
}
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
generateRecursive(arr, data, i+1, end, index+1, r, ticket);
}
}
}