Search in sources :

Example 16 with NullaryFunction

use of com.sri.ai.util.base.NullaryFunction in project aic-expresso by aic-sri-international.

the class SGDPLLTTester method testMultiVariableConstraints.

/**
 * Given a theory and a number <code>n</code> of multi-variable constraint tests,
 * generates <code>n</code> formulas in the theory
 * and see if those detected as unsatisfiable by the corresponding solver
 * are indeed unsatisfiable (checked by brute force).
 * Throws an {@link Error} with the failure description if a test fails.
 * @param theoryTestingSupport
 * @param numberOfTests
 * @param maxNumberOfLiterals
 * @param outputCount
 */
public static void testMultiVariableConstraints(boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) {
    NullaryFunction<Constraint> makeInitialConstraint = () -> new DefaultMultiVariableConstraint(theoryTestingSupport.getTheory());
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteral(context);
    // DefaultMultiVariableConstraint is incomplete
    TestRunner tester = SGDPLLTTester::testIncompleteSatisfiability;
    runTesterGivenOnSuccessiveConjunctionsOfLiterals("incomplete satisfiability", tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) BinaryFunction(com.sri.ai.util.base.BinaryFunction) Util.removeFromSetNonDestructively(com.sri.ai.util.Util.removeFromSetNonDestructively) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expression(com.sri.ai.expresso.api.Expression) Sets(com.sri.ai.grinder.library.set.Sets) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) And(com.sri.ai.grinder.library.boole.And) Util.in(com.sri.ai.util.Util.in) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) ThereExists(com.sri.ai.grinder.library.boole.ThereExists) Context(com.sri.ai.grinder.api.Context) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) LinkedHashSet(java.util.LinkedHashSet) Type(com.sri.ai.expresso.api.Type) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) Expressions.getVariablesBeingReferenced(com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) DefaultMultiVariableConstraint(com.sri.ai.grinder.core.constraint.DefaultMultiVariableConstraint) Set(java.util.Set) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) Assignment(com.sri.ai.grinder.interpreter.Assignment) Beta(com.google.common.annotations.Beta) Theory(com.sri.ai.grinder.api.Theory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Util(com.sri.ai.util.Util) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) Constraint(com.sri.ai.grinder.api.Constraint) DefaultMultiVariableConstraint(com.sri.ai.grinder.core.constraint.DefaultMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) DefaultMultiVariableConstraint(com.sri.ai.grinder.core.constraint.DefaultMultiVariableConstraint)

Example 17 with NullaryFunction

use of com.sri.ai.util.base.NullaryFunction in project aic-praise by aic-sri-international.

the class Polytopes method IntensionalConvexHullToListOfFactors.

public static List<TableFactor> IntensionalConvexHullToListOfFactors(IntensionalConvexHullOfFactors polytope) {
    TableFactor factor = (TableFactor) polytope.getFactor();
    List<TableVariable> indexes = mapIntoArrayList(polytope.getIndices(), v -> (TableVariable) v);
    List<List<Integer>> listOfListOfValues = mapIntoList(indexes, v -> mapIntoList(v.getValues(), o -> (Integer) o));
    List<NullaryFunction<Iterator<Integer>>> iteratorForListOfVariableValues = mapIntoList(listOfListOfValues, element -> () -> element.iterator());
    Iterator<ArrayList<Integer>> cartesianProduct = new CartesianProductIterator<Integer>(iteratorForListOfVariableValues);
    List<TableFactor> result = new LinkedList<>();
    for (List<Integer> instantiations : in(cartesianProduct)) {
        result.add(TableFactor.copyToSubTableFactor(factor, indexes, instantiations));
    }
    return result;
}
Also used : Util.union(com.sri.ai.util.Util.union) Util.intersect(com.sri.ai.util.Util.intersect) Box(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.box.Box) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) Util.getFirstSatisfyingPredicateOrNull(com.sri.ai.util.Util.getFirstSatisfyingPredicateOrNull) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) CartesianProductIterator(com.sri.ai.util.collect.CartesianProductIterator) ArrayList(java.util.ArrayList) Util.in(com.sri.ai.util.Util.in) Util.getFirst(com.sri.ai.util.Util.getFirst) IDENTITY_FACTOR(com.sri.ai.praise.core.representation.interfacebased.factor.core.IdentityFactor.IDENTITY_FACTOR) LinkedList(java.util.LinkedList) Iterator(java.util.Iterator) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) AtomicPolytope(com.sri.ai.praise.core.representation.interfacebased.polytope.api.AtomicPolytope) Util.mapIntoList(com.sri.ai.util.Util.mapIntoList) Polytope(com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) Util.collect(com.sri.ai.util.Util.collect) Util.subtract(com.sri.ai.util.Util.subtract) List(java.util.List) Predicate(com.google.common.base.Predicate) Util.myAssert(com.sri.ai.util.Util.myAssert) TableBoxVariable(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.box.TableBoxVariable) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) NullaryFunction(com.sri.ai.util.base.NullaryFunction) CartesianProductIterator(com.sri.ai.util.collect.CartesianProductIterator) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Util.mapIntoList(com.sri.ai.util.Util.mapIntoList) List(java.util.List) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 18 with NullaryFunction

use of com.sri.ai.util.base.NullaryFunction in project aic-expresso by aic-sri-international.

the class SGDPLLTTester method testModelCountingForSingleVariableConstraints.

/**
 * Given a theory and a number <code>n</code> of single-variable constraint tests,
 * generates <code>n</code> formulas in the theory
 * and see if the model counting solver works (checked by brute force).
 * Throws an {@link Error} with the failure description if a test fails.
 * @param theoryTestingSupport
 * @param numberOfTests
 * @param maxNumberOfLiterals
 * @param outputCount
 */
public static void testModelCountingForSingleVariableConstraints(boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) {
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Expression variable = parse(theoryTestingSupport.pickTestingVariableAtRandom());
    NullaryFunction<Constraint> makeInitialConstraint = () -> theoryTestingSupport.getTheory().makeSingleVariableConstraint(variable, context);
    Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteralOn(((SingleVariableConstraint) c).getVariable().toString(), context);
    TestRunner tester = (ls, c, tB, cT, p) -> runModelCountingTestForSingleVariableConstraint(variable, ls, c, tB, cT.getTheory(), p);
    runTesterGivenOnSuccessiveConjunctionsOfLiterals("model counting", tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) BinaryFunction(com.sri.ai.util.base.BinaryFunction) Util.removeFromSetNonDestructively(com.sri.ai.util.Util.removeFromSetNonDestructively) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expression(com.sri.ai.expresso.api.Expression) Sets(com.sri.ai.grinder.library.set.Sets) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) And(com.sri.ai.grinder.library.boole.And) Util.in(com.sri.ai.util.Util.in) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) ThereExists(com.sri.ai.grinder.library.boole.ThereExists) Context(com.sri.ai.grinder.api.Context) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) LinkedHashSet(java.util.LinkedHashSet) Type(com.sri.ai.expresso.api.Type) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) Expressions.getVariablesBeingReferenced(com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) IncompleteMultiVariableConstraint(com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) Assignment(com.sri.ai.grinder.interpreter.Assignment) Beta(com.google.common.annotations.Beta) Theory(com.sri.ai.grinder.api.Theory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Util(com.sri.ai.util.Util) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) Constraint(com.sri.ai.grinder.api.Constraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) IncompleteMultiVariableConstraint(com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint)

Example 19 with NullaryFunction

use of com.sri.ai.util.base.NullaryFunction in project aic-expresso by aic-sri-international.

the class DefaultExtensionalBound method boundProduct.

public static DefaultExtensionalBound boundProduct(Theory theory, Context context, Bound... listOfBounds) {
    if (listOfBounds.length == 0) {
        DefaultExtensionalBound singletonWithNumberOne = new DefaultExtensionalBound(parse("1"));
        return singletonWithNumberOne;
    }
    ArrayList<NullaryFunction<Iterator<Expression>>> iteratorForBoundList = mapIntoArrayList(listOfBounds, bound -> () -> getElements(bound).iterator());
    Iterator<ArrayList<Expression>> cartesianProduct = new CartesianProductIterator<Expression>(iteratorForBoundList);
    if (!cartesianProduct.hasNext()) {
        DefaultExtensionalBound singletonWithNumberOne = new DefaultExtensionalBound(parse("1"));
        println("One of the bounds on the list is { }, which is an error");
        return singletonWithNumberOne;
    }
    ArrayList<Expression> resultList = new ArrayList<>();
    for (ArrayList<? extends Expression> element : in(cartesianProduct)) {
        if (element == null || element.get(0) == null) {
            return null;
        }
        Expression product = apply("*", element);
        Expression evaluation = theory.evaluate(product, context);
        resultList.add(evaluation);
    }
    DefaultExtensionalBound result = new DefaultExtensionalBound(resultList);
    // Updating extreme points
    result = updateExtremes(result, theory, context);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) NullaryFunction(com.sri.ai.util.base.NullaryFunction) CartesianProductIterator(com.sri.ai.util.collect.CartesianProductIterator) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList)

Aggregations

NullaryFunction (com.sri.ai.util.base.NullaryFunction)19 Expression (com.sri.ai.expresso.api.Expression)18 Util.in (com.sri.ai.util.Util.in)12 Util.list (com.sri.ai.util.Util.list)12 Collection (java.util.Collection)12 Beta (com.google.common.annotations.Beta)11 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)11 Type (com.sri.ai.expresso.api.Type)11 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)11 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)11 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)11 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)11 SubExpressionsDepthFirstIterator (com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)11 GrinderUtil (com.sri.ai.grinder.helper.GrinderUtil)11 Util (com.sri.ai.util.Util)11 Util.join (com.sri.ai.util.Util.join)11 Util.pickKElementsWithoutReplacement (com.sri.ai.util.Util.pickKElementsWithoutReplacement)11 Util.removeFromSetNonDestructively (com.sri.ai.util.Util.removeFromSetNonDestructively)11 BinaryFunction (com.sri.ai.util.base.BinaryFunction)11 LinkedHashSet (java.util.LinkedHashSet)11