📄 gaafunction.java
字号:
case 8:
x = 0;
y = 0;
x1 = 0;
x2 = 0;
x3 = 0;
x4 = 0;
j = problem.genesNumber;
k = (int) (j/4);
for (i=0;i<k;i++) {
x1 += problem.alleles[4*i].value;
x2 += problem.alleles[4*i+1].value;
x3 += problem.alleles[4*i+2].value;
x4 += problem.alleles[4*i+3].value;
}
x1 = x1/k;
x2 = x2/k;
x3 = x3/k;
x4 = x4/k;
x = Math.abs(100*(x2-x1*x1)
+(1-x1)*(1-x1)
+90*(x4-x3*x3)*(x4-x3*x3)
+(1-x3)*(1-x3)
+10.1*((x2-1)*(x2-1)+(x4-1)*(x4-1))
+19.9*(x2-1)*(x4-1));
txt = "x1: "+f3.format(x1)+" x2: "+f3.format(x2)+" x3: "+f3.format(x3)+" x4: "+f3.format(x4)+" f(x): "+f3.format(x);
break;
case 9:
x = 0;
y = 0;
x1 = 0;
x2 = 0;
x3 = 0;
x4 = 0;
x5 = 0;
j = problem.genesNumber;
n = 5;
k = (int) (j/n);
for (i=0;i<k;i++) {
x1 += problem.alleles[n*i].value;
x2 += problem.alleles[n*i+1].value;
x3 += problem.alleles[n*i+2].value;
x4 += problem.alleles[n*i+3].value;
x5 += problem.alleles[n*i+4].value;
}
x1 = x1/k;
x2 = x2/k;
x3 = x3/k;
x4 = x4/k;
x5 = x5/k;
x = Math.abs(x1 * Math.sin(x1)
+ 1.7 * x2 * Math.sin(x1)
- 1.5 * x3 - 0.1 * x4 * Math.cos(x4 + x5 - x1)
+ (0.2 * x5 * x5 - x2) - 1);
txt = "x1: "+f0.format(x1)+" x2: "+f0.format(x2)+" x0: "+f0.format(x3)+" x4: "+f0.format(x4)+" x5: "+f0.format(x5)+" f(x): "+f7.format(x);
break;
case 19:
txt = "Target (Waste / Bins): "+f0.format(problem.exitValue) + " / 48 Current Best: "+f0.format(problem.currentBestVal) + " / " + f0.format(problem.currentMin);
break;
case 20:
txt = "Target (Waste / Bins): "+f0.format(problem.exitValue) + " / 20 Current Best: "+f0.format(problem.currentBestVal) + " / " + f0.format(problem.currentMin);
break;
case 21:
case 22:
case 23:
//x1 = getValue(chrom);
x1 = pop.vals[0];
problem.currentBestChrom = chrom;
problem.currentBestVal = x1;
txt = "Strategy: "+problem.currentBestChrom;
txt += " Profit: "+ f3.format(problem.currentBestVal);
break;
default:
txt = "";
}
//GaaAction.deb.debug("result = "+result);
return(txt);
}
public String getDecodedChrom(String chrom) {
double value = getValue(chrom);
problem.alleleSet.decodeChrom(chrom);
txt = "";
switch (problem.problemCode) {
case 2:
case 4:
txt = problem.problemTitle + " - Solution:\n";
txt += "Chromosome: <"+chrom+">\nLength of tour: "+f3.format(value)+"\n\n";
for (i=0;i<problem.genesNumber;i++) {
try {
x = problem.alleles[i].getValue();
txt += "Point #"+f03.format(i)+" City number: "+f03.format(x)+" - ";
if (problem.mapOrder > 0) {
for (j=0;j<problem.mapOrder;j++) {
if (j == 0)
txt += " x: "+f3.format(problem.mapAlleles[(int)x][j])+" , ";
else if (j == 1)
txt += " y: "+f3.format(problem.mapAlleles[(int)x][j])+"\n";
}
}
}
catch (Exception e) {
}
}
break;
case 6:
txt = problem.problemTitle + " - Solution:\n";
txt += "Chromosome: <"+chrom+">\nRtotal weighted connections length: "+f3.format(value)+"\n\n";
break;
case 16:
case 17:
case 18:
txt = problem.problemTitle + " - Solution:\n";
txt += "Chromosome: <"+chrom+">\n";
txt += "Value: "+f3.format(value)+"\n";
if (problem.problemCode > 16)
txt += "Known optimum: " + f3.format(problem.exitValue)+"\n\n";
for (i=0;i<problem.genesNumber;i++) {
try {
x = problem.alleles[i].getValue();
txt += "Object #"+f03.format(i);
if ((int)x == 1)
txt += " +Y, ";
else
txt += " -N,";
if (problem.mapOrder > 0) {
for (j=0;j<problem.mapOrder;j++) {
if (j == 0)
txt += " Value: "+f3.format(problem.mapAlleles[i][j])+" , ";
else if (j == 1)
txt += " Weight: "+f3.format(problem.mapAlleles[i][j])+"\n";
}
}
}
catch (Exception e) {
}
}
break;
case 19:
case 20:
txt += "Bin Packing Result\n\n";
problem.alleleSet.decodeChrom(chrom);
x = 0;
y = 0;
x1 = 0;
z = 0;
u = 0;
v = 0;
j = 0;
if (problem.problemCode == 19)
z = 150.0;
else
z = 100.0;
n = problem.genesNumber;
i = 0;
flag = true;
xi = new double[n];
for (i=0;i<n;i++) {
if (flag) {
x1++;
flag = false;
}
u = problem.alleles[i].value;
j = (int) problem.alleles[i].value;
v = problem.mapAlleles[j][0];
xi[i] = problem.mapAlleles[j][0];
x += xi[i];
if (x > z) {
y += (z-(x-xi[i]));
txt += "Bin #" + GaaMisc.f0.format(x1) + " Wasted space: " + GaaMisc.f1.format((z-(x-xi[i])))+"\n\n";
GaaMisc.dbg("");
x = xi[i];
txt += "" + i + " Object #" + j + " Weight= " + GaaMisc.f1.format(xi[i])+" Acc. Weight= "+GaaMisc.f1.format(x)+"\n";
flag = true;
}
else
txt += "" + i + " Object #" + j + " Weight= " + GaaMisc.f1.format(xi[i])+" Acc. Weight= "+GaaMisc.f1.format(x)+"\n";
}
txt += "Bin #" + GaaMisc.f0.format(x1) + " Wasted space: " + GaaMisc.f1.format((z-x))+"\n\n\n";
break;
case 21:
case 22:
case 23:
x1 = getValue(chrom);
problem.alleleSet.decodeChrom(chrom);
txt += "\nStrategy: "+chrom+"\n";
txt += "Average profit: "+f3.format(x1)+"\n\n";
for (i=0;i<problem.genesNumber;i++) {
try {
x = problem.alleles[i].getValue();
txt += "Rule #"+f02.format(i)+" Condition: "+txts[i]+" Move: "+f0.format(x)+"\n";
}
catch (Exception e) {
}
}
break;
default:
z = 0;
txt += "Chromosome: <"+chrom+">\nValue: "+f3.format(value)+"\n\n";
for (i=0;i<problem.genesNumber;i++) {
try {
x = problem.alleles[i].getValue();
txt += "Parameter #"+i+" Value: "+f3.format(x)+"\n";
z += x;
if (problem.mapOrder > 0) {
for (j=0;j<problem.mapOrder;j++) {
txt += " Mapped Parameter #"+j+" Value: "+f3.format(problem.mapAlleles[(int)x][j])+"\n";
}
}
}
catch (Exception e) {
}
}
j = problem.genesNumber;
m = problem.variablesNumber;
k = (int) (j/m);
if (k > 1) {
if (m == 1) {
z = (z/problem.genesNumber);
txt += "\nAverage value: "+z+"\n";
}
}
}
return(txt);
}
public double getSpecialValue(String chrom, int num) {
problem.alleleSet.decodeChrom(chrom);
switch (problem.problemCode) {
case 6:
x = 0;
y = 0;
n = problem.genesNumber;
k = (int) (n/2);
for (i=0;i<k;i++) {
l = 2*i;
m = l+1;
x += problem.alleles[l].value;
y += problem.alleles[m].value;
}
x = x/k;
y = y/k;
if (num == 1)
result = x;
else if (num == 2)
result = y;
else
result = -111111;
break;
default:
result = -11111;
}
//GaaAction.deb.debug("result = "+result);
return(result);
}
private void displayCurrentValues(Graphics g) {
int ii;
problem.alleleSet.decodeChrom(problem.currentBestChrom);
n = problem.genesNumber;
m = problem.variablesNumber;
k = (int) (j/m);
ii = 1;
if ((n<21) && (k<2)) {
if (n > 10) {
m = (int) (n/2);
for (ii=0;ii<m;ii++) {
j = 2*ii;
k = j+1;
xi[j] = problem.alleles[j].value;
g.drawString("x["+j+"]="+f7.format(xi[j]),20,15*(ii+1));
xi[k] = problem.alleles[k].value;
g.drawString("x["+k+"]="+f7.format(xi[k]),150,15*(ii+1));
}
}
else {
for (ii=0;ii<n;ii++) {
xi[ii] = problem.alleles[ii].value;
g.drawString("x["+ii+"]="+f7.format(xi[ii]),20,15*(ii+1));
}
}
}
g.setColor(Color.red);
g.drawString("f(x)="+f7.format(problem.currentBestVal),20,(15*(ii+2)));
g.drawString("Current generation: "+f0.format(problem.currentGeneration)+" Function Calls: "+f0.format(problem.functionCalls),20,(15*(ii+3)));
g.setColor(Color.black);
}
public void preBreed() {
switch (problem.problemCode) {
case 21:
/*
if ((!problem.inputString1.equals("")) && (problem.udfDouble > 0)) {
for (i=0;i<pop.popSize;i++) {
if (GaaMisc.flip(problem.udfDouble)) {
pop.chroms[i] = problem.inputString1;
}
}
}
*/
default:
break;
}
}
public void postBreed() {
switch (problem.problemCode) {
case 21:
case 22:
case 23:
pop.sortChromsByVals();
pop.bestChrom = pop.chroms[0];
pop.bestVal = pop.vals[0];
problem.currentBestChrom = pop.bestChrom;
problem.currentBestVal = pop.bestVal;
default:
break;
}
}
Hashtable IpdMemory() {
String[] mems;
StringBuffer sb;
Hashtable hash;
int num;
char kars[] = {'0','1'};
int nkars = kars.length;
num = (int) Math.pow(nkars,problem.mapOrder)+1;
mems = new String[num];
sb = new StringBuffer();
for (i=0;i<problem.mapOrder;i++)
sb.append('2');
for (i=0;i<num-1;i++) {
m = i;
for (j=0;j<problem.mapOrder;j++) {
k = m%nkars;
m = m/nkars;
sb.setCharAt(j,kars[k]);
}
mems[i] = sb.toString();
}
for (j=0;j<problem.mapOrder;j++) {
sb.setCharAt(j,'2');
}
mems[num-1] = sb.toString();
hash = new Hashtable();
for (i=0;i<num;i++) {
//System.out.println("#"+i+": "+mems[i]);
hash.put(mems[i],new Integer(i));
}
problem.genesNumber = num;
txts = mems;
return(hash);
}
String getPlayerMemory(int num) {
String mem;
String s2 = "2222222222";
int first2;
first2 = pop.memory[num].indexOf('2');
if ((first2 > -1) && (first2 < problem.mapOrder))
mem = s2.substring(0,problem.mapOrder);
else
mem = pop.memory[num].substring(0,problem.mapOrder);
return(mem);
}
double ipdFixed() {
GaaMisc.debugOn = true;
drawMode = 1;
result = 0;
String memory0;
String memory1;
int strat0;
int strat1;
int pid;
int debflag = 0;
int cid = pop.currentId;
int popsize = pop.popSize;
char kar;
double vals[] = pop.vals;
/*
if ((pop.vals[0]>3) && (pop.chroms[cid].charAt(16) == 'B'))
debflag = 3;
else
debflag = 0;
*/
//TFT,TF2T,2TFT,ALLD,ALLC
/*
String strats[] = {"ABABABABABABABABB",
"ABABBBBBABABBBBBB",
"AAAAABABAAAAABABA"};
*/
String strats[] = {"ABABABABABABABABB",
"ABABABABABABABABB",
"ABABABABABABABABB"};
int moves = 10;
try {
moves = Integer.parseInt(problem.udfString);
} catch (NumberFormatException e){}
if (pop.bestChrom.equals(""))
pop.bestChrom = pop.chroms[0];
if (pop.bestChrom.equals(""))
result = 0;
else {
x = 0;
y = 0;
l = problem.mapOrder;
GaaMisc.debug("Contestant No. "+cid+" Chrom: = "+pop.chroms[cid],0);
for (pid=0;pid<strats.length;pid++) {
//if (pid == 0)
// strats[pid] = problem.alleleSet.encodeRandomChrom(1);
for (int jj=0;jj<problem.mapOrder;jj++) {
pop.updateMemory(cid,jj,'2');
pop.updateMemory(pid,jj,'2');
}
for (m=0;m<moves;m++) {
memory0 = getPlayerMemory(cid);
problem.alleleSet.decodeChrom(pop.chroms[cid]);
strat0 = ((Integer)hashmem.get(memory0)).intValue();
k0 = (char)((int)(problem.alleles[strat0].value)+48);
memory1 = getPlayerMemory(pid);
problem.alleleSet.decodeChrom(strats[pid]);
strat1 = ((Integer)hashmem.get(memory1)).intValue();
k1 = (char)((int)(problem.alleles[strat1].value)+48);
if (problem.mapOrder > 2) {
for (n=2;n<problem.mapOrder;n++) {
pop.updateMemory(cid,n,pop.memory[cid].charAt(n-2));
pop.updateMemory(pid,n,pop.memory[pid].charAt(n-2));
}
}
pop.updateMemory(cid,0,k1);
pop.updateMemory(cid,1,k0);
pop.updateMemory(pid,0,k0);
pop.updateMemory(pid,1,k1);
oldx = x;
oldy = y;
if ((k0 == '1') && (k1 == '1')) {
y += 3;
x += 3;
}
else
if ((k0 == '1') && (k1 == '0')) {
x += 0;
y += 5;
}
else
if ((k0 == '0') && (k1 == '1')) {
x += 5;
y += 0;
}
else
if ((k0 == '0') && (k1 == '0')) {
y += 1;
x += 1;
}
t0 = "m0: "+memory0+" s0: "+strat0+" k0: "+k0+" dv0: "+(x-oldx)+" v0: "+x;
t1 = "m1: "+memory1+" s1: "+strat1+" k1: "+k1+" dv1: "+(y-oldy)+" v1: "+y;
t3 = "==============";
t3 += "\n" + "Generation: "+pop.generation+" Round: "+m+" "+cid+ " against "+pid;
t3 += "\n" + "n0: "+cid+" c0: "+pop.chroms[cid]+" "+t0;
t3 += "\n" + "n1: "+pid+" c1: "+strats[pid]+" "+t1;
//System.out.println(t3);
GaaMisc.debug(t3,debflag);
}
t3 = "=====================";
t3 += "\n" + "Generation: "+pop.generation+" Sum for "+cid+ " against "+pid;
t3 += "\n" + "n0: "+cid+" c0: "+pop.chroms[cid]+" v0: "+x;
t3 += "\n" + "n1: "+pid+" c1: "+strats[pid]+" v1: "+y;
//System.out.println(t3);
GaaMisc.debug(t3,debflag);
t4 = "Gen: "+pop.generation+" - "+cid+" against " + pid;
GaaApplet.statusLabel.setText(t4);
}
z = (double)(x/strats.length/moves);
try {
t3 = "============================";
t3 += "\n" + "Generation: "+pop.generation+" Id: "+cid+" (Strats Size: "+strats.length+" Moves: "+moves+")";
t3 += "\n" + pop.chroms[cid]+ " Average: "+f3.format(z)+" (Total: "+f1.format(x)+")";
//System.out.println(t3);
GaaMisc.debug(t3,debflag);
//t4 = "Gen: "+pop.generation+" Id: "+cid+" Ch: " + pop.chroms[cid]+ " Average: "+f3.format(z);
//GaaApplet.statusLabel.setText(t4);
//if (problem.withTextWindow)
//GaaAction.text.appendText(t3 + "\n");
if (problem.withLogging)
GaaMisc.dbg(t3);
}
catch (Exception e) {
}
result = z;
result = result;
}
return(result);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -