Search in sources :

Example 16 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class ParameterEstimationForExpressionBasedModelTest method testBuildOptimizedExpressionBasedModel.

// @Test
public void testBuildOptimizedExpressionBasedModel() {
    ExpressionBasedModel expressionBasedModel = ExpressionBasedModelExamples.buildModel1();
    System.out.println("expressionBasedModel before optimization : " + expressionBasedModel);
    List<Pair<Expression, Expression>> pairsQueryEvidence = new LinkedList<Pair<Expression, Expression>>();
    Pair<Expression, Expression> pair = new Pair<Expression, Expression>(parse("earthquake"), parse("null"));
    pairsQueryEvidence.add(pair);
    ParameterEstimationForExpressionBasedModel parameterEstimationForExpressionBasedModel = new ParameterEstimationForExpressionBasedModel(expressionBasedModel, pairsQueryEvidence);
    HashMap<Expression, Double> result = parameterEstimationForExpressionBasedModel.optimize(new double[] { 0 });
    ExpressionBasedModel newModel = buildOptimizedExpressionBasedModel(result, expressionBasedModel);
    System.out.println(newModel);
    List<Pair<Expression, Expression>> pairsQueryEvidence2 = new LinkedList<Pair<Expression, Expression>>();
    Pair<Expression, Expression> pair2 = new Pair<Expression, Expression>(parse("burglary"), parse("null"));
    pairsQueryEvidence2.add(pair2);
    ParameterEstimationForExpressionBasedModel parameterEstimationForExpressionBasedModel2 = new ParameterEstimationForExpressionBasedModel(newModel, pairsQueryEvidence2);
    HashMap<Expression, Double> result2 = parameterEstimationForExpressionBasedModel2.optimize(new double[] { 0 });
    ExpressionBasedModel newModel2 = buildOptimizedExpressionBasedModel(result2, newModel);
    System.out.println(newModel2);
}
Also used : UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel) ParameterEstimationForExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression) LinkedList(java.util.LinkedList) ParameterEstimationForExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForExpressionBasedModel) Pair(com.sri.ai.util.base.Pair)

Example 17 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class ParameterEstimationForExpressionBasedModelTest method testReportCompleteData.

/**
 * Tests for my report
 */
@Test
public void testReportCompleteData() {
    long startTime = System.nanoTime();
    ExpressionBasedModel expressionBasedModel = ExpressionBasedModelExamples.buildModel1();
    List<Pair<Expression, Expression>> pairsQueryEvidence = new LinkedList<Pair<Expression, Expression>>();
    Pair<Expression, Expression> pair = new Pair<Expression, Expression>(parse("earthquake and burglary and alarm"), parse("null"));
    Pair<Expression, Expression> pair2 = new Pair<Expression, Expression>(parse("not earthquake and burglary and alarm"), parse("null"));
    Pair<Expression, Expression> pair3 = new Pair<Expression, Expression>(parse("not earthquake and not burglary and not alarm"), parse("null"));
    Pair<Expression, Expression> pair4 = new Pair<Expression, Expression>(parse("earthquake and not burglary and not alarm"), parse("null"));
    Pair<Expression, Expression> pair5 = new Pair<Expression, Expression>(parse("earthquake and not burglary and alarm"), parse("null"));
    for (int i = 0; i < 3; i++) {
        pairsQueryEvidence.add(pair);
    }
    for (int i = 0; i < 10; i++) {
        pairsQueryEvidence.add(pair2);
    }
    for (int i = 0; i < 99; i++) {
        pairsQueryEvidence.add(pair3);
    }
    for (int i = 0; i < 5; i++) {
        pairsQueryEvidence.add(pair4);
    }
    for (int i = 0; i < 4; i++) {
        pairsQueryEvidence.add(pair5);
    }
    HashMap<Expression, Double> expected = new HashMap<Expression, Double>();
    expected.put(parse("Alpha"), 0.09917510761762943);
    expected.put(parse("Beta"), 0.10743645971117469);
    HashMap<Expression, Double> mapResult = runTestExpressionBased(pairsQueryEvidence, expressionBasedModel, new double[] { 0, 0 });
    long endTime = System.nanoTime();
    long totalTime = endTime - startTime;
    System.out.println("running time : " + totalTime * 0.000000001);
    System.out.println("expected : " + expected);
    System.out.println("result : " + mapResult);
    assertEquals(expected, mapResult);
}
Also used : UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel) ParameterEstimationForExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Pair(com.sri.ai.util.base.Pair) Test(org.junit.Test)

Example 18 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class ParameterEstimationForExpressionBasedModelTest method testReportIncompleteData.

// @Test
public void testReportIncompleteData() {
    long startTime = System.nanoTime();
    ExpressionBasedModel expressionBasedModel = ExpressionBasedModelExamples.buildModel1();
    List<Pair<Expression, Expression>> pairsQueryEvidence = new LinkedList<Pair<Expression, Expression>>();
    Pair<Expression, Expression> pair = new Pair<Expression, Expression>(parse("earthquake"), parse("alarm"));
    Pair<Expression, Expression> pair2 = new Pair<Expression, Expression>(parse("burglary"), parse("alarm"));
    Pair<Expression, Expression> pair3 = new Pair<Expression, Expression>(parse("not earthquake and not burglary"), parse("not alarm"));
    Pair<Expression, Expression> pair4 = new Pair<Expression, Expression>(parse("earthquake or not burglary"), parse("not alarm"));
    Pair<Expression, Expression> pair5 = new Pair<Expression, Expression>(parse("earthquake and not burglary"), parse("alarm"));
    for (int i = 0; i < 3; i++) {
        pairsQueryEvidence.add(pair);
    }
    for (int i = 0; i < 10; i++) {
        pairsQueryEvidence.add(pair2);
    }
    for (int i = 0; i < 99; i++) {
        pairsQueryEvidence.add(pair3);
    }
    for (int i = 0; i < 5; i++) {
        pairsQueryEvidence.add(pair4);
    }
    for (int i = 0; i < 4; i++) {
        pairsQueryEvidence.add(pair5);
    }
    HashMap<Expression, Double> expected = new HashMap<Expression, Double>();
    expected.put(parse("Alpha"), 0.1063354169257403);
    expected.put(parse("Beta"), 0.13618421966599503);
    HashMap<Expression, Double> mapResult = runTestExpressionBased(pairsQueryEvidence, expressionBasedModel, new double[] { 0, 0 });
    long endTime = System.nanoTime();
    long totalTime = endTime - startTime;
    System.out.println("running time : " + totalTime * 0.000000001);
    System.out.println("expected : " + expected);
    System.out.println("result : " + mapResult);
    assertEquals(expected, mapResult);
}
Also used : UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel) ParameterEstimationForExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Pair(com.sri.ai.util.base.Pair)

Example 19 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class ParameterEstimationForExpressionBasedModelTest method testExpressionBased.

// @Test
public void testExpressionBased() {
    ExpressionBasedModel expressionBasedModel = ExpressionBasedModelExamples.buildModel1();
    List<Pair<Expression, Expression>> pairsQueryEvidence = new LinkedList<Pair<Expression, Expression>>();
    Pair<Expression, Expression> pair = new Pair<Expression, Expression>(parse("earthquake"), parse("null"));
    pairsQueryEvidence.add(pair);
    pairsQueryEvidence.add(pair);
    Map<Expression, Double> expected = new HashMap<Expression, Double>();
    expected.put(parse("Alpha"), 1.0);
    Map<Expression, Double> mapResult = runTestExpressionBased(pairsQueryEvidence, expressionBasedModel, new double[] { 0 });
    System.out.println("expected : " + expected);
    System.out.println("result : " + mapResult);
    assertEquals(expected, mapResult);
    pairsQueryEvidence.clear();
    pair = new Pair<Expression, Expression>(parse("not earthquake"), parse("null"));
    pairsQueryEvidence.add(pair);
    pairsQueryEvidence.add(pair);
    expected.put(parse("Alpha"), 1.4905019930035748E-22);
    mapResult = runTestExpressionBased(pairsQueryEvidence, expressionBasedModel, new double[] { 0 });
    System.out.println("expected : " + expected);
    System.out.println("result : " + mapResult);
    assertEquals(expected, mapResult);
    pairsQueryEvidence.clear();
    pair = new Pair<Expression, Expression>(parse("earthquake"), parse("null"));
    Pair<Expression, Expression> pair2 = new Pair<Expression, Expression>(parse("not earthquake"), parse("null"));
    pairsQueryEvidence.add(pair2);
    pairsQueryEvidence.add(pair);
    pairsQueryEvidence.add(pair);
    pairsQueryEvidence.add(pair);
    pairsQueryEvidence.add(pair);
    pairsQueryEvidence.add(pair);
    pairsQueryEvidence.add(pair);
    pairsQueryEvidence.add(pair);
    pairsQueryEvidence.add(pair);
    pairsQueryEvidence.add(pair);
    expected.put(parse("Alpha"), 0.9000011823080569);
    mapResult = runTestExpressionBased(pairsQueryEvidence, expressionBasedModel, new double[] { 0 });
    System.out.println("expected : " + expected);
    System.out.println("result : " + mapResult);
    assertEquals(expected, mapResult);
    pairsQueryEvidence.clear();
    pair = new Pair<Expression, Expression>(parse("burglary"), parse("null"));
    pairsQueryEvidence.add(pair2);
    pairsQueryEvidence.add(pair);
    expected.put(parse("Alpha"), 6.289892249011522E-23);
    expected.put(parse("Beta"), 1.0);
    mapResult = runTestExpressionBased(pairsQueryEvidence, expressionBasedModel, new double[] { 0, 0 });
    System.out.println("expected : " + expected);
    System.out.println("result : " + mapResult);
    assertEquals(expected, mapResult);
    // ////////
    pairsQueryEvidence.clear();
    pair = new Pair<Expression, Expression>(parse("earthquake and not burglary"), parse("null"));
    pairsQueryEvidence.add(pair);
    expected.put(parse("Alpha"), 1.0);
    expected.put(parse("Beta"), 6.289892248981177E-23);
    mapResult = runTestExpressionBased(pairsQueryEvidence, expressionBasedModel, new double[] { 0, 0 });
    System.out.println("expected : " + expected);
    System.out.println("result : " + mapResult);
    assertEquals(expected, mapResult);
}
Also used : UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel) ParameterEstimationForExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Pair(com.sri.ai.util.base.Pair)

Example 20 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class ParameterEstimationForExpressionBasedModelTest method runTestExpressionBased.

private HashMap<Expression, Double> runTestExpressionBased(List<Pair<Expression, Expression>> pairsQueryEvidence, ExpressionBasedModel expressionBasedModel, double[] startPoint) {
    ParameterEstimationForExpressionBasedModel parameterEstimationForExpressionBasedModel = new ParameterEstimationForExpressionBasedModel(expressionBasedModel, pairsQueryEvidence);
    HashMap<Expression, Double> result = parameterEstimationForExpressionBasedModel.optimize(startPoint);
    ExpressionBasedModel newModel = buildOptimizedExpressionBasedModel(result, expressionBasedModel);
    System.out.println(" New Model : " + newModel);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel) ParameterEstimationForExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) ParameterEstimationForExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForExpressionBasedModel)

Aggregations

ExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)33 Expression (com.sri.ai.expresso.api.Expression)24 LinkedList (java.util.LinkedList)11 ArrayList (java.util.ArrayList)10 DefaultExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel)8 HOGModel (com.sri.ai.praise.core.representation.classbased.hogm.HOGModel)8 HOGMExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel)8 Pair (com.sri.ai.util.base.Pair)8 ParameterEstimationForExpressionBasedModel (com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForExpressionBasedModel)7 UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel (com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel)7 UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel (com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel)7 HashMap (java.util.HashMap)7 Test (org.junit.Test)6 Context (com.sri.ai.grinder.api.Context)5 HOGMProblemError (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.parsing.HOGMProblemError)5 Type (com.sri.ai.expresso.api.Type)3 HOGMParserWrapper (com.sri.ai.praise.core.representation.classbased.hogm.parsing.HOGMParserWrapper)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 ExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver)2