/* * Class GridPuzzleGenerator */ import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.io.*; import java.util.Calendar; import java.util.Vector; /**
Direct Known Subclasses:
GridPuzzleGenerator.Canvas

* GridPuzzleGenerator generates an MDP Grid-type problem input files to be used by the PosgInputFileParser object * @author Mark Gruman -- UMass RBR Lab
* Computer Science Department
* University of Massachusetts Amherst
* @version 1.0
* Created on July 08, 2004

*/ public class GridPuzzleGenerator { private Canvas m_canvas; private String file_name; private BufferedWriter m_log_writer; private GridInputFileParser m_parser; private int rows, cols; private int m_agents; private int m_states; private String[] m_acs; private String[] m_obs; private String[][] visible_obs; private double[] t_prob; public static final int DECIMALS = 5; /** @return the number of rows specified by the user */ public int getRows() { return rows; } /** @return the number of columns specified by the user */ public int getCols() { return cols; } /** This method sets the total number of possible states */ private void setStates() { m_states = (int)Math.pow(((getRows()-1)*(getCols()-1)),2); try { m_log_writer.write("States: " + m_states); m_log_writer.newLine(); m_log_writer.flush(); m_parser.parse(); } catch (IOException ioe) { System.out.println(ioe); } } /** This method defines all agents' actions*/ private void createActions() { String acs = "up down left right stay"; writeParam("Actions: ", acs); m_acs = acs.split("\\s"); } /** This method writes parameters and their corresponding values to the pre-defined text file * @param a the paramter tag ("actions" or "observations") * @param s the enumerated parameter values */ private void writeParam(String a, String s) { try { m_log_writer.newLine(); for (int i=0; i= epsilon return false; return true; } // ************************** // *** Child Class Canvas *** // ************************** /** Canvas is a GUI for the GridPuzzleGenerator class */ class Canvas extends Thread implements ActionListener, MouseListener, MouseMotionListener { private JFrame m_frame; // main frame window private JPanel m_screen1, m_screen3; // the viewable screens private Grid obs_grid, m_screen2; // the viewable grids int x_off=0, y_off=0, x_start=0, y_start=0; // offsets and starting locations int agents; Point a_cord, o_cord; String s; /** This method displays the appropriate screen when buttons are clicked * @param e an ActionEvent invoked by the JVM when a button is clicked */ public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Generate Grid")) { if (processScreenOne()) { setStates(); createScreen2(); m_frame.getContentPane().remove(m_screen1); m_screen2.setOpaque(false); m_frame.getContentPane().add(m_screen2); m_frame.pack(); m_frame.getContentPane().add(obs_grid); m_frame.validate(); } else System.out.println("Insufficient Data or Incorrect Data Format.\r\nPlease remember to enter the entire URL in the filename field"); } else if (e.getActionCommand().equals("Process Grid")) { if (processScreenTwo()) { setStart(); createActions(); createObservations(); createScreen3(); m_frame.getContentPane().remove(m_screen2); m_frame.getContentPane().remove(obs_grid); m_frame.getContentPane().add(m_screen3); m_frame.pack(); } } else if (e.getActionCommand().equals("Finish and Exit")) { if (processScreenThree()) { setTransProbs(); setObsrvProbs(); setRwrdProb(); System.exit(0); /* createScreen1(); m_frame.getContentPane().remove(m_screen3); m_frame.getContentPane().add(m_screen1); m_frame.pack(); */ } } } /** This method sets the specified obervations in the observations grid * @param e a MouseEvent invoked by the JVM when the mouse is clicked */ public void mouseClicked(MouseEvent e) { if (obs_grid.containsPoint(e.getX(), e.getY())) { obs_grid.fillObs(e.getX(), e.getY()); m_frame.repaint(); } } /* @param e a MouseEvent invoked by the JVM when the mouse enters */ public void mouseEntered(MouseEvent e) { } /* @param e a MouseEvent invoked by the JVM when the mouse exits */ public void mouseExited(MouseEvent e) { } /** This method record the starting location of a moving Agent or Observation marker * and creates a new marker of the same type in its origin if it has moved away from it * @param e a MouseEvent invoked by the JVM when the mouse is pressed */ public void mousePressed(MouseEvent e) { JComponent c = (JComponent)e.getSource(); if (c == obs_grid) return; x_off = e.getX(); y_off = e.getY(); s = ((JLabel)e.getComponent()).getText(); x_start = c.getX(); y_start = c.getY(); if (x_start == (int)a_cord.getX() && (y_start == (int)a_cord.getY() || y_start == (int)o_cord.getY())) insertLabel(s, m_screen2, new Point(x_start, y_start), 44); } /** This method sets the permanent new location of a moving Agent or Observation marker * if its move was valid * @param e a MouseEvent invoked by the JVM when the mouse is released */ public void mouseReleased(MouseEvent e) { JComponent c = (JComponent)e.getSource(); if (c == obs_grid) return; if (!m_screen2.contains(c)) { //released off the grid if (m_screen2.containsPoint(x_start, y_start)) { m_screen2.getData()[y_start/100][x_start/100] = 0; agents++; } m_screen2.remove(c); m_frame.repaint(); } else if (x_start/100 == c.getX()/100 && y_start/100 == c.getY()/100) { //released onto same place center(c); } else if (m_screen2.getData()[c.getY()/100][c.getX()/100] != 0) { //released onto another taken square if (m_screen2.containsPoint(x_start, y_start)) insertLabel(s, m_screen2, new Point(x_start, y_start), 44); m_screen2.remove(c); m_frame.repaint(); } else if (s.equals("A") && agents == 0 && !m_screen2.containsPoint(x_start, y_start)) { //too many agents defined System.out.println("Cannot add agent... exceeds agent limit"); if (m_screen2.containsPoint(x_start, y_start)) insertLabel(s, m_screen2, new Point(x_start, y_start), 44); m_screen2.remove(c); m_frame.repaint(); } else { center(c); if (m_screen2.containsPoint(x_start, y_start)) { m_screen2.getData()[y_start/100][x_start/100] = 0; agents++; } if (s.equals("A")) { m_screen2.getData()[c.getY()/100][c.getX()/100] = 1; agents--; } else if (s.equals("O")) m_screen2.getData()[c.getY()/100][c.getX()/100] = -1; } } /** This method sets the temporary new location of a moving Agent or Observation marker * so that it may be displayed in its new location * @param e a MouseEvent invoked by the JVM when the mouse is dragged */ public void mouseDragged(MouseEvent e) { JComponent c = (JComponent)e.getSource(); c.setBounds(c.getX()+e.getX()-x_off, c.getY()+e.getY()-y_off, c.getPreferredSize().width, c.getPreferredSize().height); } /* @param e a MouseEvent invoked by the JVM when the mouse moves */ public void mouseMoved(MouseEvent e) { } /** This method centers the Agent or Observation marker in the center of its grid cell * @param c the component representing the Agent or Observation marker */ private void center(JComponent c) { int x = c.getX() - c.getX()%100 + 50 - c.getPreferredSize().width/2; int y = c.getY() - c.getY()%100 + 50 - c.getPreferredSize().height/2; c.setBounds(x, y, c.getPreferredSize().width, c.getPreferredSize().height); } /** The constructor creates a viewable frame and calls for the creation * of all relating screens */ private Canvas() { try { JFrame.setDefaultLookAndFeelDecorated(true); m_frame = new JFrame("Grid Puzzle Generator"); m_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); m_frame.setVisible(true); } catch (HeadlessException he) { } agents = m_agents; createScreen1(); } /** This method creates the first screen for the MdpPuzzleGenerator * This screen allows the user to specify the discount, values, grid rows, grid columns, * and file. */ private void createScreen1() { m_screen1 = new JPanel(); m_screen1.setLayout(null); int x_offset = 25, y_offset = 25; int x_tot=0, y_tot=0; String[] titles = { " ", "File: ", "Rows: ", "Cols: ", "Discount: ", "Values: " }; String[] val = { " -- ", "cost", "reward" }; for (int i=1; i= (int)start.getX()) && ((jc.getX()+jc.getPreferredSize().width) <= (int)end.getX()) && (jc.getY() >= (int)start.getY()) && ((jc.getY()+jc.getPreferredSize().height) <= (int)end.getY())); } /** This method determines if a given point is contained within the boundaries of the Grid * @param x_pos the given point's x value * @param y_pos the given point's y value * @return true if the point is contained within the Grid, false otherwise */ protected boolean containsPoint(int x_pos, int y_pos) { return ((x_pos >= (int)start.getX()) && (x_pos <= (int)end.getX()) && (y_pos >= (int)start.getY()) && (y_pos <= (int)end.getY())); } /** This method marks the cells of the observation grid if the given observation is deemed feasible * @param x_pos the specified x position in the Grid * @param y_pos the specified y position in the Grid */ protected void fillObs(int x_pos, int y_pos) { if (data[(y_pos-y)/s][(x_pos-x)/s] == 0) data[(y_pos-y)/s][(x_pos-x)/s] = 2; else data[(y_pos-y)/s][(x_pos-x)/s] = 0; } /** This method graws the grid in its corresponding JPanel * @param g the JPanel's Graphics member variable */ private void drawGrid(Graphics g) { for (int rt=1; rt<=r; rt++) { g.drawLine(x+s, y+rt*s, x+c*s, y+rt*s); } for (int ct=1; ct<=c; ct++) { g.drawLine(x+ct*s, y+s, x+ct*s, y+r*s); } for (int rt=1; rt