-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberMatch.java
More file actions
executable file
·77 lines (57 loc) · 2.88 KB
/
NumberMatch.java
File metadata and controls
executable file
·77 lines (57 loc) · 2.88 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
/******************************************************************************
* A Teaching GA Developed by Hal Stringer & Annie Wu, UCF
* Version 2, January 18, 2004
*******************************************************************************/
import java.io.*;
import java.util.*;
import java.text.*;
public class NumberMatch extends FitnessFunction{
/*******************************************************************************
* INSTANCE VARIABLES *
*******************************************************************************/
/*******************************************************************************
* STATIC VARIABLES *
*******************************************************************************/
// Assumes no more than 100 values in the data file
public static int[] testValue = new int[100];
/*******************************************************************************
* CONSTRUCTORS *
*******************************************************************************/
public NumberMatch () throws java.io.IOException {
name = "Number Match Problem";
// Create Table of X values from input file
BufferedReader input = new BufferedReader(new FileReader (Parameters.dataInputFileName));
for (int i=0; i<Parameters.numGenes; i++){
testValue[i] = Integer.parseInt(input.readLine().trim());
}
input.close();
}
/*******************************************************************************
* MEMBER METHODS *
*******************************************************************************/
// COMPUTE A CHROMOSOME'S RAW FITNESS *************************************
public void doRawFitness(Chromo X){
double difference = 0;
for (int j=0; j<Parameters.numGenes; j++){
difference = (double) Math.abs(X.getIntGeneValue(j) - testValue[j]);
X.rawFitness = X.rawFitness + difference;
}
}
// PRINT OUT AN INDIVIDUAL GENE TO THE SUMMARY FILE *********************************
public void doPrintGenes(Chromo X, FileWriter output) throws java.io.IOException{
for (int i=0; i<Parameters.numGenes; i++){
Hwrite.right(X.getGeneAlpha(i),11,output);
}
output.write(" RawFitness");
output.write("\n ");
for (int i=0; i<Parameters.numGenes; i++){
Hwrite.right(X.getIntGeneValue(i),11,output);
}
Hwrite.right((int) X.rawFitness,13,output);
output.write("\n\n");
return;
}
/*******************************************************************************
* STATIC METHODS *
*******************************************************************************/
} // End of NumberMatch.java *************************************************