Search in sources :

Example 11 with Outlink

use of edu.cmu.ml.proppr.prove.wam.Outlink in project ProPPR by TeamCohen.

the class SimpleProgramProofGraphTest method test.

@Test
public void test() throws LogicProgramException, IOException {
    WamProgram program = WamBaseProgram.load(new File(SimpleProgramProverTest.PROGRAM));
    Query q = new Query(new Goal("coworker", new ConstantArgument("steve"), new ConstantArgument("X")));
    StateProofGraph pg = new StateProofGraph(q, new APROptions(), program);
    HashMap<String, Integer> solutions = new HashMap<String, Integer>();
    solutions.put("steve", 0);
    solutions.put("sven", 0);
    Map<State, Double> vec = new HashMap<State, Double>();
    vec.put(pg.getStartState(), 1.0);
    // step 1: coworker -> employee,boss
    System.out.println("Step 1");
    for (State s : vec.keySet()) System.out.println(s);
    List<Outlink> outlinks = pg.pgOutlinks(pg.getStartState(), false);
    assertEquals("1. coworker :- employee,boss", 2, outlinks.size());
    vec = nextVec(vec, normalized(outlinks));
    vec.remove(pg.getStartState());
    assertEquals("1. statecount", 1, vec.size());
    // step 2: 
    System.out.println("Step 2");
    for (State s : vec.keySet()) System.out.println(s);
    outlinks = pg.pgOutlinks(vec.keySet().iterator().next(), false);
    assertEquals("2. employee :- management,boss", 2, outlinks.size());
    vec = nextVec(vec, normalized(outlinks));
    vec.remove(pg.getStartState());
    assertEquals("2. statecount", 1, vec.size());
    // step 3: 
    System.out.println("Step 3");
    for (State s : vec.keySet()) System.out.println(s);
    outlinks = pg.pgOutlinks(vec.keySet().iterator().next(), false);
    assertEquals("3. management :- sookie", 2, outlinks.size());
    vec = nextVec(vec, normalized(outlinks));
    vec.remove(pg.getStartState());
    assertEquals("3. statecount", 1, vec.size());
    // step 4: 
    System.out.println("Step 4");
    for (State s : vec.keySet()) System.out.println(s);
    outlinks = pg.pgOutlinks(vec.keySet().iterator().next(), false);
    assertEquals("4. boss(sookie,X) :- _steve_ + sven", 2, outlinks.size());
    vec = nextVec(vec, normalized(outlinks));
    vec.remove(pg.getStartState());
    assertEquals("4. statecount", 1, vec.size());
    // step 5: 
    System.out.println("Step 5");
    for (State s : vec.keySet()) {
        System.out.println(s);
        System.out.println(Dictionary.buildString(pg.asDict(s), new StringBuilder(), "\n\t").substring(1));
    }
    outlinks = pg.pgOutlinks(vec.keySet().iterator().next(), false);
    assertEquals("5. boss(sookie,X) :- steve + sven", 3, outlinks.size());
    vec = nextVec(vec, normalized(outlinks));
    vec.remove(pg.getStartState());
    assertEquals("5. statecount", 2, vec.size());
    // step 6: 
    System.out.println("Step 6");
    for (State s : vec.keySet()) {
        System.out.println(s);
        Map<Argument, String> dict = pg.asDict(s);
        System.out.println(Dictionary.buildString(dict, new StringBuilder(), "\n\t").substring(1));
        assertTrue(s.isCompleted());
        for (String v : dict.values()) {
            if (solutions.containsKey(v))
                solutions.put(v, solutions.get(v) + 1);
        }
    }
    for (Map.Entry<String, Integer> e : solutions.entrySet()) assertEquals(e.getKey(), 1, e.getValue().intValue());
}
Also used : Outlink(edu.cmu.ml.proppr.prove.wam.Outlink) Query(edu.cmu.ml.proppr.prove.wam.Query) Argument(edu.cmu.ml.proppr.prove.wam.Argument) ConstantArgument(edu.cmu.ml.proppr.prove.wam.ConstantArgument) HashMap(java.util.HashMap) WamProgram(edu.cmu.ml.proppr.prove.wam.WamProgram) ConstantArgument(edu.cmu.ml.proppr.prove.wam.ConstantArgument) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) Goal(edu.cmu.ml.proppr.prove.wam.Goal) State(edu.cmu.ml.proppr.prove.wam.State) APROptions(edu.cmu.ml.proppr.util.APROptions) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 12 with Outlink

use of edu.cmu.ml.proppr.prove.wam.Outlink in project ProPPR by TeamCohen.

the class PprProver method normalizedOutlinks.

protected Map<State, Double> normalizedOutlinks(StateProofGraph pg, State s) throws LogicProgramException {
    List<Outlink> outlinks = pg.pgOutlinks(s, NORMLX_TRUELOOP);
    Map<State, Double> weightedOutlinks = new HashMap<State, Double>();
    double z = 0;
    for (Outlink o : outlinks) {
        o.wt = this.weighter.w(o.fd);
        weightedOutlinks.put(o.child, o.wt);
        z += o.wt;
    }
    for (Map.Entry<State, Double> e : weightedOutlinks.entrySet()) {
        e.setValue(e.getValue() / z);
    }
    return weightedOutlinks;
}
Also used : Outlink(edu.cmu.ml.proppr.prove.wam.Outlink) HashMap(java.util.HashMap) State(edu.cmu.ml.proppr.prove.wam.State) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with Outlink

use of edu.cmu.ml.proppr.prove.wam.Outlink in project ProPPR by TeamCohen.

the class GraphlikePlugin method outlinks.

@Override
public List<Outlink> outlinks(State state, WamInterpreter wamInterp, boolean computeFeatures) throws LogicProgramException {
    List<Outlink> result = new LinkedList<Outlink>();
    String indexKey = state.getJumpTo();
    int delim = indexKey.indexOf(WamInterpreter.JUMPTO_DELIMITER);
    int arity = Integer.parseInt(indexKey.substring(delim + 1));
    boolean returnWeights = indexKey.substring(0, delim).endsWith(WamPlugin.WEIGHTED_SUFFIX);
    String srcConst = wamInterp.getConstantArg(arity, 1);
    String dstConst = wamInterp.getConstantArg(arity, 2);
    String weightConst = null;
    if (returnWeights) {
        indexKey = unweightedJumpto(indexKey);
        weightConst = wamInterp.getConstantArg(arity, 3);
        if (weightConst != null) {
            throw new LogicProgramException("predicate " + state.getJumpTo() + " called with bound third argument!");
        }
    }
    if (srcConst == null) {
        //throw new LogicProgramException("predicate "+state.getJumpTo()+" called with non-constant first argument!");
        for (String src : indexGet(indexKey)) {
            wamInterp.restoreState(state);
            wamInterp.setArg(arity, 1, src);
            State srcState = wamInterp.saveState();
            outlinksPerSource(srcState, wamInterp, computeFeatures, returnWeights, indexKey, src, dstConst, weightConst, result, arity);
        }
    } else {
        outlinksPerSource(state, wamInterp, computeFeatures, returnWeights, indexKey, srcConst, dstConst, weightConst, result, arity);
    }
    return result;
}
Also used : Outlink(edu.cmu.ml.proppr.prove.wam.Outlink) LogicProgramException(edu.cmu.ml.proppr.prove.wam.LogicProgramException) State(edu.cmu.ml.proppr.prove.wam.State) LinkedList(java.util.LinkedList)

Example 14 with Outlink

use of edu.cmu.ml.proppr.prove.wam.Outlink in project ProPPR by TeamCohen.

the class LightweightStateGraph method getOutlinks.

/** Return the neighbors of node u. */
public List<Outlink> getOutlinks(State u) {
    // wwc: why do we need to recompute these each time?
    List<Outlink> result = new ArrayList<Outlink>();
    for (State v : near(u)) {
        Map<Feature, Double> fd = getFeatures(u, v);
        result.add(new Outlink(fd, v));
    }
    return result;
}
Also used : Outlink(edu.cmu.ml.proppr.prove.wam.Outlink) State(edu.cmu.ml.proppr.prove.wam.State) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) Feature(edu.cmu.ml.proppr.prove.wam.Feature)

Aggregations

Outlink (edu.cmu.ml.proppr.prove.wam.Outlink)14 State (edu.cmu.ml.proppr.prove.wam.State)6 Feature (edu.cmu.ml.proppr.prove.wam.Feature)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 LogicProgramException (edu.cmu.ml.proppr.prove.wam.LogicProgramException)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Query (edu.cmu.ml.proppr.prove.wam.Query)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 LinkedList (java.util.LinkedList)2 GrounderTest (edu.cmu.ml.proppr.GrounderTest)1 Argument (edu.cmu.ml.proppr.prove.wam.Argument)1 ConstantArgument (edu.cmu.ml.proppr.prove.wam.ConstantArgument)1 Goal (edu.cmu.ml.proppr.prove.wam.Goal)1 MutableState (edu.cmu.ml.proppr.prove.wam.MutableState)1 StateProofGraph (edu.cmu.ml.proppr.prove.wam.StateProofGraph)1 WamInterpreter (edu.cmu.ml.proppr.prove.wam.WamInterpreter)1 WamProgram (edu.cmu.ml.proppr.prove.wam.WamProgram)1 APROptions (edu.cmu.ml.proppr.util.APROptions)1