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

📄 climbsearch.java

📁 用爬山法求f(x,y)=1/(x2+y2+2)的最大值
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ClimbSearch extends JFrame implements ActionListener{
	private Graph graph = new Graph();
	private JPanel control;
	private JButton DButton,CButton;
	private JTextArea display = new JTextArea(3,3);
	private int mouseX,mouseY;
	private boolean pressed = false;
	private final int ticX = graph.returnTicX(),
					  ticY = graph.returnTicY();
	private final double ratioX = ticX,
						 ratioY = ticY,
						 MIN = 0.000001;

	public ClimbSearch(){
		initControls();
        setSize(500,650);
        setVisible(true);
		graph.addMouseListener(
			new MouseAdapter(){
				public void mousePressed(MouseEvent e){
					if(!pressed){
						Graphics g = graph.getGraphics();
						Dimension size = graph.getSize();
						g.translate(size.width/2,size.height/2);
						mouseX = e.getX() - size.width/2;
						mouseY = e.getY() - size.height/2;
						g.setColor(Color.red);
						g.drawOval(mouseX,mouseY,3,3);
						pressed = true;
					}
				}
			}
		);
	}
	
	public void initControls(){
		control= new JPanel();
		control.setBackground(new Color(255 - 90,255 - 145,255 - 30));
		DButton = new JButton("爬山Start!");
		CButton = new JButton("Clear!");
		control.add(DButton);
		control.add(CButton);
		control.add(display);
		DButton.addActionListener(this);
		CButton.addActionListener(this);
		getContentPane().add(graph,"Center");
		getContentPane().add(control,"South");
		display.append("请首先用鼠标在绘图区点击,生成爬山开始点!!!"+'\n'+"然后再进行爬山。");
		display.setEditable(false);
		setTitle("数值算法:爬山搜索(刘欣宇 050950713)");     //title
	}
	
	public double f(double x,double y){
		return 1 / (x * x + y * y + 2);
	}

	public double fx(double x,double y){
		double rec = x * x + y * y + 2;
		return -2 * x / (rec * rec);
	}

	public double fy(double x,double y){
		double rec = x * x + y * y + 2;
		return -2 * y / (rec * rec);
	}

	public double fxy2(double x,double y){
		double rec =  x * x + y * y + 2;
        return 4 * (x * x + y * y) / Math.pow(rec,4);
	}

	public static void main(String args[]){
		ClimbSearch climbsearch = new ClimbSearch();
		climbsearch.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	
	public void draw(){
		if(mouseX!=0 && mouseY!=0){
			Point start = new Point(mouseX,mouseY);
			Graphics g = graph.getGraphics();
			Dimension size = graph.getSize();
			g.translate(size.width / 2,size.height / 2);
			int startX = start.x,startY = start.y,tempX = 0,tempY = 0,output = 0;
			double x = startX / ratioX,y = startY / ratioX,step = 1.0;
			g.setColor(Color.yellow.brighter());
			g.drawString("StartPoint (" + startX / ratioX + "," + -startY / ratioY + ")",startX,startY);
			g.fillOval(startX,startY,4,4);
			
			while(fxy2(x,y) > MIN){
				output = (int)(f(x,y) * 10);
				switch(output){                                //变步长
					case 0:step = 2.0;break;
					case 1:step = 1.8;break;
					case 2:step = 1.7;break;
					case 3:step = 1.6;break;
					case 4:step = 1.5;break;
					case 5:step = 1.4;break;                   //步长随着f的增大而变小
				}
				tempX = (int)(x * ratioX);
				tempY = (int)(y * ratioY);
				x = x + step * fx(x,y);
				y = y + step * fy(x,y);
				startX = (int)(x * ratioX);
				startY = (int)(y * ratioY);

				try{
					Thread.sleep(30);
					g.drawLine(tempX,tempY,startX,startY);
				}catch(InterruptedException e){}
			}
			g.fillOval(tempX,tempY,4,4);
			g.drawString("End Point (" + startX + "," + startY + ")",startX + 5,startY - 5);
			g.setColor(Color.green);
			g.drawString("f(x,y) 的最大值为 f(x,y)=" + f(startX,startY),startX + 5,-startY + 40);
		}
	/*	else
			JOptionPane.showMessageDialog(this,"请首先用鼠标在绘图区点击,生成爬山开始点!!!");*/
	}

	public void actionPerformed(ActionEvent e){
		if(e.getSource() == DButton){
			draw();
		}
		if(e.getSource() == CButton){
			repaint();
			pressed = false;
		}
	}
}

class Graph extends JPanel{
	private final int ticX = 50,ticY = 50;
	private final double ratioX = ticX,
						 ratioY = ticY;

	public int returnTicX(){
		return ticX;
	}

	public int returnTicY(){
		return ticY;
	}

	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Color color = new Color(255 - 90,255 - 145,255 - 30);
		setBackground(color);
		Dimension size = this.getSize();
		g.setColor(Color.white);
		g.drawString("f(x,y) = 1 / ( x^2+y^2+2 ) ",10,15);
		g.translate(size.width / 2,size.height / 2);
		drawFxy(g);
		drawXYAxes(g);
	}//paintComponent()

	private void drawFxy(Graphics g){
		for(int k = -170;k <= 0;k+=5){
			float red = 230f + (float)k / 200f *  170f,
				  gre = 180f + (float)k / 200f *  40f,
				  blu =  90f + (float)k / 200f *  70f;
			Color color = new Color((int)(250 - red),(int)(250 - gre),(int)(250 - blu));
			g.setColor(color);
			g.fillOval(k,k,-2 * k,-2 * k);
		}
	}

	private void drawXYAxes(Graphics g){                   //Draw the X and Y axes
		g.setColor(Color.white);
		Dimension size = this.getSize();
		int hBound = 5 * ticX,
			vBound = 5 * ticX;

		for(int k = -vBound;k < vBound;k+=ticY){
			g.fillOval(-1,k,3,3);
		}
		g.drawLine(0,-vBound,0,vBound);                    //Draw Y axe

		for(int k = -hBound + ticX;k < hBound;k+=ticX){
			g.fillOval(k,-1,3,3);
		}
		g.drawLine(-hBound,0,hBound,0);                    //Draw X axe

//		g.drawString("0",-8,ticX / 3);
	
		g.drawString("-1",-ticX - 6,ticX / 3);             //Draw XR
		g.drawString("+1", ticX - 8,ticX / 3);

		g.drawString("-2",-2 * ticX - 6,ticX / 3);
		g.drawString("+2", 2 * ticX - 8,ticX / 3);
		
		g.drawString("-3",-3 * ticX - 6,ticX / 3);
		g.drawString("+3", 3 * ticX - 8,ticX / 3);

		g.drawString("-4",-4 * ticX - 6,ticX / 3);
		g.drawString("+4", 4 * ticX - 8,ticX / 3);

		g.drawString("x",hBound + 5,0);
		g.drawString("y",0,-vBound - 5);

		g.drawString("-1",-15, ticY + 6);                  //Draw YR
		g.drawString("+1",-15,-ticY + 6);

		g.drawString("-2",-15, 2 * ticY + 6);
		g.drawString("+2",-15,-2 * ticY + 6);

		g.drawString("-3",-15, 3 * ticY + 6);
		g.drawString("+3",-15,-3 * ticY + 6);

		g.drawString("-4",-15, 4 * ticY + 6);
		g.drawString("+4",-15,-4 * ticY + 6);

		g.drawLine(hBound - 5,-5,hBound,0);
		g.drawLine(hBound - 5,+5,hBound,0);

		g.drawLine(+5,-vBound + 5,0,-vBound);
		g.drawLine(-5,-vBound + 5,0,-vBound);
	}//drawXYAxes()
}

⌨️ 快捷键说明

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