// Executes the given action public void executeAction(Action a) { // For each state variable, retrieve the DD for it, evaluate it // w.r.t. the current state and build a new state. ArrayList new_state = new ArrayList(); int c; for (c = 0; c < (_nVars << 1); c++) { new_state.add("-"); } for (c = 0; c < (_nVars << 1); c++) { Object cur_assign = _state.get(c); if (cur_assign instanceof Boolean) { // System.out.println(a._tmID2DD); int nonprime_id = c + 1; int prime_id = nonprime_id - _nVars; Object DD = a._tmID2DD.get(new Integer(prime_id)); _state.set(prime_id - 1, TRUE); double p_true = _mdp._context.evaluate(DD, _state); // System.out.println("ID: " + nonprime_id + ", " + prime_id + ": " + // _mdp._context.printNode(DD) + " -> " + // _mdp._df.format(p_true)); new_state.set(c, (_r.nextFloat() < p_true) ? TRUE : FALSE); } } _state = new_state; }
public ChangeVO updateRow( int windowNo, int AD_Tab_ID, int queryResultID, int relRowNo, Map<String, String> context, boolean force) { if (context == null || context.size() == 0) return new ChangeVO(true, Msg.translate(m_context, "NoContext")); ArrayList<String[]> data = m_results.get(queryResultID); if (data == null || data.size() == 0) return new ChangeVO(true, Msg.translate(m_context, "CachedDataNotFound")); UITab tab = getTab(AD_Tab_ID); if (tab == null) { log.config("Not found AD_Tab_ID=" + AD_Tab_ID); return new ChangeVO(true, Msg.translate(m_context, "@NotFound@ @AD_Tab_ID@=" + AD_Tab_ID)); } CContext ctx = new CContext(m_context.entrySet()); ctx.addWindow(windowNo, context); ChangeVO retValue; if (force) retValue = tab.saveRow(ctx, windowNo, false, null); else retValue = tab.saveRow(ctx, windowNo, false, data.get(relRowNo)); if (retValue.hasError()) return retValue; // Update Results String[] dataRow = retValue.rowData.clone(); data.set(relRowNo, dataRow); postProcessChangeVO(retValue, windowNo, context, dataRow, tab); retValue.trxInfo = GridTab.getTrxInfo(tab.getTableName(), ctx, windowNo, tab.getTabNo()); if (retValue.isRefreshAll()) {} return retValue; }
// Derives QFunctions for the given value function and simulates the // greedy policy for the given number of trials and steps per trial. // Returns final value of every trial. public ArrayList simulate(int trials, int steps, long rand_seed) { ArrayList values = new ArrayList(); _r = new Random(rand_seed); for (int trial = 1; trial <= trials; trial++) { System.out.println("\n -----------\n Trial " + trial + "\n -----------"); // Initialize state _state = new ArrayList(); _nVars = _mdp._alVars.size(); for (int c = 0; c < (_nVars << 1); c++) { _state.add("-"); } Iterator i = _mdp._alVars.iterator(); _vars = new TreeSet(); while (i.hasNext()) { String s = (String) i.next(); if (!s.endsWith("\'")) { Integer gid = (Integer) _mdp._tmVar2ID.get(s); _vars.add(gid); // Note: assign level (level is gid-1 b/c gids in order) _state.set(gid.intValue() - 1, _r.nextBoolean() ? TRUE : FALSE); } } // System.out.println(_mdp._context.printNode(_mdp._valueDD) + "\n" + _state); double reward = _mdp._context.evaluate(_mdp._rewardDD, _state); System.out.print(" " + PrintState(_state) + " " + MDP._df.format(reward)); // Run steps for (int step = 1; step <= steps; step++) { // Get action Action a; if (_bUseBasis) { a = getBasisAction(); } else { a = getAction(); } // Execute action executeAction(a); // Update reward reward = (_mdp._bdDiscount.doubleValue() * reward) + _mdp._context.evaluate(_mdp._rewardDD, _state); System.out.println(", a=" + a._sName); System.out.print( " " + PrintState(_state) + " " + MDP._df.format(reward) + ": " + "Step " + step); } values.add(new Double(reward)); System.out.println(); } return values; }
// Fill in weights for specific counting aggregator approaches // // Basis function 1: Count of computers running // Basis function 2: Count of computers running and connected to // one other running computer // // Assuming action succeeds... compute next-state value based on action, // choose best action. public Action getBasisAction() { int best_reboot = -1; double best_reboot_val = -1d; for (int c = 1; c <= _nVars; c++) { ArrayList test_state = (ArrayList) _state.clone(); test_state.set(c - 1 + _nVars, TRUE); double test_val = evalBasisState(test_state, W_BASIS_1, W_BASIS_2); if (test_val > best_reboot_val) { best_reboot_val = test_val; best_reboot = c; } } return (Action) _mdp._hmName2Action.get("reboot" + best_reboot); }
/** * Refresh current row of Tab * * @param windowNo relative window * @param AD_Tab_ID tab * @param relRowNo relative row number in results * @param context current (relevant) context of new row * @return error message or null */ public ChangeVO refreshRow( int windowNo, int AD_Tab_ID, int queryResultID, int relRowNo, Map<String, String> context) { if (context == null || context.size() == 0) return new ChangeVO(true, "No Context"); UITab tab = getTab(AD_Tab_ID); if (tab == null) { log.config("Not found AD_Tab_ID=" + AD_Tab_ID); return new ChangeVO(true, "@NotFound@ @AD_Tab_ID@=" + AD_Tab_ID); } CContext ctx = new CContext(m_context.entrySet()); ctx.addWindow(windowNo, context); ChangeVO retValue = tab.refreshRow(ctx, windowNo); if (retValue.hasError()) return retValue; // Update Results ArrayList<String[]> data = m_results.get(queryResultID); if (data == null) retValue.addError("Data Not Found"); else { String[] dataRow = retValue.rowData.clone(); data.set(relRowNo, dataRow); postProcessChangeVO(retValue, windowNo, context, dataRow, tab); retValue.trxInfo = GridTab.getTrxInfo(tab.getTableName(), ctx, windowNo, tab.getTabNo()); } return retValue; } // refreshRow
public void sortResults( int WindowNo, int AD_Tab_ID, int AD_Field_ID, int queryResultID, final boolean ascending) { class SortCell { String[] row; String sort; } ArrayList<String[]> results = m_results.get(queryResultID); if (results == null) log.severe("cannot sort. results non-existent for queryResultID:" + queryResultID); UITab tab = getTab(AD_Tab_ID); final UIField field = getField(AD_Field_ID, WindowNo); final int displayType = field.getAD_Reference_ID(); final int idx = tab.getFieldIndex(AD_Field_ID); // if not a lookup, directly sort if (!field.isLookup()) { if (FieldType.isNumeric(displayType)) { Collections.sort( results, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { if (o1[idx] == null) o1[idx] = ""; if (o2[idx] == null) o2[idx] = ""; BigDecimal s1 = new BigDecimal(o1[idx].equals("") ? "-1e-10" : o1[idx]); BigDecimal s2 = new BigDecimal(o2[idx].equals("") ? "-1e-10" : o2[idx]); return ascending ? s1.compareTo(s2) : s2.compareTo(s1); } }); } else if (FieldType.isDate(displayType)) { Collections.sort( results, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { if (o1[idx] == null) o1[idx] = ""; if (o2[idx] == null) o2[idx] = ""; Long s1 = new Long(o1[idx].equals("") ? "-1000000" : o1[idx]); Long s2 = new Long(o2[idx].equals("") ? "-1000000" : o2[idx]); return ascending ? s1.compareTo(s2) : s2.compareTo(s1); } }); } else { Collections.sort( results, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { if (o1[idx] == null) o1[idx] = ""; if (o2[idx] == null) o2[idx] = ""; String s1 = o1[idx]; String s2 = o2[idx]; return ascending ? s1.compareTo(s2) : s2.compareTo(s1); } }); } return; } Comparator<SortCell> c = new Comparator<SortCell>() { public int compare(SortCell o1, SortCell o2) { if (ascending) return o1.sort.compareTo(o2.sort); else return o2.sort.compareTo(o1.sort); } }; // for look up, first get id values ArrayList<String> fieldValues = new ArrayList<String>(results.size()); for (String[] row : results) { fieldValues.add(row[idx]); } // then translate into real values ArrayList<String> sorts = getLookupValueOnlyDirect(AD_Field_ID, fieldValues, true); ArrayList<SortCell> toBeSorteds = new ArrayList<SortCell>(sorts.size()); for (int i = 0; i < sorts.size(); i++) { SortCell toBeSorted = new SortCell(); toBeSorted.row = results.get(i); toBeSorted.sort = sorts.get(i); toBeSorteds.add(toBeSorted); } // sort Collections.sort(toBeSorteds, c); // after sorting, replace col with original values int i = 0; for (SortCell toBeSorted : toBeSorteds) { results.set(i, toBeSorted.row); i++; } }