/* Grid World v. 0.2 * Author: Carlo Comin carlo.comin@studenti.unimi.it * * This code is released under GPL3 * * Date: Sept 29 2010 5:06pm * */ import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.JScrollPane; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.Box; import javax.swing.JTextPane; import java.awt.Font; public class GridWorld { private Integer gridsize; private JFrame frame; private static JTable table; private JTable table2; private JTextField textField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { GridWorld window = new GridWorld(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public GridWorld() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 800, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); final JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(276, 10, 514, 263); frame.getContentPane().add(scrollPane); final JScrollPane scrollPane_1 = new JScrollPane(); scrollPane_1.setBounds(276, 305, 514, 263); frame.getContentPane().add(scrollPane_1); textField = new JTextField(); gridsize = new Integer(5); textField.setText(Integer.toString(gridsize)); textField.setBounds(77, 57, 67, 22); frame.getContentPane().add(textField); textField.setColumns(10); final JButton btnPolicy = new JButton("Compute Policy"); btnPolicy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent argo0) { if(!checkInputOK()) JOptionPane.showMessageDialog(frame, "Insert at least one terminal state."); else{ Object[][] rewardTable = getRewardTable(); Object[][] policyTable = ValueIteration(rewardTable); String[] columnNames = new String[gridsize]; for(int i=0; i < gridsize; i++) columnNames[i] = ""; table2 = new JTable(policyTable, columnNames); table2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); scrollPane_1.setViewportView(table2); } } }); btnPolicy.setEnabled(false); btnPolicy.setBounds(140, 305, 130, 29); frame.getContentPane().add(btnPolicy); JButton btnCreate = new JButton("Create Grid"); btnCreate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { gridsize = Integer.parseInt(textField.getText()); String[] columnNames = new String[gridsize]; Object[][] data = new Object[gridsize][gridsize]; for(int i=0; i < gridsize; i++){ columnNames[i] = ""; for(int j=0; j < gridsize; j++) data[i][j] = "*"; } table = new JTable(data,columnNames); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); scrollPane.setViewportView(table); btnPolicy.setEnabled(true); } }); Box verticalBox = Box.createVerticalBox(); verticalBox.setBounds(0, 0, 1, 1); frame.getContentPane().add(verticalBox); btnCreate.setBounds(169, 53, 101, 29); frame.getContentPane().add(btnCreate); JLabel lblGridSize = new JLabel("Grid Size"); lblGridSize.setBounds(10, 60, 61, 16); frame.getContentPane().add(lblGridSize); JLabel lblGridWorld = new JLabel("Grid World 0.2"); lblGridWorld.setBounds(10, 10, 90, 22); frame.getContentPane().add(lblGridWorld); JLabel lblByCarlocharlie = new JLabel("by Charlie #754846"); lblByCarlocharlie.setBounds(106, 13, 164, 16); frame.getContentPane().add(lblByCarlocharlie); JTextPane txtpnGridCellLegend = new JTextPane(); txtpnGridCellLegend.setEnabled(false); txtpnGridCellLegend.setFont(new Font("Lucida Grande", Font.PLAIN, 14)); txtpnGridCellLegend.setEditable(false); txtpnGridCellLegend.setText("Grid Cell Legend\r* = Normal cell\rT = Terminal cell\rO = Obstacle cell\r\rYou can add as many Terminal and Obstacles cells as you want.\rThen click Compute Policy.\rEnjoy!"); txtpnGridCellLegend.setBounds(23, 96, 232, 159); frame.getContentPane().add(txtpnGridCellLegend); JTextPane txtpnPolicyLegendL = new JTextPane(); txtpnPolicyLegendL.setEnabled(false); txtpnPolicyLegendL.setText("Policy Legend\rL = Left\tR = Right\rU = Up\tD = Down\rT = Terminal O = Obstacle"); txtpnPolicyLegendL.setEditable(false); txtpnPolicyLegendL.setBounds(23, 349, 232, 72); frame.getContentPane().add(txtpnPolicyLegendL); } private Object[][] getRewardTable(){ Object [][] ret = new Object[gridsize][gridsize]; for(int i=0; i < gridsize; i++){ for(int j=0; j < gridsize; j++){ String val = (String)table.getValueAt(i, j); if(val.matches("O")||val.matches("o")) ret[i][j] = new Integer(-10000); else ret[i][j]= new Integer(-1); } } return ret; } private int reward(int i, int j, Object [][] rewardTable){ int ret = (Integer)rewardTable[i][j]; return ret; } private int UpdatedValue(int i, int j, int [][] Value, Object [][] rewardTable){ String temp = (String)table.getValueAt(i, j); if(temp.matches("T")||temp.matches("t")){ return 0; } int max=-32000; /* action: code meaning * 0 left * 1 right * 2 up * 3 down * */ for(int action=0; action < 4; action++){ int next_i=i, next_j=j; if(action==0 && j!=0) { next_i=i; next_j=j-1; } else if(action==1 && j!=gridsize-1){ next_i=i; next_j=j+1; } else if(action==2 && i!=0){ next_i=i-1; next_j=j; } else if(action==3 && i!=gridsize-1){ next_i=i+1; next_j=j; } max=Math.max(max, reward(next_i, next_j, getRewardTable()) + Value[next_i][next_j]); } return max; } private Object [][] ComputePolicy(int [][] Value, Object [][] rewardTable){ Object [][] policy = new Object[gridsize][gridsize]; for(int i=0; i < gridsize; i++) for(int j=0; j < gridsize; j++){ String temp = (String)table.getValueAt(i, j); if(temp.matches("T")||temp.matches("t")) policy[i][j]="T"; else if(temp.matches("O")||temp.matches("o")) policy[i][j]="O"; else{ int max=-32000; int oldmax=max, next_i,next_j; String action="L"; next_i=i; next_j=j; if(j!=0) { next_i=i; next_j=j-1; } max=reward(next_i, next_j, getRewardTable()) + Value[next_i][next_j]; if(max>oldmax) policy[i][j]=action; if(max==oldmax) policy[i][j] = policy[i][j] + " " + action; if(maxoldmax) policy[i][j]=action; if(max==oldmax) policy[i][j] = policy[i][j] + " " + action; if(maxoldmax) policy[i][j]=action; if(max==oldmax) policy[i][j] = policy[i][j] + " " + action; if(maxoldmax) policy[i][j]=action; if(max==oldmax) policy[i][j] = policy[i][j] + " " + action; if(max tetha); return ComputePolicy(Value, rewardTable); } private boolean checkInputOK(){ for(int i=0; i