Search in sources :

Example 1 with ModelPage

use of com.sri.ai.praise.model.common.io.ModelPage in project aic-praise by aic-sri-international.

the class Evaluation method evaluate.

public void evaluate(Evaluation.Configuration configuration, PagedModelContainer modelsToEvaluateContainer, List<SolverEvaluatorConfiguration> solverConfigurations, Evaluation.Listener evaluationListener) {
    // Note, varying domain sizes etc... is achieved by creating variants of a base model in the provided paged model container
    long evaluationStart = System.currentTimeMillis();
    try {
        List<SolverEvaluator> solvers = instantiateSolvers(solverConfigurations, configuration.getWorkingDirectory());
        // Do an initial burn in to ensure any OS caching etc... occurs so as to even out times across runs
        ModelPage burnInModel = modelsToEvaluateContainer.getPages().get(0);
        String burnInQuery = burnInModel.getDefaultQueriesToRun().get(0);
        evaluationListener.notification("Starting solver burn in based on '" + modelsToEvaluateContainer.getName() + " - " + burnInModel.getName() + " : " + burnInQuery + "'");
        for (SolverEvaluator solver : solvers) {
            SolverCallResult solverResult = callSolver(configuration, solver, burnInModel, burnInQuery);
            evaluationListener.notification("Burn in for " + solver.getName() + " complete. Average inference time = " + toDurationString(solverResult.averageInferenceTimeInMilliseconds));
        }
        // Output the report header line
        StringJoiner csvLine = new StringJoiner(",");
        csvLine.add("Problem");
        csvLine.add("Inference Type");
        csvLine.add("Domain Size(s)");
        csvLine.add("# runs values averaged over");
        for (SolverEvaluator solver : solvers) {
            csvLine.add("Solver");
            csvLine.add("Result for " + solver.getName());
            csvLine.add("Inference ms. for " + solver.getName());
            csvLine.add("HH:MM:SS.");
            csvLine.add("Translation ms. for " + solver.getName());
            csvLine.add("HH:MM:SS.");
        }
        evaluationListener.notification("Starting to generate Evaluation Report");
        evaluationListener.csvResultOutput(csvLine.toString());
        // Now evaluate each of the model-query-solver combinations.
        for (ModelPage model : modelsToEvaluateContainer.getPages()) {
            String domainSizes = getDomainSizes(model.getModel());
            for (String query : model.getDefaultQueriesToRun()) {
                csvLine = new StringJoiner(",");
                String problemName = modelsToEvaluateContainer.getName() + " - " + model.getName() + " : " + query;
                evaluationListener.notification("Starting to evaluate " + problemName);
                csvLine.add(problemName);
                csvLine.add(configuration.type.name());
                csvLine.add(domainSizes);
                csvLine.add("" + configuration.getNumberRunsToAverageOver());
                for (SolverEvaluator solver : solvers) {
                    SolverCallResult solverResult = callSolver(configuration, solver, model, query);
                    csvLine.add(solver.getName());
                    csvLine.add(solverResult.failed ? "FAILED" : "" + solverResult.answer);
                    csvLine.add("" + solverResult.averageInferenceTimeInMilliseconds);
                    csvLine.add(toDurationString(solverResult.averageInferenceTimeInMilliseconds));
                    csvLine.add("" + solverResult.averagelTranslationTimeInMilliseconds);
                    csvLine.add(toDurationString(solverResult.averagelTranslationTimeInMilliseconds));
                    evaluationListener.notification("Solver " + solver.getName() + " took an average inference time of " + toDurationString(solverResult.averageInferenceTimeInMilliseconds) + " to solve " + problemName);
                }
                evaluationListener.csvResultOutput(csvLine.toString());
            }
        }
    } catch (Exception ex) {
        evaluationListener.notificationException(ex);
    }
    long evaluationEnd = System.currentTimeMillis();
    evaluationListener.notification("Evaluation took " + toDurationString(evaluationEnd - evaluationStart) + " to run to completion.");
}
Also used : ModelPage(com.sri.ai.praise.model.common.io.ModelPage) SolverEvaluator(com.sri.ai.praise.evaluate.solver.SolverEvaluator) StringJoiner(java.util.StringJoiner)

Example 2 with ModelPage

use of com.sri.ai.praise.model.common.io.ModelPage in project aic-praise by aic-sri-international.

the class PRAiSE method run.

public static void run(String[] args, Supplier<Theory> theorySupplier) {
    try (SGSolverArgs solverArgs = getArgs(args)) {
        List<ModelPage> hogModelsToQuery = getHOGModelsToQuery(solverArgs);
        for (ModelPage hogModelToQuery : hogModelsToQuery) {
            solverArgs.out.print("MODEL NAME = ");
            solverArgs.out.println(hogModelToQuery.getName());
            solverArgs.out.println("MODEL      = ");
            solverArgs.out.println(hogModelToQuery.getModel());
            HOGMQueryRunner queryRunner = new HOGMQueryRunner(hogModelToQuery.getModel(), hogModelToQuery.getDefaultQueriesToRun());
            if (theorySupplier != null) {
                queryRunner.setOptionTheory(theorySupplier.get());
            }
            List<HOGMQueryResult> hogModelQueryResults = queryRunner.query();
            hogModelQueryResults.forEach(hogModelQueryResult -> {
                solverArgs.out.print("QUERY      = ");
                solverArgs.out.println(hogModelQueryResult.getQueryString());
                solverArgs.out.print(RESULT_PREFIX);
                solverArgs.out.println(queryRunner.simplifyAnswer(hogModelQueryResult.getResult(), hogModelQueryResult.getQueryExpression()));
                solverArgs.out.print("TOOK       = ");
                solverArgs.out.println(duration(hogModelQueryResult.getMillisecondsToCompute()) + "\n");
                if (hogModelQueryResult.isErrors()) {
                    hogModelQueryResult.getErrors().forEach(error -> {
                        solverArgs.out.println("ERROR =" + error.getErrorMessage());
                        if (error.getThrowable() != null) {
                            solverArgs.out.println("THROWABLE =");
                            error.getThrowable().printStackTrace(solverArgs.out);
                        }
                    });
                }
            });
        }
    } catch (Exception ex) {
        System.err.println("Error calling SGSolver");
        ex.printStackTrace();
    }
}
Also used : ModelPage(com.sri.ai.praise.model.common.io.ModelPage) HOGMQueryRunner(com.sri.ai.praise.sgsolver.solver.HOGMQueryRunner) HOGMQueryResult(com.sri.ai.praise.sgsolver.solver.HOGMQueryResult) OptionException(joptsimple.OptionException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with ModelPage

use of com.sri.ai.praise.model.common.io.ModelPage in project aic-praise by aic-sri-international.

the class PRAiSE method guessLanguageModel.

private static ModelLanguage guessLanguageModel(List<File> inputFiles) {
    ModelLanguage result = Arrays.stream(ModelLanguage.values()).filter(ml -> inputFiles.stream().anyMatch(inputFile -> inputFile.getName().toLowerCase().endsWith(ml.getDefaultFileExtension().toLowerCase()))).findFirst().orElse(null);
    if (result == null) {
        // Check if the input is a container file and if so get the language from it
        result = inputFiles.stream().filter(inputFile -> inputFile.getName().toLowerCase().endsWith(PagedModelContainer.DEFAULT_CONTAINER_FILE_EXTENSION.toLowerCase())).map(containerInputFile -> {
            ModelLanguage containedLanguage = null;
            try {
                List<ModelPage> models = PagedModelContainer.getModelPagesFromURI(containerInputFile.toURI());
                if (models.size() > 0) {
                    containedLanguage = models.get(0).getLanguage();
                }
            } catch (IOException ioe) {
                System.err.println(ioe.getMessage());
                ioe.printStackTrace();
            }
            return containedLanguage;
        }).findFirst().orElse(null);
    }
    if (result == null) {
        // For simplicity, defaults to HOGMv1 if nothing specified
        result = ModelLanguage.HOGMv1;
    }
    return result;
}
Also used : Arrays(java.util.Arrays) Supplier(java.util.function.Supplier) PagedModelContainer(com.sri.ai.praise.model.common.io.PagedModelContainer) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) OptionException(joptsimple.OptionException) OptionParser(joptsimple.OptionParser) HOGMQueryResult(com.sri.ai.praise.sgsolver.solver.HOGMQueryResult) ModelLanguage(com.sri.ai.praise.lang.ModelLanguage) OptionSet(joptsimple.OptionSet) PrintStream(java.io.PrintStream) OptionSpec(joptsimple.OptionSpec) Charsets(com.google.common.base.Charsets) Files(java.nio.file.Files) HOGMQueryRunner(com.sri.ai.praise.sgsolver.solver.HOGMQueryRunner) IOException(java.io.IOException) ModelPage(com.sri.ai.praise.model.common.io.ModelPage) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Beta(com.google.common.annotations.Beta) List(java.util.List) StringJoiner(java.util.StringJoiner) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ArrayList(java.util.ArrayList) List(java.util.List) ModelLanguage(com.sri.ai.praise.lang.ModelLanguage) IOException(java.io.IOException)

Example 4 with ModelPage

use of com.sri.ai.praise.model.common.io.ModelPage in project aic-praise by aic-sri-international.

the class PRAiSE method getHOGModelsToQuery.

//
// PRIVATE
//	
private static List<ModelPage> getHOGModelsToQuery(SGSolverArgs solverArgs) throws IOException {
    List<ModelPage> result = new ArrayList<>();
    // First handle container files and track non-container files
    List<File> nonContainerFiles = new ArrayList<>();
    for (File inputFile : solverArgs.inputFiles) {
        if (inputFile.getName().endsWith(PagedModelContainer.DEFAULT_CONTAINER_FILE_EXTENSION)) {
            if (solverArgs.globalQueries.size() == 0) {
                // Take the models as is
                result.addAll(PagedModelContainer.getModelPagesFromURI(inputFile.toURI()));
            } else {
                // each model page
                for (ModelPage containerModelPage : PagedModelContainer.getModelPagesFromURI(inputFile.toURI())) {
                    List<String> combinedQueries = new ArrayList<>(solverArgs.globalQueries);
                    combinedQueries.addAll(containerModelPage.getDefaultQueriesToRun());
                    result.add(new ModelPage(containerModelPage.getLanguage(), containerModelPage.getName(), containerModelPage.getModel(), combinedQueries));
                }
            }
        } else {
            nonContainerFiles.add(inputFile);
        }
    }
    // to construct a single model file
    if (nonContainerFiles.size() > 0) {
        String textModel = nonContainerFiles.stream().map(file -> {
            String fileContents = "";
            try {
                fileContents = Files.readAllLines(file.toPath()).stream().collect(Collectors.joining("\n"));
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
            return fileContents;
        }).collect(Collectors.joining("\n"));
        result.add(new ModelPage(solverArgs.inputLanguage, "Model from concatenation of non-container input files", textModel, solverArgs.globalQueries));
    }
    return result;
}
Also used : ModelPage(com.sri.ai.praise.model.common.io.ModelPage) Arrays(java.util.Arrays) Supplier(java.util.function.Supplier) PagedModelContainer(com.sri.ai.praise.model.common.io.PagedModelContainer) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) OptionException(joptsimple.OptionException) OptionParser(joptsimple.OptionParser) HOGMQueryResult(com.sri.ai.praise.sgsolver.solver.HOGMQueryResult) ModelLanguage(com.sri.ai.praise.lang.ModelLanguage) OptionSet(joptsimple.OptionSet) PrintStream(java.io.PrintStream) OptionSpec(joptsimple.OptionSpec) Charsets(com.google.common.base.Charsets) Files(java.nio.file.Files) HOGMQueryRunner(com.sri.ai.praise.sgsolver.solver.HOGMQueryRunner) IOException(java.io.IOException) ModelPage(com.sri.ai.praise.model.common.io.ModelPage) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Beta(com.google.common.annotations.Beta) List(java.util.List) StringJoiner(java.util.StringJoiner) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Example 5 with ModelPage

use of com.sri.ai.praise.model.common.io.ModelPage in project aic-praise by aic-sri-international.

the class AbstractPerspective method newModel.

@Override
public void newModel(ExamplePages examples) {
    newModel(() -> {
        List<ModelPage> pages = examples.getPages();
        Map<Integer, Supplier<ModelPageEditor>> newModelPageIdxs = new HashMap<>();
        for (int i = 0; i < pages.size(); i++) {
            ModelPage page = pages.get(i);
            newModelPageIdxs.put(i, new ModelPageEditorSupplier(page.getModel(), page.getDefaultQueriesToRun()));
        }
        return FXCollections.observableMap(newModelPageIdxs);
    });
    modelFile.set(null);
}
Also used : ModelPage(com.sri.ai.praise.model.common.io.ModelPage) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier)

Aggregations

ModelPage (com.sri.ai.praise.model.common.io.ModelPage)5 HOGMQueryResult (com.sri.ai.praise.sgsolver.solver.HOGMQueryResult)3 HOGMQueryRunner (com.sri.ai.praise.sgsolver.solver.HOGMQueryRunner)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 StringJoiner (java.util.StringJoiner)3 Supplier (java.util.function.Supplier)3 OptionException (joptsimple.OptionException)3 Beta (com.google.common.annotations.Beta)2 Charsets (com.google.common.base.Charsets)2 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)2 ModelLanguage (com.sri.ai.praise.lang.ModelLanguage)2 PagedModelContainer (com.sri.ai.praise.model.common.io.PagedModelContainer)2 File (java.io.File)2 PrintStream (java.io.PrintStream)2 Charset (java.nio.charset.Charset)2 Files (java.nio.file.Files)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2