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);
}
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());
}
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);
}
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);
}
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);
}
Aggregations