/* * Class PosgInputFileParser * * File structure and code adopted from * ParserNew.java class by Christopher Amato */ import java.io.*; import java.util.StringTokenizer; import java.util.NoSuchElementException; /** * PosgInputFileParser parses an input file and places data into appropriate data structures later to be used * by POSG-solving algorithms.
* @author Mark Gruman -- UMass RBR Lab
* Computer Science Department
* University of Massachusetts Amherst
* @version 1.1
* Created on July 02, 2004

*/ public class PosgInputFileParser { //required pre-amble variables private double discount; private int agents; private int states; private boolean cost; //Observational, Transitional, and Reward Probabilities private double[][][] O; private double[][][] T; private double[][][][] R; //variables to help setup O,T,R tables private double[] start; private BufferedReader br; private boolean t_set; private boolean r_o_set; private int tot_actions; private int tot_obs; //names of parameters public String[] state; public String[][] action; public String[][] observation; /**************************** ***** Public Methods ***** ****************************/ /** @return the number of agents */ public int getAgents() { return agents; } /** @return the number of states */ public int getStates() { return states; } /** @return true if cost, false if rewards */ public boolean getCost() { return cost; } /** @return the set discount */ public double getDiscount() { return discount; } /** @return the number of unique action combinations */ public int getActions() { return tot_actions; } /** @return the number of unique observation combinations */ public int getObs() { return tot_obs; } /** @return the Transitional Probabilities Matrix */ public double[][][] getTrnProbTable() { return T; } /** @return the Observational Probabilities Matrix */ public double[][][] getObsProbTable() { return O; } /** @return the Reward Probabilities Matrix */ public double[][][][] getRwdProbTable() { return R; } /** * @param s the state name * @return the corresponding state value */ public int getStateValue(String s) { if (s.equals("*")) return -1; try { return (Integer.parseInt(s)); } catch (Exception e) { for (int i=0; i * @param m the probability matrix to be updated * @param a the value corresponding to a unique set of actions * @param p for T and R matricies: the start state, for the O matrix: the end state * @param p2 for the R matrix: the end state (the parameter is passed a 0 value otherwise) * @param pr the corresponding probability * @param o_flag true if updating the O or R matrix, false otherwise * @param r_flag true if updating the R matrix, false otherwise */ private void setValues(Object m, int a, int p, int p2, int o, double pr, boolean o_flag, boolean r_flag) { if (a == -1) for (int i=0; i : : : %f" * or " T : : : /r/n %f" * or " O : : : : %f" * or " O : : : /r/n %f" * or " R : : : : : %f" * or " R : : : : /r/n %f" */ if ( (tokens >= (params*2 + 3) && tokens <= (params*2 + 4) && rwrd) || (tokens >= (params*2 + 2) && tokens <= (params*2 + 3) && obsr && !rwrd) || (tokens >= (params+3) && tokens <= (params+4) && !obsr && !rwrd)) { start_s = getStateValue(st.nextToken()); if (rwrd) r_end_s = getStateValue(st.nextToken()); if ( (tokens >= (params*2 + 3) && tokens <= (params*2 + 4) && rwrd) || (tokens >= (params*2 + 2) && tokens <= (params*2 + 3) && obsr && !rwrd)) { String temp2 = st.nextToken(); if (temp2.equals("*")) end_s = -1; else { obs = new String[params]; obs[0] = temp2; for (int o=1; o : " * or " O : : " * or " R : : : " */ else if (tokens == (params+3) || (tokens == (params+2) && !rwrd)) { start_s = getStateValue(st.nextToken()); if (rwrd) r_end_s = getStateValue(st.nextToken()); one_line = true; cur_line = getStates()-1; } /* line = * " T : " * or " O : " * or " R : : " */ else if (tokens == (params+2) || (tokens == (params+1) && !rwrd)) { if (rwrd) start_s = getStateValue(st.nextToken()); } /* line = * " %f %f ... %f " | identity | uniform */ else if (tokens == getStates() || tokens == tot_obs || tokens == 1) { StringTokenizer tk = new StringTokenizer(line); String temp = tk.nextToken(); if (temp.equalsIgnoreCase("IDENTITY") || temp.equalsIgnoreCase("UNIFORM")) { cur_line = getStates(); if (!one_line) start_s = -1; } if (!one_line && start_s != -1) start_s = cur_line-1; /* line = * identity */ if (temp.equalsIgnoreCase("IDENTITY") && !obsr && !rwrd) for (int s=0; s=0; num=temp, i--) { temp = num * param[i].length; if (value < temp) break; } if (i > param_col) for (int j=param_col; j