use of edu.cmu.ml.proppr.prove.wam.Query in project ProPPR by TeamCohen.
the class GrounderTest method doGroundExampleTest.
public void doGroundExampleTest(String msg, Prover p, int nodes, int edges, double value, String npos, String nneg, double alpha) throws IOException, LogicProgramException {
APROptions apr = new APROptions();
WamProgram program = WamBaseProgram.load(new File(RULES));
WamPlugin[] plugins = new WamPlugin[] { FactsPlugin.load(apr, new File(FACTS), false) };
Grounder grounder = new Grounder(apr, p, program, plugins);
Query query = Query.parse("predict(howard,Y)");
GroundedExample ex = grounder.groundExample(p, new InferenceExample(query, new Query[] { Query.parse("predict(howard,bird)") }, new Query[] { Query.parse("predict(howard,mammal)") }));
makeAssertions(ex, msg, nodes, edges, value, 1, npos, 1, nneg);
// predict(howard,Y) +predict(howard,bird) -predict(howard,mammal)
}
use of edu.cmu.ml.proppr.prove.wam.Query in project ProPPR by TeamCohen.
the class GrounderTest method noNegativeExamplesTest.
@Test
public void noNegativeExamplesTest() throws IOException, LogicProgramException {
APROptions apr = new APROptions();
WamProgram program = WamBaseProgram.load(new File(RULES));
WamPlugin[] plugins = new WamPlugin[] { FactsPlugin.load(apr, new File(FACTS), false) };
Prover p = new DprProver(apr);
Grounder grounder = new Grounder(apr, p, program, plugins);
InferenceExample ix = new InferenceExample(Query.parse("predict(howard,Y)"), new Query[] { Query.parse("predict(howard,bird)") }, new Query[] {});
GroundedExample ex = grounder.groundExample(p, ix);
makeAssertions(ex, "dpr+", 10, 23, 1.0, 1, "7", 0, "");
ix = new InferenceExample(Query.parse("predict(howard,Y)"), new Query[] { Query.parse("predict(howard,bird)") }, new Query[] {});
ProofGraph pg = p.makeProofGraph(ix, apr, program, plugins);
State pos = null;
Map<State, Double> sols = p.prove(pg, new StatusLogger());
System.out.println(pg.serialize(ex));
}
use of edu.cmu.ml.proppr.prove.wam.Query in project ProPPR by TeamCohen.
the class ProverTestTemplate method testProveState.
@Test
public void testProveState() throws LogicProgramException {
log.info("testProveState");
FeatureDictWeighter w = new InnerProductWeighter();
SymbolTable<Feature> featureTab = new SimpleSymbolTable<Feature>();
int milk = featureTab.getId(new Feature("milk"));
w.put(featureTab.getSymbol(milk), 2);
prover.setWeighter(w);
ProofGraph pg = prover.makeProofGraph(new InferenceExample(Query.parse("isa(elsie,X)"), null, null), apr, featureTab, lpMilk, fMilk);
//("isa","elsie","X"));
Map<State, Double> dist = prover.prove(pg, new StatusLogger());
double query = 0.0;
double platypus = 0.0;
double others = 0.0;
double all = 0.0;
for (Map.Entry<State, Double> s : dist.entrySet()) {
Query q = pg.fill(s.getKey());
String arg2 = q.getRhs()[0].getArg(1).getName();
if ("platypus".equals(arg2)) {
platypus = Math.max(platypus, s.getValue());
} else if ("X1".equals(arg2)) {
query = Math.max(query, s.getValue());
} else {
others = Math.max(others, s.getValue());
}
System.out.println(q + "\t" + s.getValue());
all += s.getValue();
}
System.out.println();
System.out.println("query weight: " + query);
System.out.println("platypus weight: " + platypus);
System.out.println("others weight: " + others);
// assertTrue("query should retain most weight",query > Math.max(platypus,others));
assertTrue("milk-featured paths should score higher than others", platypus > others);
assertEquals("Total weight of all states should be around 1.0", 1.0, all, 10 * this.apr.epsilon);
assertEquals("Known features", 1, prover.weighter.numKnownFeatures);
assertEquals("Unknown features", 5, prover.weighter.numUnknownFeatures);
}
use of edu.cmu.ml.proppr.prove.wam.Query in project ProPPR by TeamCohen.
the class EqualityTest method test.
@Test
public void test() throws LogicProgramException, IOException {
WamProgram program = WamBaseProgram.load(new File(EQUALITY_PROGRAM));
Prover prover = new DprProver();
ProofGraph moral = new StateProofGraph(Query.parse("moral(X)"), new APROptions(), program);
Collection<Query> bobs = prover.solvedQueries(moral, new StatusLogger()).keySet();
// Map<State,Double> ans = prover.prove(moral);
// ArrayList<Query> bobs = new ArrayList<Query>();
// for (Map.Entry<State,Double> e : ans.entrySet()) {
// if (e.getKey().isCompleted()) bobs.add(moral.fill(e.getKey()));
// }
assertEquals(1, bobs.size());
Query bob = bobs.iterator().next();
assertEquals(1, bob.getRhs().length);
assertEquals("Answer should be bob", "bob", bob.getRhs()[0].getArg(0).getName());
}
use of edu.cmu.ml.proppr.prove.wam.Query in project ProPPR by TeamCohen.
the class QueryAnswerer method findSolutions.
public String findSolutions(WamProgram program, WamPlugin[] plugins, Prover<P> prover, Query query, boolean normalize, int id) throws LogicProgramException {
P pg = prover.makeProofGraph(new InferenceExample(query, null, null), apr, featureTable, program, plugins);
if (log.isDebugEnabled())
log.debug("Querying: " + query);
long start = System.currentTimeMillis();
Map<State, Double> dist = getSolutions(prover, pg);
long end = System.currentTimeMillis();
Map<Query, Double> solutions = new TreeMap<Query, Double>();
for (Map.Entry<State, Double> s : dist.entrySet()) {
if (s.getKey().isCompleted()) {
Query x = pg.fill(s.getKey());
solutions.put(x, s.getValue());
if (log.isDebugEnabled()) {
log.debug(x.toString() + "\t" + s.getValue());
}
} else if (log.isDebugEnabled()) {
log.debug(s.toString() + "\t" + s.getValue());
}
}
if (normalize) {
log.debug("normalizing");
solutions = Dictionary.normalize(solutions);
} else {
log.debug("not normalizing");
}
List<Map.Entry<Query, Double>> solutionDist = Dictionary.sort(solutions);
if (log.isDebugEnabled())
log.debug("Writing " + solutionDist.size() + " solutions...");
StringBuilder sb = new StringBuilder("# proved ").append(String.valueOf(id)).append("\t").append(query.toString()).append("\t").append((end - start) + " msec\n");
int rank = 0;
double lastScore = 0;
int displayrank = 0;
for (Map.Entry<Query, Double> soln : solutionDist) {
++rank;
if (soln.getValue() != lastScore)
displayrank = rank;
if (numSolutions > 0 && rank > numSolutions)
break;
sb.append(displayrank + "\t").append(soln.getValue().toString()).append("\t").append(soln.getKey().toString()).append("\n");
lastScore = soln.getValue();
}
return sb.toString();
}
Aggregations