Пример #1
0
        /// <summary>
        /// Compute the dot product of this vector and the given vector
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public double DotProduct(Vector v)
        {
            if (v.NDimensions != NDimensions)
                throw new ArgumentException("Can only compute dot product for vectors of the same dimensionality");

            double sum = 0;
            for (int i = 0; i < v.NDimensions; i++)
            {
                sum += this[i]*v[i];
            }
            return sum;
        }
Пример #2
0
        /// <summary>
        /// Add another vector to this one
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public Vector Add(Vector v)
        {
            if (v.NDimensions != NDimensions)
                throw new ArgumentException("Can only add vectors of the same dimensionality");

            var vector = new Vector(v.NDimensions);
            for (int i = 0; i < v.NDimensions; i++)
            {
                vector[i] = this[i] + v[i];
            }
            return vector;
        }
Пример #3
0
        /// <summary>
        /// Test a scaling operation of the high point, and replace it if it is an improvement
        /// </summary>
        /// <param name="scaleFactor"></param>
        /// <param name="errorProfile"></param>
        /// <param name="vertices"></param>
        /// <param name="errorValues"></param>
        /// <param name="objectiveFunction"></param>
        /// <returns></returns>
        private static double _tryToScaleSimplex(double scaleFactor, ref ErrorProfile errorProfile, Vector[] vertices,
                                                 double[] errorValues, ObjectiveFunctionDelegate objectiveFunction)
        {
            // find the centroid through which we will reflect
            var centroid = _computeCentroid(vertices, errorProfile);

            // define the vector from the centroid to the high point
            var centroidToHighPoint = vertices[errorProfile.HighestIndex].Subtract(centroid);

            // scale and position the vector to determine the new trial point
            var newPoint = centroidToHighPoint.Multiply(scaleFactor).Add(centroid);

            // evaluate the new point
            var newErrorValue = objectiveFunction(newPoint.Components);

            // if it's better, replace the old high point
            if (newErrorValue < errorValues[errorProfile.HighestIndex])
            {
                vertices[errorProfile.HighestIndex] = newPoint;
                errorValues[errorProfile.HighestIndex] = newErrorValue;
            }

            return newErrorValue;
        }
Пример #4
0
 /// <summary>
 /// Contract the simplex uniformly around the lowest point
 /// </summary>
 /// <param name="errorProfile"></param>
 /// <param name="vertices"></param>
 /// <param name="errorValues"></param>
 /// <param name="objectiveFunction"></param>
 private static void _shrinkSimplex(ErrorProfile errorProfile, Vector[] vertices, double[] errorValues,
                                    ObjectiveFunctionDelegate objectiveFunction)
 {
     var lowestVertex = vertices[errorProfile.LowestIndex];
     for (int i = 0; i < vertices.Length; i++)
     {
         if (i != errorProfile.LowestIndex)
         {
             vertices[i] = (vertices[i].Add(lowestVertex)).Multiply(0.5);
             errorValues[i] = objectiveFunction(vertices[i].Components);
         }
     }
 }
Пример #5
0
        /// <summary>
        /// Construct an initial simplex, given starting guesses for the constants, and
        /// initial step sizes for each dimension
        /// </summary>
        /// <param name="simplexConstants"></param>
        /// <returns></returns>
        private static Vector[] _initializeVertices(SimplexConstant[] simplexConstants)
        {
            int numDimensions = simplexConstants.Length;
            var vertices = new Vector[numDimensions + 1];

            // define one point of the simplex as the given initial guesses
            var p0 = new Vector(numDimensions);
            for (int i = 0; i < numDimensions; i++)
            {
                p0[i] = simplexConstants[i].Value;
            }

            // now fill in the vertices, creating the additional points as:
            // P(i) = P(0) + Scale(i) * UnitVector(i)
            vertices[0] = p0;
            for (int i = 0; i < numDimensions; i++)
            {
                double scale = simplexConstants[i].InitialPerturbationScale;
                var unitVector = new Vector(numDimensions);
                unitVector[i] = 1;
                vertices[i + 1] = p0.Add(unitVector.Multiply(scale));
            }
            return vertices;
        }
Пример #6
0
 /// <summary>
 /// Evaluate the objective function at each vertex to create a corresponding
 /// list of error values for each vertex
 /// </summary>
 /// <param name="vertices"></param>
 /// <param name="objectiveFunction"></param>
 /// <returns></returns>        
 private static double[] _initializeErrorValues(Vector[] vertices, ObjectiveFunctionDelegate objectiveFunction)
 {
     var errorValues = new double[vertices.Length];
     for (int i = 0; i < vertices.Length; i++)
     {
         errorValues[i] = objectiveFunction(vertices[i].Components);
     }
     return errorValues;
 }
Пример #7
0
 /// <summary>
 /// Compute the centroid of all points except the worst
 /// </summary>
 /// <param name="vertices"></param>
 /// <param name="errorProfile"></param>
 /// <returns></returns>
 private static Vector _computeCentroid(Vector[] vertices, ErrorProfile errorProfile)
 {
     int numVertices = vertices.Length;
     // find the centroid of all points except the worst one
     var centroid = new Vector(numVertices - 1);
     for (int i = 0; i < numVertices; i++)
     {
         if (i != errorProfile.HighestIndex)
         {
             centroid = centroid.Add(vertices[i]);
         }
     }
     return centroid.Multiply(1.0d/(numVertices - 1));
 }
Пример #8
0
 /// <summary>
 /// Multiply this vector by a scalar value
 /// </summary>
 /// <param name="scalar"></param>
 /// <returns></returns>
 public Vector Multiply(double scalar)
 {
     var scaledVector = new Vector(NDimensions);
     for (int i = 0; i < NDimensions; i++)
     {
         scaledVector[i] = this[i]*scalar;
     }
     return scaledVector;
 }