package GA;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
public class GA
{
private int POPSIZE = 50; /* 初始种群大小 */
private int MAXGENES = 1000; /* 迭代运算次数 */
private int NVARS = 3; /* 优化参数个数 */
private double PXOVER = 0.80; /* 交叉概率 */
private double PMUTATION = 0.15; /* 突变概率 */
public void SetPopsize(int x)
{
POPSIZE = x;
}
public void SetMaxgenes(int x)
{
MAXGENES = x;
}
public void SetNvars(int x)
{
NVARS = x;
}
public void SetPxover(double x)
{
PXOVER = x;
}
public void SetPmutation(double x)
{
PMUTATION = x;
}
private int generation;
private int cur_best;
File outputfile = new File("e:/MyApp/mydata/迭代过程.txt");
private class genotype
{
double[] gene = new double[NVARS];
double[] upper = new double[NVARS];
double[] lower = new double[NVARS];
double fitness,rfitness,cfitness;
}
genotype[] population = new genotype[POPSIZE+1];
genotype[] newpopulation = new genotype[POPSIZE+1];
public static void main(String[] args)
{
GA ga = new GA ();
ga.generation = 0;
ga.initialize();
ga.evaluate();
ga.keep_the_best();
while(ga.generation<ga.MAXGENES)
{
ga.generation++;
ga.select();
ga.crossover();
ga.mutate();
ga.report();
ga.evaluate();
ga.elitist();
}
}
public void initialize()
{
ArrayList <String> mylist = new ArrayList <String> ();
try
{
File myfile = new File("e:/MyApp/mydata/参数取值上下限.txt");
FileReader filereader = new FileReader(myfile);
BufferedReader reader = new BufferedReader(filereader);
String line = null;
while((line = reader.readLine())!= null)
{
mylist.add(line);
}
reader.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
double[][] VarRange = new double[NVARS][2];
for(int i=0;i<NVARS;i++)
{
String[] result = mylist.get(i).split("[\\p{Space}]+");
for(int j=0;j<2;j++)
{
VarRange[i][j] = Double.parseDouble(result[j]);
}
}
/*数组population与newpopulation中带有引用变量,创建genotype对象*/
for(int n=0;n<POPSIZE+1;n++)
{
population[n] = new genotype();
newpopulation[n] = new genotype();
}
for(int j = 0; j < POPSIZE; j++)
{
population[j].fitness = 0.0;
population[j].rfitness = 0.0;
population[j].cfitness = 0.0;
for(int i = 0; i < NVARS; i++)
{
population[j].lower[i] = VarRange[i][0];
population[j].upper[i] = VarRange[i][1];
population[j].gene[i] = randval(population[j].lower[i],population[j].upper[i]);
}
}
}
public void evaluate()
{
double[] VAR = new double[NVARS+1];
for(int mem=0;mem<POPSIZE;mem++)
{
for(int i=0;i<NVARS;i++)
{
VAR[i+1] = population[mem].gene[i];
}
population[mem].fitness = VAR[1]*VAR[1]-VAR[1]*VAR[2]+VAR[3];
}
}
public void keep_the_best()
{
cur_best = 0;
for (int mem = 0; mem < POPSIZE; mem++)
{
if (population[mem].fitness > population[POPSIZE].fitness)
{
cur_best = mem;
population[POPSIZE].fitness = population[mem].fitness;
}
}
for (int i = 0; i < NVARS; i++)
{
population[POPSIZE].gene[i] = population[cur_best].gene[i];
}
}
public void select()
{
double p,sum = 0;
for (int mem = 0; mem < POPSIZE; mem++)
{
sum = sum + population[mem].fitness;
}
for (int mem = 0; mem < POPSIZE; mem++)
{
population[mem].rfitness = population[mem].fitness/sum;
}
population[0].cfitness = population[0].rfitness;
for (int mem = 1; mem < POPSIZE; mem++)
{
population[mem].cfitness = population[mem-1].cfitness + population[mem].rfitness;
}
for (int i = 0; i < POPSIZE; i++)
{
p = Math.random();
if (p < population[0].cfitness)
{
newpopulation[i] = population[0];
}
else
{
for (int j = 0; j < POPSIZE; j++)
{
if (p >= population[j].cfitness && p < population[j+1].cfitness)
{
newpopulation[i] = population[j+1];
}
}
}
}
for (int i = 0; i < POPSIZE; i++)
{
population[i] = newpopulation[i];
}
}
public void crossover()
{
double x;
int one=0,count=0;
for(int mem=0;mem<POPSIZE;mem++)
{
x = Math.random();
if(x<PXOVER)
{
count++;
if(count%2==0)
Xover(one,mem);
else
one = mem;
}
}
}
public void Xover(int one,int two)
{
int point = (int)(Math.random()*(NVARS-1)+1);
for(int i=0;i<point;i++)
{
double temp = 0;
temp = population[one].gene[i];
population[one].gene[i] = population[two].gene[i];
population[two].gene[i] = temp;
}
}
public void mutate()
{
double lbound,hbound;
for (int i = 0; i < POPSIZE; i++)
{
for (int j = 0; j < NVARS; j++)
{
double p = Math.random();
if(p < PMUTATION )
{
lbound = population[i].lower[j];
hbound = population[i].upper[j];
population[i].gene[j] = randval(lbound,hbound);
}
}
}
}
public void elitist()
{
int min_mem,max_mem;
double min_fitness,max_fitness;
min_mem=0;
min_fitness=population[0].fitness;
max_mem=0;
max_fitness=population[0].fitness;
for (int i = 0; i < POPSIZE - 1; i++)
{
if(population[i].fitness < population[i+1].fitness)
{
if (population[i].fitness <= min_fitness)
{
min_fitness = population[i].fitness;
min_mem = i;
}
if (population[i+1].fitness >= max_fitness)
{
max_fitness = population[i+1].fitness;
max_mem = i + 1;
}
}
else
{
if (population[i].fitness >= max_fitness)
{
max_fitness = population[i].fitness;
max_mem = i;
}
if (population[i+1].fitness <= min_fitness)
{
min_fitness = population[i+1].fitness;
min_mem = i + 1;
}
}
}
if (max_fitness >= population[POPSIZE].fitness)
{
for (int i = 0; i < NVARS; i++)
{
population[POPSIZE].gene[i] = population[max_mem].gene[i];
population[POPSIZE].fitness = population[max_mem].fitness;
}
}
else
{
for (int i = 0; i < NVARS; i++)
{
population[min_mem].gene[i] = population[POPSIZE].gene[i];
population[min_mem].fitness = population[POPSIZE].fitness;
}
}
}
public void report()
{
try
{
FileWriter filewriter = new FileWriter(outputfile,true);
BufferedWriter writer = new BufferedWriter(filewriter);
writer.write("代数"+"\t"+"变量"+"\t"+"最优取值"+"\r\n");
for(int i=0;i<NVARS;i++)
{
writer.write(generation+"\t"+(i+1)+"\t"+population[POPSIZE].gene[i]+"\r\n");
}