Search in sources :

Example 16 with Query

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));
}
Also used : StatusLogger(edu.cmu.ml.proppr.util.StatusLogger) GroundedExample(edu.cmu.ml.proppr.examples.GroundedExample) Query(edu.cmu.ml.proppr.prove.wam.Query) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) ProofGraph(edu.cmu.ml.proppr.prove.wam.ProofGraph) IdDprProver(edu.cmu.ml.proppr.prove.IdDprProver) PprProver(edu.cmu.ml.proppr.prove.PprProver) DprProver(edu.cmu.ml.proppr.prove.DprProver) IdPprProver(edu.cmu.ml.proppr.prove.IdPprProver) Prover(edu.cmu.ml.proppr.prove.Prover) IdDprProver(edu.cmu.ml.proppr.prove.IdDprProver) DprProver(edu.cmu.ml.proppr.prove.DprProver) WamProgram(edu.cmu.ml.proppr.prove.wam.WamProgram) InferenceExample(edu.cmu.ml.proppr.examples.InferenceExample) WamPlugin(edu.cmu.ml.proppr.prove.wam.plugins.WamPlugin) State(edu.cmu.ml.proppr.prove.wam.State) APROptions(edu.cmu.ml.proppr.util.APROptions) File(java.io.File) Test(org.junit.Test)

Example 17 with Query

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);
}
Also used : StatusLogger(edu.cmu.ml.proppr.util.StatusLogger) Query(edu.cmu.ml.proppr.prove.wam.Query) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) ProofGraph(edu.cmu.ml.proppr.prove.wam.ProofGraph) Feature(edu.cmu.ml.proppr.prove.wam.Feature) InferenceExample(edu.cmu.ml.proppr.examples.InferenceExample) FeatureDictWeighter(edu.cmu.ml.proppr.prove.FeatureDictWeighter) SimpleSymbolTable(edu.cmu.ml.proppr.util.SimpleSymbolTable) State(edu.cmu.ml.proppr.prove.wam.State) Map(java.util.Map) InnerProductWeighter(edu.cmu.ml.proppr.prove.InnerProductWeighter) Test(org.junit.Test)

Example 18 with Query

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());
}
Also used : StatusLogger(edu.cmu.ml.proppr.util.StatusLogger) Query(edu.cmu.ml.proppr.prove.wam.Query) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) ProofGraph(edu.cmu.ml.proppr.prove.wam.ProofGraph) DprProver(edu.cmu.ml.proppr.prove.DprProver) Prover(edu.cmu.ml.proppr.prove.Prover) DprProver(edu.cmu.ml.proppr.prove.DprProver) WamProgram(edu.cmu.ml.proppr.prove.wam.WamProgram) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) APROptions(edu.cmu.ml.proppr.util.APROptions) File(java.io.File) Test(org.junit.Test)

Example 19 with Query

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();
}
Also used : Query(edu.cmu.ml.proppr.prove.wam.Query) TreeMap(java.util.TreeMap) InferenceExample(edu.cmu.ml.proppr.examples.InferenceExample) State(edu.cmu.ml.proppr.prove.wam.State) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap)

Example 20 with Query

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

the class PathDprProver method main.

public static void main(String[] args) throws LogicProgramException {
    CustomConfiguration c = new CustomConfiguration(args, //input
    Configuration.USE_PARAMS, //output
    0, //constants
    Configuration.USE_WAM | Configuration.USE_SQUASHFUNCTION, //modules
    0) {

        String query;

        @Override
        protected void addCustomOptions(Options options, int[] flags) {
            options.getOption(Configuration.PARAMS_FILE_OPTION).setRequired(false);
            options.addOption(OptionBuilder.withLongOpt("query").withArgName("functor(arg1,Var1)").hasArg().isRequired().withDescription("specify query to print top paths for").create());
        //TODO: add prompt option (for large datasets)
        }

        @Override
        protected void retrieveCustomSettings(CommandLine line, int[] flags, Options options) {
            query = line.getOptionValue("query");
        }

        @Override
        public Object getCustomSetting(String name) {
            return query;
        }
    };
    PathDprProver p = new PathDprProver(c.apr);
    Query query = Query.parse((String) c.getCustomSetting(null));
    StateProofGraph pg = new StateProofGraph(query, c.apr, c.program, c.plugins);
    p.prove(pg, new StatusLogger());
}
Also used : StatusLogger(edu.cmu.ml.proppr.util.StatusLogger) Options(org.apache.commons.cli.Options) APROptions(edu.cmu.ml.proppr.util.APROptions) CommandLine(org.apache.commons.cli.CommandLine) Query(edu.cmu.ml.proppr.prove.wam.Query) CustomConfiguration(edu.cmu.ml.proppr.util.CustomConfiguration) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph)

Aggregations

Query (edu.cmu.ml.proppr.prove.wam.Query)28 Test (org.junit.Test)21 StateProofGraph (edu.cmu.ml.proppr.prove.wam.StateProofGraph)19 WamProgram (edu.cmu.ml.proppr.prove.wam.WamProgram)18 APROptions (edu.cmu.ml.proppr.util.APROptions)15 Prover (edu.cmu.ml.proppr.prove.Prover)14 ProofGraph (edu.cmu.ml.proppr.prove.wam.ProofGraph)12 File (java.io.File)12 StatusLogger (edu.cmu.ml.proppr.util.StatusLogger)11 InferenceExample (edu.cmu.ml.proppr.examples.InferenceExample)10 DprProver (edu.cmu.ml.proppr.prove.DprProver)10 GroundedExample (edu.cmu.ml.proppr.examples.GroundedExample)8 State (edu.cmu.ml.proppr.prove.wam.State)8 ConstantArgument (edu.cmu.ml.proppr.prove.wam.ConstantArgument)7 Feature (edu.cmu.ml.proppr.prove.wam.Feature)7 Map (java.util.Map)7 Grounder (edu.cmu.ml.proppr.Grounder)5 Goal (edu.cmu.ml.proppr.prove.wam.Goal)5 WamPlugin (edu.cmu.ml.proppr.prove.wam.plugins.WamPlugin)5 HashMap (java.util.HashMap)4