-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAisAlgorithm.java
executable file
·164 lines (153 loc) · 6.73 KB
/
AisAlgorithm.java
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
import java.util.*;
import java.math.*;
public class AisAlgorithm {
//***********************************************Variable*****************************************************************
final static int pop_size = 50 ;
final static int Iter = 50;
final static double Mut_rate = 0.05;
final static double Clone_rate = 0.1;
static double [][] population = new double[pop_size][4]; // X-Y-Fittness
final static double max = 1.0;
final static double min = 0.0;
//************************************************Initialize****************************************************************
public static void initializePopulation(){ // Initialize (random)
double n=0.0,r=0.0;
for(int i=0;i<pop_size;i++)
for(int j=0;j<2;j++){
population[i][j] =(0.4+ (double) (Math.random() * ((0.6 - (0.4)))));
//(double)Math.round(Math.random() * 10000) / 10000;
}
}
//*************************************************cloneRateNum*****************************************************************
public static double cloneRateNum(double [][] arr,int popNum){
// Clone Rate Calculate
double r=0;
r = (Clone_rate*pop_size*arr[popNum][2])+1;
r=Math.round(r);
return r;
}
//*************************************************Fitness*****************************************************************
public static double fitnessFunction(double x,double y){
double c = 15*x*y*(1-x)*(1-y)*Math.sin(9*Math.PI*x)*Math.sin(9*Math.PI*y) ;
c =(double)Math.round(Math.pow(c,2) * 10000)/10000;
return c;
}
//****************************************************Mutation**************************************************************
public static double [][] mutationFunction(double[][] arr,int popNum){
double delta = 0;
double rand =0;
if(arr[popNum][2] != 0){
delta = arr[popNum][2]/1000;
delta = 1/delta;
rand =(-0.0002+ (double) (Math.random() * ((0.0002 - (-0.0002)))));
rand = (double)Math.round(rand * 10000) / 10000;
delta = delta * rand;
delta = delta/1000;
}
else{
rand = (-0.0002+ (double) (Math.random() * ((0.0002 - (-0.0002)))));
rand = (double)Math.round(rand * 10000) / 10000;
delta = rand/100;
}
delta =(double)Math.round(delta * 10000)/10000;
if( arr[popNum][0] +delta <=1 && arr[popNum][0] +delta>=0 ){
arr[popNum][0] = arr[popNum][0] +delta;
arr[popNum][0]=(double)Math.round(arr[popNum][0] * 10000)/10000;
}
else if(arr[popNum][0] -delta <=1 && arr[popNum][0] -delta>=0) {
arr[popNum][0] = arr[popNum][0] -delta;
arr[popNum][0]=(double)Math.round(arr[popNum][0] * 10000)/10000;
}
if( arr[popNum][1] +delta <=1 && arr[popNum][1] +delta>=0 ){
arr[popNum][1] = arr[popNum][1] +delta;
arr[popNum][1]=(double)Math.round(arr[popNum][1] * 10000)/10000;
}
else if(arr[popNum][1] -delta <=1 && arr[popNum][1] -delta>=0) {
arr[popNum][1] = arr[popNum][1] -delta;
arr[popNum][1]=(double)Math.round(arr[popNum][1] * 10000)/10000;
}
return arr;
}
//****************************************************Sort**************************************************************
public static double[][] sort(double [][] arr ,int n){
double [] temp = new double [4];
for(int i = 0; i < n; i++){
for(int j = 1; j < (n-i); j++){
if(arr[j-1][2] < arr[j][2]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
//****************************************************Clone**************************************************************
public static double[][] Clone(double [][] clone,int n ,double [][] pop , int m){
int k=0 ;
for (int i =0;i<m;i++) {
for(int j=0;j<pop[i][3];j++){
clone[k] = pop[i];
k++;
}
}
return clone;
}
//**************************************************Selection************************************************************
public static double [][] selection(double [][] clone,int sizeClone,double [][]oldPopulation){
int k=0;
double [][] newPopulation = new double[pop_size][4];
// keep the 2 previews population which had the better fitness
// newPopulation[0] = oldPopulation[0];
//newPopulation[1] = oldPopulation[1];
// choose the good fitness
for(int i =0; i<pop_size && k<sizeClone;i++){
newPopulation[i] =clone[k];
k++;
}
return newPopulation;
}
//****************************************************print**************************************************************
public static void print(double [][]arr,int n){
for(int i=0;i<n;i++)
{
for(int j=0;j<4;j++)
System.out.print(arr[i][j]+" ");
System.out.println();
}
}
//*****************************************************Main*****************************************************************
public static void main(String[] args) {
int count=0;
double sum =0;
initializePopulation();
while(count<=Iter){//Condition
sum=0;
count++;
//FITNESS
for (int i =0;i<pop_size;i++) {
population[i][2] = fitnessFunction(population[i][0],population[i][1]);
population[i][3]=cloneRateNum(population,i);
sum = sum + population[i][3];
}
//SORT
population = sort(population,pop_size);
double [][] populationClone = new double[(int)sum][4];
//CLONE
populationClone = Clone(populationClone,(int)sum,population,pop_size);
//MUTATION
for (int i =0;i<(int)sum;i++) {
populationClone=mutationFunction(populationClone,i);
}
//FITNESS
for (int i =0;i<(int)sum;i++) {
populationClone[i][2] = fitnessFunction(populationClone[i][0], populationClone[i][1]);
}
//SORT
populationClone = sort(populationClone,(int)sum);
//SELECTION
population = selection(populationClone,(int)sum,population);
}
System.out.println(population[0][0]+" "+population[0][1]);
}
}