public IAbstractDomainWithTransferFunctions <Expression> TestFalse(Expression guard)
        {
            IntervalEnvironment <Rational, Expression> resultLeft  = (IntervalEnvironment <Rational, Expression>)left.TestFalse(guard);
            WeakUpperBounds <Expression, Rational>     resultRight = (WeakUpperBounds <Expression, Rational>)right.TestFalse(guard);

            return((IAbstractDomainWithTransferFunctions <Expression>)Factory(resultLeft, resultRight));
        }
        public SubPoly(IExpressionDecoder <Expression> decoder, IExpressionEncoder <Expression> encoder)
        {
            IntervalEnvironment <Expression>         intv  = new IntervalEnvironment <Expression>(decoder, encoder);
            LinearEqualitiesEnvironment <Expression> lineq = new LinearEqualitiesEnvironment <Expression>(decoder, encoder, true, false);

            this.subpoly = new SubPolyhedra <Expression>(lineq, intv, decoder, encoder);
        }
Ejemplo n.º 3
0
        public INumericalAbstractDomain <Variable, Expression> RemoveRedundanciesWith(INumericalAbstractDomain <Variable, Expression> oracle)
        {
            IntervalEnvironment <Variable, Expression> resultLeft = (IntervalEnvironment <Variable, Expression>) this.Left.RemoveRedundanciesWith(oracle);
            StripeWithIntervals <Variable, Expression, MetaDataDecoder> resultRight = (StripeWithIntervals <Variable, Expression, MetaDataDecoder>) this.Right.RemoveRedundanciesWith(oracle);

            return((INumericalAbstractDomain <Variable, Expression>)Factory(resultLeft, resultRight));
        }
Ejemplo n.º 4
0
        public INumericalAbstractDomain <Variable, Expression> TestTrueEqual(Expression /*!*/ exp1, Expression /*!*/ exp2)
        {
            IntervalEnvironment <Variable, Expression> resultLeft = this.Left.TestTrueEqual(exp1, exp2);
            StripeWithIntervals <Variable, Expression, MetaDataDecoder> resultRight = (StripeWithIntervals <Variable, Expression, MetaDataDecoder>) this.Right.TestTrueEqual(exp1, exp2);

            return((INumericalAbstractDomain <Variable, Expression>)Factory(resultLeft, resultRight));
        }
Ejemplo n.º 5
0
        public virtual IAbstractDomainForEnvironments <Variable, Expression> TestFalse(Expression guard)
        {
            IntervalEnvironment <Variable, Expression> resultLeft = this.Left.TestFalse(guard);
            StripeWithIntervals <Variable, Expression, MetaDataDecoder> resultRight = (StripeWithIntervals <Variable, Expression, MetaDataDecoder>) this.Right.TestFalse(guard);

            return((IAbstractDomainForEnvironments <Variable, Expression>)Factory(resultLeft, resultRight));
        }
Ejemplo n.º 6
0
        private PolyhedraEnvironment(IExpressionDecoder <Variable, Expression> decoder, IExpressionEncoder <Variable, Expression> encoder, AI.PolyhedraLattice.Element value, IntervalEnvironment <Variable, Expression> intv)
        {
            this.decoder = decoder;
            this.encoder = encoder;
            embedded     = value;
            this.intv    = intv;

            testTrueVisitor = new PolyhedraEnvironment <Expression> .PolyhedraTestTrueVisitor(decoder);

            testFalseVisitor = new PolyhedraEnvironment <Expression> .PolyhedraTestFalseVisitor(decoder);

            testTrueVisitor.FalseVisitor = testFalseVisitor;
            testFalseVisitor.TrueVisitor = testTrueVisitor;
        }
Ejemplo n.º 7
0
        public PolyhedraEnvironment(IExpressionDecoder <Variable, Expression> decoder, IExpressionEncoder <Variable, Expression> encoder)
        {
            this.decoder = decoder;
            this.encoder = encoder;
            embedded     = UnderlyingPolyhedra.Top;
            intv         = new IntervalEnvironment <Variable, Expression>(decoder, encoder);

            testTrueVisitor = new PolyhedraEnvironment <Expression> .PolyhedraTestTrueVisitor(decoder);

            testFalseVisitor = new PolyhedraEnvironment <Expression> .PolyhedraTestFalseVisitor(decoder);

            testTrueVisitor.FalseVisitor = testFalseVisitor;
            testFalseVisitor.TrueVisitor = testTrueVisitor;
        }
        public IntervalEnvironment <Variable, Expression> AbstractsAwayTooLargeBounds(long min, long max)
        {
            Contract.Requires(min <= max);
            Contract.Ensures(Contract.Result <IntervalEnvironment <Variable, Expression> >() != null);

            if (this.IsBottom || this.IsTop)
            {
                return(this);
            }

            var buff = new IntervalEnvironment <Variable, Expression>(this.ExpressionManager);

            foreach (var pair in this.Elements)
            {
                // Is lower bound small enough?
                if (min <= pair.Value.LowerBound)
                {
                    // Is upper bound small enough?
                    if (pair.Value.UpperBound <= max)
                    {
                        buff[pair.Key] = pair.Value;
                    }
                    else
                    { // Abstract upper bound
                        buff[pair.Key] = Interval.For(pair.Value.LowerBound, Rational.PlusInfinity);
                    }
                }
                else
                { // We know lower bound is too large
                    if (pair.Value.UpperBound <= max)
                    {
                        buff[pair.Key] = Interval.For(Rational.MinusInfinity, pair.Value.UpperBound);
                    }
                    else
                    {
                        // Interval too large, let's forget it!
                    }
                }
            }

            return(buff);
        }
        public static bool TryEvalWithCache(
            IntervalEnvironment <Variable, Expression> env, Variable exp, Dictionary <Variable, Interval> cache, out Interval valueIfNormal)
        {
            Contract.Requires(env != null);
            Contract.Requires(cache != null);

            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out valueIfNormal) != null);

            if (!cache.TryGetValue(exp, out valueIfNormal))
            {
                valueIfNormal = env.Eval(exp);
                cache[exp]    = valueIfNormal;
            }
            else
            {
                Contract.Assume(valueIfNormal != null);
            }

            return(valueIfNormal.IsNormal);
        }
        /// <summary>
        /// The pairwise widening
        /// </summary>
        public override IAbstractDomain Widening(IAbstractDomain prev)
        {
            if (this.IsBottom)
            {
                return(prev);
            }
            if (prev.IsBottom)
            {
                return(this);
            }

            Debug.Assert(prev is ProductIntervalsWeakUpperBounds <Expression>, "Wrong type of the domain for the widening...");

            ProductIntervalsWeakUpperBounds <Expression> asIntWSUB = (ProductIntervalsWeakUpperBounds <Expression>)prev;

            IntervalEnvironment <Rational, Expression> widenLeft  = (IntervalEnvironment <Rational, Expression>) this.Left.Widening(asIntWSUB.Left);
            WeakUpperBounds <Expression, Rational>     widenRight = (WeakUpperBounds <Expression, Rational>) this.Right.Widening(asIntWSUB.Right);

            ProductIntervalsWeakUpperBounds <Expression> result = (ProductIntervalsWeakUpperBounds <Expression>) this.Factory(widenLeft, widenRight);

            return(result);
        }
Ejemplo n.º 11
0
 public StripeWithIntervals(IntervalEnvironment <Variable, Expression> /*!*/ left, StripeWithIntervals <Variable, Expression, MetaDataDecoder> /*!*/ right, IExpressionDecoder <Variable, Expression> decoder, Logger Log)
     : base(left, right, Log)
 {
     this.decoder = decoder;
 }
 Reduce(IntervalEnvironment <Rational, Expression> left, WeakUpperBounds <Expression, Rational> right)
 {
     return(this.Factory(left, right));
 }
 Factory(IntervalEnvironment <Rational, Expression> left, WeakUpperBounds <Expression, Rational> right)
 {
     return(new ProductIntervalsWeakUpperBounds <Expression>(left, right, this.decoder));
 }
 private IntervalEnvironment(IntervalEnvironment <Variable, Expression> original)
     : base(original)
 {
 }
Ejemplo n.º 15
0
 private PolyhedraEnvironment(PolyhedraEnvironment <Expression> pe, AI.PolyhedraLattice.Element value, IntervalEnvironment <Variable, Expression> intv)
     : this(pe.decoder, pe.encoder, value, intv)
 {
     testTrueVisitor  = pe.testTrueVisitor;
     testFalseVisitor = pe.testFalseVisitor;
 }
Ejemplo n.º 16
0
 Factory(IntervalEnvironment <Variable, Expression> left, StripeWithIntervals <Variable, Expression, MetaDataDecoder> right)
 {
     return(new StripeWithIntervals <Variable, Expression, MetaDataDecoder>(left, right, this.decoder, this.Log));
 }
 /// <summary>
 /// Please note that the decoder MUST be already be set for the <code>left</code> and <code>right</code> abstract domains
 /// </summary>
 public ProductIntervalsWeakUpperBounds(IntervalEnvironment <Rational, Expression> /*!*/ left, WeakUpperBounds <Expression, Rational> /*!*/ right, IExpressionDecoder <Expression> decoder)
     : base(left, right)
 {
     this.decoder = decoder;
 }
Ejemplo n.º 18
0
 Reduce(IntervalEnvironment <Variable, Expression> left, StripeWithIntervals <Variable, Expression, MetaDataDecoder> right)
 {
     return(this.Factory(left, right));
 }
        [ContractVerification(false)] // The analysis of this method takes forever. Should consider refactoring
        internal Set <Polynomial <Variable, Expression> > ConvexHullHelper(
            IntervalEnvironment <Variable, Expression> other, Variable slack, SubPolyhedra.JoinConstraintInference inference)
        {
            Contract.Requires(other != null);

            var result = new Set <Polynomial <Variable, Expression> >();

            var commonVariables = this.VariablesNonSlack.SetIntersection(other.VariablesNonSlack);

            var oct = false;

            if (commonVariables.Count <= SubPolyhedra.MaxVariablesInOctagonsConstraintInference)
            {
                oct = true;
            }
            else if (inference == SubPolyhedra.JoinConstraintInference.Standard)
            {
                return(result);
            }

            bool CH =
                inference == SubPolyhedra.JoinConstraintInference.CHOct ||
                inference == SubPolyhedra.JoinConstraintInference.ConvexHull2D;

            oct =
                oct ||
                inference == SubPolyhedra.JoinConstraintInference.CHOct ||
                inference == SubPolyhedra.JoinConstraintInference.Octagons;

            int i = 0, j = 0;

            var this_Bounds  = new Dictionary <Variable, Interval>();
            var other_Bounds = new Dictionary <Variable, Interval>();

            foreach (var e1 in commonVariables)
            {
                i++;

                Interval e1Left, e1Right;

                if (
                    !TryEvalWithCache(this, e1, this_Bounds, out e1Left) ||
                    !TryEvalWithCache(other, e1, other_Bounds, out e1Right))
                {
                    continue;
                }

                j = 0;
                foreach (var e2 in commonVariables)
                {
                    j++;

                    // either e1 == e2 or we already have relations between e1 and e2
                    if (e1.Equals(e2) || j <= i)
                    {
                        continue;
                    }

                    Interval e2Left, e2Right;
                    if (
                        !TryEvalWithCache(this, e2, this_Bounds, out e2Left) ||
                        !TryEvalWithCache(other, e2, other_Bounds, out e2Right))
                    {
                        continue;
                    }

                    if (oct)
                    {
                        #region Adding octogonal constraints to get at least as much precision as octagons

                        var monomials = new Monomial <Variable>[] { new Monomial <Variable>(e1), new Monomial <Variable>(-1, e2), new Monomial <Variable>(slack) };

                        Polynomial <Variable, Expression> pol;
                        if (!Polynomial <Variable, Expression> .TryToPolynomialForm(true, monomials, out pol))
                        {
                            throw new AbstractInterpretationException("Impossible case");
                        }

                        result.Add(pol);

                        #endregion
                    }


                    if (CH)
                    {
                        #region Convex Hull

                        // adaptation of the Monotone Chain algorithm

                        var vertices = new PairNonNull <Rational, Rational>[8]
                        {
                            new PairNonNull <Rational, Rational>(e1Left.LowerBound, e2Left.LowerBound),
                            new PairNonNull <Rational, Rational>(e1Left.LowerBound, e2Left.UpperBound),
                            new PairNonNull <Rational, Rational>(e1Left.UpperBound, e2Left.LowerBound),
                            new PairNonNull <Rational, Rational>(e1Left.UpperBound, e2Left.UpperBound),
                            new PairNonNull <Rational, Rational>(e1Right.LowerBound, e2Right.LowerBound),
                            new PairNonNull <Rational, Rational>(e1Right.LowerBound, e2Right.UpperBound),
                            new PairNonNull <Rational, Rational>(e1Right.UpperBound, e2Right.LowerBound),
                            new PairNonNull <Rational, Rational>(e1Right.UpperBound, e2Right.UpperBound)
                        };

                        try
                        {
                            Array.Sort(vertices,
                                       delegate(PairNonNull <Rational, Rational> x, PairNonNull <Rational, Rational> y)
                            {
                                if ((x.One - y.One).Sign == 0)
                                {
                                    return((x.Two - y.Two).Sign);
                                }
                                else
                                {
                                    return((x.One - y.One).Sign);
                                }
                            });
                        }
                        catch (InvalidOperationException)
                        {
                            return(result);
                        }

                        var IsInHull = new bool[8];
                        try
                        {
                            Polynomial <Variable, Expression> pol;
                            #region Computation of the Lower Hull
                            IsInHull[0] = IsFinite(vertices[0]);
                            IsInHull[2] = IsFinite(vertices[2]);
                            IsInHull[4] = IsFinite(vertices[4]);

                            if (IsInHull[0] && IsInHull[2] && IsInHull[4] &&
                                (vertices[2].One - vertices[0].One) * (vertices[4].Two - vertices[0].Two) <= (vertices[4].One - vertices[0].One) * (vertices[2].Two - vertices[0].Two))
                            {
                                IsInHull[2] = false;
                            }

                            IsInHull[6] = IsFinite(vertices[6]);

                            if (IsInHull[2] && IsInHull[4] && IsInHull[6] &&
                                (vertices[4].One - vertices[2].One) * (vertices[6].Two - vertices[2].Two) <= (vertices[6].One - vertices[2].One) * (vertices[4].Two - vertices[2].Two))
                            {
                                IsInHull[4] = false;
                            }

                            if (IsInHull[0] && IsInHull[4] && IsInHull[6] &&
                                (vertices[4].One - vertices[0].One) * (vertices[6].Two - vertices[0].Two) <= (vertices[6].One - vertices[0].One) * (vertices[4].Two - vertices[0].Two))
                            {
                                IsInHull[4] = false;
                            }

                            if (IsInHull[0] && IsInHull[2] && IsInHull[6] &&
                                (vertices[2].One - vertices[0].One) * (vertices[6].Two - vertices[0].Two) <= (vertices[6].One - vertices[0].One) * (vertices[2].Two - vertices[0].Two))
                            {
                                IsInHull[2] = false;
                            }
                            #endregion

                            #region Computation of the Upper Hull
                            IsInHull[1] = IsFinite(vertices[1]);

                            IsInHull[3] = IsFinite(vertices[3]);

                            IsInHull[5] = IsFinite(vertices[5]);

                            if (IsInHull[1] && IsInHull[3] && IsInHull[5] &&
                                (vertices[3].One - vertices[1].One) * (vertices[5].Two - vertices[1].Two) >= (vertices[5].One - vertices[1].One) * (vertices[3].Two - vertices[1].Two))
                            {
                                IsInHull[3] = false;
                            }

                            IsInHull[7] = IsFinite(vertices[7]);

                            if (IsInHull[3] && IsInHull[5] && IsInHull[7] &&
                                (vertices[5].One - vertices[3].One) * (vertices[7].Two - vertices[3].Two) >= (vertices[7].One - vertices[3].One) * (vertices[5].Two - vertices[3].Two))
                            {
                                IsInHull[5] = false;
                            }

                            if (IsInHull[1] && IsInHull[5] && IsInHull[7] &&
                                (vertices[5].One - vertices[1].One) * (vertices[7].Two - vertices[1].Two) >= (vertices[7].One - vertices[1].One) * (vertices[5].Two - vertices[1].Two))
                            {
                                IsInHull[5] = false;
                            }

                            if (IsInHull[1] && IsInHull[3] && IsInHull[7] &&
                                (vertices[3].One - vertices[1].One) * (vertices[7].Two - vertices[1].Two) >= (vertices[7].One - vertices[1].One) * (vertices[3].Two - vertices[1].Two))
                            {
                                IsInHull[3] = false;
                            }
                            #endregion

                            #region Removing points that are in the hull of the subset of finite points but not in the hull due to infinite extreme points
                            int index = 0;
                            var value = Rational.For(0);

                            if (vertices[0].One.IsInfinity)
                            {
                                index = 2;
                                value = vertices[2].Two;
                                for (int n = 4; n < 8; n += 2)
                                {
                                    if (vertices[n].Two <= value)
                                    {
                                        for (int m = index; m < n; m += 2)
                                        {
                                            IsInHull[m] = false;
                                        }
                                        index = n;
                                        value = vertices[n].Two;
                                    }
                                }
                            }

                            if (vertices[6].One.IsInfinity)
                            {
                                index = 4;
                                value = vertices[4].Two;
                                for (int n = 2; n >= 0; n -= 2)
                                {
                                    if (vertices[n].Two <= value)
                                    {
                                        for (int m = index; m > n; m -= 2)
                                        {
                                            IsInHull[m] = false;
                                        }
                                        index = n;
                                        value = vertices[n].Two;
                                    }
                                }
                            }

                            if (vertices[1].One.IsInfinity)
                            {
                                index = 3;
                                value = vertices[3].Two;
                                for (int n = 5; n < 8; n += 2)
                                {
                                    if (vertices[n].Two >= value)
                                    {
                                        for (int m = index; m < n; m += 2)
                                        {
                                            IsInHull[m] = false;
                                        }
                                        index = n;
                                        value = vertices[n].Two;
                                    }
                                }
                            }

                            if (vertices[7].One.IsInfinity)
                            {
                                index = 5;
                                value = vertices[5].Two;
                                for (int n = 3; n >= 0; n -= 2)
                                {
                                    if (vertices[n].Two >= value)
                                    {
                                        for (int m = index; m > n; m -= 2)
                                        {
                                            IsInHull[m] = false;
                                        }
                                        index = n;
                                        value = vertices[n].Two;
                                    }
                                }
                            }
                            #endregion

                            #region Adding to the result the Polynomials for the edges that are in the hull and neither horizontal nor vertical
                            var      point = new PairNonNull <Rational, Rational>();
                            Rational c;
                            int      numberOfPointsInHull = 0;

                            for (int k = 0; k < 8; k += 2)
                            {
                                if (IsInHull[k])
                                {
                                    if (numberOfPointsInHull > 0 && point.One != vertices[k].One && point.Two != vertices[k].Two)
                                    {
                                        try
                                        {
                                            c = (vertices[k].One - point.One) / (point.Two - vertices[k].Two);
                                        }
                                        catch (ArithmeticExceptionRational)
                                        {
                                            continue;
                                        }

                                        var list = new Monomial <Variable>[] { new Monomial <Variable>(e1), new Monomial <Variable>(c, e2), new Monomial <Variable>(slack) };

                                        if (!Polynomial <Variable, Expression> .TryToPolynomialForm(true, list, out pol))
                                        {
                                            throw new AbstractInterpretationException("Impossible case");
                                        }
                                        result.Add(pol);
                                    }
                                    point = vertices[k];
                                    numberOfPointsInHull++;
                                }
                            }
                            for (int k = 1; k < 8; k += 2)
                            {
                                if (IsInHull[k])
                                {
                                    if (numberOfPointsInHull > 0 && point.One != vertices[k].One && point.Two != vertices[k].Two)
                                    {
                                        try
                                        {
                                            c = (vertices[k].One - point.One) / (point.Two - vertices[k].Two);
                                        }
                                        catch (ArithmeticExceptionRational)
                                        {
                                            continue;
                                        }

                                        var list = new Monomial <Variable>[] { new Monomial <Variable>(e1), new Monomial <Variable>(c, e2), new Monomial <Variable>(slack) };

                                        if (!Polynomial <Variable, Expression> .TryToPolynomialForm(true, list, out pol))
                                        {
                                            throw new AbstractInterpretationException("Impossible case");
                                        }

                                        result.Add(pol);
                                    }
                                    point = vertices[k];
                                    numberOfPointsInHull++;
                                }
                            }
                            #endregion
                        }
                        catch (ArithmeticExceptionRational)
                        {
                            // Ignore the constraint
                        }
                        #endregion
                    }
                }
            }
            return(result);
        }