/* * Created on Aug 16, 2004 * * $Log: MDPFileParser.java,v $ * Revision 1.4 2004/08/25 02:21:55 bh * Changed setTransitionProbability to accumTransitionProbability, which * is only used in parsing agent-centric transition function specifications. * * Revision 1.3 2004/08/23 23:42:18 bh * Source clean. * * Revision 1.2 2004/08/23 15:48:20 bh * Changed parseTransitionModel to accomodate the redesign of transition * model. * */ package cs.decision; import java.io.FileNotFoundException; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author bh * */ public class MDPFileParser { static final int ACTION_UP = 0; static final int ACTION_RIGHT = 1; static final int ACTION_DOWN = 2; static final int ACTION_LEFT = 3; int[][] transitionAction = { {ACTION_LEFT, ACTION_UP, ACTION_RIGHT, ACTION_DOWN}, {ACTION_RIGHT, ACTION_DOWN, ACTION_LEFT, ACTION_UP} }; IniParser mdpFile; MarkovDecisionProcess mdp=null; int rows, cols; public MDPFileParser(String filename) throws FileNotFoundException { mdpFile = new IniParser(filename); } public MarkovDecisionProcess parse() { mdpFile.parseIniFile(); IniSection section = mdpFile.getSection("Size"); if(section == null){ System.out.println("No size info, check your configuration file."); return null; } rows = Integer.parseInt(section.getValue("rows")); cols = Integer.parseInt(section.getValue("cols")); mdp = new MarkovDecisionProcess(rows, cols); section = mdpFile.getSection("Holes"); if(section != null) parseHoles(section); section = mdpFile.getSection("Rewards"); if(section != null) parseRewards(section); section = mdpFile.getSection("Terminate states"); if(section != null) parseTerminateStates(section); mdp.compileStates(); section = mdpFile.getSection("Transition model"); if(section != null) parseTransitionModel(section); return mdp; } public static String stripWhiteSpace(String s){ return s.replaceAll("\\s*", ""); } private void extractRowCol(String value, String[] s) { //value is if form "(3,4)" // could use RegEx, but why overkill s[0]=value.substring(value.indexOf('(')+1, value.indexOf(',')); s[1]=value.substring(value.indexOf(',')+1, value.lastIndexOf(')')); } private void parseHoles(IniSection section){ String value; String[] lr = new String[2]; section.vectorize(); int numVals = section.size(); for(int i=0; i