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

📄 swingapplet.java

📁 用java写的一个强化学习程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//<APPLET CODE = "SwingApplet.class" WIDTH = 700 HEIGHT = 400 ></applet>

// load for early releases
//import com.sun.java.swing.*;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class SwingApplet extends JApplet implements ActionListener,Runnable{
	static final int BW=300, BH=300, BX=8, BY=8, NUM_WALLS=20,
		SAMP_W = 100, SAMP_H = 100;
	static final int DEF_EPOCHS = 50000;
	static final long DELAY=500;
	static int MAXX=400, MAXY=400;
	
	CatAndMouseGame game;
	CatAndMouseWorld trainWorld, playWorld; // seperate world from playing world
	RLController rlc;
	RLearner rl;
		
	JTabbedPane tabbedPane;
	Container instructions, playPanel, trainPanel, worldPanel;
	
	// world setting components
	JTextField rows, cols, obst;
	sampleWorlds samples;
	boolean[][] selectedWalls;
	ButtonGroup worldSelGroup;
	boolean sampleWorld=true, designWorld=false;
	
	// instructions components
	JLabel instructLabel, usageLabel;
	final String INSTRUCT_MESSAGE = "<html><p>This applet demonstrates how reinforcement <p>learning can be used to train an agent to play <p>a simple game.  In this case the game is Cat and <p>Mouse- the mouse tries to get to the cheese <p>and back to it's hole, the cat tries to catch the mouse.",
		USAGE_MESSAGE = "<html><p>You can train the agent by selecting the Train tab.  At <p>any time you can select the Play tab to see how <p>well the agent is performing!  Of course, the more <p>training, the better the chance the mouse <p>has of surviving :)";

	// train panel components
	public static final String START="S", CONT_CHECK="C";
	final String SETTINGS_TEXT = "These settings adjust some of the internal workings of the reinforcement learning algorithm.",
		SETTINGS_TEXT2 = "Please see the web pages for more details on what the parameters do.";
	JTextField alpha, gamma, epsilon, epochs, penalty, reward;
	JButton startTraining, stopTraining;
	JRadioButton softmax, greedy, sarsa, qlearn;
	JProgressBar progress;
	JLabel learnEpochsDone;	
	
	// play panel components
	JButton startbutt, stopbutt, pausebutt;
	boardPanel bp;
	public int mousescore=0, catscore =0;
	JLabel catscorelabel, mousescorelabel;
	final String MS_TEXT = "Mouse Score:", CS_TEXT = "Cat Score:";
	JSlider speed, smoothSlider;
	Image catImg, mouseImg;
	chartPanel graphPanel;
	JLabel winPerc;
			
	boardObject cat, mouse, cheese, back, hole, wall;

					
	public SwingApplet() {
		getRootPane().putClientProperty("defeatSystemEventQueueCheck",Boolean.TRUE);
		
	}
	
	public void init() {
		// load images
		catImg = getImage(getCodeBase(), "cat.gif");
		mouseImg = getImage(getCodeBase(), "mouse.gif");
		Image wallImg = getImage(getCodeBase(), "wall.gif");
		Image cheeseImg = getImage(getCodeBase(), "cheese.gif");
		Image floorImg = getImage(getCodeBase(), "floor.gif");
		
/*		Image catImg = getImage(ClassLoader.getSystemResource("cat.gif"));
		Image mouseImg = getImage(ClassLoader.getSystemResource("mouse.gif"));
		Image wallImg = getImage(ClassLoader.getSystemResource("wall.gif"));
		Image cheeseImg = getImage(ClassLoader.getSystemResource("cheese.gif"));*/

		// set up board objects
		cat = new boardObject(catImg);
		mouse = new boardObject(mouseImg);
		cheese = new boardObject(cheeseImg);
		back = new boardObject(floorImg);
		hole = new boardObject(Color.orange);
		wall = new boardObject(wallImg);
		
		// setup content panes
		tabbedPane = new JTabbedPane();
		
		//instructions = makeInstructions();
		worldPanel = makeWorldPanel();
		playPanel = makePlayPanel();
		trainPanel = makeTrainPanel();
		
		tabbedPane.addTab("World", worldPanel);
		tabbedPane.addTab("Play", playPanel);
		//tabbedPane.addTab("Instructions", instructions);
		tabbedPane.addTab("Train", trainPanel);
		tabbedPane.setSelectedIndex(0);

		// disable panes until world created
		tabbedPane.setEnabledAt(1,false);
		tabbedPane.setEnabledAt(2,false);
		
		// set up controls
		//setContentPane(new JPanel());
		//getContentPane().add(tabbedPane);
		
		getContentPane().add(tabbedPane);
	}

	public void worldInit(int xdim, int ydim, int numwalls) { 
		trainWorld = new CatAndMouseWorld(xdim, ydim,numwalls);
		gameInit(xdim,ydim);
	}
	public void worldInit(boolean[][] givenWalls) {
		int xdim = givenWalls.length, ydim = givenWalls[0].length;
		trainWorld = new CatAndMouseWorld(xdim, ydim,givenWalls);
		gameInit(xdim,ydim);		
	}
	private void gameInit(int xdim, int ydim) {
		// disable this pane
		tabbedPane.setEnabledAt(0,false);
		
		playWorld = new CatAndMouseWorld(xdim, ydim,trainWorld.walls);

		bp.setDimensions(xdim, ydim);
		
		rlc = new RLController(this, trainWorld, DELAY);
		rl = rlc.learner;
		rlc.start();
		
		game = new CatAndMouseGame(this, DELAY, playWorld, rl.getPolicy());
		game.start();

		// set text fields on panels
		penalty.setText(Integer.toString(trainWorld.deathPenalty));
		reward.setText(Integer.toString(trainWorld.cheeseReward));
		alpha.setText(Double.toString(rl.getAlpha()));
		gamma.setText(Double.toString(rl.getGamma()));
		epsilon.setText(Double.toString(rl.getEpsilon()));

		// enable other panes
		tabbedPane.setEnabledAt(1,true);
		tabbedPane.setEnabledAt(2,true);
		
		// switch active pane
		tabbedPane.setSelectedIndex(1);

		// set first position on board
		updateBoard();
	}
	
	// this method is triggered by SwingUtilities.invokeLater in other threads
	public void run() { updateBoard(); }
	
	/************ general functions ****************/
	public void updateBoard() {
		// update score panels
		mousescorelabel.setText(MS_TEXT+" "+Integer.toString(mousescore));
		catscorelabel.setText(CS_TEXT+" "+Integer.toString(catscore));
		if (game.newInfo) {
			updateScore();
			game.newInfo = false;
		}
		
		// update progress info
		progress.setValue(rlc.epochsdone);
		learnEpochsDone.setText(Integer.toString(rlc.totaldone));
		if (rlc.newInfo) endTraining();
		
		// update game board
		bp.clearBoard();

		// draw walls
		boolean[][] w = game.getWalls(); 
		for (int i=0; i<w.length; i++) {
			for (int j=0; j<w[0].length; j++) {
				if (w[i][j]) bp.setSquare(wall, i, j);
			}
		}

		// draw objects (cat over mouse over cheese)
		bp.setSquare(cheese, game.getCheese());
		bp.setSquare(mouse, game.getMouse());
		bp.setSquare(cat, game.getCat());
		//bp.setSquare(hole, game.getHole());
					
		// display text representation
		//System.out.println(bp);
		bp.repaint();
	}

	void doTraining() {
		// begin training
		int episodes = Integer.parseInt(epochs.getText());
		double aval = Double.parseDouble(alpha.getText());
		double gval = Double.parseDouble(gamma.getText());
		double eval = Double.parseDouble(epsilon.getText());
		int cval = Integer.parseInt(reward.getText());
		int dval = Integer.parseInt(penalty.getText());
				
		rl.setAlpha(aval);
		rl.setGamma(gval);
		rl.setEpsilon(eval);
				
		// disable controls
		startTraining.setEnabled(false);
		epochs.setEnabled(false);
		reward.setEnabled(false);
		penalty.setEnabled(false);
		alpha.setEnabled(false);
		gamma.setEnabled(false);
		epsilon.setEnabled(false);
		softmax.setEnabled(false);
		greedy.setEnabled(false);
		sarsa.setEnabled(false);
		qlearn.setEnabled(false);
				
		// fix progress bar
		progress.setMinimum(0);
		progress.setMaximum(episodes);
		progress.setValue(0);
				
		// enable stop button
		stopTraining.setEnabled(true);
				
		// start training
		trainWorld.cheeseReward = cval;
		trainWorld.deathPenalty = dval;
		rlc.setEpisodes(episodes);
	}

	void endTraining() {
		// stop training
		rlc.stopLearner();
		
		// enable buttons
		startTraining.setEnabled(true);
		epochs.setEnabled(true);
		reward.setEnabled(true);
		penalty.setEnabled(true);
		alpha.setEnabled(true);
		gamma.setEnabled(true);
		epsilon.setEnabled(true);
		softmax.setEnabled(true);
		greedy.setEnabled(true);
		sarsa.setEnabled(true);
		qlearn.setEnabled(true);

		// disable stop button
		stopTraining.setEnabled(false);
	}

	void updateScore() {
		double newScore = Math.round(1000*((double)mousescore)/(catscore + mousescore))/10;
		winPerc.setText(Double.toString(newScore)+"%");
		graphPanel.updateScores();
		graphPanel.repaint();
		game.gameActive = true;		
	}	
	/************ general functions ****************/

	/********** Methods to construct panels *************/
	Container makeWorldPanel() {
		JPanel worldPane = new JPanel();
		worldPane.setLayout(new BorderLayout());

		worldSelGroup = new ButtonGroup();
		
		worldPane.add(chooseWorld(), BorderLayout.CENTER);
		//worldPane.add(customWorld(), BorderLayout.EAST);
		JButton startbutt = new JButton("Click here to start!");
		startbutt.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				// selected world type, choose action
				if (sampleWorld) {
					worldInit(selectedWalls);
				} else if (designWorld) {
					// custom designed world
				
				} else {
					// random world
					worldInit(Integer.parseInt(cols.getText()),
						Integer.parseInt(rows.getText()),
						Integer.parseInt(obst.getText()));
				}
			}
		});
		worldPane.add(startbutt, BorderLayout.SOUTH);
		return worldPane;
	}
	
	Container customWorld() {
		JPanel pane = new JPanel();
		pane.setLayout(new BorderLayout());
		
		JRadioButton random = new JRadioButton("Random World");
		random.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				// enable obstacles field
				obst.setEnabled(true);
				designWorld=false;
				sampleWorld=false;
			}
		});
		worldSelGroup.add(random);
		pane.add(random, BorderLayout.NORTH);
		
		/*JRadioButton custom = new JRadioButton("Custom Design");
		custom.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				// enable obstacles field
				obst.setEnabled(false);
				designWorld=true;
				sampleWorld=false;
			}
		});*/
		
		// add controls to set dimensions
		JPanel labelpane = new JPanel();
		labelpane.setLayout(new GridLayout(0,2));

		//worldSelGroup.add(custom);
		
		//JPanel controls = new JPanel();
		//controls.setLayout(new GridLayout(0,1));
		rows = new JTextField(Integer.toString(BY), 20);
		cols = new JTextField(Integer.toString(BX), 20);
		obst = new JTextField(Integer.toString(NUM_WALLS), 20);
		
		labelpane.add(new JLabel("Rows:",JLabel.RIGHT));
		labelpane.add(rows);
		labelpane.add(new JLabel("Columns:",JLabel.RIGHT));
		labelpane.add(cols);
		labelpane.add(new JLabel("Obstacles:",JLabel.RIGHT));
		labelpane.add(obst);
		
		//labelpane.setBorder(BorderFactory.createTitledBorder("Custom World"));
		//labelpane.add(random);
		//labelpane.add(custom);

		pane.add(labelpane, BorderLayout.CENTER);
		//pane.add(controls, BorderLayout.EAST);
		//pane.add(labelpane);
		//pane.add(controls);
		
		return pane;	
	}
	
	Container chooseWorld() {
		JPanel pane = new JPanel();
		pane.setLayout(new GridLayout(0,3));

		// grab each sample
		samples = new sampleWorlds();
		for (int i=0; i<samples.numSamples(); i++) {
			JPanel thisPanel = new JPanel();
			thisPanel.setLayout(new BorderLayout());
			
			// set first as selected
			if (i==0) selectedWalls = samples.getWalls(i);
			
			JRadioButton b = new JRadioButton(samples.getTitle(i), (i==0));
			b.setHorizontalAlignment(SwingConstants.LEFT);
			b.setActionCommand(Integer.toString(i));
			b.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					int index = Integer.parseInt(e.getActionCommand());
					selectedWalls = samples.getWalls(index);
					sampleWorld=true;
				}
			});
			
			boolean[][] w = samples.getWalls(i);
			
			// create boardPanel object for this world
			boardPanel pic = new boardPanel(back, w.length, w[0].length,SAMP_W, SAMP_H);
			// add walls to panel
			for (int x=0; x<w.length; x++)
				for (int y=0; y<w.length; y++)
					if (w[x][y]) pic.setSquare(wall, x, y);
			
			worldSelGroup.add(b); // add to button group
			pic.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
			
			thisPanel.add(pic, BorderLayout.CENTER);
			thisPanel.add(b, BorderLayout.NORTH); // add to pane
			thisPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
			
			pane.add(thisPanel);
		}

		// add random world option
		pane.add(customWorld());
		pane.setBorder(BorderFactory.createTitledBorder("Choose World"));
		return pane;
	}
	
	Container makeInstructions() {
		JPanel pane = new JPanel();
		pane.setLayout(new GridLayout(2,1));
		
		instructLabel = new JLabel(INSTRUCT_MESSAGE);
		usageLabel = new JLabel(USAGE_MESSAGE);
		

⌨️ 快捷键说明

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