public int against(String team1, String team2) { if (!teamID.contains(team1) || !teamID.contains(team2)) throw new java.lang.IllegalArgumentException("Wrong team name input in against function"); int id1 = teamID.get(team1); int id2 = teamID.get(team2); return teamInfo[id1][id2 + 3]; }
public static ST<String, String> backtracking( ST<String, String> config, ST<String, SET<String>> domain, Graph g) { // recursion base case - check configuration completeness if (complete(config)) return config; ST<String, String> result = null; // get a variable String v = getVariable(config); // get a SET of all the variable's values SET<String> vu = orderDomainValue(v, domain); // loop through all the variable's values for (String u : vu) { // if(consistent(u, v, config, g)) { if (consistent(u, v, config, g)) { config.put(v, u); result = backtracking(config, domain, g); if (result != null) return result; config.remove(v); } } return null; }
// return a string representation public String toString() { String s = ""; for (int i : st.keys()) { s += "(" + i + ", " + st.get(i) + ") "; } return s; }
@Override public void enterProgramHeading(pascalParser.ProgramHeadingContext ctx) { programName = ctx.getChild(1).getText(); ST st = stg.getInstanceOf("enterProgram"); st.add("v", programName); System.out.println(st.render()); }
public int remaining(String team) { int index = 2; if (teamID.contains(team)) { int id = teamID.get(team); return teamInfo[id][index]; } else { throw new java.lang.IllegalArgumentException("Wrong team name input:" + team); } }
@Override public void enterAssignmentStatement(pascalParser.AssignmentStatementContext ctx) { ST st = stg.getInstanceOf("assignment"); st.add("ident", ctx.getChild(0).getText()); st.add("value", ctx.getChild(2).getText()); System.out.print(st.render().trim() + "\n"); }
public ST createStringTemplate(CompiledST impl) { ST st = new ST(); st.impl = impl; st.groupThatCreatedThisInstance = this; if (impl.formalArguments != null) { st.locals = new Object[impl.formalArguments.size()]; Arrays.fill(st.locals, ST.EMPTY_ATTR); } return st; }
public int losses(String team) // number of losses for given team { int index = 1; if (teamID.contains(team)) { int id = teamID.get(team); return teamInfo[id][index]; } else { throw new java.lang.IllegalArgumentException("Wrong team name input:" + team); } }
public SymbolGraph(String filename, String delimiter) { st = new ST<String, Integer>(); // First pass builds the index by reading strings to associate // distinct strings with an index In in = new In(filename); while (in.hasNextLine()) { String[] a = in.readLine().split(delimiter); for (int i = 0; i < a.length; i++) { if (!st.contains(a[i])) st.put(a[i], st.size()); } } // inverted index to get string keys in an aray keys = new String[st.size()]; for (String name : st.keys()) { keys[st.get(name)] = name; } // second pass builds the graph by connecting first vertex on each // line to all others G = new Graph(st.size()); in = new In(filename); while (in.hasNextLine()) { String[] a = in.readLine().split(delimiter); int v = st.get(a[0]); for (int i = 1; i < a.length; i++) { int w = st.get(a[i]); G.addEdge(v, w); } } }
@Override public void enterConstantDefinitionPart(pascalParser.ConstantDefinitionPartContext ctx) { List<ParseTree> values = ctx.children; ArrayList<String> valueNames = new ArrayList<String>(); for (int i = 1; i < ctx.getChildCount(); i += 3) valueNames.add(values.get(i).getText()); ST st = stg.getInstanceOf("VAR"); st.add("values", valueNames); System.out.println(st.render()); }
public Iterable<String> certificateOfElimination(String team) // subset R of teams that eliminates given team; null if not eliminated { int id = teamID.get(team); Bag<String> temp = new Bag<String>(); for (int i : subset[id]) { String name = teamName.get(i); if (name != null) temp.add(name); } return temp; }
private void prefetch() { // reset prefetched element this.nextElement = null; while (this.tagEnum.hasNext() && this.nextElement == null) { ST tag = this.tagEnum.next(); // can tag be used? if (!tag.hidden()) { this.nextElement = tag; } } }
@Override public void enterVariableDeclaration(pascalParser.VariableDeclarationContext ctx) { // for variables List<ParseTree> values = ctx.children; ArrayList<String> valueNames = new ArrayList<String>(); for (int i = 0; i < ctx.getChildCount(); i += 3) valueNames.add(values.get(i).getText()); ST st = stg.getInstanceOf("VAR"); st.add("values", valueNames); System.out.println(st.render()); }
@Override public void enterIfStatement(pascalParser.IfStatementContext ctx) { String comp = ctx.getChild(1).getChild(1).getText(); if (comp.compareTo("=") == 0) comp = "=="; ST st = stg.getInstanceOf("ifClause"); st.add("operand1", ctx.getChild(1).getChild(0).getText()); st.add("comparator", comp); st.add("operand2", ctx.getChild(1).getChild(2).getText()); System.out.print(st.render().trim() + "\n"); }
/** Create singleton template for use with dictionary values */ public ST createSingleton(Token templateToken) { String template; if (templateToken.getType() == GroupParser.BIGSTRING) { template = Misc.strip(templateToken.getText(), 2); } else { template = Misc.strip(templateToken.getText(), 1); } CompiledST impl = compile(getFileName(), null, null, template, templateToken); ST st = createStringTemplateInternally(impl); st.groupThatCreatedThisInstance = this; st.impl.hasFormalArgs = false; st.impl.name = ST.UNKNOWN_NAME; st.impl.defineImplicitlyDefinedTemplates(this); return st; }
@Override public void enterForStatement(pascalParser.ForStatementContext ctx) { String comp = ctx.getChild(3).getChild(1).getText(); if (comp.compareTo("TO") == 0) comp = "<="; String comp1 = ctx.getChild(2).getText(); if (comp1.compareTo(":=") == 0) comp1 = "="; ST st = stg.getInstanceOf("ForLoop"); st.add("operand1", ctx.getChild(1).getText()); st.add("comparator", comp); st.add("operand2", ctx.getChild(3).getChild(0).getText()); st.add("operand3", ctx.getChild(3).getChild(2).getText()); System.out.print(st.render().trim() + "\n"); }
public static boolean consistent( String value, String variable, ST<String, String> config, ST<String, ST<String, ST<String, SET<String>>>> constraintsTable) { // we need to get the constraint list for the variable for (String constraints : constraintsTable.get(variable)) { // if the adjacency list member's value is equal to the variable's selected value, then // consistency fails if (!config.get(constraints).equals("") && !(constraintsTable.get(constraints).get(value).contains(config.get(constraints)))) { return false; } } // consistency check passed according to the variable's adjacancy list return true; }
@Override public void enterProcedureOrFunctionDeclaration( pascalParser.ProcedureOrFunctionDeclarationContext ctx) { // for procedures ST st = stg.getInstanceOf("enterProcedure"); st.add("v", ctx.getChild(0).getChild(1).getText()); for (int k = 1; k < ctx.getChild(0).getChild(2).getChildCount(); k += 2) { for (int i = 1; i < ctx.getChild(0).getChild(2).getChild(k).getChildCount(); i += 1) { for (int j = 0; j < ctx.getChild(0).getChild(2).getChild(k).getChild(i).getChildCount(); j += 3) { st.add( "values", ctx.getChild(0).getChild(2).getChild(k).getChild(i).getChild(j).getText()); } } } System.out.print(st.render().trim() + "\n"); }
public static String getVariable(ST<String, String> config) { // retrieve a variable based on a heuristic or the next 'unfilled' one if there is no heuristic for (String s : config) { if (config.get(s).equalsIgnoreCase("")) return s; } // get variable failed (all variables have been coloured) return null; }
@Override public void enterProcedureStatement(pascalParser.ProcedureStatementContext ctx) { String procName = ctx.getChild(0).getText(); List<ParseTree> values = ctx.children; ArrayList<String> params = new ArrayList<String>(); ArrayList<String> test = new ArrayList<String>(); ST st = stg.getInstanceOf("procCall"); for (int j = 2; j < ctx.getChildCount(); j += 4) { for (int i = 0; i < ctx.getChild(j).getChildCount(); i += 2) params.add(values.get(2).getChild(i).getText()); if (ctx.getChild(0).getText().compareTo("write") == 0) { return; } else if (ctx.getChild(0).getText().compareTo("writeln") == 0) { return; } else if (ctx.getChild(0).getText().compareTo("Write") == 0) { return; } else if (ctx.getChild(0).getText().compareTo("Writeln") == 0) { return; } else if (ctx.getChild(0).getText().compareTo("readln") == 0) { return; } else if (ctx.getChild(0).getText().compareTo("Readln") == 0) { return; } else { st.add("v", procName); st.add("values", params); System.out.println(st.render()); } } // attempt at look ahead method /*for (int h=0; h<ctx.getChildCount(); h+=1) test.add(values.get(0).getText()); for (String test : input) { if (test.matches("(?!.*write).*PROCEDURE.*")) { System.out.println(test + ": matches"); } else { System.out.println(test + ": does not match"); } }*/ }
public static boolean complete(ST<String, String> config) { for (String s : config) { // if we find a variable in the config with no value, then this means that the config is NOT // complete if (config.get(s).equalsIgnoreCase("")) return false; } // ALL variables in config have a value, so the configuration is complete return true; }
protected void add(ST tag) throws SharkKBException { // only add if not yet in storage ST st = this.getSemanticTag(tag.getSI()); if (st != null) return; // do nothing if (tag instanceof InMemoSemanticTag) { ((InMemoSemanticTag) tag).setStorage(this); } this.put(tag); }
@Override public void enterBlock(pascalParser.BlockContext ctx) { ArrayList<String> procName = new ArrayList<String>(); ArrayList<String> varName = new ArrayList<String>(); ST st = stg.getInstanceOf("enterProcedure"); for (int h = 2; h < ctx.getChild(0).getChild(1).getChildCount(); h += 10) { for (int j = 3; j < ctx.getChild(0).getChild(1).getChild(0).getChildCount(); j += 10) { for (int i = 1; i < ctx.getChild(0).getChild(1).getChild(0).getChildCount(); i += 10) { procName.add(ctx.getChild(0).getChild(1).getChild(0).getChild(i).getText()); if (procName.get(0).compareTo(",") == 0) { return; } else { st.add("v", procName); st.add("values", ctx.getChild(0).getChild(1).getChild(0).getChild(j)); st.add("values", ctx.getChild(0).getChild(1).getChild(h).getText()); System.out.println(st.render()); } } } } }
protected void removeSemanticTag(ST tag) { this.tags.remove(tag); // tag is removed - remove reference in si2tag list String[] sis = tag.getSI(); if (sis == null | sis.length == 0) { return; } for (int i = 0; i < sis.length; i++) { this.si2tag.remove(sis[i]); } }
protected final void put(ST tag) { this.tags.add(tag); // recreate si list - not a very performant implementation have to confess... // this.initSi(); String[] sis = tag.getSI(); if (sis == null) { return; } for (int i = 0; i < sis.length; i++) { this.si2tag.put(sis[i], tag); } }
public static boolean consistent( String value, String variable, ST<String, String> config, Graph g) { // we need to get the adjacency list for the variable for (String adj : g.adjacentTo(variable)) { // if the adjacency list member's value is equal to the variable's selected value, then // consistency fails if (config.get(adj).equalsIgnoreCase(value)) { // consistency check fail return false; } } // consistency check passed according to the variable's adjacancy list return true; }
ST merge(ST source) throws SharkKBException { SemanticTag copyTag = null; if (source == null) { return null; } // try to find tag copyTag = this.getSemanticTag(source.getSI()); if (copyTag == null) { if (source instanceof PeerTXSemanticTag) { copyTag = InMemoSharkKB.createInMemoCopy((PeerTXSemanticTag) source); } else if (source instanceof PeerSNSemanticTag) { copyTag = InMemoSharkKB.createInMemoCopy((PeerTXSemanticTag) source); } else if (source instanceof TimeSemanticTag) { copyTag = InMemoSharkKB.createInMemoCopy((TimeSemanticTag) source); } else if (source instanceof SpatialSemanticTag) { copyTag = InMemoSharkKB.createInMemoCopy((SpatialSemanticTag) source); } else if (source instanceof TXSemanticTag) { copyTag = InMemoSharkKB.createInMemoCopy((TXSemanticTag) source); } else if (source instanceof PeerSemanticTag) { copyTag = InMemoSharkKB.createInMemoCopy((PeerSemanticTag) source); } else if (source instanceof SNSemanticTag) { copyTag = InMemoSharkKB.createInMemoCopy((SNSemanticTag) source); } else if (source instanceof SemanticTag) { copyTag = InMemoSharkKB.createInMemoCopy((SemanticTag) source); } this.add((ST) copyTag); } else { SharkCSAlgebra.merge(copyTag, source); } // this.initSi(); return (ST) copyTag; }
public int[] findVerticalSeam() { // sequence of indices for vertical seam ST<Double, int[]> shortestPathTable = new ST<Double, int[]>(); pixelEnergy = new double[width()][height()]; for (int x = 0; x < width(); x++) { for (int y = 0; y < height(); y++) { pixelEnergy[x][y] = energy(x, y); } } for (int col = 0; col < width(); col++) { int x = col; int y = 0; int[] array = new int[height()]; double pathLength = pixelEnergy[x][y]; double path1, path2, path3; array[y] = x; while (y < height() - 1) { if (x == 0) { path1 = pathLength + pixelEnergy[x][y + 1]; path2 = pathLength + pixelEnergy[x + 1][y + 1]; if (path1 < path2) { pathLength = path1; } else { pathLength = path2; x = x + 1; } } else if (x == width() - 1) { path1 = pathLength + pixelEnergy[x][y + 1]; path2 = pathLength + pixelEnergy[x - 1][y + 1]; if (path1 < path2) { pathLength = path1; } else { pathLength = path2; x = x - 1; } } else { path1 = pathLength + pixelEnergy[x][y + 1]; path2 = pathLength + pixelEnergy[x - 1][y + 1]; path3 = pathLength + pixelEnergy[x + 1][y + 1]; if (path1 <= path2 && path1 <= path3) { pathLength = path1; } if (path2 <= path1 && path2 <= path3) { x = x - 1; pathLength = path2; } if (path3 <= path1 && path3 <= path2) { x = x + 1; pathLength = path3; } } y = y + 1; array[y] = x; } shortestPathTable.put(pathLength, array); } return shortestPathTable.get(shortestPathTable.min()); }
public BaseballElimination(String filename) { In in = new In(filename); V = in.readInt(); teamID = new ST<String, Integer>(); teamName = new ST<Integer, String>(); teamInfo = new int[V][V + 3]; subset = (Bag<Integer>[]) new Bag[V]; elimin = new Boolean[V]; for (int i = 0; i < V; i++) { subset[i] = new Bag<Integer>(); } for (int i = 0; i < V; i++) { String name = in.readString(); teamID.put(name, i); teamName.put(i, name); for (int j = 0; j < V + 3; j++) { teamInfo[i][j] = in.readInt(); } } int offset = 1 + V * (V - 1) / 2; for (int x = 0; x < V; x++) { if (trivalElimination(x)) { // StdOut.println("team " + teamName.get(x) + "is trivally eliminated"); elimin[x] = true; continue; } FlowNetwork G = new FlowNetwork(offset + V + 1); int s = 0; int t = offset + V; int point = 1; int remainGameNumber = 0; for (int i = 0; i < V; i++) { for (int j = i + 1; j < V; j++) { if (i == x || j == x) { point++; continue; } if (teamInfo[i][j + 3] > 0) { FlowEdge edge = new FlowEdge(s, point, teamInfo[i][j + 3]); remainGameNumber += teamInfo[i][j + 3]; G.addEdge(edge); FlowEdge edge1 = new FlowEdge(point, offset + i, Double.POSITIVE_INFINITY); FlowEdge edge2 = new FlowEdge(point, offset + j, Double.POSITIVE_INFINITY); G.addEdge(edge1); G.addEdge(edge2); } point++; } } for (int i = 0; i < V; i++) { if (i == x) continue; int capacity = teamInfo[x][0] + teamInfo[x][2] - teamInfo[i][0]; FlowEdge edge = new FlowEdge(offset + i, t, capacity); G.addEdge(edge); } FordFulkerson maxflow = new FordFulkerson(G, s, t); if (maxflow.value() == remainGameNumber) { elimin[x] = false; } else { elimin[x] = true; if (debug == true) { StdOut.println("FlowNetWork" + G.toString()); } for (int i = 0; i < V; i++) { if (i == x) continue; if (maxflow.inCut(offset + i)) { subset[x].add(i); } } } } }
public boolean isEliminated(String team) { int id = teamID.get(team); return elimin[id]; }