use of com.sri.ai.praise.evaluate.solver.SolverEvaluatorProbabilityEvidenceResult in project aic-praise by aic-sri-international.
the class VECSolverEvaluator method solveProbabilityEvidence.
@Override
public SolverEvaluatorProbabilityEvidenceResult solveProbabilityEvidence(String solveRequestId, ModelLanguage modelLanguage, String model, String evidenceQuery) throws Exception {
if (modelLanguage != ModelLanguage.HOGMv1) {
throw new UnsupportedOperationException(modelLanguage.name() + " is currently not supported by this solver.");
}
Translator inputToUAITranslator = TranslatorFactory.newTranslator(modelLanguage, ModelLanguage.UAI);
// NOTE: This trick is dependent on the input model being HOGMv1
String hogmv1Model = model + "\nrandom UAIQuery : Boolean;\nif " + evidenceQuery + " then UAIQuery else not UAIQuery;\n";
VECCallResult partitionResult = callVECPR("Partition Function " + solveRequestId, inputToUAITranslator, hogmv1Model, new Reader[] { new StringReader(hogmv1Model) });
VECCallResult evidenceResult = callVECPR("Evidence " + solveRequestId, inputToUAITranslator, hogmv1Model + "\nUAIQuery;", new Reader[] { new StringReader(hogmv1Model), new StringReader("UAIQuery") });
Double probabilityResult = Math.pow(10, evidenceResult.resultLog10) / Math.pow(10, partitionResult.resultLog10);
SolverEvaluatorProbabilityEvidenceResult result = new SolverEvaluatorProbabilityEvidenceResult(Math.max(partitionResult.translationTookMS, evidenceResult.translationTookMS), Math.max(partitionResult.vecProcessTookMS, evidenceResult.vecProcessTookMS), probabilityResult.isNaN() ? null : Expressions.makeSymbol(probabilityResult));
return result;
}
use of com.sri.ai.praise.evaluate.solver.SolverEvaluatorProbabilityEvidenceResult in project aic-praise by aic-sri-international.
the class Evaluation method callSolver.
private SolverCallResult callSolver(Evaluation.Configuration configuration, SolverEvaluator solver, ModelPage model, String query) throws Exception {
SolverCallResult result = new SolverCallResult();
long sumOfTotalInferenceTimeInMilliseconds = 0L;
long sumOfTotalTranslationTimeInMilliseconds = 0L;
for (int i = 0; i < configuration.getNumberRunsToAverageOver(); i++) {
if (configuration.type == Type.PR) {
SolverEvaluatorProbabilityEvidenceResult prResult = solver.solveProbabilityEvidence(model.getName() + " - " + query, model.getLanguage(), model.getModel(), query);
sumOfTotalInferenceTimeInMilliseconds += prResult.getTotalInferenceTimeInMilliseconds();
sumOfTotalTranslationTimeInMilliseconds += prResult.getTotalTranslationTimeInMilliseconds();
if (prResult.getProbabilityOfEvidence() == null) {
result.failed = true;
} else {
result.answer = prResult.getProbabilityOfEvidence();
}
} else {
throw new UnsupportedOperationException(configuration.type.name() + " type evaluations are currently not supported");
}
}
result.averageInferenceTimeInMilliseconds = sumOfTotalInferenceTimeInMilliseconds / configuration.getNumberRunsToAverageOver();
result.averagelTranslationTimeInMilliseconds = sumOfTotalTranslationTimeInMilliseconds / configuration.getNumberRunsToAverageOver();
return result;
}
use of com.sri.ai.praise.evaluate.solver.SolverEvaluatorProbabilityEvidenceResult in project aic-praise by aic-sri-international.
the class SGSolverEvaluator method solveProbabilityEvidence.
@Override
public SolverEvaluatorProbabilityEvidenceResult solveProbabilityEvidence(String solveRequestId, ModelLanguage modelLanguage, String model, String evidenceQuery) throws Exception {
if (modelLanguage != ModelLanguage.HOGMv1) {
throw new UnsupportedOperationException(modelLanguage.name() + " is currently not supported by this solver.");
}
SGSolverCallResult prResult = prCallSGSolverCLI(model, evidenceQuery);
Expression probabilityEvidence = null;
if (prResult.resultExpression != null) {
Expression queryExpr = Expressions.parse(evidenceQuery);
Expression resultExpr = Expressions.parse(prResult.resultExpression);
// Simplify if possible
if (IfThenElse.isIfThenElse(resultExpr)) {
Expression condition = IfThenElse.condition(resultExpr);
if (condition.equals(queryExpr)) {
probabilityEvidence = IfThenElse.thenBranch(resultExpr);
} else if (Not.isNegation(condition) && condition.get(0).equals(queryExpr)) {
probabilityEvidence = IfThenElse.elseBranch(resultExpr);
}
} else if (resultExpr.equals(queryExpr)) {
probabilityEvidence = Expressions.ONE;
} else if (Not.isNegation(resultExpr)) {
if (resultExpr.get(0).equals(queryExpr)) {
probabilityEvidence = Expressions.ZERO;
}
}
// If unable to simplify just return the result as computed (i.e. likely symbolic).
if (probabilityEvidence == null) {
probabilityEvidence = resultExpr;
}
}
SolverEvaluatorProbabilityEvidenceResult result = new SolverEvaluatorProbabilityEvidenceResult(0, prResult.sgSolverProcessTookMS, probabilityEvidence);
return result;
}
Aggregations