源程序如下:
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
import java.io.*;
public class GeneticAlgorithm {
static int POPSIZE = 5; /* population size */
static int MAXGENS = 5; /* max. number of generations */
static int NVARS = 3; /* no. of problem variables */
static float PXOVER = 0.8f; /* probability of crossover */
static float PMUTATION = 0.3f; /* probability of mutation */
int E = 6;
int T = 120;
int generation; /* current generation no. */
int cur_best; /* best individual */
BufferedReader infile;
FileReader in;
BufferedWriter outfile;
FileWriter out;
Genotype population[];
Genotype newpopulation[];
int p[][][] = { { {0, 1, 1}, {0, 0, 0}, {0, 1, 1}, {0, 0, 0}
}, { {1, 0, 0}, {0, 0, 0}, {1, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 1, 1}, {0, 0, 0}, {0, 1, 1}
}, { {0, 0, 0}, {1, 0, 0}, {0, 0, 0}, {1, 0, 0}
}
};
double a[][][] = { { {10, 1, 1}, {10, 2, 4}, {1, 2.3, 1}, {3, 5, 1}
}, { {1, 3, 0.8}, {2, 3, 1.5}, {1, 1, 2}, {2, 0.7, 4}
}, { {2.5, 1.2, 3.4}, {10, 1, 1}, {1, 0.8, 13}, {1.9, 1, 1}
}, { {0.4, 3, 2.3}, {1, 2.1, 4}, {3.2, 1, 0.5}, {1, 1.4, 1.6}
}
};
double u[][][] = { { {2, 1, 1}, {3, 2, 4}, {1, 2.3, 1}, {3, 2, 1}
}, { {1, 3, 0.8}, {2, 3, 1.5}, {1, 1, 0.9}, {2, 0.7, 4}
}, { {0.7, 1.2, 3.4}, {0.5, 1, 1}, {1, 0.8, 0.9}, {1.9, 1, 1}
}, { {0.4, 3, 2.3}, {1, 2.1, 4}, {0.4, 1, 0.3}, {1, 1.4, 0.8}
}
};
double s[][][] = { { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}
};
GeneticAlgorithm() {
population = new Genotype[POPSIZE + 1]; /* population */
newpopulation = new Genotype[POPSIZE + 1]; /* new population; */
for (int i = 0; i <= POPSIZE; i++) {
population[i] = new Genotype();
newpopulation[i] = new Genotype();
}
}
class Genotype /* genotype (GT), a member of the population */
{
double gene[];
double fitness;
double upper[];
double lower[];
double cfitness;
double rfitness;
Genotype() {
gene = new double[NVARS]; /* a string of variables */
fitness = 0; /* GT's fitness */
upper = new double[NVARS];
lower = new double[NVARS]; /* GT's variables lower bound */
rfitness = 0;
cfitness = 0;
}
}
void initialize() {
String str;
String parts[];
try {
in = new FileReader("gadata.txt");
infile = new BufferedReader(in);
int i, j;
double lbound, ubound;
while ((str = infile.readLine()) != null) {
parts = str.split(" ");
for (i = 0; i < NVARS; i++) {
lbound = Double.parseDouble(parts[0]);
ubound = Double.parseDouble(parts[1]); for (j = 0; j < POPSIZE; j++) {
population[j].fitness = 0;
population[j].rfitness = 0;
population[j].cfitness = 0;
population[j].lower[i] = lbound;
population[j].upper[i] = ubound;
population[j].gene[i] = randval(population[j].lower[i],
population[j].upper[i]);
}
}
}
infile.close();
} catch (IOException e1) {
System.out.println("Can not open input file!");
System.exit(0);
}
}
double randval(double low, double high) {
double val;
val = (double) java.lang.Math.random() * (high - low) + low;
return (val);
}
void display() {
int i, j;
for (j = 0; j <= POPSIZE; j++) {
for (i = 0; i < NVARS; i++) {
System.out.print(population[j].gene[i] + "\t");
}
System.out.print(" " + population[j].fitness);
System.out.print("\n");
}
System.out.print("\n");
} void evaluate() {
int mem;
int i, j, k;
double t[] = new double[NVARS + 1]; double sum = 0;
double min = 0; for (mem = 0; mem < POPSIZE; mem++) {
sum = 0;
min = 0;
for (i = 0; i < NVARS; i++) {
t[i] = population[mem].gene[i];
sum += t[i];
}
t[3] = T - sum;
if (t[3] < E || t[3] > (T - 3 * E)) {
population[mem].fitness = 0;
continue;
}
for (i = 1; i < 5; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 3; k++) {
if (s[i - 1][j][k] + a[i - 1][j][k] * t[i - 1] >=
p[i - 1][j][k] * u[i - 1][j][k] * t[i - 1]) {
s[i][j][k] = s[i - 1][j][k] + a[i - 1][j][k] * t[i -
1] - p[i - 1][j][k] * u[i -
1][j][k] * t[i - 1];
} else {
s[i][j][k] = 0;
}
}
}
}
for (j = 0; j < 4; j++) {
for (k = 0; k < 3; k++) {
//s[0][j][k]=s[4][j][k];
min += s[4][j][k];
}
}
population[mem].fitness = 10000 - min;
}
}
void keep_the_best() {
int mem;
int i;
cur_best = 0; /* stores the index of the best individual */ for (mem = 0; mem < POPSIZE; mem++) {
if (population[mem].fitness > population[POPSIZE].fitness) {
cur_best = mem;
population[POPSIZE].fitness = population[mem].fitness;
}
}
/* once the best member in the population is found, copy the genes */
for (i = 0; i < NVARS; i++) {
population[POPSIZE].gene[i] = population[cur_best].gene[i];
} }
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
import java.io.*;
public class GeneticAlgorithm {
static int POPSIZE = 5; /* population size */
static int MAXGENS = 5; /* max. number of generations */
static int NVARS = 3; /* no. of problem variables */
static float PXOVER = 0.8f; /* probability of crossover */
static float PMUTATION = 0.3f; /* probability of mutation */
int E = 6;
int T = 120;
int generation; /* current generation no. */
int cur_best; /* best individual */
BufferedReader infile;
FileReader in;
BufferedWriter outfile;
FileWriter out;
Genotype population[];
Genotype newpopulation[];
int p[][][] = { { {0, 1, 1}, {0, 0, 0}, {0, 1, 1}, {0, 0, 0}
}, { {1, 0, 0}, {0, 0, 0}, {1, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 1, 1}, {0, 0, 0}, {0, 1, 1}
}, { {0, 0, 0}, {1, 0, 0}, {0, 0, 0}, {1, 0, 0}
}
};
double a[][][] = { { {10, 1, 1}, {10, 2, 4}, {1, 2.3, 1}, {3, 5, 1}
}, { {1, 3, 0.8}, {2, 3, 1.5}, {1, 1, 2}, {2, 0.7, 4}
}, { {2.5, 1.2, 3.4}, {10, 1, 1}, {1, 0.8, 13}, {1.9, 1, 1}
}, { {0.4, 3, 2.3}, {1, 2.1, 4}, {3.2, 1, 0.5}, {1, 1.4, 1.6}
}
};
double u[][][] = { { {2, 1, 1}, {3, 2, 4}, {1, 2.3, 1}, {3, 2, 1}
}, { {1, 3, 0.8}, {2, 3, 1.5}, {1, 1, 0.9}, {2, 0.7, 4}
}, { {0.7, 1.2, 3.4}, {0.5, 1, 1}, {1, 0.8, 0.9}, {1.9, 1, 1}
}, { {0.4, 3, 2.3}, {1, 2.1, 4}, {0.4, 1, 0.3}, {1, 1.4, 0.8}
}
};
double s[][][] = { { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}
};
GeneticAlgorithm() {
population = new Genotype[POPSIZE + 1]; /* population */
newpopulation = new Genotype[POPSIZE + 1]; /* new population; */
for (int i = 0; i <= POPSIZE; i++) {
population[i] = new Genotype();
newpopulation[i] = new Genotype();
}
}
class Genotype /* genotype (GT), a member of the population */
{
double gene[];
double fitness;
double upper[];
double lower[];
double cfitness;
double rfitness;
Genotype() {
gene = new double[NVARS]; /* a string of variables */
fitness = 0; /* GT's fitness */
upper = new double[NVARS];
lower = new double[NVARS]; /* GT's variables lower bound */
rfitness = 0;
cfitness = 0;
}
}
void initialize() {
String str;
String parts[];
try {
in = new FileReader("gadata.txt");
infile = new BufferedReader(in);
int i, j;
double lbound, ubound;
while ((str = infile.readLine()) != null) {
parts = str.split(" ");
for (i = 0; i < NVARS; i++) {
lbound = Double.parseDouble(parts[0]);
ubound = Double.parseDouble(parts[1]); for (j = 0; j < POPSIZE; j++) {
population[j].fitness = 0;
population[j].rfitness = 0;
population[j].cfitness = 0;
population[j].lower[i] = lbound;
population[j].upper[i] = ubound;
population[j].gene[i] = randval(population[j].lower[i],
population[j].upper[i]);
}
}
}
infile.close();
} catch (IOException e1) {
System.out.println("Can not open input file!");
System.exit(0);
}
}
double randval(double low, double high) {
double val;
val = (double) java.lang.Math.random() * (high - low) + low;
return (val);
}
void display() {
int i, j;
for (j = 0; j <= POPSIZE; j++) {
for (i = 0; i < NVARS; i++) {
System.out.print(population[j].gene[i] + "\t");
}
System.out.print(" " + population[j].fitness);
System.out.print("\n");
}
System.out.print("\n");
} void evaluate() {
int mem;
int i, j, k;
double t[] = new double[NVARS + 1]; double sum = 0;
double min = 0; for (mem = 0; mem < POPSIZE; mem++) {
sum = 0;
min = 0;
for (i = 0; i < NVARS; i++) {
t[i] = population[mem].gene[i];
sum += t[i];
}
t[3] = T - sum;
if (t[3] < E || t[3] > (T - 3 * E)) {
population[mem].fitness = 0;
continue;
}
for (i = 1; i < 5; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 3; k++) {
if (s[i - 1][j][k] + a[i - 1][j][k] * t[i - 1] >=
p[i - 1][j][k] * u[i - 1][j][k] * t[i - 1]) {
s[i][j][k] = s[i - 1][j][k] + a[i - 1][j][k] * t[i -
1] - p[i - 1][j][k] * u[i -
1][j][k] * t[i - 1];
} else {
s[i][j][k] = 0;
}
}
}
}
for (j = 0; j < 4; j++) {
for (k = 0; k < 3; k++) {
//s[0][j][k]=s[4][j][k];
min += s[4][j][k];
}
}
population[mem].fitness = 10000 - min;
}
}
void keep_the_best() {
int mem;
int i;
cur_best = 0; /* stores the index of the best individual */ for (mem = 0; mem < POPSIZE; mem++) {
if (population[mem].fitness > population[POPSIZE].fitness) {
cur_best = mem;
population[POPSIZE].fitness = population[mem].fitness;
}
}
/* once the best member in the population is found, copy the genes */
for (i = 0; i < NVARS; i++) {
population[POPSIZE].gene[i] = population[cur_best].gene[i];
} }
而且程序写的很不规范,没有详细的注释,比如
int E = 6;
int T = 120;
别人很难看懂的。
你可以介绍一下整个程序的流程(当然最好能介绍点遗传的基本知识)
再做下详细的注释,或许就有很多人帮你了!
E是一个定值..T是一个约束条件.
程序要实现.t1+t2+t3+t4=T 且每个 E=< t <=T-3*E 且使得
F=10000 - min最大, 目的是要使min最小,min是根据题目设定的一个函数...用遗传算法来做,通过模拟生物进化来选择最优,可以更快地找到最优解.
主要通过选择、交叉、变异、保留已得出的最优解、计算适应值。如此循环往复,一步步逼进
最佳解。其实在数学建模的matlab里有这样的工具包,但的确是有点难懂。这个程序我是从c++语言改过来的,我仔细对过,硬是找不出有什么不对。但就是不出现预想的结果!
应该用Random类,rd.nextDouble();