📄 gaapopulation.java
字号:
if (vals[0] < lastVal) {
stagnationCounter = 0;
degradeCounter = 0;
}
else {
stagnationCounter++;
}
}
else {
if (vals[0] > lastVal) {
stagnationCounter = 0;
degradeCounter = 0;
}
else {
stagnationCounter++;
}
}
}
public void kick() {
int i;
problem.function.preBreed();
for (i=0;i<popSize;i++) {
currentId = i;
if (GaaMisc.flip((double) problem.kickDistribution)) {
chroms[i] = problem.mutation.mutation(chroms[i],problem.shuffleRate);
vals[i] = problem.function.getValue(chroms[i]);
totalShuffles++;
//GaaMisc.dbg("Kick/Shuffle. Total = "+totalShuffles);
}
else {
chroms[i] = problem.inversion.inversion(chroms[i], problem.inversionShuffle);
vals[i] = problem.function.getValue(chroms[i]);
totalInversions++;
//GaaMisc.dbg("Kick/Inversion. Total= "+totalInversions);
}
}
problem.function.postBreed();
stagnationCounter = 0;
degradeCounter = 0;
}
private void preScale() {
/*
newFit = A*oldFit + B
Requirements:
A*avg+B=avg
A*max+B=Scale*avg
A*min+B>0
A*min > -B
B=avg*(1-A)
A*min > avg(A-1)
min > avg(1 - 1/A)
A*max + avg - A*avg = Scale*avg
A*(max-avg) = avg*(Scale-1)
A = avg*(Scale-1) / (max-avg)
a_coeff = avg * (Scale - 1) / (max - avg)
b_coeff = avg * (1 - a_coeff)
*/
int i;
double tmin = 10e35;
double tmax = -10e35;
double tsum = 0;
double fitness;
double factor = 1.80;
double a_coeff = 1;
double b_coeff = 0;
double delta = 0;
double tavg = 0;
double temp = 0;
double lowest = 0;
sortChroms();
for (i=0;i<popSize;i++) {
tsum += fits[i];
}
tmax = fits[0];
tmin = fits[popSize-1];
tavg = tsum/popSize;
temp = (tmax+tmin)/2;
/*
deb.debug("Generation #" + generation);
deb.debug("tsum= "+tsum+" size = "+popSize);
deb.debug("tmax= "+tmax+" tmin = "+tmin+" tavg= "+tavg);
deb.debug("factor= "+factor+" a = "+a_coeff+" b= "+b_coeff);
double a = (factor * tavg - tmax) / (factor - 1);
boolean b = (tmin > (factor * tavg - tmax) / (factor - 1));
deb.debug("factor * tavg - tmax) / (factor - 1)= " + a+" tmin= "+tmin+" >= "+b);
*/
if (factor > 1) {
if (tmin > (factor * tavg - tmax) / (factor - 1)) {
delta = tmax - tavg;
if (delta != 0) {
a_coeff = (factor - 1) * tavg / delta;
b_coeff = tavg * (tmax - factor * tavg) / delta;
}
}
else {
delta = tavg - tmin;
if (delta != 0) {
a_coeff = tavg / delta;
b_coeff = -tmin * tavg / delta;
}
}
}
//deb.debug("After. delta= "+delta+" factor= "+factor+" a = "+a_coeff+" b= "+b_coeff);
//deb.debug("Before First = "+fits[0]+" (Last-1)= "+fits[popSize-2]);
for (i=0;i<popSize;i++) {
fits[i] = (double) (fits[i]*a_coeff + b_coeff);
lowest = fits[i];
}
if (Math.abs(lowest)<1)
lowest = 1;
if (lowest == 0)
lowest = fits[popSize-2];
for (i=0;i<popSize;i++) {
fits[i] = fits[i]/lowest;
}
//deb.debug("After First = "+fits[0]+" (Last-1)= "+fits[popSize-2]);
//deb.debug(" ");
}
public void sortChroms() {
sortChroms(chroms,fits,vals,true);
}
public void sortChromsByVals() {
if (problem.minmaxType == 2)
sortChroms(chroms,vals,fits,false);
else
sortChroms(chroms,vals,fits,true);
}
public void sortKidsByVals() {
if (problem.minmaxType == 2)
sortChroms(kids,kidVals,kidFits,false);
else
sortChroms(kids,kidVals,kidFits,true);
}
public void sortChroms (String[] list, double values[], double secvals[], boolean descending) {
int inc, comp;
if (descending)
comp = -1;
else
comp = 1;
for (inc = 1; inc <= list.length / 9; inc = 3 * inc + 1) ;
for ( ; inc > 0; inc /= 3){
for (int i = inc + 1; i <= list.length; i += inc){
double v = values[i - 1];
double s = secvals[i - 1];
String l = list[i - 1];
int j = i;
while ((j > inc) && (GaaMisc.compareDouble(values[j - inc - 1],v) == comp))
{
values[j - 1] = values[j - inc - 1];
secvals[j - 1] = secvals[j - inc - 1];
list[j - 1] = list[j - inc - 1];
j -= inc;
}
values[j - 1] = v;
secvals[j - 1] = s;
list[j - 1] = l;
}
}
}
public void sortChromsByInsertion (String[] list, double values[], boolean descending) {
int inc, comp;
if (descending)
comp = -1;
else
comp = 1;
for (int i=1;i<list.length;++i) {
double t = values[i];
String s = list[i];
int j = i;
while ((j>0) && (GaaMisc.compareDouble(values[j-1],t) == comp)) {
list[j] = list[j-1];
values[j] = values[j-1];
--j;
}
values[j] = t;
list[j] = s;
}
}
public void sortDoubles (double values[], boolean descending) {
int inc, comp;
if (descending)
comp = -1;
else
comp = 1;
for (inc = 1; inc <= values.length / 9; inc = 3 * inc + 1) ;
for ( ; inc > 0; inc /= 3){
for (int i = inc + 1; i <= values.length; i += inc){
double v = values[i - 1];
int j = i;
while ((j > inc) && (GaaMisc.compareDouble(values[j - inc - 1],v) == comp))
{
values[j - 1] = values[j - inc - 1];
j -= inc;
}
values[j - 1] = v;
}
}
}
public double getDoublesMax(double values[]) {
double result;
sortDoubles(values,true);
result = values[0];
return(result);
}
public String poplistTableString() {
int i;
String txt = "";
try {
sortChromsByVals();
txt = "\nCurrent Population (with calculated (V) and fitness (F) values)\n\n";
for (i=0;i<problem.popSize;i++) {
txt = txt + chroms[i] + " V: " + dFormat.format(vals[i]) + " F: " + dFormat.format(fits[i])+"\n";
}
}
catch (Exception e) {
}
return(txt);
}
public String poplistTableString(int mode) {
int i;
String txt = "";
try {
sortChromsByVals();
txt = "\n\n";
txt += DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG).format(new Date());
txt += "\nPopulation List, Generation #" + generation+"\n\n";
for (i=0;i<problem.popSize;i++) {
if (mode == 1)
txt += chroms[i] + " V: " + dFormat.format(vals[i]) + " F: " + dFormat.format(fits[i])+"\n";
else if (mode == 2)
txt += "#" + GaaMisc.f06.format(i) + " Value: " + dFormat.format(vals[i]) + " Fitness: " + dFormat.format(fits[i])+"\n";
else if (mode == 3)
txt += "#" + GaaMisc.f06.format(i) + " Value: " + dFormat.format(vals[i]) + " Fitness: " + dFormat.format(fits[i])+" String: " + chroms[i] + "\n";
else if (mode == 4) {
txt += "#" + GaaMisc.f06.format(i) +" String: " + chroms[i] + "\n";
txt += "#" + GaaMisc.f06.format(i) + " Value: " + dFormat.format(vals[i]) + " Fitness: " + dFormat.format(fits[i]) + "\n";
txt += "\n";
}
}
}
catch (Exception e) {
}
txt += "=============================================";
return(txt);
}
public String popStatusString() {
int i;
String txt = "";
try {
sortChromsByVals();
txt = "\n\n";
txt += DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG).format(new Date());
txt += "\n";
txt += "Current Status:\n\n";
txt += "Generation: " + generation+ "\n";
txt += "Best Chromosome: " + chroms[0]+ "\n";
txt += "Best Value: " + vals[0]+ "\n";
}
catch (Exception e) {
}
txt += "=============================================";
return(txt);
}
public String poplistTableKids() {
int i;
String txt = "";
try {
sortKidsByVals();
txt = "\nCurrent Kids (with calculated (V) and fitness (F) values)\n\n";
for (i=0;i<problem.popSize;i++) {
txt = txt + kids[i] + " \tV: " + dFormat.format(kidVals[i]) + " \t\tF: " + dFormat.format(kidFits[i])+ "\n";
}
}
catch (Exception e) {
}
return(txt);
}
public void updateMemory(int num, int pos, char kar) {
try {
StringBuffer sb = new StringBuffer(memory[num]);
sb.setCharAt(pos,kar);
memory[num] = sb.toString();
}
catch (Exception e) {
String s = e.toString();
System.out.println(s);
pos=pos;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -