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

📄 gaaapplet.java

📁 Java实现的遗传算法工具集:GA Playground
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

		String s;
		double par;

		s=getParameter(name);
		if (s==null)
			par=def;
		else 
			par = Double.valueOf(s).doubleValue();
		
		return par;

	}


	int GetOneParameter(String name, int def) {

		String s;
		int par;

		s=getParameter(name);
		if (s==null)
			par=def;
		else 
			par = Integer.parseInt(s);		
		
		return par;

	}


	String GetOneParameter(String name, String def) {

		String s;
		String par;

		s=getParameter(name);
		if (s==null)
			par=def;
		else 
			par = s;		
		
		return par;

	}


	void checkMemory(int num) {
		
		String txt = "Total memory = "+Runtime.getRuntime().totalMemory()+"      Free Memory = "+Runtime.getRuntime().freeMemory();
		if (num == 1) {
		  System.gc();
		  txt = txt + "     Recovered Free Memory (System) = "+Runtime.getRuntime().freeMemory();
		 }
		else if (num == 2) {
			Runtime.getRuntime().gc();
			txt = txt + "     Recovered Free Memory (Runtime) = "+Runtime.getRuntime().freeMemory();
		}
		statusLabel.setText(txt);
			
	}
	
	
	
	void test1() {
		
		int i, j, k;
		double rand, num, tot, res, n1, n2, n3;
		
		
		/*
		double x = -0.870173;
		double d1 = Math.pow(x,4)-12*Math.pow(x,3)+15*Math.pow(x,2)+56*x-60;
		x = -0.856;
		double d2 = Math.pow(x,4)-12*Math.pow(x,3)+15*Math.pow(x,2)+56*x-60;
		*/
		
		/*
		double x = 0.57934785;
		double d1 = (Math.pow(x,3) + 5)/Math.sqrt(x+2);
		x = 0.585;
		double d2 = (Math.pow(x,3) + 5)/Math.sqrt(x+2);
		x = 0.575;
		double d3 = (Math.pow(x,3) + 5)/Math.sqrt(x+2);
		*/
		
		/*
		double x1 = 0.5;
		double x2 = 0.6;
		double x3 = 0.7;
		double x4 = 0.8;
		
		x1 = 1;
		x2 = 1;
		x3 = 1;
		x4 = 1;

		x1 = 1.1;
		x2 = 1.5;
		x3 = 1.7;
		x4 = 1.2;
		

		double d1 = 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));		

		x1 = 0.99999999997664;
		x2 = 0.999999999949512;
		x3 = 1.00000000001489;
		x4 = 1.00000000003710;

		double d2 = 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));		
		*/
		
		/*
		double x1 = 69;
		double x2 = 42;
		double x3 = -7;
		double x4 = 61;
		double x5 = -16;
		
		double d1 = 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);

		x1 = -17;
		x2 = 10;
		x3 = 0;
		x4 = 67;
		x5 = -5;
		
		double d2 = 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);
		
		statusLabel.setText("d1 = "+d1+" d2: "+d2);
		*/
		
		/*
		char kar;
		for (int n=0;n<190;n++) {
			kar = (char) (65+n);
			GaaMisc.dbg("n= "+n+ "  kar= "+kar+"\n");
		}
		*/
		
		/*
		double d1 = 0;
		double d2 = 0;
		double x = 0;
		double z = 10;
		double xi[];
		xi = new double[20];
		int n = problem.genesNumber;
		for (i=0;i<n;i++) {
			xi[i] = 0;
			x += (xi[i]*xi[i] - z*Math.cos(2*Math.PI*xi[i]));
		}
		d1 = n*z + x;
		
		x = 0;
		for (i=0;i<n;i++) {
			xi[i] = 00;
			if (i == 2)
				xi[i] = 0.1;
			x += (xi[i]*xi[i] - z*Math.cos(2*Math.PI*xi[i]));
		}
		d2 = n*z + x;
		*/
		
		/*
		double d1 = 0;
		double d2 = 0;
		double x = 0;
		double y = 0;
		double xi[];
		xi = new double[20];
		int n = problem.genesNumber;
		
		x = 0;
		y = 0;
		n = problem.genesNumber;
		for (i=0;i<n;i++) {
			xi[i] = 0;
			x += xi[i]*xi[i];
			y += Math.cos(2.0*Math.PI*xi[i]);
		}
		d1 = 20.0 + Math.E - 20.0*Math.exp(-0.2*Math.sqrt((1.0/n)*x)) - Math.exp((1.0/n)*y);
		
		x=0;
		y=0;
		x = 0;
		y = 0;
		n = problem.genesNumber;
		for (i=0;i<n;i++) {
			xi[i] = 0.1;
			x += xi[i]*xi[i];
			y += Math.cos(2.0*Math.PI*xi[i]);
		}
		d2 = 20.0 + Math.E - 20.0*Math.exp(-0.2*Math.sqrt((1.0/n)*x)) - Math.exp((1.0/n)*y);
		*/
		
		/*
		double d1 = 0;
		double d2 = 0;
		double x = 0;
		double y = 0;
		double z = 0;
		double xi[];
		xi = new double[20];
		int n = problem.genesNumber;
		
		x = 0;
		y = 0;
		n = problem.genesNumber;
		
		x = 0;
		y = 0;
		z = 418.9829;
		n = problem.genesNumber;
		for (i=0;i<n;i++) {
			xi[i] = 420.9687;
			x += -xi[i]*Math.sin(Math.sqrt(Math.abs(xi[i])));
		}
		d1 = z*n + x;
		
		x = 0;
		y = 0;
		z = 418.9829;
		n = problem.genesNumber;
		for (i=0;i<n;i++) {
			xi[i] = -302.5232;
			x += -xi[i]*Math.sin(Math.sqrt(Math.abs(xi[i])));
		}
		d2 = z*n + x;
		*/
		
		/*
		double d1 = 0;
		double d2 = 0;
		double x = 0;
		double y = 0;
		double z = 0;
		double xi[];
		xi = new double[20];
		int n = problem.genesNumber;
		
		xi[0] = 1.0;
		for (i=1;i<n;i++) {
			xi[i] = 1.0;
			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]);
		}
		d1 = x;
		
		x=0;
		xi[0] = 0;
		for (i=1;i<n;i++) {
			xi[i] = 0;
			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]);
		}
		d2 = x;
		
		
		
		statusLabel.setText("d1 = "+d1+"    d2: "+d2);
		*/
				GaaMisc.dbg("Bin Packing Result");
				problem.alleleSet.decodeChrom(problem.currentBestChrom);

				double x = 0;
				double y = 0;
				double x1 = 0;
				double z = 0;
				double u;
				double v;
				j = 0;
				if (problem.problemCode == 19)
					z = 150.0;
				else
					z = 100.0;
				int n = problem.genesNumber;
				i = 0;
				boolean newBin = true;
				double xi[] = new double[n];
				for (i=0;i<n;i++) {
					if (newBin) {
						x1++;
						newBin = 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]));
						GaaMisc.dbg("Bin #" + GaaMisc.f0.format(x1) + " Wasted space: " + GaaMisc.f1.format((z-(x-xi[i]))));
						GaaMisc.dbg("");	
						x = xi[i];	
						GaaMisc.dbg("i = " + i + " j = " + j + " xi[i]= " + GaaMisc.f1.format(xi[i])+" x= "+GaaMisc.f1.format(x));				
						newBin = true;
					}
					else
						GaaMisc.dbg("i = " + i + " j = " + j + " xi[i]= " + GaaMisc.f1.format(xi[i])+" x= "+GaaMisc.f1.format(x));
				}
				GaaMisc.dbg("Bin #" + GaaMisc.f0.format(x1) + " Wasted space: " + GaaMisc.f1.format((z-x)));

	}
	
	
	/*
	class myFilter implements FilenameDilter {
		
		public boolean accept(File dir, String name) {
			return(name.endswith(".out"));
		}
		
	}
	*/
	
	void saveLog() {
		
		int ret;
		FileWriter fw;
		String dir;
		String fil;
		int i;
		String txt = "";
		Date date;
		String dateString;
		
		
		if (gaaAppMode.equals("Applet"))
			ret = MsgDialog.msg("Sorry", "This option is not supported in an applet", MsgDialog.MB_OK, 0);
		else {		
			
			date = new Date();
			dateString = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG).format(date);
			
			dir = System.getProperty("user.dir");
			
			Frame f = new Frame("Save Log Data");
			final FileDialog fd = new FileDialog(f,"Save log data to file",FileDialog.SAVE);
			fd.setDirectory(dir);
			fd.setFile("*.log");
			fd.show();
			fil = fd.getFile();
			if (fil == null) {
				return;
			}
			fil = dir + File.separator + fil;
			
			try {
				fw = new FileWriter(fil);
			} catch(IOException e) {
				return;
			}
			
			PrintWriter prw = new PrintWriter(fw, false);
		
			try {
				txt = problem.problemTitle + " executed: " + dateString;
				prw.write(txt);
				prw.println();
				prw.println();
				
				txt = GaaAction.deb.getText();
				String[] lines = FileInput.parseStr(txt, "\n");
				for (i=0;i<lines.length;i++) {
					prw.write(lines[i]);
					prw.println();
				}
				
			}
			catch (Exception e) {
			}

			prw.flush();
			
			prw.close();	
			ret = MsgDialog.msg("Ok", "Log data written to "+fil, MsgDialog.MB_OK, 0);
			
		}	
		
	}
	
	
	void loadPopulation() {
		
		FileReader fr;
		String dir;
		String fil;
		int ret;
		int i;
		String txt = "";
		
		
		if (gaaAppMode.equals("Applet"))
			ret = MsgDialog.msg("Sorry", "This option is not supported in an applet", MsgDialog.MB_OK, 0);
		else {		
		
			dir = System.getProperty("user.dir");
			
			Frame f = new Frame("Load Population");
			final FileDialog fd = new FileDialog(f,"Load population from file",FileDialog.LOAD);
			fd.setDirectory(dir);
			fd.setFile("*.pop");
			fd.show();
			fil = fd.getFile();
			if (fil == null) {
				return;
			}
			fil = dir + File.separator + fil;
		
			try {
				fr = new FileReader(fil);
		  } catch(FileNotFoundException e) {return;}
		  
			BufferedReader br = new BufferedReader(fr, 4096);
			
			i = 0;
			String line = "!";
			
			while (line != null) {
				try {
					line = br.readLine();
					i++;
					if ((i > 3) && (line != null)) {
						pop.chroms[i-4] = line.substring(0,problem.genesNumber);
						txt = 	pop.chroms[i-4];
					}
				} catch(IOException e) {return;}
			}
			
			try {
				br.close();
			} catch(IOException e) {return;}
			
		}
		
	}
	
	
	
	void saveCurpop() {
		
		int ret;
		FileWriter fw;
		String dir;
		String fil;
		int i;
		String txt = "";
		Date date;
		String dateString;
		
		
		if (gaaAppMode.equals("Applet"))
			ret = MsgDialog.msg("Sorry", "This option is not supported in an applet", MsgDialog.MB_OK, 0);
		else {		
			
			date = new Date();
			dateString = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG).format(date);
			
			dir = System.getProperty("user.dir");
			
			Frame f = new Frame("Save Current Population");
			final FileDialog fd = new FileDialog(f,"Save current population to file",FileDialog.SAVE);
			fd.setDirectory(dir);
			fd.setFile("*.pop");
			fd.show();
			fil = fd.getFile();
			if (fil == null) {
				return;
			}
			fil = dir + File.separator + fil;
			
			try {
				fw = new FileWriter(fil);
			} catch(IOException e) {
				return;
			}
			
			PrintWriter prw = new PrintWriter(fw, false);
		
			try {
				txt = problem.problemTitle + " executed: " + dateString;
				prw.write(txt);
				prw.println();
				txt = "Current Population (with calculated (V) and fitness (F) values)";
				prw.write(txt);
				prw.println("---");
				prw.println();
				for (i=0;i<problem.popSize;i++) {
					txt = pop.chroms[i] + "   V:  " + GaaMisc.f4.format(pop.vals[i]) + "   F:  " + GaaMisc.f4.format(pop.fits[i]);
					prw.write(txt);
					prw.println();
				}
			}
			catch (Exception e) {
			}

			prw.flush();
			
			prw.close();	
			ret = MsgDialog.msg("Ok", "Data written to "+fil, MsgDialog.MB_OK, 0);
			
		}	
		
	}
	
	
	void loadParameters() {
		int ret;
		FileWriter fw;
		String dir;
		String fil;
		int i;
		String txt = "";
		Date date;
		String dateString;
		String argv[];
		URL url;
		
		if (gaaAppMode.equals("Applet"))
			ret = MsgDialog.msg("Sorry", "This option is not supported in an applet", MsgDialog.MB_OK, 0);
		else {		
			
			dir = System.getProperty("user.dir");
			
			Frame f = new Frame("Load Parameters");
			final FileDialog fd = new FileDialog(f,"Load parameters from file",FileDialog.LOAD);
			fd.setDirectory(dir);
			fd.setFile("*.par");
			fd.show();
			fil = fd.getFile();
			if (fil == null) {
				return;
			}
			

⌨️ 快捷键说明

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