Search in sources :

Example 1 with SolverEvaluatorProbabilityEvidenceResult

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;
}
Also used : Translator(com.sri.ai.praise.lang.translate.Translator) SolverEvaluatorProbabilityEvidenceResult(com.sri.ai.praise.evaluate.solver.SolverEvaluatorProbabilityEvidenceResult) StringReader(java.io.StringReader)

Example 2 with SolverEvaluatorProbabilityEvidenceResult

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;
}
Also used : SolverEvaluatorProbabilityEvidenceResult(com.sri.ai.praise.evaluate.solver.SolverEvaluatorProbabilityEvidenceResult)

Example 3 with SolverEvaluatorProbabilityEvidenceResult

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) SolverEvaluatorProbabilityEvidenceResult(com.sri.ai.praise.evaluate.solver.SolverEvaluatorProbabilityEvidenceResult)

Aggregations

SolverEvaluatorProbabilityEvidenceResult (com.sri.ai.praise.evaluate.solver.SolverEvaluatorProbabilityEvidenceResult)3 Expression (com.sri.ai.expresso.api.Expression)1 Translator (com.sri.ai.praise.lang.translate.Translator)1 StringReader (java.io.StringReader)1