-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGene.java
More file actions
40 lines (34 loc) · 937 Bytes
/
Gene.java
File metadata and controls
40 lines (34 loc) · 937 Bytes
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
package aiproject1;
import java.util.Random;
/**
*
* @author Layan
*/
public class Gene {
int fitness = 0;
int geneLength = GeneticAlgorithm.length;
char [] genes = new char [geneLength]; //[X,Y,X,X,Y...]
public Gene(){
Random rn = new Random();
//constructor > random > X Y
for(int i=0;i<genes.length;i++){
double v=0;
v=Math.abs(rn.nextInt()%2);
if(v==0) genes[i]='X';
else genes[i]='Y';
}
fitness=0;
}
//calculate fitness
public void calcfitness(){
fitness=0;
int subX=0, subY=0;
for(int i=0;i<genes.length;i++){
if (genes[i]=='X'){
subX+=GeneticAlgorithm.getJob(i); }//end if,values of x
else
subY+=GeneticAlgorithm.getJob(i);//values of y
}//END FOR
fitness = Math.abs(subX - subY); //f(n) = S1 - S2
}
}//END GENE