📄 algosimulator.java
字号:
} public void actionPerformed(ActionEvent evt) { if (evt.getActionCommand().equals("LoadMaze")) { Utility.show("Loading maze"); //fileChooser fc = new JFileChooser("./mazes/"); int returnVal = fc.showOpenDialog(AlgoSimulator.this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { File file = fc.getSelectedFile(); FileInputStream fis = new FileInputStream(file); GZIPInputStream gzis = new GZIPInputStream(fis); ObjectInputStream in = new ObjectInputStream(gzis); myMaze = (Maze)in.readObject(); in.close(); mazeStatus = " [ "+myMaze.width+" X "+myMaze.height+" Maze, Wall Penalty:"+((Wall)myMaze.walls.get(0)).penalty +"] | "; if(algoType==Algorithms.ValueIter) basicAlgStatus = " Value Iteration"; else basicAlgStatus = " Policy Iteration"; additionalAlgStatus = "-> Click Initialise..."; sqSize = (int)Math.min(Math.floor((this.getSize().width-X-10)/myMaze.width), Math.floor((this.getSize().height-Y-10)/myMaze.height)); if(sqSize>200) sqSize=200; sqSizeTextField.setText(""+sqSize); } catch(Exception e) { Utility.show(e.getMessage()); } alg=null; } repaint(); } else if(evt.getActionCommand().equals("Initialize") && myMaze!=null) { Utility.show("Initialize"); double pjog = Double.parseDouble(pjogTextField.getText()); double precision = Double.parseDouble(jConverErrorTextField.getText()); additionalAlgStatus = ""; if(algoType == Algorithms.ValueIter) { alg = new ValueIteration(myMaze,pjog,precision); } if(algoType == Algorithms.PolicyIter) { int valueLimit = Integer.parseInt(jVFLimitTextField.getText()); int iterLimit = Integer.parseInt(jIterLimitTextField.getText()); alg = new PolicyIteration(myMaze,pjog,precision,valueLimit,iterLimit); } repaint(); } else if(evt.getActionCommand().equals("Update")) { if(algoType == Algorithms.ValueIter) { alg.setProperty(ValueIteration.Properties.PJOG, pjogTextField.getText()); alg.setProperty(ValueIteration.Properties.ConvergenceError, jConverErrorTextField.getText()); } else { alg.setProperty(PolicyIteration.Properties.PJOG, pjogTextField.getText()); alg.setProperty(PolicyIteration.Properties.ConvergenceError, jConverErrorTextField.getText()); alg.setProperty(PolicyIteration.Properties.ValueFunctionLimit,jVFLimitTextField.getText()); alg.setProperty(PolicyIteration.Properties.IterationLimit,jIterLimitTextField.getText()); } } else if(evt.getActionCommand().equals("Step")) { Utility.show("step"); if(alg!=null) { if(alg.step()) { additionalAlgStatus = " -- Converged after "+alg.getNumOfIters()+" steps!!"; additionalAlgStatus += " TimeTaken: "+alg.getTime()+"ms"; } else additionalAlgStatus = " -- "+alg.getNumOfIters()+" steps."; } repaint(); } else if (evt.getActionCommand().equals("Execute")) { Utility.show("execute"); int delay = Integer.parseInt(jDelayTextField.getText()); if(alg!=null) { while(!alg.step()){ additionalAlgStatus = " -- "+alg.getNumOfIters()+" steps."; myUpdate(); Utility.delay(delay); } additionalAlgStatus = " -- Converged after "+alg.getNumOfIters()+" steps!!"; additionalAlgStatus += " TimeTaken: "+alg.getTime()+"ms"; myUpdate(); } } else if(evt.getActionCommand().equals("Refresh")) { Utility.show("Refresh"); repaint(); } else if(evt.getSource().getClass().getName().equals("javax.swing.JTextField")){ repaint(); //System.out.println(sqSizeTextField.getText()); } else if(evt.getSource().getClass().getName().equals("javax.swing.JCheckBox")){ JCheckBox jcb = (JCheckBox)evt.getSource(); if(jcb.getText().equals("Show Values")) ShowValue = jcb.isSelected(); if(jcb.getText().equals("Show Policy")) ShowPolicy = jcb.isSelected(); repaint(); } } int sqSize=40; int X= 192; int Y= 100; Color GoldColor = new Color(200,100,55); public void paint(Graphics g) { super.paint(g); this.myUpdate(g); } public void myUpdate() { myUpdate(getGraphics()); } private void myUpdate(Graphics g) { jSeparator1.setSize(4,jPanel.getBounds().height-2); jStatusLabel.setText(mazeStatus+basicAlgStatus+additionalAlgStatus); showGrid(getGraphics()); } public void showGrid(Graphics g) { sqSize = Integer.parseInt(sqSizeTextField.getText()); if(alg!=null) { drawInfo(g); } if(myMaze!=null) { if(alg==null){ g.setColor(new Color(220,220,220)); g.fillRect(X,Y,sqSize*myMaze.width,sqSize*myMaze.height); drawGoal(g); } drawMaze(g); drawWalls(g); } } private void drawMaze(Graphics g) { g.setColor(Color.black); for (int counter = 0 ; counter <= myMaze.width; counter++) { g.drawLine(X+sqSize*counter,Y+0,X+sqSize*counter,Y+sqSize*myMaze.height); } for (int counter = 0 ; counter <= myMaze.height; counter++) { g.drawLine(X+0,Y+sqSize*counter,X+sqSize*myMaze.width,Y+sqSize*counter); } } private void drawGoal(Graphics g) { Vector goals = myMaze.goals; State s; for (int i=0;i<goals.size();i++) { s = (State)goals.get(i); g.setColor(GoldColor); g.fillRect(X+s.x*sqSize+1, Y+(myMaze.height-1-s.y)*sqSize+1, sqSize-2, sqSize-2); } } private void drawWalls(Graphics g) { GraphicsUtil gr = new GraphicsUtil(); int aX, aY, bX, bY; //start and end points of the wall for (int i=0;i<myMaze.walls.size();i++) { Wall w = (Wall)myMaze.walls.get(i); int nodeX = w.x; int nodeY = w.y; switch(w.dir) { case Wall.UP: aX = nodeX*sqSize; bX = (nodeX+1)*sqSize; aY = (myMaze.height - nodeY - 1)*sqSize; bY = (myMaze.height - nodeY - 1)*sqSize; GraphicsUtil.drawLine(g,X+aX,Y+aY,X+bX,Y+bY,5); break; case Wall.DOWN: aX = nodeX*sqSize; bX = (nodeX+1)*sqSize; aY = (myMaze.height - nodeY)*sqSize; bY = (myMaze.height - nodeY)*sqSize; GraphicsUtil.drawLine(g,X+aX,Y+aY,X+bX,Y+bY,5); break; case Wall.RIGHT: aX = (nodeX+1)*sqSize; bX = (nodeX+1)*sqSize; aY = (myMaze.height-nodeY-1)*sqSize; bY = (myMaze.height-nodeY)*sqSize; GraphicsUtil.drawLine(g,X+aX,Y+aY,X+bX,Y+bY,5); break; case Wall.LEFT: aX = (nodeX)*sqSize; bX = (nodeX)*sqSize; aY = (myMaze.height-nodeY)*sqSize; bY = (myMaze.height-nodeY-1)*sqSize; // Utility.show("left wall ax,ay,bx,by= "+aX+","+aY+","+bX+","+bY); GraphicsUtil.drawLine(g,X+aX,Y+aY,X+bX,Y+bY,5); break; } } } private void drawInfo(Graphics g) { ValueFunction valuefunc = alg.getValueFunction(); double[][] values = valuefunc.stateValue; int max = 1+(int)Math.ceil(valuefunc.getMax()); int[][] policy = alg.getPolicy(); for (int xval=0 ; xval <myMaze.width; xval++) { for (int y=0; y < myMaze.height; y++) { int yval = myMaze.height-1-y; if (values[xval][y] > 0) { int red = 155-Math.min((int)(255.0*(values[xval][y])/max),155); int green = 155-Math.min((int)(255.0*(values[xval][y])/max),155); int b = 255-Math.min((int)(255.0*(values[xval][y])/max),220); g.setColor(new Color(red,green,b)); } else g.setColor(GoldColor); g.fillRect(X+xval*sqSize+1,Y+yval*sqSize+1,sqSize-1,sqSize-1); g.setColor(Color.white); if(ShowValue) g.drawString(df.format(values[xval][y]),X+xval*sqSize+5,Y+(yval+1)*sqSize-5); if(ShowPolicy) { int x1 = xval*sqSize+sqSize/2; int y1 = yval*sqSize+sqSize/2; int x2 = x1; int y2 = y1; switch (policy[xval][y]) { case Action.UP: y2=yval*sqSize; break; case Action.LEFT: x2=xval*sqSize; break; case Action.DOWN: y2=yval*sqSize+sqSize; break; case Action.RIGHT: x2=xval*sqSize+sqSize; break; default: continue; } g.drawLine(X+x1,Y+y1,X+x2,Y+y2); } } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -