Ejemplo n.º 1
0
        /// <summary>
        /// Find vector x that minimizes the function f(x) using the Nelder-Mead Simplex algorithm.
        /// For more options and diagnostics consider to use <see cref="NelderMeadSimplex"/> directly.
        /// </summary>
        public static (double P0, double P1, double P2, double P3) OfFunction(Func <double, double, double, double, double> function, double initialGuess0, double initialGuess1, double initialGuess2, double initialGuess3, double tolerance = 1e-8, int maxIterations = 1000)
        {
            var objective = ObjectiveFunction.Value(v => function(v[0], v[1], v[2], v[3]));
            var result    = NelderMeadSimplex.Minimum(objective, CreateVector.Dense(new[] { initialGuess0, initialGuess1, initialGuess2, initialGuess3 }), tolerance, maxIterations);

            return(result.MinimizingPoint[0], result.MinimizingPoint[1], result.MinimizingPoint[2], result.MinimizingPoint[3]);
        }
Ejemplo n.º 2
0
    public Neural()
    {
        nodes   = new Vector <float> [nodeSetup.Length];
        weights = new Matrix <float> [nodeSetup.Length - 1];
        biases  = new Vector <float> [nodeSetup.Length - 1];

        gammaValues  = new Vector <float> [nodeSetup.Length - 1];
        deltaWeights = new Matrix <float> [nodeSetup.Length - 1];
        deltaBiases  = new Vector <float> [nodeSetup.Length - 1];

        for (int i = 0; i < nodeSetup.Length; i++)
        {
            nodes[i] = CreateVector.Dense <float>(nodeSetup[i]);
        }

        for (int i = 0; i < nodeSetup.Length - 1; i++)
        {
            weights[i]      = CreateMatrix.Dense <float>(nodeSetup[i + 1], nodeSetup[i]);
            deltaWeights[i] = CreateMatrix.Dense <float>(nodeSetup[i + 1], nodeSetup[i]);

            biases[i]      = CreateVector.Dense <float>(nodeSetup[i + 1]);
            deltaBiases[i] = CreateVector.Dense <float>(nodeSetup[i + 1]);
            gammaValues[i] = CreateVector.Dense <float>(nodeSetup[i + 1]);
        }

        InitialiseWeights();
        InitialiseBiases();
    }
Ejemplo n.º 3
0
        /// <summary>
        /// Find vector x that minimizes the function f(x) using the Nelder-Mead Simplex algorithm.
        /// For more options and diagnostics consider to use <see cref="NelderMeadSimplex"/> directly.
        /// </summary>
        public static double OfScalarFunction(Func <double, double> function, double initialGuess, double tolerance = 1e-8, int maxIterations = 1000)
        {
            var objective = ObjectiveFunction.Value(v => function(v[0]));
            var result    = NelderMeadSimplex.Minimum(objective, CreateVector.Dense(new[] { initialGuess }), tolerance, maxIterations);

            return(result.MinimizingPoint[0]);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Objective model with a user supplied jacobian for non-linear least squares regression.
        /// </summary>
        public static IObjectiveModel NonlinearModel(Func <Vector <double>, double, double> function,
                                                     Func <Vector <double>, double, Vector <double> > derivatives,
                                                     Vector <double> observedX, Vector <double> observedY, Vector <double> weight = null)
        {
            Vector <double> func(Vector <double> point, Vector <double> x)
            {
                var functionValues = CreateVector.Dense <double>(x.Count);

                for (int i = 0; i < x.Count; i++)
                {
                    functionValues[i] = function(point, x[i]);
                }

                return(functionValues);
            }

            Matrix <double> prime(Vector <double> point, Vector <double> x)
            {
                var derivativeValues = CreateMatrix.Dense <double>(x.Count, point.Count);

                for (int i = 0; i < x.Count; i++)
                {
                    derivativeValues.SetRow(i, derivatives(point, x[i]));
                }

                return(derivativeValues);
            }

            var objective = new NonlinearObjectiveFunction(func, prime);

            objective.SetObserved(observedX, observedY, weight);
            return(objective);
        }
Ejemplo n.º 5
0
        public static Vector <double> GetDirection(Vector <double> a, Vector <double> b)
        {
            var ab     = (b - a);
            var ab_hat = ab / ab.L2Norm();

            return(CreateVector.Dense(new double[] { Math.Round(ab_hat[0], 2), Math.Round(ab_hat[1], 2) }));
        }
Ejemplo n.º 6
0
        Vector <double> ToRandomBytes()
        {
            Vector <double> vector  = CreateVector.Dense <double>(dimension * multiplier);
            int             rand    = random.Next(0, 1073741824);
            string          bytes   = Convert.ToString(rand, 2);
            int             starter = dimension * multiplier - bytes.Length;

            for (int i = 0; i < starter; i++)
            {
                vector[i] = -1;
            }
            for (int i = starter; i < dimension * multiplier; i++)
            {
                if (Convert.ToInt32(bytes[i - starter]) == 49)
                {
                    vector[i] = 1;
                }
                else if (Convert.ToInt32(bytes[i - starter]) == 48)
                {
                    vector[i] = -1;
                }
                else
                {
                    throw new Exception("Some error in converting to bytes occured");
                }
            }
            return(vector);
        }
Ejemplo n.º 7
0
 public PhysicBall()
 {
     Location      = CreateVector.Dense <float>(2);
     Speed         = CreateVector.Dense <float>(2);
     Acceleration  = DenseVector.OfArray(new float[] { 1, 1 });
     DeltaLocation = CreateVector.Dense <float>(2);
 }
Ejemplo n.º 8
0
        public override MathNet.Numerics.LinearAlgebra.Vector <double> Predict(MathNet.Numerics.LinearAlgebra.Vector <double> input)
        {
            //Ax where A is the input and x are the weights

            if (Degree > 1)
            {
                double[] row = new double[input.Count * Degree];

                for (int j = 0; j < input.Count; j++)
                {
                    row[j] = input[j];
                    for (int k = 1; k < Degree; k++)
                    {
                        row[k * input.Count + j] = Math.Pow(row[j], k + 1);
                    }
                }
                input = CreateVector.Dense <double>(row);
            }

            if (bias)
            {
                List <double> data = input.ToList();
                data.Insert(0, 1.0);
                input = CreateVector.Dense <double>(data.ToArray <double>());
            }
            Vector <double> output = CreateVector.Dense <double>(new double[] { input.DotProduct(Weights.Column(0)) });

            return(output);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Initilizes the multibody system. After calling this method, no changes to elements should occure.
        /// </summary>
        internal void InitilizeSystem()
        {
            this.TotalDegreesOfFreedom = 0;
            // Setting up all element ids and system reference as well as the state existance vector.
            var data = new List <double>();

            elementStateExistancesVectors = new Vector <double> [Elements.Count];
            foreach ((int id, Element element) in Elements.Select((x, i) => (i, x)))
            {
                element.ElementId                 = id;
                element.System                    = this;
                TotalDegreesOfFreedom            += element.BaseJoint.DegreesOfFreedom.Count;
                elementStateExistancesVectors[id] = CreateVector.Dense <double>(12, 0);
                foreach (var index in element.BaseJoint.DegreesOfFreedom.SelectMany(x => new int[] { (int)x, (int)x + 6 }))
                {
                    elementStateExistancesVectors[id][index] = 1;
                }
            }
            int offset = Elements.Count * 6;

            StateExistanceVector = CreateVector.Dense <double>(Elements.Count * 12, 0);
            Elements.SelectMany((element, index) =>
                                element.BaseJoint.DegreesOfFreedom.SelectMany(y => new int[] { (index + 1) * (int)y, (index + 1) * (int)y + offset }))
            .ToList().ForEach(x => StateExistanceVector[x] = 1);
        }
Ejemplo n.º 10
0
    public Neural(List <float> flatValues)
    {
        nodes   = new Vector <float> [nodeSetup.Length];
        weights = new Matrix <float> [nodeSetup.Length - 1];
        biases  = new Vector <float> [nodeSetup.Length - 1];

        gammaValues  = new Vector <float> [nodeSetup.Length - 1];
        deltaWeights = new Matrix <float> [nodeSetup.Length - 1];
        deltaBiases  = new Vector <float> [nodeSetup.Length - 1];

        for (int i = 0; i < nodeSetup.Length; i++)
        {
            nodes[i] = CreateVector.Dense <float>(nodeSetup[i]);
        }

        for (int i = 0; i < nodeSetup.Length - 1; i++)
        {
            weights[i]      = CreateMatrix.Dense <float>(nodeSetup[i + 1], nodeSetup[i]);
            deltaWeights[i] = CreateMatrix.Dense <float>(nodeSetup[i + 1], nodeSetup[i]);

            biases[i]      = CreateVector.Dense <float>(nodeSetup[i + 1]);
            deltaBiases[i] = CreateVector.Dense <float>(nodeSetup[i + 1]);
            gammaValues[i] = CreateVector.Dense <float>(nodeSetup[i + 1]);
        }

        LoadFromAllValues(flatValues);
    }
Ejemplo n.º 11
0
        public static Vector <double> GenerateComplementaryVector(int size, int[] mask)
        {
            var ret    = CreateVector.Dense <double>(size, 1.0);
            var matrix = ret.ToRowMatrix();

            matrix = (matrix - 1).PointwiseAbs();
            return(matrix.Row(0));
        }
Ejemplo n.º 12
0
        public static Vector <double> GenerateMaskVector(int size, int[] masks)
        {
            var ret    = CreateVector.Dense <double>(size, 1.0);
            var matrix = ret.ToRowMatrix();

            matrix.ClearColumns(masks);
            return(matrix.Row(0));
        }
Ejemplo n.º 13
0
        public override AA1_MLP.Entities.DataSet LoadData(string datasetLocation, int featureVectorLength, int outputLength = 1, int skip = 0, bool Normalize = false, bool standardize = false, int?numberOfExamples = null, bool reportOsutput = true, bool permute = false, int?seed = null)
        {
            string l;

            System.IO.StreamReader file   = new System.IO.StreamReader(datasetLocation);
            Matrix <double>        input  = CreateMatrix.Dense <double>(1, featureVectorLength, 0);
            Matrix <double>        output = CreateMatrix.Dense <double>(1, outputLength, 0);
            int i = 0;

            while ((l = file.ReadLine()) != null)
            {
                if (!string.IsNullOrWhiteSpace(l) && !l.StartsWith("#"))
                {
                    var line = l.Split(',');
                    if (i == 0)
                    {
                        input.SetRow(i, line.Skip(skip).Take(featureVectorLength).Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray());
                        if (reportOsutput)
                        {
                            output.SetRow(i, line.Skip(featureVectorLength + skip).Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray());
                        }
                    }
                    else
                    {
                        input = input.InsertRow(i, CreateVector.Dense(line.Skip(skip).Take(featureVectorLength).Select(s => double.Parse(s)).ToArray()));
                        if (reportOsutput)
                        {
                            output = output.InsertRow(i, CreateVector.Dense(line.Skip(featureVectorLength + skip).Select(s => double.Parse(s)).ToArray()));
                        }
                    }
                    i++;
                    if (numberOfExamples != null)
                    {
                        numberOfExamples--;
                        if (numberOfExamples == 0)
                        {
                            break;
                        }
                    }
                }
            }

            file.Close();
            // string[] lines = File.ReadAllLines(datasetLocation);
            //  lines = lines.Select(s => !string.IsNullOrWhiteSpace(s));



            // DataSet trainingSet = new DataSet(input.NormalizeColumns(2.0), output);
            DataSet trainingSet = new DataSet(Normalize ? input.NormalizeColumns(2.0) : input, output);

            if (standardize)
            {
                StandardizeData(trainingSet);
            }
            return(trainingSet);
        }
Ejemplo n.º 14
0
        public HopfieldNetwork(List <Vector <double> > patterns)
        {
            this.patterns = patterns;
            var N = patterns[0].Count;
            var K = CreateMatrix.DenseOfColumnVectors(patterns);

            this.W = (1.0 / N) * K.TransposeAndMultiply(K);
            this.W.SetDiagonal(CreateVector.Dense(N, 0.0));
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Updates all vectors and matrices of each element. Has to be run at every time step
 /// </summary>
 /// <param name="GlobalStateVector"></param>
 public void UpdateElements(StateVector GlobalStateVector)
 {
     foreach (Element i in this.Elements)
     {
         ///Vector<double> ls = GlobalStateVector.GetStateVectorForId(i.ElementId);
         Vector <double> ls = CreateVector.Dense <double>(6 * 2);
         i.Update(ls, this.KeepMatrix);
     }
 }
Ejemplo n.º 16
0
 public TargetODE(double x01, double x02, double Tmax, double[] TimeValues)
 {
     _x0         = CreateVector.Dense <double>(2);
     _x0[0]      = x01;
     _x0[1]      = x02;
     _tMax       = Tmax;
     _nSwitch    = TimeValues.Length - 1;
     _timeValues = TimeValues;
 }
Ejemplo n.º 17
0
        static Vector <double> CalculateFuncValue(Vector <double> x)
        {
            Vector <double> value = CreateVector.Dense <double>(x.Count);

            for (int i = 0; i < functions.Length; ++i)
            {
                value[i] = functions[i](x);
            }
            return(value);
        }
Ejemplo n.º 18
0
        public Point SolveRayScreenRodrigues(double[] rod, ScreenProperties properties)
        {
            double[,] rotMat;
            Core.Cv.Rodrigues(rod, out rotMat);

            var rotMatMat = CreateMatrix.DenseOfArray(rotMat);
            var tempVec   = CreateVector.Dense(new double[] { 0, 0, -1 }) * rotMatMat;

            return(SolveRayScreenVector(new Point3D(tempVec.ToArray()), properties));
        }
Ejemplo n.º 19
0
        // Data from https://www.mathworks.com/help/stats/examples/weighted-nonlinear-regression.html

        private Vector <double> PollutionModel(Vector <double> p, Vector <double> x)
        {
            var y = CreateVector.Dense <double>(x.Count);

            for (int i = 0; i < x.Count; i++)
            {
                y[i] = p[0] * (1.0 - Math.Exp(-p[1] * x[i]));
            }
            return(y);
        }
Ejemplo n.º 20
0
        // model: Rat43 (https://www.itl.nist.gov/div898/strd/nls/data/ratkowsky3.shtml)
        //       f(x; a, b, c, d) =  a / ((1 + exp(b - c * x))^(1 / d))
        // best fitted parameters:
        //       a = 6.9964151270E+02 +/- 1.6302297817E+01
        //       b = 5.2771253025E+00 +/- 2.0828735829E+00
        //       c = 7.5962938329E-01 +/- 1.9566123451E-01
        //       d = 1.2792483859E+00 +/- 6.8761936385E-01
        private Vector <double> Rat43Model(Vector <double> p, Vector <double> x)
        {
            var y = CreateVector.Dense <double>(x.Count);

            for (int i = 0; i < x.Count; i++)
            {
                y[i] = p[0] / Math.Pow(1.0 + Math.Exp(p[1] - p[2] * x[i]), 1.0 / p[3]);
            }
            return(y);
        }
Ejemplo n.º 21
0
        // model: Rosenbrock
        //       f(x; a, b) = (1 - a)^2 + 100*(b - a^2)^2
        // derivatives:
        //       df/da = 400*a^3 - 400*a*b + 2*a - 2
        //       df/db = 200*(b - a^2)
        // best fitted parameters:
        //       a = 1
        //       b = 1
        private Vector <double> RosenbrockModel(Vector <double> p, Vector <double> x)
        {
            var y = CreateVector.Dense <double>(x.Count);

            for (int i = 0; i < x.Count; i++)
            {
                y[i] = Math.Pow(1.0 - p[0], 2) + 100.0 * Math.Pow(p[1] - p[0] * p[0], 2);
            }
            return(y);
        }
Ejemplo n.º 22
0
        private static Vector <double> CalculateRatios(
            DatabaseFin individual,
            List <FeaturePointType> benchmarkFeatures,
            List <FeaturePointType> landmarkFeatures,
            List <IEnumerable <FeaturePointType> > ratioPermutations,
            bool useRemappedOutline = false)
        {
            var coordinates = new Dictionary <FeaturePointType, PointF>();

            foreach (var featurePoint in landmarkFeatures)
            {
                if (useRemappedOutline)
                {
                    coordinates[featurePoint] = individual.FinOutline.GetRemappedFeaturePointCoords(featurePoint);
                }
                else
                {
                    coordinates[featurePoint] = individual.FinOutline.GetFeaturePointCoords(featurePoint);
                }
            }

            var benchmarkDistance = MathHelper.GetDistance(
                coordinates[benchmarkFeatures[0]].X,
                coordinates[benchmarkFeatures[0]].Y,
                coordinates[benchmarkFeatures[1]].X,
                coordinates[benchmarkFeatures[1]].Y);

            Vector <double> ratios = CreateVector.Dense <double>(ratioPermutations.Count - 1);

            int i = 0;

            foreach (var permutation in ratioPermutations)
            {
                var permutationList = permutation.ToList();

                if ((permutationList[0] == benchmarkFeatures[0] && permutationList[1] == benchmarkFeatures[1]) ||
                    (permutationList[0] == benchmarkFeatures[1] && permutationList[1] == benchmarkFeatures[0]))
                {
                    // This is our benchmark
                    continue;
                }

                var currentDistance = MathHelper.GetDistance(
                    coordinates[permutationList[0]].X,
                    coordinates[permutationList[0]].Y,
                    coordinates[permutationList[1]].X,
                    coordinates[permutationList[1]].Y);

                ratios[i] = currentDistance / benchmarkDistance;

                i += 1;
            }

            return(ratios);
        }
        public Vector <double> T_round(Vector <double> vec, int v_init, double alpha, List <List <int> > active_edges)
        {
            var res = CreateVector.Dense <double>(n);

            const double eps = 1e-8;

            foreach (var edge in active_edges)
            {
                var    argmaxs = new List <int>();
                var    argmins = new List <int>();
                double maxval = double.MinValue, minval = double.MaxValue;
                foreach (var v in edge)
                {
                    var val = vec[v] / w_Degree(v);
                    if (val > maxval + eps)
                    {
                        maxval = val;
                        argmaxs.Clear();
                        argmaxs.Add(v);
                    }
                    else if (val > maxval - eps)
                    {
                        argmaxs.Add(v);
                    }

                    if (val < minval - eps)
                    {
                        minval = val;
                        argmins.Clear();
                        argmins.Add(v);
                    }
                    else if (val < minval + eps)
                    {
                        argmins.Add(v);
                    }
                }
                foreach (var v in argmaxs)
                {
                    res[v] += weights[ID[edge]] * (maxval - minval) / argmaxs.Count;
                }
                foreach (var v in argmins)
                {
                    res[v] -= weights[ID[edge]] * (maxval - minval) / argmins.Count;
                }
            }

            var res_init = CreateVector.Dense <double>(n);

            vec.CopyTo(res_init);
            res_init[v_init] -= 1;

            var mix = (1 - alpha) * res + alpha * res_init;

            return(mix);
        }
Ejemplo n.º 24
0
        public NonlinearMinimizationResult FindMinimum(IObjectiveModel objective, double[] initialGuess,
                                                       double[] lowerBound = null, double[] upperBound = null, double[] scales = null, bool[] isFixed = null)
        {
            var lb = (lowerBound == null) ? null : CreateVector.Dense <double>(lowerBound);
            var ub = (upperBound == null) ? null : CreateVector.Dense <double>(upperBound);
            var sc = (scales == null) ? null : CreateVector.Dense <double>(scales);
            var fx = (isFixed == null) ? null : isFixed.ToList();

            return(Minimum(Subproblem, objective, CreateVector.DenseOfArray <double>(initialGuess), lb, ub, sc, fx,
                           GradientTolerance, StepTolerance, FunctionTolerance, RadiusTolerance, MaximumIterations));
        }
Ejemplo n.º 25
0
    public InputLayer(int size, double[] input)
    {
        if (size != input.Length)
        {
            Debug.LogError("Layer: Input size and size parameter do not match");
        }

        this.size     = size;
        this.rawInput = input;
        this.input    = CreateVector.Dense <double>(input);
    }
Ejemplo n.º 26
0
        /// <summary>
        /// Calculates the local force vector of the element
        /// </summary>
        /// <returns>The local force vector</returns>
        private Vector <double> GetLocalForceMomentVector()
        {
            Vector <double> LocalForceMomentVector = CreateVector.Dense <double>(6);
            Vector <double> LocalForceVector       = CreateVector.Dense <double>(3);
            Vector <double> LocalMomentVector      = CreateVector.Dense <double>(3);

            LocalForceVector += System.GravitationVector * this.Mass;
            LocalForceMomentVector.InsertAtIndex(LocalForceVector, 0);
            LocalForceMomentVector.InsertAtIndex(LocalMomentVector, 3);
            return(LocalForceMomentVector);
        }
Ejemplo n.º 27
0
        /// this callback calculates Gaussian function f(c,x) = c0 + c1*EXP[-(x-c2)^2/2*c3^2]
        /// c0, c1, c2 and c3 are the background, amplitude, centre and width of the Gaussians.
        /// where x is a position on X-axis and c is adjustable parameter
        //public static void GaussianFunc(double[] c, double[] x, ref double func, object obj)
        //{
        //   double arg = (x[0] - c[2]) / c[3];
        //   double ex = System.Math.Exp(-arg * arg / 2);
        //   func = c[1] * ex + c[0];
        //}
        private static Vector <double> GaussianFunc(Vector <double> c, Vector <double> x)
        {
            var y = CreateVector.Dense <double>(x.Count);

            for (int i = 0; i < x.Count; i++)
            {
                double arg = (x[i] - c[2]) / c[3];
                double ex  = System.Math.Exp(-arg * arg / 2);
                y[i] = c[1] * ex + c[0];
            }
            return(y);
        }
Ejemplo n.º 28
0
        [InlineData(new[] { 1.0D, 2 }, new[] { 4.0D, 6 }, new[] { -4.0D, 3, -2 })] //any

        public void CreateTest(double[] a, double[] b, double[] expected_raw)
        {
            var A      = CreateVector.Dense(a);
            var B      = CreateVector.Dense(b);
            var expect = Line.Create(CreateVector.Dense(expected_raw));

            var result1 = Line.Create(A, B);
            var result2 = Line.Create(B, A);

            Assert.Equal(result1, result2, new Line.LineEqualityComparer());
            Assert.Equal(expect, result1, new Line.LineEqualityComparer());
        }
Ejemplo n.º 29
0
        public Vector <double> BlochVector()
        {
            double z = 2 * StateOperator[0, 0].Real - 1;
            double x = StateOperator[0, 1].Real + StateOperator[1, 0].Real;
            double y = StateOperator[0, 1].Imaginary - StateOperator[1, 0].Imaginary;

            Vector <double> bloch = CreateVector.Dense <double>(3);

            bloch[0] = x; bloch[1] = y; bloch[2] = z;

            return(bloch);
        }
Ejemplo n.º 30
0
        public static Tuple <Vector <double>, int> Newton(Vector <double> x, double eps)
        {
            Vector <double> y     = CreateVector.Dense <double>(x.Count);
            int             count = 0;

            do
            {
                y = x;
                x = x + LUDecomposition.SolveSLE(CalculateJacobiMatrixValue(x), -CalculateFuncValue(x));
                ++count;
            } while ((x - y).L2Norm() >= eps);
            return(Tuple.Create(x, count));
        }