Search in sources :

Example 1 with Pair

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

the class IndexExpressions method getIndexAndDomain.

public static Pair<Expression, Expression> getIndexAndDomain(Expression indexExpression) {
    boolean bothIndexAndDomain = indexExpression.hasFunctor("in") && indexExpression.numberOfArguments() == 2;
    Expression index;
    Expression indexDomain;
    if (bothIndexAndDomain) {
        index = indexExpression.get(0);
        indexDomain = indexExpression.get(1);
    } else {
        index = indexExpression;
        indexDomain = type(index);
    }
    return new Pair<Expression, Expression>(index, indexDomain);
}
Also used : Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Pair(com.sri.ai.util.base.Pair)

Example 2 with Pair

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

the class DefaultPolynomial method divide.

@Override
public Pair<Polynomial, Polynomial> divide(Polynomial divisor) throws IllegalArgumentException {
    assertSameVariables(divisor);
    Pair<Polynomial, Polynomial> result;
    if (isZero()) {
        // 0 / divisor = 0
        result = new Pair<>(this, this);
    } else // Base case
    if (isMonomial() && divisor.isMonomial()) {
        Pair<Monomial, Monomial> monomialQuotientAndRemainder = asMonomial().divide(divisor.asMonomial());
        result = new Pair<>(makeFromMonomial(monomialQuotientAndRemainder.first, getVariables()), makeFromMonomial(monomialQuotientAndRemainder.second, getVariables()));
    } else if (divisor.isNumericConstant()) {
        // In this case do not need to worry about remainders as can always
        // divide using a numeric constant divisor.
        Monomial monomialDivisor = divisor.asMonomial();
        List<Monomial> quotients = new ArrayList<>();
        for (Monomial term : getOrderedSummands()) {
            Pair<Monomial, Monomial> monomialQuotientAndRemainder = term.divide(monomialDivisor);
            if (!monomialQuotientAndRemainder.second.isZero()) {
                throw new IllegalStateException("Got an unexpected remainder from " + term + " / " + divisor);
            }
            quotients.add(monomialQuotientAndRemainder.first);
        }
        result = new Pair<>(new DefaultPolynomial(quotients, getVariables()), makeFromMonomial(DefaultMonomial.ZERO, getVariables()));
    } else {
        // Univariate case
        if (getVariables().size() == 1) {
            // TODO - implement faster synthetic division version
            //        see: https://en.wikipedia.org/wiki/Synthetic_division
            // Perform Polynomial Long Division
            Polynomial quotient = makeFromMonomial(DefaultMonomial.ZERO, getVariables());
            Polynomial remainder = this;
            Monomial leadingDivisorTerm = divisor.getOrderedSummands().get(0);
            do {
                Monomial leadingNumeratorTerm = remainder.getOrderedSummands().get(0);
                Pair<Monomial, Monomial> monomialQuotientAndRemainder = leadingNumeratorTerm.divide(leadingDivisorTerm);
                if (!monomialQuotientAndRemainder.second.isZero()) {
                    // Could not divide, i.e. have a remainder
                    break;
                }
                Polynomial monomialQuotient = makeFromMonomial(monomialQuotientAndRemainder.first, getVariables());
                quotient = quotient.add(monomialQuotient);
                remainder = remainder.minus(divisor.times(monomialQuotient));
            } while (!remainder.isZero());
            result = new Pair<>(quotient, remainder);
        } else {
            // TODO - Multivariate case, currently not supported 
            // See: https://en.wikipedia.org/wiki/Gr%C3%B6bner_basis
            // for generalization of long division to the multivariate case.
            result = new Pair<>(makeFromMonomial(DefaultMonomial.ZERO, getVariables()), this);
        }
    }
    return result;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) Monomial(com.sri.ai.grinder.polynomial.api.Monomial) ArrayList(java.util.ArrayList) Pair(com.sri.ai.util.base.Pair)

Example 3 with Pair

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

the class HOGModelGroundingTest method test.

/**
	 * @throws AssertionError
	 */
@Test
public void test() throws AssertionError {
    long start = System.currentTimeMillis();
    StringJoiner sj = new StringJoiner("\n");
    sj.add("sort People : 10, Putin;");
    sj.add("sort Countries : 10, USA, Russia;");
    sj.add("random country : Countries;");
    sj.add("random president : People;");
    sj.add("random communism : Boolean;");
    sj.add("random democracy : Boolean;");
    sj.add("random votePutin : 1..15;");
    sj.add("if country = Russia then if president = Putin then communism else not communism else if democracy then not communism else communism;");
    sj.add("if country = Russia then if votePutin > 5 then president = Putin else not president = Putin;");
    HOGMParserWrapper parser = new HOGMParserWrapper();
    ParsedHOGModel parsedModel = parser.parseModel(sj.toString());
    FactorsAndTypes factorsAndTypes = new ExpressionFactorsAndTypes(parsedModel);
    List<Expression> evidence = new ArrayList<>();
    evidence.add(Expressions.parse("communism"));
    StringJoiner outputBuffer = new StringJoiner("");
    HOGModelGrounding.ground(factorsAndTypes, evidence, new // NOTE: an example listener that outputs in the UAI format
    HOGModelGrounding.Listener() {

        int numberVariables;

        StringJoiner preamble = new StringJoiner("");

        StringJoiner functionTables = new StringJoiner("");

        List<Pair<Integer, Integer>> evidence = new ArrayList<>();

        @Override
        public void numberGroundVariables(int number) {
            this.numberVariables = number;
            preamble.add("MARKOV\n");
            preamble.add("" + number + "\n");
        }

        @Override
        public void groundVariableCardinality(int variableIndex, int cardinality) {
            preamble.add("" + cardinality);
            if (variableIndex == (numberVariables - 1)) {
                preamble.add("\n");
            } else {
                preamble.add(" ");
            }
        }

        @Override
        public void numberFactors(int number) {
            preamble.add("" + number + "\n");
        }

        @Override
        public void factorParticipants(int factorIndex, int[] variableIndexes) {
            preamble.add("" + variableIndexes.length);
            for (int i = 0; i < variableIndexes.length; i++) {
                preamble.add(" " + variableIndexes[i]);
            }
            preamble.add("\n");
        }

        @Override
        public void factorValue(int numberFactorValues, boolean isFirstValue, boolean isLastValue, Rational value) {
            if (isFirstValue) {
                functionTables.add("\n" + numberFactorValues + "\n");
            } else {
                functionTables.add(" ");
            }
            functionTables.add("" + value.doubleValue());
            if (isLastValue) {
                functionTables.add("\n");
            }
        }

        @Override
        public void evidence(int variableIndex, int valueIndex) {
            evidence.add(new Pair<>(variableIndex, valueIndex));
        }

        @Override
        public void groundingComplete() {
            long end = System.currentTimeMillis() - start;
            outputBuffer.add("--- MODEL ---\n");
            outputBuffer.add(preamble.toString());
            outputBuffer.add(functionTables.toString());
            outputBuffer.add("--- EVIDENCE ---\n");
            outputBuffer.add("" + evidence.size());
            for (Pair<Integer, Integer> evidenceAssignment : evidence) {
                outputBuffer.add(" ");
                outputBuffer.add(evidenceAssignment.first.toString());
                outputBuffer.add(" ");
                outputBuffer.add(evidenceAssignment.second.toString());
            }
            System.out.println(outputBuffer.toString());
            System.out.println("\nTime taken for grounding (not printing): " + end + " ms.");
        }
    });
    String expected = "--- MODEL ---\n" + "MARKOV\n" + "5\n" + "10 10 2 2 15\n" + "2\n" + "4 0 1 2 3\n" + "3 0 4 1\n" + "\n" + "400\n" + "0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 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0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5\n" + "--- EVIDENCE ---\n" + "1 2 1";
    assertEquals(expected, outputBuffer.toString());
}
Also used : ParsedHOGModel(com.sri.ai.praise.model.v1.hogm.antlr.ParsedHOGModel) FactorsAndTypes(com.sri.ai.praise.sgsolver.solver.FactorsAndTypes) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) Rational(com.sri.ai.util.math.Rational) ArrayList(java.util.ArrayList) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper) HOGModelGrounding(com.sri.ai.praise.lang.grounded.model.HOGModelGrounding) Expression(com.sri.ai.expresso.api.Expression) StringJoiner(java.util.StringJoiner) Pair(com.sri.ai.util.base.Pair) Test(org.junit.Test)

Example 4 with Pair

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

the class InversionPerformanceEvaluationTest method evaluate.

private Pair<Long, Expression> evaluate(Expression sumProduct, SumProductInterpreter sumProductInterpreter) {
    long start = System.currentTimeMillis();
    Expression evalResult = sumProductInterpreter.apply(sumProduct, context);
    long duration = System.currentTimeMillis() - start;
    Pair<Long, Expression> result = new Pair<>(duration, evalResult);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) Pair(com.sri.ai.util.base.Pair)

Example 5 with Pair

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

the class AbstractSingleVariableNumericConstraintFeasibilityRegionStepSolver method makeUpperBoundsAndStrictness.

/** 
	 * A method setting {@link #upperBoundsIncludingImplicitOnes} and {@link #fromUpperBoundsIncludingImplicitOnesToStrictness}
	 * from constraint and variable's type.
	 * @param context
	 */
protected void makeUpperBoundsAndStrictness(Context context) {
    AbstractSingleVariableConstraint abstractSingleVariableConstraint = (AbstractSingleVariableConstraint) constraint;
    FunctionIterator<Expression, Pair<Expression, Boolean>> upperBoundsFromPositiveNormalizedAtomsIterator = functionIterator(predicateIterator(abstractSingleVariableConstraint.getPositiveNormalizedAtoms(), e -> e.hasFunctor(LESS_THAN)), // strict
    e -> processExplicitUpperBoundAndStrictnessPair(e.get(1), true, context));
    FunctionIterator<Expression, Pair<Expression, Boolean>> upperBoundsFromNegativeNormalizedAtomsIterator = functionIterator(predicateIterator(abstractSingleVariableConstraint.getNegativeNormalizedAtoms(), // not (X > Y) <=> X <= Y, so Y is a non-strict upper bound
    e -> e.hasFunctor(GREATER_THAN)), // non-strict
    e -> processExplicitUpperBoundAndStrictnessPair(e.get(1), false, context));
    Pair<Expression, Boolean> typeUpperBound = getTypeUpperBoundAndStrictness(context);
    Iterator<Pair<Expression, Boolean>> upperBoundsAndStrictnessIterator = new NestedIterator<>(upperBoundsFromPositiveNormalizedAtomsIterator, upperBoundsFromNegativeNormalizedAtomsIterator, typeUpperBound);
    upperBoundsIncludingImplicitOnes = arrayList();
    fromUpperBoundsIncludingImplicitOnesToStrictness = map();
    for (Pair<Expression, Boolean> boundAndStrictness : in(upperBoundsAndStrictnessIterator)) {
        Expression bound = boundAndStrictness.first;
        upperBoundsIncludingImplicitOnes.add(bound);
        Boolean strictness = boundAndStrictness.second;
        Boolean previousStrictness = fromUpperBoundsIncludingImplicitOnesToStrictness.get(bound);
        if (previousStrictness == null || (!previousStrictness && strictness)) {
            // if no strictness information so far, store current one; otherwise, only need to change it if previous occurrences were non-strict and this one is strict
            fromUpperBoundsIncludingImplicitOnesToStrictness.put(bound, strictness);
        }
    }
}
Also used : LESS_THAN_OR_EQUAL_TO(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.LESS_THAN_OR_EQUAL_TO) Expressions(com.sri.ai.expresso.helper.Expressions) NestedIterator(com.sri.ai.util.collect.NestedIterator) INFINITY(com.sri.ai.expresso.helper.Expressions.INFINITY) PairOf(com.sri.ai.util.base.PairOf) Expression(com.sri.ai.expresso.api.Expression) CartesianProductIterator(com.sri.ai.util.collect.CartesianProductIterator) PairOf.makePairOf(com.sri.ai.util.base.PairOf.makePairOf) ArrayList(java.util.ArrayList) Util.in(com.sri.ai.util.Util.in) Util.map(com.sri.ai.util.Util.map) Symbol(com.sri.ai.expresso.api.Symbol) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) ConstantStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantStepSolver) Map(java.util.Map) Pair.pair(com.sri.ai.util.base.Pair.pair) AbstractExpressionWithPropagatedLiteralsStepSolver(com.sri.ai.grinder.sgdpllt.core.solver.AbstractExpressionWithPropagatedLiteralsStepSolver) Util.arrayList(com.sri.ai.util.Util.arrayList) FunctionIterator(com.sri.ai.util.collect.FunctionIterator) LiteralStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.LiteralStepSolver) Pair(com.sri.ai.util.base.Pair) Equality(com.sri.ai.grinder.sgdpllt.library.Equality) PredicateIterator.predicateIterator(com.sri.ai.util.collect.PredicateIterator.predicateIterator) Util.arrayListFrom(com.sri.ai.util.Util.arrayListFrom) Function(com.google.common.base.Function) Iterator(java.util.Iterator) Util.iterator(com.sri.ai.util.Util.iterator) Util.list(com.sri.ai.util.Util.list) MINUS_INFINITY(com.sri.ai.expresso.helper.Expressions.MINUS_INFINITY) AbstractSingleVariableConstraint(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractSingleVariableConstraint) EQUALITY(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.EQUALITY) Context(com.sri.ai.grinder.sgdpllt.api.Context) GREATER_THAN_OR_EQUAL_TO(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.GREATER_THAN_OR_EQUAL_TO) Beta(com.google.common.annotations.Beta) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) StepSolver(com.sri.ai.grinder.sgdpllt.api.StepSolver) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) FunctionIterator.functionIterator(com.sri.ai.util.collect.FunctionIterator.functionIterator) ConstantExpressionStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver) LESS_THAN(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.LESS_THAN) AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver) MaximumExpressionStepSolver(com.sri.ai.grinder.sgdpllt.helper.MaximumExpressionStepSolver) Util(com.sri.ai.util.Util) PairOfElementsInListIterator(com.sri.ai.util.collect.PairOfElementsInListIterator) FunctorConstants(com.sri.ai.grinder.sgdpllt.library.FunctorConstants) GREATER_THAN(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.GREATER_THAN) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) Expression(com.sri.ai.expresso.api.Expression) NestedIterator(com.sri.ai.util.collect.NestedIterator) AbstractSingleVariableConstraint(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractSingleVariableConstraint) Pair(com.sri.ai.util.base.Pair)

Aggregations

Pair (com.sri.ai.util.base.Pair)19 Expression (com.sri.ai.expresso.api.Expression)16 ArrayList (java.util.ArrayList)13 Map (java.util.Map)9 LinkedHashMap (java.util.LinkedHashMap)7 Context (com.sri.ai.grinder.sgdpllt.api.Context)6 Expressions (com.sri.ai.expresso.helper.Expressions)5 List (java.util.List)5 Type (com.sri.ai.expresso.api.Type)4 TupleType (com.sri.ai.expresso.type.TupleType)4 FunctorConstants (com.sri.ai.grinder.sgdpllt.library.FunctorConstants)4 Beta (com.google.common.annotations.Beta)3 Expressions.apply (com.sri.ai.expresso.helper.Expressions.apply)3 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)3 Polynomial (com.sri.ai.grinder.polynomial.api.Polynomial)3 EQUALITY (com.sri.ai.grinder.sgdpllt.library.FunctorConstants.EQUALITY)3 GREATER_THAN_OR_EQUAL_TO (com.sri.ai.grinder.sgdpllt.library.FunctorConstants.GREATER_THAN_OR_EQUAL_TO)3 LESS_THAN_OR_EQUAL_TO (com.sri.ai.grinder.sgdpllt.library.FunctorConstants.LESS_THAN_OR_EQUAL_TO)3 Function (com.google.common.base.Function)2 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)2