Example #1
0
        static public List <State> Split(List <State> states, int dim, int divs)
        {
            State[] result = new State[divs];

            foreach (State s in states)
            {
                List <DoubleInterval[]> sec = new List <DoubleInterval[]>(divs);
                double dimL = s.continuousState.axes[dim].left;
                double dimR = s.continuousState.axes[dim].right;
                var    next = new DoubleInterval[s.continuousState.axes.Length];
                for (int i = 0; i < s.continuousState.axes.Length; ++i)
                {
                    next[i] = s.continuousState.axes[i];
                }
                double prevR = dimL;
                for (int j = 1; j < divs; ++j)
                {
                    double R = (dimR - dimL) * j / divs + dimL;
                    next[dim] = new DoubleInterval(prevR, R);
                    // intermix the results for faster coverage
                    int index = (j % 2 == 1) ? (j / 2 + 1) : (divs - j / 2);
                    result[index] = new State(s.step, s.continuousNames, new DoubleBoundingBox(next), s.discreteNames, s.discreteState);
                    prevR         = R;
                }
                next[dim] = new DoubleInterval(prevR, dimR);
                result[0] = new State(s.step, s.continuousNames, new DoubleBoundingBox(next), s.discreteNames, s.discreteState);
            }
            List <State> list = new List <State>(divs);

            foreach (var s in result)
            {
                list.Add(s);
            }
            return(list);
        }
Example #2
0
        static public Polynomial Expansion(List<TaylorStructure> fs, string[] variables, Dictionary<string, DoubleInterval> x0, int maxOrder)
        {
            Polynomial polynomial = new Polynomial();
            int[] deg = new int[variables.Length];
            foreach (TaylorStructure e in fs)
            {
                DoubleInterval fval = e.func.Eval(x0) / e.coeff;
                // Constant: d^alphaF / alpha!
                Polynomial p = new Polynomial(new Monomial(fval, variables.Length));
                // Multiply by h^alpha = (x-a)^alpha (first var is time)
                for (int i = 1; i < variables.Length; ++i)
                {
                    DoubleInterval a = x0[variables[i]];
                    deg[i] = 1;
                    var mx = new Monomial(new DoubleInterval(1.0), deg);
                    var ma = new Monomial(a, deg.Length);
                    for (int j = 0; j < e.degrees[i]; ++j)
                        p *= (new Polynomial(mx) - new Polynomial(ma));
                    deg[i] = 0;
                }
                p.Cutoff();
                polynomial += p;
            }

            return polynomial;
        }
Example #3
0
        protected override void VerifiedSafeState(State q)
        {
            if (plotter == null || q.step > 0)
            {
                return;
            }
            if (!timeAxis && !searchIndexAxis)
            {
                return;
            }

            // Extract visualization data
            double top    = 0;
            double bottom = 1;
            double left   = 0;
            double right  = 1;
            Dictionary <string, DoubleInterval> currentValues = GetSystemState(q);

            if (timeAxis && searchIndexAxis)
            {
                DoubleInterval current = axisValue[1].Eval(currentValues);
                top    = (current.left - axisInitialValue[1].left) / axisInitialValue[1].width;
                bottom = (current.right - axisInitialValue[1].left) / axisInitialValue[1].width;
            }
            else if (timeAxis)
            {
                DoubleInterval currentY = axisValue[1].Eval(currentValues);
                DoubleInterval currentZ = axisValue[2].Eval(currentValues);
                if (axisInitialValue[1].width > 1E-6)
                {
                    top    = (currentY.left - axisInitialValue[1].left) / axisInitialValue[1].width;
                    bottom = (currentY.right - axisInitialValue[1].left) / axisInitialValue[1].width;
                }
                if (axisInitialValue[2].width > 1E-6)
                {
                    left  = (currentZ.left - axisInitialValue[2].left) / axisInitialValue[2].width;
                    right = (currentZ.right - axisInitialValue[2].left) / axisInitialValue[2].width;
                }
            }
            else if (searchIndexAxis)
            {
                DoubleInterval currentX = axisValue[0].Eval(currentValues);
                DoubleInterval currentY = axisValue[1].Eval(currentValues);
                if (axisInitialValue[0].width > 1E-6)
                {
                    top    = (currentX.left - axisInitialValue[0].left) / axisInitialValue[0].width;
                    bottom = (currentX.right - axisInitialValue[0].left) / axisInitialValue[0].width;
                }
                if (axisInitialValue[1].width > 1E-6)
                {
                    left  = (currentY.left - axisInitialValue[1].left) / axisInitialValue[1].width;
                    right = (currentY.right - axisInitialValue[1].left) / axisInitialValue[1].width;
                }
            }
            lock (plotter.plotDataLock)
            {
                verifiedRegions.Add(new RectangleF((float)left, (float)top, (float)(right - left), (float)(bottom - top)));
            }
        }
Example #4
0
        private double[] NormalizeAlongAxis(DoubleInterval value, int axis)
        {
            double[] result =
            {
                value.left, value.right
            };

            return(result);
        }
Example #5
0
 public override AST Simplify()
 {
     var A = children[0].Simplify();
     if (A.type == TYPE.REAL)
     {
         DoubleInterval value = ((REAL)A).value;
         return new REAL(value.Sqrt());
     }
     return new SQRT(A);
 }
Example #6
0
 public DoubleBoundingBox Eval()
 {
     IntPtr[] ival = new IntPtr[numVars];
     EvalFlowpipe(this.ptr, ival);
     DoubleInterval[] result = new DoubleInterval[numVars];
     for (int i = 0; i < numVars; ++i)
     {
         result[i] = new DoubleInterval(ival[i]);
     }
     return(new DoubleBoundingBox(result));
 }
Example #7
0
 public override DoubleInterval Eval(Dictionary<string,DoubleInterval> values)
 {
     DoubleInterval value = children[0].Eval(values);
     DoubleInterval result = new DoubleInterval(1.0);
     if (power > 0)
         for (int k = 0; k < power; ++k)
             result *= value;
     else
         for (int k = 0; k > power; --k)
             result /= value;
     return result;
 }
Example #8
0
 public override AST Simplify()
 {
     var A = children[0].Simplify();
     if (A.type == TYPE.REAL)
     {
         DoubleInterval value = ((REAL)A).value;
         if (value.left == 0 && value.right == 0)
             return A;
         return new REAL(-value);
     }
     return new NEG(A);
 }
Example #9
0
        protected FPIntegerInterval Measure(DoubleInterval d, uint bits, uint decimals)
        {
            double lower = d.left;
            double upper = d.right;

            for (int i = 0; i < decimals; ++i)
            {
                lower *= 2;
                upper *= 2;
            }
            return(new FPIntegerInterval((int)lower, (int)(upper + 1), bits, decimals));
        }
Example #10
0
        public Flowpipe(State state)
        {
            this.numVars = state.continuousNames.Length;

            DoubleInterval[] domain     = new DoubleInterval[numVars];
            IntPtr[]         domain_ptr = new IntPtr[numVars];
            for (int i = 0; i < numVars; ++i)
            {
                domain[i]     = state.continuousState.axes[i].Clone();
                domain_ptr[i] = domain[i].ptr;
            }

            ptr = CreateFlowpipe(numVars, domain_ptr, new DoubleInterval(0.0).ptr);
        }
Example #11
0
 public override AST GetDerivative(string dvar)
 {
     var A = children[0].GetDerivative(dvar).Simplify();
     var B = children[1].GetDerivative(dvar).Simplify();
     if (B.type == TYPE.REAL)
     {
         DoubleInterval value = ((REAL)B).value;
         if (value.left == 0 && value.right == 0)
             return new DIV(A, children[1]);
     }
     return
         new DIV(
             new SUB(
                 new MUL(A, children[1]),
                 new MUL(children[0], B)),
             new MUL(children[1], children[1]));
 }
Example #12
0
 public override AST Simplify()
 {
     var A = children[0].Simplify();
     var B = children[1].Simplify();
     if (A.type == TYPE.REAL)
     {
         DoubleInterval value = ((REAL)A).value;
         if (value.left == 0 && value.right == 0)
             return A;
     }
     if (B.type == TYPE.REAL)
     {
         DoubleInterval value = ((REAL)B).value;
         if (value.left == 1 && value.right == 1)
             return A;
     }
     return new DIV(A, B);
 }
Example #13
0
        public override AST Simplify()
        {
            if (power == 0)
                return new REAL(1);

            var A = children[0].Simplify();
            if (power == 1)
                return A;
            if (A.type == TYPE.REAL)
            {
                DoubleInterval value = ((REAL)A).value;
                DoubleInterval v = value;
                for (int i = 1; i < power; ++i)
                    v = value * v;
                return new REAL(v);
            }
            return new POW(A, power);
        }
Example #14
0
 /// <summary>
 /// Returns true if this interval is subset of A
 /// </summary>
 /// <param name="A"></param>
 /// <returns></returns>
 public bool Subseteq(DoubleInterval A)
 {
     return(SubseteqInterval(this.ptr, A.ptr));
 }
Example #15
0
        private List <VizData> GetStateRepresentation()
        {
            List <VizData> result    = new List <VizData>();
            bool           allUnsafe = true;

            for (int j = 0; j < plotData.Count; ++j)
            {
                if (plotData[j].type != PlotData.PlotType.UNSAFE)
                {
                    allUnsafe = false;
                    break;
                }
            }
            for (int j = 0; j < plotData.Count; ++j)
            {
                PlotData data = plotData[j];
                if (data.q == null)
                {
                    continue;
                }
                Dictionary <string, DoubleInterval> varValues = GetSystemState(data.q);

                if (timeAxis)
                {
                    PlotData datanext = j < plotData.Count - 1 ? plotData[j + 1] : null;
                    if (datanext == null || datanext.q == null || data.searchIndex != datanext.searchIndex)
                    {
                        continue;
                    }
                    Dictionary <string, DoubleInterval> varValuesNext = datanext != null?GetSystemState(datanext.q) : null;

                    // X is time
                    double time     = NormalizeAlongAxis(data.q.step, 0);
                    double timenext = NormalizeAlongAxis(datanext.q.step, 0);
                    if (searchIndexAxis)
                    {
                        // Z is search index
                        double index = NormalizeAlongAxis(data.searchIndex, 2) * searchZDepth;

                        DoubleInterval value     = axisValue[1].Eval(varValues);
                        DoubleInterval valueNext = axisValue[1].Eval(varValuesNext);

                        double[] nvalue     = NormalizeAlongAxis(value, 1);
                        double[] nvalueNext = NormalizeAlongAxis(valueNext, 1);
                        VizData  viz        = new VizData();
                        viz.data    = data;
                        viz.quad[0] = new Vector3d(time, nvalue[1], index);
                        viz.quad[1] = new Vector3d(time, nvalue[0], index);
                        viz.quad[2] = new Vector3d(timenext, nvalueNext[0], index);
                        viz.quad[3] = new Vector3d(timenext, nvalueNext[1], index);
                        result.Add(viz);
                    }
                    else
                    {
                        if (data.type == PlotData.PlotType.UNSAFE && !allUnsafe)
                        {
                            continue;
                        }
                        // Y-Z are variables
                        DoubleInterval valueY = axisValue[1].Eval(varValues);
                        DoubleInterval valueZ = axisValue[2].Eval(varValues);

                        DoubleInterval valueYnext = axisValue[1].Eval(varValuesNext);
                        DoubleInterval valueZnext = axisValue[2].Eval(varValuesNext);

                        double[] nvalueY     = NormalizeAlongAxis(valueY, 1);
                        double[] nvalueZ     = NormalizeAlongAxis(valueZ, 2);
                        double[] nvalueYnext = NormalizeAlongAxis(valueYnext, 1);
                        double[] nvalueZnext = NormalizeAlongAxis(valueZnext, 2);

                        VizData viz = new VizData();
                        viz.data    = data;
                        viz.quad[0] = new Vector3d(time, nvalueY[1], nvalueZ[1]);
                        viz.quad[1] = new Vector3d(time, nvalueY[0], nvalueZ[1]);
                        viz.quad[2] = new Vector3d(timenext, nvalueYnext[0], nvalueZnext[1]);
                        viz.quad[3] = new Vector3d(timenext, nvalueYnext[1], nvalueZnext[1]);
                        result.Add(viz);

                        viz         = new VizData();
                        viz.data    = data;
                        viz.quad[0] = new Vector3d(time, nvalueY[1], nvalueZ[0]);
                        viz.quad[1] = new Vector3d(time, nvalueY[0], nvalueZ[0]);
                        viz.quad[2] = new Vector3d(timenext, nvalueYnext[0], nvalueZnext[0]);
                        viz.quad[3] = new Vector3d(timenext, nvalueYnext[1], nvalueZnext[0]);
                        result.Add(viz);

                        viz         = new VizData();
                        viz.data    = data;
                        viz.quad[0] = new Vector3d(time, nvalueY[1], nvalueZ[0]);
                        viz.quad[1] = new Vector3d(time, nvalueY[1], nvalueZ[1]);
                        viz.quad[2] = new Vector3d(timenext, nvalueYnext[1], nvalueZnext[1]);
                        viz.quad[3] = new Vector3d(timenext, nvalueYnext[1], nvalueZnext[0]);
                        result.Add(viz);

                        viz         = new VizData();
                        viz.data    = data;
                        viz.quad[0] = new Vector3d(time, nvalueY[0], nvalueZ[0]);
                        viz.quad[1] = new Vector3d(time, nvalueY[0], nvalueZ[1]);
                        viz.quad[2] = new Vector3d(timenext, nvalueYnext[0], nvalueZnext[1]);
                        viz.quad[3] = new Vector3d(timenext, nvalueYnext[0], nvalueZnext[0]);
                        result.Add(viz);
                    }
                }
                else
                {
                    if (searchIndexAxis)
                    {
                        // X-Y are variables
                        double         index  = NormalizeAlongAxis(data.searchIndex, 2) * searchZDepth;
                        DoubleInterval valueX = axisValue[0].Eval(varValues);
                        DoubleInterval valueY = axisValue[1].Eval(varValues);

                        double[] nvalueX = NormalizeAlongAxis(valueX, 0);
                        double[] nvalueY = NormalizeAlongAxis(valueY, 1);

                        VizData viz = new VizData();
                        viz.data    = data;
                        viz.quad[0] = new Vector3d(nvalueX[1], nvalueY[0], index);
                        viz.quad[1] = new Vector3d(nvalueX[0], nvalueY[0], index);
                        viz.quad[2] = new Vector3d(nvalueX[0], nvalueY[1], index);
                        viz.quad[3] = new Vector3d(nvalueX[1], nvalueY[1], index);
                        result.Add(viz);
                    }
                }
            }

            return(result);
        }
Example #16
0
        public FormulaSystem(string filename, Plot3d plotter)
            : base()
        {
            axisValue[0] = new REAL(0.0);
            axisValue[1] = new REAL(0.0);
            axisValue[2] = new REAL(0.0);
            Load(filename, "M");

            string[]            cNames   = new string[cVariables.Count];
            string[]            dNames   = new string[dVariables.Count];
            DoubleInterval[]    cInitial = new DoubleInterval[cVariables.Count];
            FPIntegerInterval[] dInitial = new FPIntegerInterval[dVariables.Count];
            int i = 0;

            foreach (var kvp in cVariables)
            {
                cNames[i]   = kvp.Key.name;
                cInitial[i] = kvp.Value.Clone();
                i++;
            }
            i = 0;
            foreach (var kvp in dVariables)
            {
                dNames[i]   = kvp.Key.Expr;
                dInitial[i] = kvp.Value.Clone();
                i++;
            }

            // build the ODEs
            ode = new List <AST>();
            foreach (var kvp in cVariables)
            {
                AST f;
                if (odes.TryGetValue(kvp.Key.name, out f))
                {
                    ode.Add(f);
                }
            }

            Print(Log.Output);
            Print(Log.Debug);

            Initialize(cNames, cInitial, dNames, dInitial, order, period);

            if (IsPolynomial)
            {
                Log.WriteLine("Using polynomial solver");
            }
            else if (ContainsSqrt)
            {
                Log.WriteLine("Using approximate Taylor expansion solver");
            }
            else
            {
                Log.WriteLine("Using non-polynomial Taylor model solver");
            }

            // look for lower and upper bounds
            foreach (var kvp in dVariables)
            {
                FixedPointNumber fp = kvp.Key;
                int lo     = -(1 << ((int)fp.bits - 1));
                int up     = (1 << ((int)fp.bits - 1)) - 1;
                var solver = ctx.MkSimpleSolver();
                AddController(ctx, solver, kvp.Key.Expr);
                lo = LowerBound(ctx, solver, fp.Expr, fp.bits, lo, up);
                up = UpperBound(ctx, solver, fp.Expr, fp.bits, lo, up);
                var ival = new FPIntegerInterval(lo, up, fp.bits, fp.decimals);
                controlBounds.Add(fp.Expr, ival);
                Log.Debug.WriteLine("Control variable '{0}' always in {1}", kvp.Key, ival);
            }

            Dictionary <string, DoubleInterval> initialValues = GetSystemState(initialState); // initialState.ToDictionary();

            if (!timeAxis)
            {
                axisInitialValue[0] = axisValue[0].Eval(initialValues);
            }
            axisInitialValue[1] = axisValue[1].Eval(initialValues);
            if (!searchIndexAxis)
            {
                axisInitialValue[2] = axisValue[2].Eval(initialValues);
            }

            this.plotter = plotter;
            if (plotter != null)
            {
                plotter.SetMinMax(axisMin[0], axisMax[0], axisMin[1], axisMax[1], axisMin[2], axisMax[2]);
                plotter.DefaultSettings();
                plotter.DrawEvent += Draw;
            }
        }
Example #17
0
 public Monomial(DoubleInterval I, int[] degs)
 {
     ptr = CreateMonomial(I.ptr, degs.Length, degs);
 }
Example #18
0
 public Monomial(DoubleInterval I, int numVars)
 {
     ptr = CreateConstantMonomial(I.ptr, numVars);
 }
Example #19
0
 public REAL(double value)
 {
     this.type = TYPE.REAL;
     this.value = new DoubleInterval(value);
 }
Example #20
0
 public TaylorModel(Polynomial polynomial, DoubleInterval I)
 {
     ptr = CreateTaylorModel(polynomial.ptr, I.ptr);
 }
Example #21
0
 public REAL(DoubleInterval value)
 {
     this.value = value;
 }