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

📄 form1.java

📁 产生式系统中旅行商问题的求解
💻 JAVA
字号:
import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;

/**
 * This class can take a variable number of parameters on the command
 * line. Program execution begins with the main() method. The class
 * constructor is not invoked unless an object of type 'Form1' is
 * created in the main() method.
 */
public class Form1 extends Form
{
	PointPlus[] points;
	Being a;
	Being b;
	Being c;
	Being d;
	int[] bestIndividual;
	double best=99999999;
	int depth;
	String[] cities;
	
	public Form1()
	{
		// Required for Visual J++ Form Designer support
		initForm();		

		// TODO: Add any constructor code after initForm call
	}

	/**
	 * Form1 overrides dispose so it can clean up the
	 * component list.
	 */
	public void dispose()
	{
		super.dispose();
		components.dispose();
	}

	private void button1_click(Object source, Event e)
	{   
		try{
		
		this.comboBox1.setEnabled(true);
		this.richEdit2.setText("");
		this.richEdit3.setText("");
		
		String line[]=this.richEdit1.getLines();
	    this.points=new PointPlus[line.length];
		this.cities=new String[line.length];
	 
	 for(int index=0;index<line.length;index++)
	 {
		 
		 int i=0;
		 
		 StringBuffer s=new StringBuffer();
			  
		    while(line[index].charAt(i)!='(')
		    {s.append(line[index].charAt(i));i++;}
		
		   this.cities[index]=s.toString();
		 
		 i++;  
		 
		             s=new StringBuffer();
			  
		    while(line[index].charAt(i)!=',')
		    {s.append(line[index].charAt(i));i++;}
		
		   double x=Double.valueOf(((String)s.toString())).doubleValue();
		 
		 i++;
		 
		                s=new StringBuffer();
		    while(line[index].charAt(i)!=')')
		    {s.append(line[index].charAt(i));i++;}
			double y=Double.valueOf(((String)s.toString())).doubleValue();
		  
		 
	 points[index]=new PointPlus(x,y);
	 }
	 
	  a=new Being(points);
	  b=new Being(points);
	  c=new Being(points);
	  d=new Being(points);
	 bestIndividual=new int[line.length-1];
	 for(int i=0;i<points.length-1;i++)this.bestIndividual[i]=i;
	
		}catch(Exception exception){Warning warning = new Warning();
											warning.showDialog ();
		                                    this.richEdit1.setText("");}

	}

	private void button3_click(Object source, Event e)
	{
		Application.exit();
	}

	private void button2_click(Object source, Event e)
	{
        this.openFileDialog1.setTitle("请选择一个城市点坐标文本文件..");
		this.openFileDialog1.setInitialDir("c:\\");
		this.openFileDialog1.setFilter("RTF Files(*.rtf)|*.RTF");
		this.openFileDialog1.setDefaultExt("rtf");
		this.openFileDialog1.showDialog();  
		if (this.openFileDialog1.getFileName() != "")
			{
			 this.richEdit1.loadFile(this.openFileDialog1.getFileName()); 
			}
	}

	private void comboBox1_selectedIndexChanged(Object source, Event e)
	{
		
		String s=((String)this.comboBox1.getSelectedItem());
		this.depth=Integer.valueOf(s).intValue();
		this.button4.setEnabled(true);
	}

	private void button4_click(Object source, Event e)
	{   this.button4.setEnabled(false);
		this.comboBox1.setEnabled(false);
		int counter=0;int outCounter=0;best=a.fmin();
		while(counter<this.depth)
		{b=a.NaturalChoose(points);  
		 c=b.Exchange(points);
		 a=c.Evalution(points);
		 counter++;outCounter++;
		 if (a.fmin()<best) 
		 {best=a.fmin();counter=0;
		  this.richEdit2.setText(this.richEdit2.getText()+
								 "\n\n"+"于第"+outCounter+"代发现更优解:"+"\n"+best);
		  //this.bestIndividual=(int[])a.bestIndividual();
		  System.arraycopy((int[])a.bestIndividual(),0,this.bestIndividual,0,this.bestIndividual.length);
		 
		 }
		//try{ch=(char)System.in.read();}catch(IOException e){};	
		}
		this.richEdit3.setText("得到最优的旅行方案为:"+"\n");
		this.richEdit3.setText(richEdit3.getText()+"从"+this.cities[0]+"出发,经");
	
		for(int i=0;i<this.bestIndividual.length;i++)
	    this.richEdit3.setText(richEdit3.getText()+this.cities[this.bestIndividual[i]+1]+"," );
        this.richEdit3.setText(richEdit3.getText()+"再回到"+this.cities[0]+"。"+"\n");
		this.richEdit3.setText(richEdit3.getText()+"全程长"+best+"公里。");

	}

	private void button5_click(Object source, Event e)
	{
		About myAbout = new About();
		myAbout.showDialog ();

	}

	/**
	 * NOTE: The following code is required by the Visual J++ form
	 * designer.  It can be modified using the form editor.  Do not
	 * modify it using the code editor.
	 */
	Container components = new Container();
	GroupBox groupBox1 = new GroupBox();
	GroupBox groupBox2 = new GroupBox();
	GroupBox groupBox3 = new GroupBox();
	GroupBox groupBox4 = new GroupBox();
	RichEdit richEdit1 = new RichEdit();
	Button button1 = new Button();
	Button button2 = new Button();
	Button button3 = new Button();
	OpenFileDialog openFileDialog1 = new OpenFileDialog();
	RichEdit richEdit2 = new RichEdit();
	Label label1 = new Label();
	Label label2 = new Label();
	Label label3 = new Label();
	ComboBox comboBox1 = new ComboBox();
	Button button4 = new Button();
	RichEdit richEdit3 = new RichEdit();
	Label label4 = new Label();
	Button button5 = new Button();

	private void initForm()
	{
		// NOTE:  This form is storing resource information in an
		// external file.  Do not modify the string parameter to any
		// resources.getObject() function call. For example, do not
		// modify "foo1_location" in the following line of code
		// even if the name of the Foo object changes: 
		//   foo1.setLocation((Point)resources.getObject("foo1_location"));

		IResourceManager resources = new ResourceManager(this, "Form1");
		this.setText("人工智能实验程序之二(旅行商问题)");
		this.setAutoScaleBaseSize(new Point(6, 12));
		this.setClientSize(new Point(558, 547));
		this.setIcon((Icon)resources.getObject("this_icon"));
		this.setStartPosition(FormStartPosition.CENTER_SCREEN);

		groupBox1.setLocation(new Point(360, 8));
		groupBox1.setSize(new Point(184, 496));
		groupBox1.setTabIndex(0);
		groupBox1.setTabStop(false);
		groupBox1.setText("城市位置");

		groupBox2.setLocation(new Point(16, 8));
		groupBox2.setSize(new Point(328, 200));
		groupBox2.setTabIndex(1);
		groupBox2.setTabStop(false);
		groupBox2.setText("问题说明");

		groupBox3.setLocation(new Point(16, 216));
		groupBox3.setSize(new Point(328, 192));
		groupBox3.setTabIndex(2);
		groupBox3.setTabStop(false);
		groupBox3.setText("工作区");

		groupBox4.setLocation(new Point(16, 416));
		groupBox4.setSize(new Point(328, 120));
		groupBox4.setTabIndex(3);
		groupBox4.setTabStop(false);
		groupBox4.setText("结论");

		richEdit1.setFont(Font.DEFAULT_GUI);
		richEdit1.setForeColor(Color.WINDOWTEXT);
		richEdit1.setLocation(new Point(16, 24));
		richEdit1.setSize(new Point(152, 416));
		richEdit1.setTabIndex(0);
		richEdit1.setText("");
		richEdit1.setScrollBars(RichEditScrollBars.BOTH);

		button1.setLocation(new Point(96, 456));
		button1.setSize(new Point(72, 24));
		button1.setTabIndex(1);
		button1.setText("确定");
		button1.addOnClick(new EventHandler(this.button1_click));

		button2.setLocation(new Point(16, 456));
		button2.setSize(new Point(72, 24));
		button2.setTabIndex(2);
		button2.setText("从文件打开");
		button2.addOnClick(new EventHandler(this.button2_click));

		button3.setLocation(new Point(408, 512));
		button3.setSize(new Point(96, 24));
		button3.setTabIndex(4);
		button3.setText("关闭");
		button3.addOnClick(new EventHandler(this.button3_click));

		/* @designTimeOnly openFileDialog1.setLocation(new Point(392, 152)); */

		richEdit2.setFont(Font.DEFAULT_GUI);
		richEdit2.setForeColor(Color.WINDOWTEXT);
		richEdit2.setLocation(new Point(8, 32));
		richEdit2.setSize(new Point(152, 152));
		richEdit2.setTabIndex(0);
		richEdit2.setText("");
		richEdit2.setReadOnly(true);
		richEdit2.setScrollBars(RichEditScrollBars.VERTICAL);

		label1.setLocation(new Point(48, 16));
		label1.setSize(new Point(64, 16));
		label1.setTabIndex(1);
		label1.setTabStop(false);
		label1.setText("工作状态");
		label1.setTextAlign(HorizontalAlignment.CENTER);

		label2.setLocation(new Point(216, 16));
		label2.setSize(new Point(64, 16));
		label2.setTabIndex(2);
		label2.setTabStop(false);
		label2.setText("控制台");
		label2.setTextAlign(HorizontalAlignment.CENTER);

		label3.setFont(new Font("幼圆", 11.0f, FontSize.POINTS, FontWeight.NORMAL, false, false, false, CharacterSet.DEFAULT, 0));
		label3.setLocation(new Point(168, 40));
		label3.setSize(new Point(152, 64));
		label3.setTabIndex(3);
		label3.setTabStop(false);
		label3.setText("请选择合适的遗传深度,它决定在多少步内若无更优解时,当前解为极优解。");
		label3.setTextAlign(HorizontalAlignment.CENTER);

		comboBox1.setEnabled(false);
		comboBox1.setLocation(new Point(192, 112));
		comboBox1.setSize(new Point(112, 20));
		comboBox1.setTabIndex(4);
		comboBox1.setText("请输入遗传深度");
		comboBox1.setItems(new Object[] {
						   "2000", 
						   "4000", 
						   "6000", 
						   "10000", 
						   "40000"});
		comboBox1.addOnSelectedIndexChanged(new EventHandler(this.comboBox1_selectedIndexChanged));

		button4.setEnabled(false);
		button4.setLocation(new Point(200, 152));
		button4.setSize(new Point(80, 24));
		button4.setTabIndex(5);
		button4.setText("开始");
		button4.addOnClick(new EventHandler(this.button4_click));

		richEdit3.setFont(Font.DEFAULT_GUI);
		richEdit3.setForeColor(Color.WINDOWTEXT);
		richEdit3.setLocation(new Point(24, 24));
		richEdit3.setSize(new Point(280, 80));
		richEdit3.setTabIndex(0);
		richEdit3.setText("");
		richEdit3.setReadOnly(true);
		richEdit3.setScrollBars(RichEditScrollBars.HORIZONTAL);

		label4.setFont(new Font("幼圆", 11.0f, FontSize.POINTS, FontWeight.NORMAL, false, false, false, CharacterSet.DEFAULT, 0));
		label4.setLocation(new Point(8, 16));
		label4.setSize(new Point(312, 160));
		label4.setTabIndex(0);
		label4.setTabStop(false);
		label4.setText("    从前,在A城有一个商人。有一天,他决定带上他所有的货物周游其他的N个城市做一笔大生意 。当找到地图准备确定路线时,他却犯了愁:怎样决定行程才能使车马费最少呢?(假设旅程长度与路费呈正比)                                     这个“旅行商问题”是典型的组合爆炸问题。当问题中的城市数目线性增加时,组合状态数呈指数增加。这使得一般的穷搜索法在此问题面前无能为力。                                          本程序使用人工智能中的遗传算法技术很好地解决了这个古老的问题。");

		button5.setLocation(new Point(232, 168));
		button5.setSize(new Point(80, 24));
		button5.setTabIndex(1);
		button5.setText("关于...");
		button5.addOnClick(new EventHandler(this.button5_click));

		this.setNewControls(new Control[] {
							button3, 
							groupBox4, 
							groupBox3, 
							groupBox2, 
							groupBox1});
		groupBox1.setNewControls(new Control[] {
								 button2, 
								 button1, 
								 richEdit1});
		groupBox2.setNewControls(new Control[] {
								 button5, 
								 label4});
		groupBox3.setNewControls(new Control[] {
								 button4, 
								 comboBox1, 
								 label3, 
								 label2, 
								 label1, 
								 richEdit2});
		groupBox4.setNewControls(new Control[] {
								 richEdit3});
	}

	/**
	 * The main entry point for the application. 
	 *
	 * @param args Array of parameters passed to the application
	 * via the command line.
	 */
	public static void main(String args[])
	{
		Application.run(new Form1());
	}
}

⌨️ 快捷键说明

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