📄 accounting.txt
字号:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.net.URL;
import java.net.MalformedURLException;
import NetAgentQL1s1a ;
//====================================================================
public class NetAgentQL1s1aWindow extends Frame implements ActionListener {
//-----
NetAgentQL1s1a netAgent;
//----------------------------------------------
QLAgentCanvas agentCanvas;
FileDialog ParamLoadDialog;
FileDialog ParamSaveDialog;
FileDialog RewardLogFileDialog;
FileDialog StateActRewardLogFileDialog;
//----------
static int portNumber ; // TCP-IP port number
static String hostname ;
//-----------------------------------------
//-----------------------------------------
NetAgentQL1s1aWindow(){
super( "NetAgentQL1s1aWindow"); // Window Title
setFont( new Font( "Courier", Font.PLAIN, 12 )) ;
setSize( 550, 460 );
//-------Menu, MenuBar
Menu menu1 = new Menu("Files");
MenuItem LoadParamFile = new MenuItem("LoadParamFile");
MenuItem SaveParamFile = new MenuItem("SaveParamFile");
MenuItem SetRewardFile = new MenuItem("SetRewardFile");
MenuItem SetSARFile = new MenuItem("SetState-Action-RewardFile");
menu1.add( LoadParamFile );
menu1.add( SaveParamFile );
menu1.add( SetRewardFile );
menu1.add( SetSARFile );
//--------
MenuItem Start = new MenuItem("Start");
MenuItem Stop = new MenuItem("Stop");
MenuItem InitParam = new MenuItem("InitParam");
MenuItem quit = new MenuItem("quit");
Menu menu2 = new Menu("System");
menu2.add( Start );
menu2.add( Stop );
menu2.add( InitParam );
menu2.add( quit );
//---------
MenuItem step10 = new MenuItem("10steps");
MenuItem step100 = new MenuItem("100steps");
MenuItem step200 = new MenuItem("200steps");
MenuItem step500 = new MenuItem("500steps");
MenuItem step1000 = new MenuItem("1000steps");
MenuItem step2000 = new MenuItem("2000steps");
MenuItem step5000 = new MenuItem("5000steps");
MenuItem step10000 = new MenuItem("10000steps");
MenuItem step20000 = new MenuItem("20000steps");
MenuItem step50000 = new MenuItem("50000steps");
Menu menu3 = new Menu("TimeSteps");
menu3.add( step10 );
menu3.add( step100 );
menu3.add( step200 );
menu3.add( step500 );
menu3.add( step1000 );
menu3.add( step2000 );
menu3.add( step5000 );
menu3.add( step10000 );
menu3.add( step20000 );
menu3.add( step50000 );
//------------
MenuBar mbar = new MenuBar();
mbar.add( menu1 );
mbar.add( menu2 );
mbar.add( menu3 );
setMenuBar( mbar );
//-------
LoadParamFile.addActionListener(this);
SaveParamFile.addActionListener(this);
SetRewardFile.addActionListener(this);
SetSARFile.addActionListener(this);
Start.addActionListener(this);
Stop.addActionListener(this);
InitParam.addActionListener(this);
quit.addActionListener(this);
step10.addActionListener(this);
step100.addActionListener(this);
step200.addActionListener(this);
step500.addActionListener(this);
step1000.addActionListener(this);
step2000.addActionListener(this);
step5000.addActionListener(this);
step10000.addActionListener(this);
step20000.addActionListener(this);
step50000.addActionListener(this);
setVisible( true ); // show();
//-------Agent display initialize
agentCanvas = new QLAgentCanvas();
add( "Center", agentCanvas );
agentCanvas.start();
//--------Set FileDialog
ParamLoadDialog = new FileDialog( this, "Parameter: Load" );
ParamSaveDialog = new FileDialog( this, "Parameter: Save", FileDialog.SAVE );
RewardLogFileDialog = new FileDialog( this, "RewardLog: Save", FileDialog.SAVE );
StateActRewardLogFileDialog = new FileDialog( this, "State-Act-RewardLog: Save", FileDialog.SAVE );
//----------Agent
netAgent = new NetAgentQL1s1a( hostname, portNumber, this ) ;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
public static void main( String args[] ){
//------
if( args.length < 2 ){
System.out.println("NetAgentQL1s1aWindow <hostname> <TCP-IP port number> " );
return ;
}
hostname = args[0].toString() ;
portNumber = Integer.valueOf( args[1] ).intValue() ;
System.out.println("hostname = " + hostname );
System.out.println("TCP-IP port number = " + portNumber );
//--------------
NetAgentQL1s1aWindow nawin = new NetAgentQL1s1aWindow();
nawin.setVisible( true ); // show();
}
//-----------------------------------------
public void DrawQTable( double[][] qtable, int currentState, int previousState, int previousAction ){
agentCanvas.DrawQTable( qtable, currentState, previousState, previousAction );
}
//=======================================GUI EVENT
public void actionPerformed( ActionEvent e ){
if( e.getActionCommand().equals("InitParam")){
netAgent.initParam();
}else if( e.getActionCommand().equals("SetRewardFile")){
RewardLogFileDialog.show();
if( RewardLogFileDialog.getFile() == null ){return;}
netAgent.setRewardLogFilename( RewardLogFileDialog.getDirectory() + RewardLogFileDialog.getFile() );
}else if( e.getActionCommand().equals("SetState-Action-RewardFile")){
StateActRewardLogFileDialog.show();
if( StateActRewardLogFileDialog.getFile() == null ){ return ; }
netAgent.setStateActRewardLogFilename( StateActRewardLogFileDialog.getDirectory() + StateActRewardLogFileDialog.getFile() );
}else if( e.getActionCommand().equals("LoadParamFile")){
ParamLoadDialog.show();
if( ParamLoadDialog.getFile() == null ){ return ; }
netAgent.loadParam( ParamLoadDialog.getDirectory() + ParamLoadDialog.getFile() );
}else if( e.getActionCommand().equals("SaveParamFile")){
ParamSaveDialog.show();
if( ParamSaveDialog.getFile() == null ){ return ; }
netAgent.saveParam( ParamSaveDialog.getDirectory() + ParamSaveDialog.getFile() );
}else if( e.getActionCommand().equals("Start")){
if( !netAgent.isLocked() ){
netAgent.start() ;
}else{
System.out.println( "The agent has been running!");
}
}else if( e.getActionCommand().equals("Stop")){
netAgent.setMaxSteps( 0 );
}else if( e.getActionCommand().equals("10steps")){
netAgent.setMaxSteps( 10 );
}else if( e.getActionCommand().equals("100steps")){
netAgent.setMaxSteps( 100 );
}else if( e.getActionCommand().equals("200steps")){
netAgent.setMaxSteps( 200 );
}else if( e.getActionCommand().equals("500steps")){
netAgent.setMaxSteps( 500 );
}else if( e.getActionCommand().equals("1000steps")){
netAgent.setMaxSteps( 1000 );
}else if( e.getActionCommand().equals("2000steps")){
netAgent.setMaxSteps( 2000 );
}else if( e.getActionCommand().equals("5000steps")){
netAgent.setMaxSteps( 5000 );
}else if( e.getActionCommand().equals("10000steps")){
netAgent.setMaxSteps( 10000 );
}else if( e.getActionCommand().equals("20000steps")){
netAgent.setMaxSteps( 20000 );
}else if( e.getActionCommand().equals("50000steps")){
netAgent.setMaxSteps( 50000 );
}else if( e.getActionCommand().equals("quit")){
netAgent.setMaxSteps( 0 );
System.exit( 0 );
}
}
}
//==================================================================
class QLAgentCanvas extends Canvas implements Runnable{
Thread runner;
int SleepTime;
Font theFont ; //
Image offscreenImage;
Graphics offscreenGraphics;
int cwidth, cheight ;
int step;
int reward;
//-----------------------------------------
QLAgentCanvas(){
//------
SleepTime = 200;
theFont = new Font("Courier", Font.PLAIN, 12 ); // "TimesRoman",Font.BOLD,16);
cwidth = 16; cheight = 16;
}
//-----------------------------------------
public void start(){
if( runner == null ){
runner = new Thread( this );
runner.start();
}
}
//-----------------------------------------
public void run(){
while( true ){
repaint();
try{ Thread.sleep( SleepTime ); }
catch ( InterruptedException e ){ }
}
}
//-----------------------------------------
public void setSleepTime( int t ){ SleepTime = t;}
//-----------------------------------------
public void paint( Graphics g ){
//-------offscreen
if( (cwidth != this.getSize().width)||(cheight != this.getSize().height )){
cwidth = this.getSize().width;
cheight = this.getSize().height;
offscreenImage = createImage( this.getSize().width, this.getSize().height );
offscreenGraphics = offscreenImage.getGraphics();
offscreenGraphics.setFont( theFont );
}
//-----------offscreen to DC
g.drawImage( offscreenImage, 0, 0, this );
//----------
}
public void update( Graphics g ){
paint( g );
}
//---------
public void DrawQTable( double[][] qtable, int currentState, int previousState, int previousAction ){
// this.getSize().width ;
// this.getSize().height ;
//----------Graphic clear
offscreenGraphics.setColor( Color.black );
offscreenGraphics.fillRect( 0, 0, this.getSize().width, this.getSize().height );
int xi = 4, yi = 0, currentX=0, currentY=0, prevX=0, prevY=0 ;
for( int bi = 0; bi < qtable.length; bi++ ){
if( bi == previousState ){
prevX = xi; prevY = yi;
offscreenGraphics.setColor( Color.red );
// offscreenGraphics.drawRect( xi -1, yi -1 +24 -6, 89, (qtable[0].length * 15) +1 );
offscreenGraphics.drawRect( xi +26, yi + 24 -6 +(15 * previousAction), 60, 15 );
}
if( bi == currentState ){
currentX = xi; currentY = yi;
offscreenGraphics.setColor( Color.yellow );
offscreenGraphics.drawRect( xi -1, yi -1 + 24 - 6, 89, (qtable[0].length * 15) +1 );
System.out.println("Current State " + bi);
}
String stateStr = "S" + bi ;
offscreenGraphics.setColor( Color.white );
offscreenGraphics.drawString( stateStr, xi, yi + 24 + 6 );
int maxAct = 0;
double maxQ = qtable[bi][0] ;
for( int act = 1; act < qtable[bi].length; act++ ){
if( qtable[bi][act] > maxQ ){ maxAct = act; maxQ = qtable[bi][act];}
}
for( int act = 0; act < qtable[bi].length; act++ ){
StringBuffer strBuf = new StringBuffer();
double v = qtable[bi][act] ;
if( Math.abs( v ) < 0.001 ){ v = 0;}
strBuf.append( "A" + act + " " + v + " " );
strBuf.setLength( 8 );
if( act == maxAct ){
offscreenGraphics.setColor( Color.white );
}else{
offscreenGraphics.setColor( Color.green );
}
offscreenGraphics.drawString( strBuf.toString(), xi + 26, yi + 24 + 6 + (act * 15) );
}
xi += 110 ;
if( xi + 55 > this.getSize().width ){xi = 4; yi += (qtable[0].length * 20) ;}
}
offscreenGraphics.setColor( Color.yellow );
offscreenGraphics.drawLine( prevX +12, prevY +24, currentX +12, currentY +24 );
repaint() ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -