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

📄 gaafunction.java

📁 Java实现的遗传算法工具集:GA Playground
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		
		int width, height;
		double xfactor, yfactor;
		
		width = d.width;
		height = d.height;

		g.setColor(Color.black);
		
		switch (problem.problemCode) {
			case 1:
				yfactor = height/(problem.exitValue*1.2);
				y = problem.function.getValue(chrom);
				x = Math.random()*width;
				if (y >= problem.exitValue)
					g.setColor(Color.red);
				else
					g.setColor(Color.black);
				g.fillOval((int) x, (int) (y*yfactor), 10, 10);
				g.drawLine (0, (int) (problem.exitValue*yfactor), width, (int) (problem.exitValue*yfactor));
				g.setColor(Color.white);
				g.fillRect(0, height-25, width, 25);
				g.setColor(Color.red);
				g.drawString("Current generation: "+f0.format(problem.currentGeneration)+"   Function Calls: "+f0.format(problem.functionCalls),20,height-15);
				g.setColor(Color.black);
				break;
			case 5:
				yfactor = (double) width/120;
				y = problem.function.getValue(chrom);
				
				x = width*0.1 + Math.abs(y)*yfactor;
				z = width*0.1 + 89*yfactor;
				double xxx = problem.exitValue;
				if (y <= problem.exitValue)
					g.setColor(Color.green);
				else
					g.setColor(Color.black);
				
				g.setColor(Color.green);
				g.fillRect((int) z, (int) (height*0.1), 10, (int) (height*0.8));
				g.drawLine (0,(int) (height/2), (int) x, (int) (height/2));
				g.setColor(Color.white);
				g.fillRect(5, 5, width-10, 40);
				
				g.setColor(Color.red);
				g.fillOval((int) x, (int) (height/2-5), 10, 10);
				g.drawString(getSpecialString(chrom),20, 20);
				g.drawString("Current generation: "+f0.format(problem.currentGeneration)+"   Function Calls: "+f0.format(problem.functionCalls),20,height-15);
				g.setColor(Color.black);
				break;
			case 7:
			case 8:
			case 9: //MultiVarMin
			case 10:
			case 11:
			case 12:
			case 13:
			case 14:
			case 15:
				displayCurrentValues(g);
				break;
			case 16:
				problem.alleleSet.decodeChrom(problem.currentBestChrom);
				n = problem.genesNumber;
				txt = "";
				result = 0;
				x = 0;
				y = 0;
				z = 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;
				
				g.setColor(Color.red);
				g.drawString("Current Best (Profit)= "+f0.format(problem.currentBestVal),20,(15*2));
				//g.drawString("Current Profit= "+f0.format(x1),20,(15*3));
				g.drawString("Current Weight= "+f0.format(x2)+"  (max = 625)",20,(15*4));

				g.drawString("Current generation: "+f0.format(problem.currentGeneration)+"   Function Calls: "+f0.format(problem.functionCalls),20,(15*7));
				g.setColor(Color.black);
				break;
			case 17:
				problem.alleleSet.decodeChrom(problem.currentBestChrom);
				n = problem.genesNumber;
				txt = "";
				x1 = 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;
				
				g.setColor(Color.red);
				g.drawString("Current Best (Profit)= "+f0.format(problem.currentBestVal),20,(15*2));
				g.drawString("Weight of sack #1= "+f0.format(x5)+"  (max = 600)",20,(15*4));
				g.drawString("Weight of sack #2= "+f0.format(x6)+"  (max = 600)",20,(15*5));

				g.drawString("Current generation: "+f0.format(problem.currentGeneration)+"   Function Calls: "+f0.format(problem.functionCalls),20,(15*7));
				g.setColor(Color.black);
				break;
			case 18:
				problem.alleleSet.decodeChrom(problem.currentBestChrom);
				n = problem.genesNumber;
				txt = "";
				z = 0;
				x6 = 0;
				x7 = 0;
				x8 = 0;
				x9 = 0;
				x10 = 0;
				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;
				}
				
				g.setColor(Color.red);
				g.drawString("Current Best (Profit)= "+f0.format(problem.currentBestVal),20,(15*2));
				//g.drawString("Current Profit= "+f0.format(z),20,(15*3));
				g.drawString("Weight of sack #1= "+f0.format(x6)+"  (max = 400)",20,(15*4));
				g.drawString("Weight of sack #2= "+f0.format(x7)+"  (max = 500)",20,(15*5));
				g.drawString("Weight of sack #3= "+f0.format(x8)+"  (max = 500)",20,(15*6));
				g.drawString("Weight of sack #4= "+f0.format(x9)+"  (max = 600)",20,(15*7));
				g.drawString("Weight of sack #5= "+f0.format(x10)+"  (max = 600)",20,(15*8));

				g.drawString("Current generation: "+f0.format(problem.currentGeneration)+"   Function Calls: "+f0.format(problem.functionCalls),20,(15*10));
				g.setColor(Color.black);
				break;
			case 19:
			case 20:
				g.drawString(getSpecialString(chrom),20, 20);
				g.drawString("Current generation: "+f0.format(problem.currentGeneration)+"   Function Calls: "+f0.format(problem.functionCalls),20,height-15);
				break;
			case 21:
			case 22:
				g.drawString(getSpecialString(chrom),20, 20);
				g.drawString("Current generation: "+f0.format(problem.currentGeneration)+"   Function Calls: "+f0.format(problem.functionCalls),20,height-15);
				break;
				
			case 2:
				yfactor = height/3;
				problem.alleleSet.decodeChrom(chrom);
				
				g.setColor(Color.red);
				g.drawString(getSpecialString(chrom),20, 15);
				g.drawString("Current generation: "+f0.format(problem.currentGeneration)+"   Function Calls: "+f0.format(problem.functionCalls),20,30);
				g.setColor(Color.black);
				
				for (i=0;i<problem.genesNumber;i++) {
					try {
						n = (int)problem.alleles[i].value;
						double a = problem.mapAlleles[n][0];
						double b = problem.mapAlleles[n][1];
						x = width/2 + yfactor*problem.mapAlleles[n][0];
						y = height/2 + yfactor*problem.mapAlleles[n][1];
						if (problem.withGraphicText)
							g.drawString(""+i,(int) x, (int) y);
						if (i > 0) { 
							g.drawLine((int) oldx, (int) oldy, (int) x, (int) y);
						}
						oldx = x;
						oldy = y;
					}
					catch (Exception e) {
						txt = "GaaFunction Draw Error \n" +
							e.toString() + "\n" +
							"n= "+n+"  x= "+(int)x+"   y= "+(int)y;
						GaaApplet.statusLabel.setText(txt);
					}
						
				}
				break;
			case 4:
				yfactor = height*0.8;
				problem.alleleSet.decodeChrom(chrom);
				g.setColor(Color.red);
				g.drawString(getSpecialString(chrom),20, 15);
				g.drawString("Current generation: "+f0.format(problem.currentGeneration)+"   Function Calls: "+f0.format(problem.functionCalls),20,30);
				g.setColor(Color.black);
				
				for (i=0;i<problem.genesNumber;i++) {
					try {
						n = (int)problem.alleles[i].value;
						double a = problem.mapAlleles[n][0];
						double b = problem.mapAlleles[n][1];
						x = width*0.1 + yfactor*problem.mapAlleles[n][0];
						y = height*0.1 + yfactor*problem.mapAlleles[n][1];
						if (problem.withGraphicText)
							g.drawString(""+i,(int) x, (int) y);
						if (i > 0) { 
							g.drawLine((int) oldx, (int) oldy, (int) x, (int) y);
						}
						oldx = x;
						oldy = y;
					}
					catch (Exception e) {
						txt = "GaaFunction Draw Error \n" +
							e.toString() + "\n" +
							"n= "+n+"  x= "+(int)x+"   y= "+(int)y;
						GaaApplet.statusLabel.setText(txt);
					}
						
				}
				break;
			case 6:
				
				if (problem.problemTitle.equals("Steiner's Problem")) {
					yfactor = height/8*3;
					r = 2;
					s = 2;
				}
				else {
					yfactor = height/4*3;
					r = 8;
					s = 8;
				}
				
				oldx = getSpecialValue(chrom,1);
				oldy = getSpecialValue(chrom,2);
				oldx = width/r + yfactor*oldx;
				oldy = height/s + yfactor*oldy;	
				u = 0;
				v = 0;		
				n = problem.genesNumber;
				
				problem.alleleSet.decodeChrom(chrom);
				for (i=0;i<problem.genesNumber;i++) {
					try {
						u += problem.mapAlleles[i][0];
						v += problem.mapAlleles[i][1];
						x = width/r + yfactor*problem.mapAlleles[i][0];
						y = height/s + yfactor*problem.mapAlleles[i][1];
						g.setColor(Color.black);
						//if (problem.withGraphicText)
							g.drawString(""+i,(int) x, (int) y);
						g.drawLine((int) oldx, (int) oldy, (int) x, (int) y);
						g.setColor(Color.red);
					}
					catch (Exception e) {
						txt = "GaaFunction Draw Error \n" +
							e.toString() + "\n" +
							"n= "+n+"  x= "+(int)x+"   y= "+(int)y;
						GaaApplet.statusLabel.setText(txt);
					}
						
				}
				u = width/r + yfactor*u/n;
				v = height/s + yfactor*v/n;
				g.drawOval((int) (u-5),(int) (v-5),10,10);
				g.drawString(getSpecialString(chrom),20, 20);
				g.drawString("Current generation: "+f0.format(problem.currentGeneration)+"   Function Calls: "+f0.format(problem.functionCalls),20,height-15);
				break;
			case 25:
				if (!problem.currentBestChrom.equals("")) {
				problem.alleleSet.decodeChrom(problem.currentBestChrom);
				n = problem.genesNumber;
				
				if (n > 4) {
					m = (int) (n/4);
					for (i=0;i<m;i++) {
						j = (int) problem.alleles[4*i].value;
						k = (int) problem.alleles[4*i+1].value;
						l = (int) problem.alleles[4*i+2].value;
						i = (int) problem.alleles[4*i+3].value;
						t1 = " "+j+k+l+i;
						g.drawString("i= "+t2,20,15*(i+1));
					}
				}
				else {
					for (i=0;i<n;i++) {
						j = (int) problem.alleles[i].value;
						k = (int) problem.alleles[i+1].value;
						l = (int) problem.alleles[i+2].value;
						i = (int) problem.alleles[i+3].value;
						t1 = " "+j+k+l+i;
						g.drawString("i= "+t2,20,15*(2));
					}
				}
				
				g.setColor(Color.red);
				g.drawString("f(x)="+f7.format(problem.currentBestVal),20,(15*(i+2)));
				g.drawString("Current generation: "+f0.format(problem.currentGeneration)+"   Function Calls: "+f0.format(problem.functionCalls),20,(15*(i+3)));
				g.setColor(Color.black);
				}
				break;		
				
			default:
				displayCurrentValues(g);
				break;
				
		}
		
	}
	
	public boolean initFunction() {
	
	  boolean flag = true;	
	  
		switch (problem.problemCode) {
			case 3:
				
				if (!GaaApplet.gaaAppMode.equals("Applet")) {
					GaaMisc.debug("Creating JEL object");

					jl = new JelProcs();
					GaaMisc.debug("Initializing JEL libraries");

					flag = jl.initLibs(problem.udfExpression);
				}
				break;
				
			case 21:
				/*
				StringBuffer sb = new StringBuffer("2222");
				
				m = 0;
				txts = new String[21];
				for (i=0;i<2;i++) {
					sb.setCharAt(0,(char)(i+48));
					for (j=0;j<2;j++) {
						sb.setCharAt(1,(char)(j+48));
						for (k=0;k<2;k++) {
							sb.setCharAt(2,(char)(k+48));
							for (l=0;l<2;l++) {
								sb.setCharAt(3,(char)(l+48));
					
								txts[m] = sb.toString();
								t1 = txts[m];
								m++;
							}
						}
					}
				}
			
				
				txts[16] = "0022";
				txts[17] = "0122";
				txts[18] = "1022";
				txts[19] = "1122";
				txts[20] = "2222";
				*/
		
				hashmem = IpdMemory();
			
				break;
				
			case 22:
				hashmem = IpdMemory();
			
				break;
	  
		case 23:
				int memorySize = 2;
				StringBuffer sb23 = new StringBuffer(memorySize);
				char kars[] = {'T','R','P','S'};
				
				m = 0;
				txts = new String[21];
				for (i=0;i<memorySize;i++) {
					sb23.setCharAt(0,kars[i]);
					for (j=0;j<kars.length;j++) {
						sb23.setCharAt(i,kars[j]);
					
							txts[m] = sb23.toString();
							t1 = txts[m];
							m++;
					}
				}
			
				txts[16] = "S-";
				txts[17] = "P-";
				txts[18] = "R-";
				txts[19] = "T-";
				txts[20] = "--";
			
				break;
			
		}
			
		return flag;
	}
	
	
	public boolean createAllelesMap() {
		
		double ang, oldx, oldy;
		boolean flag = false;
		char kar;
		
		
		switch (problem.problemCode) {
			case 1:
				break;
			case 3:
				break;
			case 2:
				n = problem.genesNumber;
				result=0;
				oldx=0;
				oldy=0;
				
				problem.mapAlleles = new double[n][problem.mapOrder];

				//GaaAction.deb.debug("\nFunction alleles");
				for (i=0;i<n;i++) {
					ang = (double) i*Math.PI*2/n;
					x = (double) Math.cos(ang);
					y = (double) Math.sin(ang);
					problem.mapAlleles[i][0] = x;
					problem.mapAlleles[i][1] = y;
					if (i > 0) {
						z = Math.sqrt((x-oldx)*(x-oldx) + (y-oldy)*(y-oldy));
						result += z;
					}
					//GaaAction.deb.debug("i= "+i+" ang= "+ang+" x= "+x+" y= "+y+" z= "+z);
					oldx = x;
					oldy = y;
				}
				problem.exitValue = result;
				//GaaAction.deb.debug("exit value = "+result);
				flag = true;
				break;
			case 6:
				n = problem.genesNumber;
				result=0;
				
				problem.mapAlleles = new double[n][problem.mapOrder];

				//GaaAction.deb.debug("\nFunction alleles");
				for (i=0;i<n;i++) {
					ang = (double) i*Math.PI*2/n;
					x = (double) Math.cos(ang);
					y = (double) Math.sin(ang);
					z = 1.0;
					problem.mapAlleles[i][0] = x;
					problem.mapAlleles[i][1] = y;
					problem.mapAlleles[i][2] = z;
				}
				problem.exitValue = n;
				//GaaAction.deb.debug("exit value = "+result);
				flag = true;
				break;
			
			case 21:
			case 22:

				n = problem.genesNumber;
				result=0;
				
				problem.mapAlleles = new double[n][problem.mapOrder];

				for (i=0;i<n;i++) {
					t1 = txts[i];
					for (j=0;j<4;j++) {
						kar = txts[i].charAt(j);
						x1 = kar - 48;
						problem.mapAlleles[i][j] = txts[i].charAt(j) - 48;
					}
				}
				flag = true;
				break;
				
			case 23:

				n = problem.genesNumber;
				m = txts[0].length();
				result=0;
				
				problem.mapAlleles = new double[n][problem.mapOrder];

				for (i=0;i<n;i++) {
					t1 = txts[i];
					for (j=0;j<m;j++) {
						kar = txts[i].charAt(j);
						x1 = kar - 48;
						problem.mapAlleles[i][j] = txts[i].charAt(j) - 48;
					}
				}
				flag = true;
				break;
			
			
			}
			return(flag);
	}
	
	
	public String getSpecialString(String chrom) {
		
		problem.alleleSet.decodeChrom(chrom);
		
		switch (problem.problemCode) {
			case 2:
				txt = "Target Value: "+f3.format(problem.exitValue) + "  Best so far: "+f3.format(problem.currentBestVal);
				break;
			case 4:
				txt = "Current Best: "+f3.format(problem.currentBestVal);
				break;
			case 5:
				x = 0;
				y = 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;
					y += z;
				}
				x = x/j;	
				y = y/j;
				txt = "Current x: "+f3.format(y)+"   f(x)= "+f3.format(x);
				break;
			case 6:
				x = 0;
				y = 0;
				j = problem.genesNumber;
				k = (int) (j/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;
				txt = "Current Point: x= "+f3.format(x)+"  y= "+f3.format(y);
				break;
			case 7:
				x = 0;
				y = 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);
					y += z;
				}
				x = x/j;	
				y = y/j;
				txt = "Current x: "+f7.format(y)+"   f(x)= "+f7.format(x);
				break;

⌨️ 快捷键说明

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