-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlg.java
More file actions
1190 lines (965 loc) · 43.1 KB
/
Alg.java
File metadata and controls
1190 lines (965 loc) · 43.1 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
///////////////////////////////////////////////////////////////////////////////
// ------------------------------------------------------------------------- //
// ------------------------------ Algorithms ------------------------------- //
// ------------------------------------------------------------------------- //
///////////////////////////////////////////////////////////////////////////////
/**
* Algorithms Class
*/
public class Alg {
// Global Variable
public static String inputfile; // Input file path + name
public static String outputfile; // Output file path + name
public static List<String> outprint; // Output data to write on file
public static double[][][] C; // Cost Matrix
public static int G = 100; // Number of generations
public static int P = 10; // Number of generations for each checkpoint
public static int Ssize = 5; // Solutions size
public static int total = 0; // Amount of debris
public static List<Integer> Best; // Best Solution
public static int Kb; // Fitness of Best Solution
public static int mdays = 365; // Maximum days allowed for the mission
public static int repeat = 1; // Number of repetitions to run in the same cost matrix
public static boolean noprint = false; // Number of repetitions to run in the same cost matrix
public static long timer; // Timer to keep track of how long the algorithms are taking
/**
* Main Function
* Check for first argument expecting a specific algorithm call
*/
public static void main(String[] args) throws IOException {
// Initiate argument variables
List<String> algorithm = new ArrayList<String>();
double alpha = 0.99;
int loop = 1;
int tweakType = -1;
int popsize = 10;
int elitesize = 2;
double mp = 0.1;
// Argument Parse
int argpos = 0;
while (argpos < args.length && args[argpos].startsWith("-")) {
// Read current argument
String arg = args[argpos++];
// Algorithm
if (arg.equals("-a") || arg.equals("--algorithm")) {
if (argpos < args.length){
if (!args[argpos].equals("SA") && !args[argpos].equals("GA")) {
System.err.println("\nWrong algorithm "+args[argpos]+" is unknown.)\n");
TerminateWithHelp();
}
algorithm.add(args[argpos++]);
}
else {
System.err.println("\nAlgorithm must be informed (-a NAME | --algorithm NAME)\n");
TerminateWithHelp();
}
}
// Input
if (arg.equals("-i") || arg.equals("--input")) {
if (argpos < args.length)
inputfile = args[argpos++];
else {
System.err.println("\nInput file not infomed (-i filename | --input filename)\n");
TerminateWithHelp();
}
}
// Output
if (arg.equals("-o") || arg.equals("--output")) {
if (argpos < args.length)
outputfile = args[argpos++];
else {
System.err.println("\nOutput file not infomed (-o filename | --output filename)\n");
TerminateWithHelp();
}
}
// Generations
if (arg.equals("-g") || arg.equals("--generations")) {
if (argpos < args.length)
G = Integer.parseInt(args[argpos++]);
else {
System.err.println("\nNumber of generations not infomed (-g number | --generations number)\n");
TerminateWithHelp();
}
}
// Generation Checkpoint
if (arg.equals("-c") || arg.equals("--checkpoint")) {
if (argpos < args.length)
P = Integer.parseInt(args[argpos++]);
else {
System.err.println("\nNumber of generations for each checkpoint not (-c number | --checkpoint number)\n");
TerminateWithHelp();
}
}
// Solution Size
if (arg.equals("-s") || arg.equals("--size")) {
if (argpos < args.length)
Ssize = Integer.parseInt(args[argpos++]);
else {
System.err.println("\nSolution size not informed (-s number | --size number)\n");
TerminateWithHelp();
}
}
// Maximum Days Restriction
if (arg.equals("-d") || arg.equals("--days")) {
if (argpos < args.length)
mdays = Integer.parseInt(args[argpos++]);
else {
System.err.println("\nMaximum number of days for each mission not informed (-d number | --days number)\n");
TerminateWithHelp();
}
}
// Repetitions
if (arg.equals("-r") || arg.equals("--repeat")) {
if (argpos < args.length)
repeat = Integer.parseInt(args[argpos++]);
else {
System.err.println("\nNumber of repetitions not informed (-r number | --repeat number)\n");
TerminateWithHelp();
}
}
// Ignore prints
if (arg.equals("-n") || arg.equals("--noprint")) {
noprint = true;
}
// SA - Temperature Decay Rate
if (arg.equals("-t") || arg.equals("--temperature")) {
if (argpos < args.length)
alpha = Double.parseDouble(args[argpos++]);
else {
System.err.println("\nTemperature decay rate not informed (-t number | --temperature number)\n");
TerminateWithHelp();
}
}
// SA - Loop
if (arg.equals("-l") || arg.equals("--loop")) {
if (argpos < args.length)
loop = Integer.parseInt(args[argpos++]);
else {
System.err.println("\nAmount of loops without changing temperature not defined (-l number | --loop number)\n");
TerminateWithHelp();
}
}
// SA - Tweak Type
if (arg.equals("-w") || arg.equals("--tweak")) {
if (argpos < args.length)
tweakType = Integer.parseInt(args[argpos++]);
else {
System.err.println("\ntype of tweak not defined (-w number | --tweak number)\n");
TerminateWithHelp();
}
}
// GA - Population Size
if (arg.equals("-p") || arg.equals("--popsize")) {
if (argpos < args.length)
popsize = Integer.parseInt(args[argpos++]);
else {
System.err.println("\nPopulation size not informed (-p number | --popsize number)\n");
TerminateWithHelp();
}
}
// GA - Elite Size
if (arg.equals("-e") || arg.equals("--elitesize")) {
if (argpos < args.length)
elitesize = Integer.parseInt(args[argpos++]);
else {
System.err.println("\nAmount of elite individuals not informed (-e number | --elitesize number)\n");
TerminateWithHelp();
}
}
// GA - Mutation Probability
if (arg.equals("-m") || arg.equals("--mutation")) {
if (argpos < args.length)
mp = Integer.parseInt(args[argpos++]);
else {
System.err.println("\nMutation probability not informed (-m number | --mutation number)\n");
TerminateWithHelp();
}
}
// Help
if (arg.equals("-h") || arg.equals("--help")) {
TerminateWithHelp();
}
}
// Check required arguments
if (algorithm.size() <= 0) {
System.err.println("\nMissing argument algorithm (-a NAME | --algorithm NAME)\n");
TerminateWithHelp();
}
// Prepare time dependent Cost Matrix
PrepareCostMatrix();
// Adjust number of generations
if (G < 0)
G = 1;
// Adjust number of debris (to collect and total)
if (C.length > 0) {
if (C[0].length > 0)
total = C[0][0].length-1;
}
if (Ssize > total)
Ssize = total;
// Adjust number of maximum days restriction
if (mdays < 0)
mdays = 1;
// Adjust number of generations for each checkpoint
if (P <= 0 || P > G)
P = G;
// Adjust elite size to avoid losing 1 individual in population when filling next generation
if (elitesize < popsize) {
if (elitesize % 2 != popsize % 2)
elitesize++;
}
else
elitesize = popsize;
String baseoutputfile = outputfile;
int filenumber = 0;
// Call Algorithms
System.out.println("Running Algorithms...\n");
for (int r = repeat ; r > 0 ; r--) {
for (String A : algorithm) {
// Output file name
if (baseoutputfile != null) {
outputfile = baseoutputfile+"_"+(++filenumber)+"_"+A+".stat";
outprint = new ArrayList<String>();
}
// Initialize timer
timer = 0;
// Verify which algorithm to run
if (A.equals("SA")) {
// Simulated Annealing
SA(args, alpha, loop, tweakType);
}
else if (A.equals("GA")) {
// Genetic Algorithm
GA(args, popsize, elitesize, mp);
}
// Show Results
if (!noprint) {
System.out.println("Results:");
System.out.print(" Best Solution = [ ");
for (int i : Best) {
System.out.print(i+" ");
}
System.out.println("]");
if (Kb <= mdays)
System.out.println(" Mission Duration (Fitness) = "+Kb+" days");
else
System.out.println(" Fitness = over "+mdays+" limit");
System.out.println(" Time Consumed: "+(timer)+"ns\n");
}
// Write output file
if (baseoutputfile != null) {
PrintWriter writer = new PrintWriter(outputfile, "UTF-8");
for (String line : outprint)
writer.println(line);
writer.close();
}
}
}
return;
}
/**
* Terminate with Help Function
*/
public static void TerminateWithHelp() {
// Print Help Text
System.err.println("COMMAND LINE PARAMETERS:\n"
+" INPUT TYPE DEFAULT DESCRIPTION\n"
+"\n"
+" required:\n"
+" -a, --algorithm string none selected algorithm\n"
+" SA = Simulated Annealing\n"
+" GA = Genetic Algorithm\n"
+"\n"
+" optional:\n"
+" -i, --input string none path to debris data file\n"
+" -o, --output string none path to output file\n"
+" -g, --generations int 100 number of generations\n"
+" -c, --checkpoint int 10 generations between checkpoints\n"
+" -s, --size int 5 solution size (number of debris)\n"
+" -d, --days int 365 limit of mission days\n"
+" -r, --repeat int 1 repeat algorithms in same matrix\n"
+" -n, --noprint bool avoid printing in console\n"
+"\n"
+" -t, --temperature double 0.99 [SA only] temperature decay rate\n"
+" -l, --loop int 1 [SA only] loops/generation\n"
+" -w, --tweak int -1 [SA only] type of tweak\n"
+"\n"
+" -p, --popsize int 10 [GA only] population size\n"
+" -e, --elitesize int 2 [GA only] amount of elite\n"
+" -m, --mutation double 0.1 [GA only] mutation probability\n"
);
// Terminate
System.exit(0);
}
/**
* Simulated Annealing Function
*/
public static void SA(String[] args, double alpha, int loop, int tweakType) {
// Header
if (!noprint) {
System.out.println("\n"
+"///////////////////////////////////////////////////////////////////////////////\n"
+"// ------------------------------------------------------------------------- //\n"
+"// -------------------------- Simulated Annealing -------------------------- //\n"
+"// ------------------------------------------------------------------------- //\n"
+"///////////////////////////////////////////////////////////////////////////////\n"
+"\n"
+"Subject: Bio-inspired Computing\n"
+"Student: Eric G. Müller\n");
// Print parameters
System.out.println("Parameters:");
System.out.println(" Number of generations = "+G+" (checkpoint at each "+P+")");
System.out.println(" Number of debris to collect = "+Ssize);
System.out.println(" [Restriction] Maximum number of days in mission = "+mdays);
System.out.println(" Temperature decay rate = "+alpha);
System.out.println("");
}
// Variable declaration
int generation = 0; // Initial generation
int loopcount = 0; // Count iterations that are not changing temperature
double t = 0; // Initial Temperature
List<Integer> S; // Initial Solution
// Step 1: Calculate initial temperature
int K4t = 0;
int delta = 0;
// Initiate timer
timer = System.currentTimeMillis();
// Generate 10 more random solutions
for (int j = 0 ; j < 10 ; j++){
// Generates a new random solution
S = new ArrayList<Integer>();
for (int i = 0 ; i < Ssize ; i++) {
// Create a proposed value and only accept it if it isn't in the solution
int r;
do {
r = 1 + (int)(Math.random() * total);
} while (S.contains(r));
// Add the random value to the solution
S.add(r);
}
// Add up the difference in fitness
if (j != 0)
delta += Math.abs(K4t-K(S));
K4t = K(S);
}
// Calculate temperature based on Johnson's method
t = -delta/Math.log(0.8);
if (!noprint)
System.out.println("Initial Temperature: "+t);
// Step 2: Generate initial solution
if (!noprint)
System.out.print("Initial Solution: [ ");
S = new ArrayList<Integer>();
for (int i = 0 ; i < Ssize ; i++) {
// Create a proposed value and only accept it if it isn't in the solution
int r;
do {
r = 1 + (int)(Math.random() * total);
} while (S.contains(r));
// Add the random value to the solution
S.add(r);
if (!noprint)
System.out.print(r+" ");
}
if (!noprint)
System.out.println("]\n");
int Ks = K(S);
// Step 3: Set initial solution as best
Best = S;
Kb = Ks;
// Step 4: Repeat
do {
// Step 6: Tweak current solution
List<Integer> R;
if (tweakType == -1)
R = Tweak(S, total);
else
R = TweakImprove(S, Ks, total, tweakType);
// Step 7: Verify if tweaked solution must be used instead of the current solution
int Kr = K(R);
if (Kr < Ks) {
// Step 8: Replace current solution for tweaked solution
S = R;
Ks = Kr;
}
// Step 9: Decrease temperatura
if (++loopcount == loop) {
t = t * alpha;
++generation;
loopcount = 0;
}
// Step 10: Verify if current solution is best
if (Ks < Kb) {
Best = S;
Kb = Ks;
}
// Checkpoint
if (!noprint && loopcount == 0 && generation%P == 0) {
System.out.print("["+generation+"] current solution: S = [ ");
for (int i : S)
System.out.print(i+" ");
System.out.println("] -> Fitness = "+Ks);
System.out.print("["+generation+"] best solution: Best = [ ");
for (int i : Best)
System.out.print(i+" ");
System.out.println("] -> Fitness = "+Kb);
System.out.println("["+generation+"] time: "+(System.currentTimeMillis()-timer)+"ns\n");
}
// Output
if (loopcount == 0 && outputfile != null) {
String data = new String(generation+" "+Kb+" [");
for (int i : Best)
data += i+",";
outprint.add(data+"] "+(System.currentTimeMillis()-timer));
}
// Step 12: Stop condition if maximum number of generations reached or best is the ideal solution
} while (generation < G || Kb <= 0);
// Terminate timer
timer = System.currentTimeMillis() - timer;
// Step 13: Return best solution (already global variables)
return;
}
/**
* Genetic Algorithm Function
*/
public static void GA(String[] args, int popsize, int elitesize, double mp) {
// Header
if (!noprint) {
System.out.println("\n"
+"///////////////////////////////////////////////////////////////////////////////\n"
+"// ------------------------------------------------------------------------- //\n"
+"// --------------------------- Genetic Algorithm --------------------------- //\n"
+"// ------------------------------------------------------------------------- //\n"
+"///////////////////////////////////////////////////////////////////////////////\n"
+"\n"
+"Subject: Bio-inspired Computing\n"
+"Student: Eric G. Müller\n");
// Print parameters
System.out.println("Parameters:");
System.out.println(" Number of generations = "+G+" (checkpoint at each "+P+")");
System.out.println(" Number of debris to collect = "+Ssize);
System.out.println(" [Restriction] Maximum number of days in mission = "+mdays);
System.out.println(" GA - Population Size = "+popsize);
System.out.println(" GA - Amount of Elite individuals = "+elitesize);
System.out.println(" GA - Mutation probability = "+mp);
System.out.println("");
}
// Variable declaration
int generation = 0; // Initial generation
// Step 3: Initiate population
List<List<Integer>> pop = new ArrayList<List<Integer>>();
List<Integer> Ks = new ArrayList<Integer>();
// Initiate timer
timer = System.currentTimeMillis();
// Step 4: In each population
for (int j = 0 ; j < popsize ; j++) {
// Step 5: Create a random solution for each individual
pop.add(new ArrayList<Integer>());
for (int i = 0 ; i < Ssize ; i++) {
// Create a proposed value and only accept it if it isn't in the solution
int r;
do {
r = 1 + (int)(Math.random() * total);
} while (pop.get(j).contains(r));
// Add the random value to the solution
pop.get(j).add(r);
}
Ks.add(K(pop.get(j)));
}
// Step 6: Initiate Best Solution
Best = new ArrayList<Integer>();
Kb = 99999;
// Step 7: Repeat
do {
// Step 8: For each individual
for (int j = 0 ; j < popsize ; j++) {
// Step 9: Verify if the current individual is the best
if (Best.size() <= 0 || Ks.get(j) < Kb) {
Best = pop.get(j);
Kb = Ks.get(j);
}
}
// Step 11: Start next generation with the elite
List<List<Integer>> nextgen = new ArrayList<List<Integer>>();
List<Integer> nextKs = new ArrayList<Integer>();
int refK = -1;
for (int i = 0 ; i < elitesize ; i++) {
// Check all individuals to find the elite one (starting from the last elite)
int Ke = Ks.get(0);
int je = 0;
for (int j = 1 ; j < popsize ; j++) {
if (Ks.get(j) > refK && Ks.get(j) < Ke) {
je = j;
Ke = Ks.get(j);
}
}
// Add elite one to next generation and set new reference
refK = Ke;
nextgen.add(pop.get(je));
nextKs.add(Ke);
}
// Step 12: Fill the rest of the next generation
for (int i = (popsize - elitesize)/2 ; i > 0 ; --i) {
// Step 13: Crossover in random position using 2 parents choosen by tournament
List<List<Integer>> Children = Crossover(pop.get(Tournament(Ks, 4)), pop.get(Tournament(Ks, 4)));
// Step 14: Add children to next generation after Mutation
Children = Mutation(Children, 0.1);
for (List<Integer> child : Children) {
nextgen.add(child);
nextKs.add(K(child));
}
}
// Step 15: New Generation
pop = nextgen;
Ks = nextKs;
// Next generation
++generation;
// Checkpoint
if (!noprint) {
if ((generation) % P == 0) {
System.out.println("["+generation+"] current Fitness = "+Ks);
System.out.println("["+generation+"] best Fitness = "+Kb);
System.out.println("["+generation+"] time: "+(System.currentTimeMillis()-timer)+"ns\n");
}
}
// Output
if (outputfile != null) {
String data = new String(generation+" "+Kb+" [");
for (int i : Best)
data += i+",";
outprint.add(data+"] "+(System.currentTimeMillis()-timer));
}
// Step 16: Stop condition if maximum number of generations reached or best is the ideal solution
} while (generation < G || Kb <= 0);
// Terminate timer
timer = System.currentTimeMillis() - timer;
// Step 17: Return best solution (already global variables)
return;
}
/**
* Tweak Function
*/
public static List<Integer> Tweak(List<Integer> R, int total) {
// Determine where to tweak
int pos = (int)(Math.random() * R.size());
// Create a proposed value and only accept a different value
int r;
do {
r = 1 + (int)(Math.random() * total);
} while (r == R.get(pos));
// If the tweaked value is already on the solution, switch it
if (R.contains(r)) {
for (int i = 0 ; i < R.size() ; i++) {
if (R.get(i) == r) {
R.set(i, R.get(pos));
break;
}
}
}
// Tweak the solution with the random value
R.set(pos, r);
return R;
}
/**
* Tweak Function
*/
public static List<Integer> TweakImprove(List<Integer> R, int Kr, int total, int amount) {
// Determine where to tweak
int pos = (int)(Math.random() * R.size());
// Find the value to be replaced that minimized the solution
List<Integer> T = R;
int Kb = Kr;
int Kt;
int r = pos;
for (int i = 1 ; i < C[0].length ; i++) {
T.set(pos, i);
Kt = K(T);
if (Kt < Kb) {
r = i;
Kb = Kt;
if (--amount <= 0)
break;
}
}
// Only accept a different value
if (r == R.get(pos)) {
do {
r = 1 + (int)(Math.random() * total);
} while (r == R.get(pos));
}
// If the tweaked value is already on the solution, switch it
if (R.contains(r)) {
for (int i = 0 ; i < R.size() ; i++) {
if (R.get(i) == r) {
R.set(i, R.get(pos));
break;
}
}
}
// Tweak the solution with the random value
R.set(pos, r);
return R;
}
/**
* Tournament Function
*/
public static int Tournament(List<Integer> Ks, int amount) {
// Generate the first random position
int winpos = (int)(Math.random() * Ks.size());
// Compare with other random positions
for (int i = 1 ; i < amount ; i++) {
// Generate next random positions
int pos = (int)(Math.random() * Ks.size());
// Replace position based on best fitness
if (Ks.get(pos) < Ks.get(winpos))
winpos = pos;
}
return winpos;
}
/**
* Crossover Function
*/
public static List<List<Integer>> Crossover(List<Integer> Pa, List<Integer> Pb) {
// Initiate children
List<List<Integer>> Children = new ArrayList<List<Integer>>();
Children.add(new ArrayList<Integer>());
Children.add(new ArrayList<Integer>());
// Find where to cross
int cross = (int)(Math.random() * Pa.size());
// Build new child
for (int i = 0 ; i < Pa.size() ; i++) {
// Input values are added based on crossing point
if (i < cross) {
// Add parent values
Children.get(0).add(Pa.get(i));
Children.get(1).add(Pb.get(i));
}
if (i >= cross) {
int Ca = Pb.get(i);
int Cb = Pa.get(i);
// Verify an solve repeating values
if (Children.get(0).contains(Ca)) {
for (int j = 0 ; j < i ; j++) {
if (Children.get(0).get(j) == Ca) {
if (Children.get(0).contains(Pb.get(j))) {
while (Children.get(0).contains(Ca)) {
Ca = 1 + (int)(Math.random() * total);
}
}
else {
Ca = Pb.get(j);
}
break;
}
}
}
if (Children.get(1).contains(Cb)) {
for (int j = 0 ; j < i ; j++) {
if (Children.get(1).get(j) == Cb) {
if (Children.get(1).contains(Pa.get(j))) {
while (Children.get(1).contains(Cb)) {
Cb = 1 + (int)(Math.random() * total);
}
}
else {
Cb = Pa.get(j);
}
break;
}
}
}
// Add new values
Children.get(0).add(Ca);
Children.get(1).add(Cb);
}
}
return Children;
}
/**
* Mutation Function
*/
public static List<List<Integer>> Mutation(List<List<Integer>> Children, double mp) {
// Each child must try to mutate
for (List<Integer> child : Children) {
// Try mutating each index of the solution
for (int j = 0 ; j < child.size() ; j++) {
// Only mutate if random value is under mutation probability
if (Math.random() >= mp)
continue;
// Create a proposed value and only accept a different value
int r;
do {
r = 1 + (int)(Math.random() * total);
} while (r == child.get(j));
// If the tweaked value is already on the solution, switch it
if (child.contains(r)) {
for (int i = 0 ; i < child.size() ; i++) {
if (child.get(i) == r) {
child.set(i, child.get(j));
break;
}
}
}
// Tweak the solution with the random value
child.set(j, r);
}
}
// Return as children
return Children;
}
/**
* Fitness Function
*/
public static int K(List<Integer> S) {
// Initiate day count
int days = 0;
// Verify cost of edge based on each debris
for (int j = 0 ; j < S.size() ; j++) {
// If cost exceeds the limit of avaiable mission days in Cost Matrix return a big number
if (days >= C.length)
return mdays + 25;
// Check how many days for transfer from current position to next debris + 5 days for processing
if (j > 0)
days += C[days][S.get(j-1)][S.get(j)] + 5;
else
days += C[days][S.get(j)][0] + 5;
}
return (int)days;
}
/**
* Prepare Cost Matrix Function
*/
public static void PrepareCostMatrix() {
// ???? to be made, test with random values
List<String[][]> tle = LoadTLE();
if (tle == null) {
// If couldn't read TLE data from input a random cost matrix will be used
System.out.println("Generating Random Cost Matrix...\n");
RandomCostMatrix(365, 100, 200);
}
else {
// After reading TLE data from input the cost matrix must be generated
System.out.println("Generating TLE Based Cost Matrix...\n");
SimulateCostMatrix(365, tle);
}
}
/**
* Load TLE from input
*/
public static List<String[][]> LoadTLE() {
// Read input file
if (inputfile == null)
return null;
try {
// Initiate file reader
BufferedReader input = new BufferedReader(new FileReader(inputfile));
// Read data and store
List<String[][]> readData = new ArrayList<String[][]>();
String line = input.readLine();
String[][] currData = new String[2][13];
while (line != null && line.length() > 0) {
// Verify line number
switch (line.charAt(0)) {
// First line of a TLE
case '1':
// Satellite Number
currData[0][0] = line.substring(2, 6);
// Classification (U=Unclassified)
currData[0][1] = ""+line.charAt(7);
// International Designator (Last two digits of launch year)
currData[0][2] = line.substring(9, 10);
// International Designator (Launch number of the year)
currData[0][3] = line.substring(11, 13);
// International Designator (Piece of the launch)
currData[0][4] = line.substring(14, 16);
// Epoch Year (Last two digits of year)
currData[0][5] = line.substring(18, 19);
// Epoch (Day of the year and fractional portion of the day)
currData[0][6] = line.substring(20, 31);
// First Time Derivative of the Mean Motion
currData[0][7] = line.substring(33, 42);
// Second Time Derivative of Mean Motion (decimal point assumed)
currData[0][8] = line.substring(44, 51);
// BSTAR drag term (decimal point assumed)
currData[0][9] = line.substring(53, 60);
// Ephemeris type
currData[0][10] = ""+line.charAt(62);
// Element number
currData[0][11] = line.substring(64, 67);
// Checksum (Modulo 10) = (Letters, blanks, periods, plus signs = 0; minus signs = 1)
currData[0][12] = ""+line.charAt(68);
break;
// Second line of a TLE
case '2':
// Satellite Number
currData[1][0] = line.substring(2, 6);
// Inclination [Degrees]
currData[1][1] = line.substring(8, 15);
// Right Ascension of the Ascending Node [Degrees]
currData[1][2] = line.substring(17, 24);
// Eccentricity (decimal point assumed)
currData[1][3] = line.substring(26, 32);
// Argument of Perigee [Degrees]
currData[1][4] = line.substring(34, 41);
// Mean Anomaly [Degrees]
currData[1][5] = line.substring(43, 50);