Search in sources :

Example 1 with Not

use of com.sri.ai.grinder.sgdpllt.library.boole.Not in project aic-praise by aic-sri-international.

the class UAIMARSolver method main.

public static void main(String[] args) throws IOException {
    if (args.length != 4) {
        throw new IllegalArgumentException("Usage: UAIMARSolver <file or directory with UAI-format files> <solution directory> <timeout in ms> equalities|difference_arithmetic");
    }
    File uaiInput = new File(args[0]);
    if (!uaiInput.exists()) {
        throw new IllegalArgumentException("File or directory specified does not exist: " + uaiInput.getAbsolutePath());
    }
    File solutionDir = new File(args[1]);
    if (!solutionDir.exists() || !solutionDir.isDirectory()) {
        throw new IllegalArgumentException("Solution directory is invalid: " + solutionDir.getAbsolutePath());
    }
    int maxSolverTimeInSeconds = Integer.parseInt(args[2]);
    Theory theory;
    if (args[3].equals("equalities")) {
        theory = new CompoundTheory(new PropositionalTheory(), new EqualityTheory(true, true));
    } else if (args[3].equals("difference_arithmetic")) {
        theory = new CompoundTheory(new PropositionalTheory(), new DifferenceArithmeticTheory(true, true));
    } else {
        throw new IllegalArgumentException("4-th argument must be either 'equalities' or 'difference_arithmetic'");
    }
    List<UAIModel> models = new ArrayList<>();
    Map<UAIModel, File> modelToFile = new HashMap<>();
    if (uaiInput.isDirectory()) {
        for (File uaiFile : uaiInput.listFiles((dir, name) -> name.endsWith(".uai"))) {
            UAIModel model = read(uaiFile, solutionDir);
            models.add(model);
            modelToFile.put(model, uaiFile);
        }
    } else {
        UAIModel model = read(uaiInput, solutionDir);
        models.add(model);
        modelToFile.put(model, uaiInput);
    }
    // Sort based on what we consider to be the simplest to hardest
    //Collections.sort(models, (model1, model2) -> Double.compare(model1.ratioUniqueTablesToTables(), model2.ratioUniqueTablesToTables()));
    //Collections.sort(models, (model1, model2) -> Integer.compare(model1.largestNumberOfFunctionTableEntries(), model2.largestNumberOfFunctionTableEntries()));
    Collections.sort(models, (model1, model2) -> Integer.compare(model1.totalNumberEntriesForAllFunctionTables(), model2.totalNumberEntriesForAllFunctionTables()));
    //Collections.sort(models, (model1, model2) -> Integer.compare(model1.numberTables(), model2.numberTables()));
    Map<String, Boolean> modelSolvedStatus = new LinkedHashMap<>();
    Map<String, Long> modelSolvedTime = new LinkedHashMap<>();
    System.out.println("#models read=" + models.size());
    final AtomicInteger cnt = new AtomicInteger(1);
    models.stream().forEach(model -> {
        System.out.println("Starting to Solve: " + modelToFile.get(model).getName() + " (" + cnt.getAndAdd(1) + " of " + models.size() + ")");
        long start = System.currentTimeMillis();
        boolean solved = solve(model, model.getEvidence(), model.getMARSolution(), maxSolverTimeInSeconds, theory);
        long took = (System.currentTimeMillis() - start);
        System.out.println("---- Took " + took + "ms. solved=" + solved);
        modelSolvedStatus.put(modelToFile.get(model).getName(), solved);
        modelSolvedTime.put(modelToFile.get(model).getName(), took);
    });
    System.out.println("MODELS SOLVE STATUS");
    modelSolvedStatus.entrySet().stream().forEach(e -> System.out.printf("%-25s %-5b %12sms.\n", e.getKey(), e.getValue(), modelSolvedTime.get(e.getKey())));
    System.out.println("SUMMARY");
    System.out.println("#models   solved=" + modelSolvedStatus.values().stream().filter(status -> status == true).count());
    System.out.println("#models unsolved=" + modelSolvedStatus.values().stream().filter(status -> status == false).count());
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) MultiIndexQuantifierEliminator(com.sri.ai.grinder.sgdpllt.api.MultiIndexQuantifierEliminator) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) Expressions(com.sri.ai.expresso.helper.Expressions) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Expression(com.sri.ai.expresso.api.Expression) FactorsAndTypes(com.sri.ai.praise.sgsolver.solver.FactorsAndTypes) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Future(java.util.concurrent.Future) Not(com.sri.ai.grinder.sgdpllt.library.boole.Not) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InferenceForFactorGraphAndEvidence(com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence) UAIUtil.convertGenericTableToInstance(com.sri.ai.praise.model.v1.imports.uai.UAIUtil.convertGenericTableToInstance) Map(java.util.Map) And(com.sri.ai.grinder.sgdpllt.library.boole.And) Division(com.sri.ai.grinder.sgdpllt.library.number.Division) ExecutorService(java.util.concurrent.ExecutorService) Equality(com.sri.ai.grinder.sgdpllt.library.Equality) IOException(java.io.IOException) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Collectors(java.util.stream.Collectors) IfThenElse(com.sri.ai.grinder.sgdpllt.library.controlflow.IfThenElse) File(java.io.File) Executors(java.util.concurrent.Executors) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Beta(com.google.common.annotations.Beta) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) GraphicalNetwork(com.sri.ai.praise.lang.grounded.common.GraphicalNetwork) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) UAIUtil.constructGenericTableExpressionUsingEqualities(com.sri.ai.praise.model.v1.imports.uai.UAIUtil.constructGenericTableExpressionUsingEqualities) FunctionTable(com.sri.ai.praise.lang.grounded.common.FunctionTable) Collections(java.util.Collections) FunctorConstants(com.sri.ai.grinder.sgdpllt.library.FunctorConstants) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) ArrayList(java.util.ArrayList) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) LinkedHashMap(java.util.LinkedHashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File)

Aggregations

Beta (com.google.common.annotations.Beta)1 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)1 Expression (com.sri.ai.expresso.api.Expression)1 Expressions (com.sri.ai.expresso.helper.Expressions)1 MultiIndexQuantifierEliminator (com.sri.ai.grinder.sgdpllt.api.MultiIndexQuantifierEliminator)1 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)1 Equality (com.sri.ai.grinder.sgdpllt.library.Equality)1 FunctorConstants (com.sri.ai.grinder.sgdpllt.library.FunctorConstants)1 And (com.sri.ai.grinder.sgdpllt.library.boole.And)1 Not (com.sri.ai.grinder.sgdpllt.library.boole.Not)1 IfThenElse (com.sri.ai.grinder.sgdpllt.library.controlflow.IfThenElse)1 Division (com.sri.ai.grinder.sgdpllt.library.number.Division)1 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)1 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)1 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)1 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)1 FunctionTable (com.sri.ai.praise.lang.grounded.common.FunctionTable)1 GraphicalNetwork (com.sri.ai.praise.lang.grounded.common.GraphicalNetwork)1 UAIUtil.constructGenericTableExpressionUsingEqualities (com.sri.ai.praise.model.v1.imports.uai.UAIUtil.constructGenericTableExpressionUsingEqualities)1 UAIUtil.convertGenericTableToInstance (com.sri.ai.praise.model.v1.imports.uai.UAIUtil.convertGenericTableToInstance)1