private double FunctionValueHalfTime(INonlinearModel model, int rowVid,
                                                 ValuesByIndex values, bool newValues)
            {
                foreach (Variable c in data.Variables)
                {
                    if (c.ConstName == "T1/2")
                    {
                        c.ConstValue = values[model.GetIndexFromKey(c.ConstName)];
                        break;
                    }
                }

                double R    = data.Variables[0].ConstValue;
                double w    = data.Variables[1].ConstValue;
                double Kon  = data.Variables[2].ConstValue;
                double Koff = data.Variables[3].ConstValue;
                double Df   = data.Variables[4].ConstValue;
                double Ceq  = data.Variables[5].ConstValue;
                double Feq  = 1 - Ceq;
                double Tht  = data.Variables[6].ConstValue;

                data.FitYVals = new double[data.XVals.Length];

                Laplace lap = new Laplace();

                lap.InitStehfest(14);

                return(Math.Abs((R)*lap.InverseTransform(R, w, Feq, Ceq, Kon, Koff, Df, Tht) - 0.5 * R));
            }
        private void Compute(ValuesByIndex values)
        {
            for (var index = 0; index < this.curVariableVals.Length; ++index)
            {
                this.curVariableVals[index] = values[this.variables[index]];
            }

            this.curVal = this.func(this.curVariableVals, this.curGradient);
        }
        private double FunctionEvaluator(INonlinearModel model, int rowVid, ValuesByIndex values, bool newValues)
        {
            if (newValues)
            {
                this.Compute(values);
            }

            return this.curVal;
        }
        private void Compute(ValuesByIndex values)
        {
            for (var index = 0; index < this.curVariableVals.Length; ++index)
            {
                this.curVariableVals[index] = values[this.variables[index]];
            }

            this.curVal = this.func(this.curVariableVals, this.curGradient);
        }
        private double FunctionEvaluator(INonlinearModel model, int rowVid, ValuesByIndex values, bool newValues)
        {
            if (newValues)
            {
                this.Compute(values);
            }

            return(this.curVal);
        }
Example #6
0
        private double FunctionValueHalfTime(INonlinearModel model, int rowVid,
                                             ValuesByIndex values, bool newValues)
        {
            e.Parameters["t"] = values[model.GetIndexFromKey("t")];

            double val = 0;

            double.TryParse(e.Evaluate().ToString(), out val);

            return(Math.Abs(half - val));
        }
            private double FunctionValue(INonlinearModel model, int rowVid,
                                         ValuesByIndex values, bool newValues)
            {
                foreach (Variable c in data.Variables)
                {
                    c.ConstValue = values[model.GetIndexFromKey(c.ConstName)];
                }

                double dev = FindDeviation(data);

                return(dev);
            }
        private void GradientEvaluator(INonlinearModel model, int rowVid, ValuesByIndex values, bool newValues, ValuesByIndex gradient)
        {
            if (newValues)
            {
                this.Compute(values);
            }

            for (var index = 0; index < this.curGradient.Length; ++index)
            {
                gradient[this.variables[index]] = this.curGradient[index];
            }
        }
        private double SinxValue(INonlinearModel model, int rowVid, ValuesByIndex values, bool newValues)
        {
            //_decay[0][0] = 1.000;

            // By CQN solver convention, first (and in this case only) variable always has the vid of 1

            double sum = 0.0;

            for (int i = _begin; i < _end; i++)
            {
                sum += Math.Pow
                       (
                    ((+1 * (_step * 2 / 1000.0) / values[1]) * Math.Exp(-1.0 * ((_time[i] - _begin0 + 0) / 1000.0) / (values[1] * values[2] / 1000.0)) * 1000.0 + (values[3]) - (_decay[i] * _factor)),
                    2
                       );
            }

            return(sum);
        }
Example #10
0
        private double DiffMapping(INonlinearModel model, int rowVid, ValuesByIndex v, bool newValues)
        {
            int a         = 1;
            var translate = new Vector3D(v[0 + a], v[1 + a], v[2 + a]);
            var rotate    = new Vector3D(v[3 + a], v[4 + a], v[5 + a]);
            var fov       = v[6 + a];
            var shift     = new Vector2D(v[7 + a], v[8 + a]);

            double sum = 0;
            int    i   = 0;

            foreach (var r in FReal)
            {
                var result = Project(r, translate, rotate, fov, shift) - FMapped[i];
                sum += result.x * result.x + result.y * result.y;
                i++;
            }
            return(sum);            // of least squares
        }
        /// <summary>
        /// Function value callback for the original Rosenbrock's function
        /// f(x, y) = (1 - x)^2 + 100(y - x^2)^2
        /// <param name="model">the model.</param>
        /// <param name="rowVid">the row index.</param>
        /// <param name="values">the variable values.</param>
        /// <param name="newValues">is first evaluator call with those variable values.</param>
        /// <returns>the row value</returns>
        private static double OriginalRosenbrockFunction(INonlinearModel model, int rowVid, ValuesByIndex values, bool newValues)
        {
            double value = Math.Pow(1 - values[1], 2) + 100 * (Math.Pow(values[2] - (values[1] * values[1]), 2));

            return(value);
        }
        private void GradientEvaluator(INonlinearModel model, int rowVid, ValuesByIndex values, bool newValues, ValuesByIndex gradient)
        {
            if (newValues)
            {
                this.Compute(values);
            }

            for (var index = 0; index < this.curGradient.Length; ++index)
            {
                gradient[this.variables[index]] = this.curGradient[index];
            }
        }
        /// <summary>
        /// Function value callback for first variant of Rosenbrock's function.
        /// This is the multidimensional variant of Ronsenbrock when n must be pair
        /// f(x) = sum(i, from 1 to N){ [alpha(x(2i) - x(2i-1)^2)^2] + [(1 - x(2i-1))^2] }
        /// <param name="model">the model.</param>
        /// <param name="rowVid">the row index.</param>
        /// <param name="values">the variable values.</param>
        /// <param name="newValues">is first evaluator call with those variable values.</param>
        /// <returns>the row value</returns>
        private static double FirstRosenbrockVariantFunction(INonlinearModel model, int rowVid, ValuesByIndex values, bool newValues)
        {
            const int firstVid   = 1;
            const int alpha      = 100;
            double    value      = 0;
            int       dimentions = model.VariableCount;

            if (dimentions < 2)
            {
                throw new ArgumentException("Multidimensional variant require at least two dimensions");
            }
            for (int i = firstVid; i <= dimentions / 2; i++)
            {
                value += alpha * (Math.Pow((values[2 * i] - values[2 * i - 1] * values[2 * i - 1]), 2)) + (Math.Pow(1 - values[2 * i - 1], 2));
            }
            return(value);
        }
        /// <summary>
        /// Gradient value callback for the first variant of Rosenbrock's function.
        /// This is the multidimensional variant of Ronsenbrock when n must be pair
        /// f(x) = sum(i, from 1 to N){ [alpha(x(2i) - x(2i-1)^2)^2] + [(1 - x(2i-1))^2] }
        /// </summary>
        /// <param name="model">the model.</param>
        /// <param name="rowVid">the row index.</param>
        /// <param name="values">the variable values.</param>
        /// <param name="newValues">is first evaluator call with those variable values.</param>
        /// <param name="gradient">the gradient values (set by the user).</param>
        private static void FirstRosenbrockVariantGradient(INonlinearModel model, int rowVid, ValuesByIndex values, bool newValues, ValuesByIndex gradient)
        {
            const int firstVid   = 1;
            const int alpha      = 100;
            int       dimentions = model.VariableCount;

            if (dimentions < 2)
            {
                throw new ArgumentException("Multidimensional variant require at least two dimensions");
            }
            for (int i = firstVid; i <= dimentions / 2; i++)
            {
                gradient[2 * i - 1] = (4 * alpha * values[2 * i - 1] * (values[2 * i - 1] * values[2 * i - 1] - values[2 * i]) + 2 * values[2 * i - 1] - 2);
                gradient[2 * i]     = (2 * alpha * -1 * (values[2 * i - 1] * values[2 * i - 1] - values[2 * i]));
            }
        }
        /// <summary>
        /// Function value callback for the second multidimensional variant of Rosenbrock's function
        /// f(x) = sum(i, from 1 to N-1){ [alpha(x(i+1) - x(i)^2)^2] + [(1 - x(i))^2] }
        /// <param name="model">the model.</param>
        /// <param name="rowVid">the row index.</param>
        /// <param name="values">the variable values.</param>
        /// <param name="newValues">is first evaluator call with those variable values.</param>
        /// <returns>the row value</returns>
        private static double SecondRosenbrockVariantFunction(INonlinearModel model, int rowVid, ValuesByIndex values, bool newValues)
        {
            const int firstVid   = 1;
            const int alpha      = 100;
            int       dimentions = model.VariableCount;

            if (dimentions < 2)
            {
                throw new ArgumentException("Multidimensional variant require at least two dimensions");
            }

            // first dimention special case
            double value = alpha * (Math.Pow((values[firstVid + 1] - values[firstVid] * values[firstVid]), 2)) +
                           (Math.Pow(1 - values[firstVid], 2));

            for (int i = firstVid + 1; i < dimentions; i++)
            {
                value += alpha * (Math.Pow((values[i + 1] - values[i] * values[i]), 2)) + (Math.Pow(1 - values[i], 2));
            }
            return(value);
        }
        /// <summary>
        /// Gradient value callback for second multidimensional variant of Rosenbrock's function
        /// f(x) = sum(i, from 1 to N-1){ [alpha(x(i+1) - x(i)^2)^2] + [(1 - x(i))^2] }
        /// </summary>
        /// <param name="model">the model.</param>
        /// <param name="rowVid">the row index.</param>
        /// <param name="values">the variable values.</param>
        /// <param name="newValues">is first evaluator call with those variable values.</param>
        /// <param name="gradient">the gradient values (set by the user).</param>
        private static void SecondRosenbrockVariantGradient(INonlinearModel model, int rowVid, ValuesByIndex values, bool newValues, ValuesByIndex gradient)
        {
            const int firstVid = 1;
            // common alpha is 100
            int alpha      = 100;
            int dimentions = model.VariableCount;

            if (dimentions < 2)
            {
                throw new ArgumentException("Multidimensional variant require at least two dimensions");
            }
            // first dimention special case
            gradient[firstVid] = 4 * alpha * values[firstVid] * (values[firstVid] * values[firstVid] - values[firstVid + 1]) +
                                 2 * (values[firstVid] - 1);
            for (int i = firstVid + 1; i < dimentions; i++)
            {
                gradient[i] = -2 * alpha * (values[i - 1] * values[i - 1] - values[i]) +
                              4 * alpha * values[i] * (values[i] * values[i] - values[i + 1]) +
                              2 * (values[i] - 1);
            }
            // last dimention special case
            gradient[dimentions] = -2 * alpha * (values[dimentions - 1] * values[dimentions - 1] - values[dimentions]);
        }
 /// <summary>
 /// Gradient value callback for the original Rosenbrock's function
 /// f(x, y) = (1 - x)^2 + 100(y - x^2)^2
 /// </summary>
 /// <param name="model">the model.</param>
 /// <param name="rowVid">the row index.</param>
 /// <param name="values">the variable values.</param>
 /// <param name="newValues">is first evaluator call with those variable values.</param>
 /// <param name="gradient">the gradient values (set by the user).</param>
 private static void OriginalRosenbrockGradient(INonlinearModel model, int rowVid, ValuesByIndex values, bool newValues, ValuesByIndex gradient)
 {
     gradient[1] = -2 * (1 - values[1]) - 400 * values[1] * (values[2] - (values[1] * values[1]));
     gradient[2] = 200 * (values[2] - (values[1] * values[1]));
 }