Search in sources :

Example 11 with ExpressionBasedModel

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

the class RegularParameterEstimationTest method testOptimization.

@Test
public void testOptimization() {
    ExpressionBasedModel expressionBasedModel = buildModel1();
    List<Expression> queryExpressionList = new LinkedList<Expression>();
    queryExpressionList.add(parse("not earthquake and not burglary"));
    queryExpressionList.add(parse("not earthquake and not burglary"));
    queryExpressionList.add(parse("earthquake and burglary"));
    queryExpressionList.add(parse("not earthquake and burglary"));
    queryExpressionList.add(parse("earthquake and not burglary"));
    queryExpressionList.add(parse("earthquake and burglary"));
    queryExpressionList.add(parse("earthquake and burglary"));
    queryExpressionList.add(parse("earthquake and burglary"));
    queryExpressionList.add(parse("earthquake and burglary"));
    queryExpressionList.add(parse("not earthquake and burglary"));
    RegularParameterEstimation regularParameterEstimation = new RegularParameterEstimation(expressionBasedModel, queryExpressionList);
    Map<Expression, Double> result = regularParameterEstimation.optimize();
    System.out.println(result);
    ExpressionBasedModel expressionBasedModel3 = buildModel1();
    List<Expression> queryExpressionList3 = new LinkedList<Expression>();
    queryExpressionList3.add(parse("not earthquake and not burglary"));
    queryExpressionList3.add(parse("not earthquake and not burglary"));
    queryExpressionList3.add(parse("earthquake and burglary"));
    RegularParameterEstimation regularParameterEstimation3 = new RegularParameterEstimation(expressionBasedModel3, queryExpressionList3);
    Map<Expression, Double> result3 = regularParameterEstimation3.optimize();
    System.out.println(result3);
}
Also used : ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression) RegularParameterEstimation(com.sri.ai.praise.learning.symbolicparameterestimation.regularparameterestimation.RegularParameterEstimation) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 12 with ExpressionBasedModel

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

the class UtilTest method main.

/**
 * Test if conversion between String model and ExpressionBasedModel is working.
 */
public static void main(String[] args) {
    String modelString = "random earthquake: Boolean;\n" + "random burglary: Boolean;\n" + "random alarm: Boolean;\n" + "earthquake 0.01;\n" + "burglary 0.1;\n" + "if earthquake\n" + "then if burglary\n" + "then alarm 0.95\n" + "else alarm 0.6\n" + "else if burglary\n" + "then alarm 0.9\n" + "else alarm 0.01;\n";
    List<HOGMProblemError> modelErrors = new ArrayList<>();
    HOGModel hogModel = parseModelStringToHOGMModel(modelString, modelErrors);
    ExpressionBasedModel expressionBasedModel = parseHOGModelToExpressionBasedModel(hogModel);
    System.out.println(expressionBasedModel.toString() + "\n");
    ExpressionBasedModelToFeatureBasedModelTranslation translation = new ExpressionBasedModelToFeatureBasedModelTranslation(expressionBasedModel, new LinkedList<>());
    System.out.println("translation : " + translation.featureBasedModel.toString());
}
Also used : HOGMProblemError(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.parsing.HOGMProblemError) HOGModel(com.sri.ai.praise.core.representation.classbased.hogm.HOGModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel) ArrayList(java.util.ArrayList) ExpressionBasedModelToFeatureBasedModelTranslation(com.sri.ai.praise.core.representation.classbased.featurebased.ExpressionBasedModelToFeatureBasedModelTranslation)

Example 13 with ExpressionBasedModel

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

the class ParameterEstimationForHOGModelTest method testHOGMBased.

// TODO: not matching original expected result put in by Sarah
// @Test
public void testHOGMBased() {
    String modelString = "random terrorAttacks : 0..20;\n" + "random newJobs : 0..100000;\n" + "random dow: 11000..18000;\n" + "random economyIsPoor : Boolean;\n" + "random economyIsGreat : Boolean;\n" + "random attackPerception: Boolean;\n" + "random likeIncumbent  : 0..100000000;\n" + "random likeChallenger : 0..100000000;\n" + "constant Alpha: Real;\n" + "economyIsPoor <=> dow < 13000 and newJobs < 30000;\n" + "economyIsGreat <=> dow > 16000 and newJobs > 70000;\n" + "attackPerception <=> terrorAttacks > 4;\n" + "if economyIsGreat\n" + "then if likeIncumbent > 70000000 then Alpha/30000000 else (1-Alpha)/(70000000 + 1)\n" + "else if economyIsPoor\n" + "then if likeIncumbent < 40000000 then 0.8/40000000 else 0.2/(60000000 + 1)\n" + "else if attackPerception\n" + "then if likeIncumbent < 60000000 then 0.9/60000000 else 0.1/(40000000 + 1);\n";
    List<HOGMProblemError> modelErrors = new ArrayList<>();
    List<Pair<Expression, Expression>> pairsQueryEvidence = new LinkedList<Pair<Expression, Expression>>();
    Pair<Expression, Expression> pair = new Pair<Expression, Expression>(parse("likeIncumbent > likeChallenger"), parse("null"));
    pairsQueryEvidence.add(pair);
    ParameterEstimationForHOGModel parameterEstimationForHOGModel = new ParameterEstimationForHOGModel(modelString, pairsQueryEvidence, modelErrors);
    HashMap<Expression, Double> expected = new HashMap<Expression, Double>();
    expected.put(parse("Alpha"), 1.0);
    HashMap<Expression, Double> mapResult = runTestHOGModelBased(expected, parameterEstimationForHOGModel, new double[] { 0 });
    HOGModel test = parameterEstimationForHOGModel.buildOptimizedHOGModel(mapResult);
    ExpressionBasedModel newModel = parseHOGModelToExpressionBasedModel(test);
    System.out.println(newModel.toString());
    assertEquals(expected, mapResult);
}
Also used : ParameterEstimationForHOGModel(com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForHOGModel) HashMap(java.util.HashMap) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel(com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HOGMProblemError(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.parsing.HOGMProblemError) ParameterEstimationForHOGModel(com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForHOGModel) HOGModel(com.sri.ai.praise.core.representation.classbased.hogm.HOGModel) Expression(com.sri.ai.expresso.api.Expression) Pair(com.sri.ai.util.base.Pair)

Example 14 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 testExpressionBasedPairs.

// @Test
public void testExpressionBasedPairs() {
    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"));
    Pair<Expression, Expression> pair2 = new Pair<Expression, Expression>(parse("not earthquake"), parse("null"));
    pairsQueryEvidence.add(pair);
    for (int i = 0; i < 999; i++) {
        pairsQueryEvidence.add(pair2);
    }
    HashMap<Expression, Double> expected = new HashMap<Expression, Double>();
    expected.put(parse("Alpha"), 9.999997011753915E-4);
    HashMap<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("earthquake"), parse("alarm"));
    pair2 = new Pair<Expression, Expression>(parse("not earthquake"), parse("not alarm"));
    pairsQueryEvidence.add(pair);
    for (int i = 0; i < 999; i++) {
        pairsQueryEvidence.add(pair2);
    }
    expected = new HashMap<Expression, Double>();
    expected.put(parse("Alpha"), 0.008214845805751847);
    expected.put(parse("Beta"), 8.527665270285399E-9);
    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"), parse("burglary and alarm"));
    pair2 = new Pair<Expression, Expression>(parse("not earthquake"), parse("not alarm"));
    pairsQueryEvidence.add(pair);
    pairsQueryEvidence.add(pair2);
    expected = new HashMap<Expression, Double>();
    expected.put(parse("Alpha"), 0.7550338919520323);
    expected.put(parse("Beta"), 7.003268978167072E-6);
    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 15 with ExpressionBasedModel

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

the class RegularSymbolicParameterEstimationTest method testOptimization.

@Test
public void testOptimization() {
    ExpressionBasedModel expressionBasedModel = buildModel1();
    List<Expression> queryExpressionList = new LinkedList<Expression>();
    queryExpressionList.add(parse("not earthquake and not burglary"));
    queryExpressionList.add(parse("not earthquake and not burglary"));
    queryExpressionList.add(parse("earthquake and burglary"));
    queryExpressionList.add(parse("not earthquake and burglary"));
    queryExpressionList.add(parse("earthquake and not burglary"));
    queryExpressionList.add(parse("earthquake and burglary"));
    queryExpressionList.add(parse("earthquake and burglary"));
    queryExpressionList.add(parse("earthquake and burglary"));
    queryExpressionList.add(parse("earthquake and burglary"));
    queryExpressionList.add(parse("not earthquake and burglary"));
    RegularSymbolicParameterEstimation regularParameterEstimation = new RegularSymbolicParameterEstimation(expressionBasedModel, queryExpressionList);
    Map<Expression, Double> result = regularParameterEstimation.optimize();
    System.out.println(result);
    ExpressionBasedModel expressionBasedModel2 = buildModel3();
    RegularSymbolicParameterEstimation regularParameterEstimation2 = new RegularSymbolicParameterEstimation(expressionBasedModel2, queryExpressionList);
    Map<Expression, Double> result2 = regularParameterEstimation2.optimize();
    System.out.println(result2);
    ExpressionBasedModel expressionBasedModel3 = buildModel1();
    List<Expression> queryExpressionList3 = new LinkedList<Expression>();
    queryExpressionList3.add(parse("not earthquake and not burglary"));
    queryExpressionList3.add(parse("not earthquake and not burglary"));
    queryExpressionList3.add(parse("earthquake and burglary"));
    RegularSymbolicParameterEstimation regularParameterEstimation3 = new RegularSymbolicParameterEstimation(expressionBasedModel3, queryExpressionList3);
    Map<Expression, Double> result3 = regularParameterEstimation3.optimize();
    System.out.println(result3);
}
Also used : ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Expression(com.sri.ai.expresso.api.Expression) LinkedList(java.util.LinkedList) RegularSymbolicParameterEstimation(com.sri.ai.praise.learning.symbolicparameterestimation.RegularSymbolicParameterEstimation) Test(org.junit.Test)

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