⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gaafunction.java

📁 Java实现的遗传算法工具集:GA Playground
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.lang.Math.*;
import java.awt.*;
import java.text.*;
import java.io.PrintStream;
import java.util.Hashtable;

public class GaaFunction {
		
	GaaProblem problem;
	GaaPopulation pop;
	GaaLog deb;
	int drawMode;
	double currentResult;
	double result;
	double xi[];
	char kars[];
	String txts[];
	
	int i,j,k,l,m,n,o,p,q;
	long l1,l2,l3,l4,l5,l6,l7,l8,l9,l10;
	double x=0,y=0,z=0,u=0,v=0,r=0,s=0;
	double x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10;
	double oldx=0,oldy=0,oldz=0;
	String txt,t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;	
	char k0,k1,k2,k3,k4,k5;
	boolean flag;
	boolean first = true;
	Hashtable hashmem;
	
	JelProcs jl;
	JelVariableProvider vp;
	
	DecimalFormat f0 = new DecimalFormat("0");
	DecimalFormat f02 = new DecimalFormat("00");
	DecimalFormat f03 = new DecimalFormat("000");
	
	DecimalFormat f1 = new DecimalFormat("0.0");
	DecimalFormat f2 = new DecimalFormat("0.00");
	DecimalFormat f3 = new DecimalFormat("0.000");
	DecimalFormat f4 = new DecimalFormat("0.0000");
	DecimalFormat f7 = new DecimalFormat("0.0000000");
	
	PrintStream out;

		
	public GaaFunction (GaaProblem pr) {
		
		problem = pr;
		deb = GaaMisc.deb;
		xi = new double[500];
		kars = new char[10];
		result = -11111111;
		n = 0;
		out = System.out;
			
	}
	
	public void setPop(GaaPopulation pl) {
		
		pop = pl;
		
	}
	
	
	public double getValue(String chrom) {
		
		problem.functionCalls++;
		problem.alleleSet.decodeChrom(chrom);
		
		switch (problem.problemCode) {
			case 1: //Simpleton
				drawMode = 2;
				result = 1;
				j = problem.genesNumber;
				k = (int) (Math.floor(j/2));
				for (i=0;i<j;i++) {
					n = (int)problem.alleles[i].value;
					if (i < k)
					   result = result * n;
					else
						 result = result / n;
				}
				break;
			case 2: //TespDemo
			case 4: //YspBayg29, TspAtt48
				//GaaAction.deb.debug("\nGet Fitness");
				drawMode = 1;
				result = 0;
				n = problem.genesNumber;
				for (i=0;i<problem.genesNumber;i++) {
					try {
						n = (int)problem.alleles[i].value;
						x = problem.mapAlleles[n][0];
						y = problem.mapAlleles[n][1];
						if (i > 0) {
							z = Math.sqrt((x-oldx)*(x-oldx) + (y-oldy)*(y-oldy));
							result += z;
						}
						//GaaAction.deb.debug("i= "+i+" x= "+x+" y= "+y+" z= "+z);
						oldx = x;
						oldy = y;
					}
					catch (Exception e) {
						txt = "GaaFunction GetValue Error\n" +
							e.toString() + "\n" +
							"n= " + n + "  x= " + (int)x + "  y= " + (int)y;
						GaaAction.deb.debug(txt);
					}
				}
				break;
			case 3: //GenFunc
				if (!GaaApplet.gaaAppMode.equals("Applet")) {
					drawMode = 1;
					result = 1;
					j = problem.genesNumber;
					m = problem.variablesNumber;
					k = (int) (j/m);
					//k = problem.redundancyFactor;
	
					for (i=0;i<j;i++) {
						xi[i+1] = problem.alleles[i].value;
					}
					try {
						if (k < 2) {
							jl.vp.initVariables(xi);
							try {
					    	result = jl.compiledExpr.evaluate_double(jl.rtp);
					    }
					    catch (Throwable e) {
					    	result = -12345;
					    }
					  }
					  else {
					  	result = 0;
					  	if (j%k != 0)
					  		result = -23456;
					  	else {
						  	m = j/k;
						  	for (i=1;i<k+1;i++) {
									jl.vp.initVariables(xi,m,i*m);
									try {
							    	result += jl.compiledExpr.evaluate_double(jl.rtp);
							    }
							    catch (Throwable e) {
							    	result = -12345;
							    }
							  }
							  result = result/k;
							}
					  }
					  	
					}
					catch (Exception e) {
						String t = e.toString();
					}
				}
				break;
			case 5:  //SingleVarMin
				drawMode = 1;
				result = 1;
				x = 0;
				j = problem.genesNumber;
				for (i=0;i<j;i++) {
					z = problem.alleles[i].value;
					x += Math.pow(z,4) - 12*Math.pow(z,3) + 15*Math.pow(z,2) + 56*z - 60;
				}
				result = x/j;	
				break;
			case 6:
				drawMode = 1;
				result = 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;
					for (j=0;j<n;j++) {
						x1 = problem.mapAlleles[j][0];
						x2 = problem.mapAlleles[j][1];
						x3 = problem.mapAlleles[j][2];
						x4 = x - x1;
						x5 = y - x2;
						x6 = Math.sqrt(x4*x4 + x5*x5);
						result += x6*x3;
					}
				}
				result = result/k;
				break;
			case 7:
				drawMode = 1;
				result = 1;
				x = 0;
				j = problem.genesNumber;
				for (i=0;i<j;i++) {
					z = problem.alleles[i].value;
					x += (Math.pow(z,3) + 5)/Math.sqrt(z+2);
				}
				result = x/j;	
				break;
			case 8:
				drawMode = 1;
				result = 1;
				x = 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;
					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));		
				}
				result = x/k;	
				break;
			case 9:
				drawMode = 1;
				result = 1;
				x = 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;
					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);
				}
				result = x/k;	
				break;
			case 10:  //Sphere Model
				drawMode = 1;
				result = 0;
				n = problem.genesNumber;
				for (i=0;i<n;i++) {
					xi[i] = problem.alleles[i].value;
					result += (xi[i]-1)*(xi[i]-1);
				}
				break;
			case 11:  //Grienwank
				drawMode = 1;
				result = 0;
				x = 0;
				y = 1;
				z = 4000.0;
				u = 100.0;
				n = problem.genesNumber;
				for (i=0;i<n;i++) {
					xi[i] = problem.alleles[i].value;
					x += (xi[i]-u)*(xi[i]-u);
					x1 = Math.cos((xi[i]-u)/Math.sqrt(i+1));
					y *= Math.cos((xi[i]-u)/Math.sqrt(i+1));
				}
				result = x/z - y +1.0;
				break;
			case 12:  //Ratrigin
				drawMode = 1;
				result = 0;
				x = 0;
				z = 10.0;
				n = problem.genesNumber;
				for (i=0;i<n;i++) {
					xi[i] = problem.alleles[i].value;
					x += (xi[i]*xi[i] - z*Math.cos(2.0*Math.PI*xi[i]));
				}
				result = n*z + x;
				break;
			case 13:  //Ackley
				drawMode = 1;
				result = 0;
				x = 0;
				y = 0;
				n = problem.genesNumber;
				for (i=0;i<n;i++) {
					xi[i] = problem.alleles[i].value;
					x += xi[i]*xi[i];
					y += Math.cos(2.0*Math.PI*xi[i]);
				}
				result = 20.0 + Math.E - 20.0*Math.exp(-0.2*Math.sqrt((1.0/n)*x)) - Math.exp((1.0/n)*y);
				break;
			case 14:  //Schwefel (Sine Root)
				drawMode = 1;
				result = 0;
				x = 0;
				y = 0;
				z = 418.9829;
				n = problem.genesNumber;
				for (i=0;i<n;i++) {
					xi[i] = problem.alleles[i].value;
					x += -xi[i]*Math.sin(Math.sqrt(Math.abs(xi[i])));
				}
				result = z*n + x;
				break;
			case 15:  //Rosenbrock's Saddle
				drawMode = 1;
				result = 0;
				x = 0;
				n = problem.genesNumber;
				xi[0] = problem.alleles[0].value;
				for (i=1;i<n;i++) {
					xi[i] = problem.alleles[i].value;
					x += 100.0*(xi[i]-xi[i-1]*xi[i-1])*(xi[i]-xi[i-1]*xi[i-1]) + (1-xi[i-1])*(1-xi[i-1]);
				}
				result = x;
				break;
			case 16:  //0-1 Single Knapsack
				drawMode = 1;
				result = 0;
				x1 = 0;
				x2 = 0;
				n = problem.genesNumber;
				for (i=0;i<n;i++) {
					x = problem.alleles[i].value;
					y = problem.mapAlleles[i][0];
					z = problem.mapAlleles[i][1];
					x1 += x*y;
					x2 += x*z;
				}
				x3 = 625.0 - x2;
				if (x3 < 0)
					x1 = 0;
				result = x1;
				break;
			case 17:  //Weing1 0-1 Multiple Knapsack 2/28
				drawMode = 1;
				result = 0;
				x1 = 0;
				x2 = 0;
				x5 = 0;
				x6 = 0;
				n = problem.genesNumber;
				for (i=0;i<n;i++) {
					x = problem.alleles[i].value;
					y = problem.mapAlleles[i][0];
					x3 = problem.mapAlleles[i][1];
					x4 = problem.mapAlleles[i][2];
					
					x1 += x*y;
					x5 += x*x3;
					x6 += x*x4;
				}
				x7 = 600.0 - x5;
				x8 = 600.0 - x6;
				if ((x7 < 0) || (x8 < 0))
					x1 = 0;
				result = x1;
				break;
			case 18:  //Weish01 0-1 Multiple Knapsack 5/30
				drawMode = 1;
				result = 0;
				z = 0;
				x6 = 0;
				x7 = 0;
				x8 = 0;
				x9 = 0;
				x10 = 0;
				n = problem.genesNumber;
				for (i=0;i<n;i++) {
					x = problem.alleles[i].value;
					y = problem.mapAlleles[i][0];
					x1 = problem.mapAlleles[i][1];
					x2 = problem.mapAlleles[i][2];
					x3 = problem.mapAlleles[i][3];
					x4 = problem.mapAlleles[i][4];
					x5 = problem.mapAlleles[i][5];

					z += x*y;
					x6 += x*x1;
					x7 += x*x2;
					x8 += x*x3;
					x9 += x*x4;
					x10 += x*x5;
				}
				xi[0] = 400.0 - x6;
				xi[1] = 500.0 - x7;
				xi[2] = 500.0 - x8;
				xi[3] = 600.0 - x9;
				xi[4] = 600.0 - x10;
				
				for (i=0;i<5;i++) {
					if (xi[i] < 0)
						z = 0;
				}
				result = z;
				break;
			case 19:  //Binpack1_00
			case 20:  //Binpack2_00
				drawMode = 1;
				result = 0;
				x = 0;
				y = 0;
				x1 = 0;
				boolean newBin = true;
				if (problem.problemCode == 19)
					z = 150.0;
				else
					z = 100.0;
				n = problem.genesNumber;
				for (i=0;i<n;i++) {
					if (newBin) {
						x1++;
						newBin = false;
					}
					j = (int) problem.alleles[i].value;
					xi[i] = problem.mapAlleles[j][0];
					x += xi[i];
					if (x > z) {
						y += (z-(x-xi[i]));
						x = xi[i];
						newBin = true;
					}
				}
				result = y;
				if (x1 < problem.currentMin)
					problem.currentMin = x1;
				break;
			case 21:  //IPD
				GaaMisc.debugOn = true;
				drawMode = 1;
				result = 0;
				String memory0;
				String memory1;
				int strat0;
				int strat1;
				int pid;
			
				int cid = pop.currentId;
				int popsize = pop.popSize;
				char kar;
				double vals[] = pop.vals;
			
			  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<pop.popSize;pid++) {
						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(pop.chroms[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: "+pop.chroms[pid]+" "+t1;
							//System.out.println(t3);
			       GaaMisc.debug(t3,0);
						}
					
						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: "+pop.chroms[pid]+" v1: "+y;
						//System.out.println(t3);
		       GaaMisc.debug(t3,0);
						
						//t4  = "Gen: "+pop.generation+" - "+cid+" against " + pid;
						//GaaApplet.statusLabel.setText(t4);
						
					}
					z = (double)(x/popsize/moves);
	 				
					try {
						
						t3 = "============================";
						t3 += "\n" + "Generation: "+pop.generation+" Id: "+cid+" (Pop Size: "+pop.popSize+" Moves: "+moves+")";
						t3 += "\n" + pop.chroms[cid]+ " Average: "+f3.format(z)+" (Total: "+f1.format(x)+")";
						//System.out.println(t3);
		       GaaMisc.debug(t3,0);

						//t4  = "Gen: "+pop.generation+" Id: "+cid+" Ch: " + pop.chroms[cid]+ " Average: "+f3.format(z);
						//GaaApplet.statusLabel.setText(t4);
						
						if ((z<1) || (z>4))
							GaaMisc.debug("!!!!!!!!!!",0);
						//	k = MsgDialog.msg("Suspicious result", "Suspicious result. z = "+z, MsgDialog.MB_OK, 0);
						
						//if (problem.withTextWindow)
				    	//GaaAction.text.appendText(t3 + "\n");
				    if (problem.withLogging)
				    	GaaMisc.dbg(t3);	
				  }
				  catch (Exception e) {
				  }		
				  		
					result = z;
					result = result;
				}
				break;
				
			case 22: //IPD
				z = ipdFixed();
				break;
			
			default:
				result = -11111;
		}

		x1 = pop.bestVal;
		x2 = pop.lastVal;
		x3 = pop.vals[0];
		x4 = result;
		
		currentResult = result;
		return(result);
	
	}
	
	
	public void draw(Graphics g, String chrom, Dimension d) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -