public String getStateDescription(State s) throws EvalException { StringBuilder sb = new StringBuilder(); TYPE_NAME xpos_type = new TYPE_NAME("xpos"); ArrayList<LCONST> list_xpos = s._hmObject2Consts.get(xpos_type); TYPE_NAME ypos_type = new TYPE_NAME("ypos"); ArrayList<LCONST> list_ypos = s._hmObject2Consts.get(ypos_type); PVAR_NAME GOAL = new PVAR_NAME("GOAL"); PVAR_NAME robot_at = new PVAR_NAME("robot-at"); PVAR_NAME P = new PVAR_NAME("P"); if (_bd == null) { IS_OBFUSCATED = !list_xpos.get(0).toString().equals("x1"); int max_row = list_ypos.size() - 1; int max_col = list_xpos.size(); _bd = new BlockDisplay( "RDDL Navigation Simulation", "RDDL Navigation Simulation", max_row + 2, max_col + 2); } // Set up an arity-1 parameter list ArrayList<LCONST> params = new ArrayList<LCONST>(2); params.add(null); params.add(null); _bd.clearAllCells(); _bd.clearAllLines(); for (LCONST xpos : list_xpos) { for (LCONST ypos : list_ypos) { int col = new Integer(xpos.toString().substring(2, xpos.toString().length())); int row = new Integer(ypos.toString().substring(2, ypos.toString().length())); if (IS_OBFUSCATED) { row = (int) Math.sqrt(row - 11); col = (int) Math.sqrt(col - 5); } row = row - 1; params.set(0, xpos); params.set(1, ypos); boolean is_goal = (Boolean) s.getPVariableAssign(GOAL, params); boolean robot = (Boolean) s.getPVariableAssign(robot_at, params); float prob = 1f - ((Number) s.getPVariableAssign(P, params)).floatValue(); if (robot && is_goal) _bd.setCell(row, col, Color.red, "G!"); else if (is_goal) _bd.setCell(row, col, Color.cyan, "G"); else if (robot) _bd.setCell(row, col, Color.blue, null); else { Color cell_color = new Color(prob, prob, prob); _bd.setCell(row, col, cell_color, null); } } } _bd.repaint(); // Sleep so the animation can be viewed at a frame rate of 1000/_nTimeDelay per second try { Thread.currentThread().sleep(_nTimeDelay); } catch (InterruptedException e) { System.err.println(e); e.printStackTrace(System.err); } return sb.toString(); }
static String createXMLTurn( State state, int turn, DOMAIN domain, HashMap<PVAR_NAME, HashMap<ArrayList<LCONST>, Object>> observStore) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document dom = db.newDocument(); Element rootEle = dom.createElement(TURN); dom.appendChild(rootEle); Element turnNum = dom.createElement(TURN_NUM); Text textTurnNum = dom.createTextNode(turn + ""); turnNum.appendChild(textTurnNum); rootEle.appendChild(turnNum); // System.out.println("PO: " + domain._bPartiallyObserved); if (!domain._bPartiallyObserved || observStore != null) { for (PVAR_NAME pn : (domain._bPartiallyObserved ? observStore.keySet() : state._state.keySet())) { // System.out.println(turn + " check2 Partial Observ " + pn +" : "+ // domain._bPartiallyObserved); // No problem to overwrite observations, only ever read from if (domain._bPartiallyObserved && observStore != null) state._observ.put(pn, observStore.get(pn)); ArrayList<ArrayList<LCONST>> gfluents = state.generateAtoms(pn); for (ArrayList<LCONST> gfluent : gfluents) { // for ( Map.Entry<ArrayList<LCONST>,Object> gfluent : // (domain._bPartiallyObserved // ? observStore.get(pn).entrySet() // : state._state.get(pn).entrySet())) { Element ofEle = dom.createElement(OBSERVED_FLUENT); rootEle.appendChild(ofEle); Element pName = dom.createElement(FLUENT_NAME); Text pTextName = dom.createTextNode(pn.toString()); pName.appendChild(pTextName); ofEle.appendChild(pName); for (LCONST lc : gfluent) { Element pArg = dom.createElement(FLUENT_ARG); Text pTextArg = dom.createTextNode(lc.toString()); pArg.appendChild(pTextArg); ofEle.appendChild(pArg); } Element pValue = dom.createElement(FLUENT_VALUE); Object value = state.getPVariableAssign(pn, gfluent); if (value == null) { System.out.println("STATE:\n" + state); throw new Exception("ERROR: Could not retrieve value for " + pn + gfluent.toString()); } Text pTextValue = dom.createTextNode(value.toString()); pValue.appendChild(pTextValue); ofEle.appendChild(pValue); } } } else { // No observations (first turn of POMDP) Element ofEle = dom.createElement(NULL_OBSERVATIONS); rootEle.appendChild(ofEle); } if (SHOW_XML) { printXMLNode(dom); System.out.println(); System.out.flush(); } return (Client.serialize(dom)); } catch (Exception e) { System.out.println("FATAL SERVER EXCEPTION: " + e); e.printStackTrace(); throw e; // System.exit(1); // return null; } }