-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutation.java
More file actions
125 lines (116 loc) · 3.8 KB
/
Mutation.java
File metadata and controls
125 lines (116 loc) · 3.8 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
import java.util.ArrayList;
import java.util.Random;
public class Mutation {
public static Random rand = new Random();
public static Chromosome Mutation(Chromosome individual, String type)
{
Chromosome r1=new Chromosome(individual);
int number_of_genes=r1.size();
int number_of_bits_in_genes=r1.chromosome.get(0).size();
int total_bits_in_chromosome=number_of_genes*number_of_bits_in_genes;
int randomInt1=rand.nextInt(((total_bits_in_chromosome-1) - 0) + 1) + 0;
int randomInt2=rand.nextInt(((total_bits_in_chromosome-1) - 0) + 1) + 0;
if(randomInt1==randomInt2)
{
randomInt2=(randomInt1+10)%total_bits_in_chromosome;
}
int max=randomInt1;
int min=randomInt2;
if(randomInt1<randomInt2)
{
max=randomInt2;
min=randomInt1;
}
if(type.equals("Multiple"))
{
ArrayList<Integer> range=new ArrayList<Integer>();
int index=0;
for(int g=0;g<number_of_genes;g++)
{
for (int b = 0; b < number_of_bits_in_genes; b++)
{
if(min<=index && max>=index)
{
int temp1=r1.chromosome.get(g).gene.get(b);
r1.chromosome.get(g).gene.set(b,(temp1+1)%2);
}
index++;
}
}
return r1;
}
if(type.equals("Flip"))
{
int index=0;
for(int g=0;g<number_of_genes;g++)
{
for (int b = 0; b < number_of_bits_in_genes; b++)
{
if(min<=index && max>=index)
{
int temp1=r1.chromosome.get(g).gene.get(b);
r1.chromosome.get(g).gene.set(b,(temp1+1)%2); //flipping
}
index++;
}
}
return r1;
}
if(type.equals("Inversion"))
{
ArrayList<Integer> range=new ArrayList<Integer>();
int index=0;
for(int g=0;g<number_of_genes;g++)
{
for (int b = 0; b < number_of_bits_in_genes; b++)
{
if(min<=index && max>=index)
{
int temp1=r1.chromosome.get(g).gene.get(b);
range.add(temp1);
}
index++;
}
}
index=0;
int counter=0;
for(int g=0;g<number_of_genes;g++)
{
for (int b = 0; b < number_of_bits_in_genes; b++)
{
if(min<=index && max>=index)
{
int val=range.get(range.size()-1-counter);
r1.chromosome.get(g).gene.set(b,val);
counter++;
}
index++;
}
}
return r1;
}
if(type.equals("SingleBitFlip"))
{
int index=0;
for(int g=0;g<number_of_genes;g++)
{
for (int b = 0; b < number_of_bits_in_genes; b++)
{
if(randomInt1==index)
{
int temp1=r1.chromosome.get(g).gene.get(b);
r1.chromosome.get(g).gene.set(b,(temp1+1)%2); //flipping
return r1;
}
index++;
}
}
System.out.println("problem in mutation1");
return r1;
}
else {
System.out.println("problem in mutation2");
return individual;
}
}
}