public virtual void minus(DMatrixRMaj A, /**/ double b, DMatrixRMaj output)
        {
            CommonOps_DDRM.subtract(A, (double)b, output);
        }

        public virtual void plus(DMatrixRMaj A, /**/ double b, DMatrixRMaj output)
        {
            CommonOps_DDRM.add(A, (double)b, output);
        }
Beispiel #2
0
        /// <summary>
        /// Returns the result of scalar addition:
        /// <code>c = a + b</code>
        /// where c is the return matrix, a is this matrix, and b is the passed in double.
        /// </summary>
        /// <see cref="CommonOps_DDRM.add(DMatrixD1, double, DMatrixD1)"/>
        public override SimpleMatrixD plus(double b)
        {
            SimpleMatrixD ret = createMatrix(numRows(), numCols());

            var m  = getMatrix();
            var rm = ret.getMatrix();

            CommonOps_DDRM.add(m, b, rm);

            return(ret);
        }
Beispiel #3
0
        /**
         * <p>
         * Creates a randomly generated set of orthonormal vectors.  At most it can generate the same
         * number of vectors as the dimension of the vectors.
         * </p>
         *
         * <p>
         * This is done by creating random vectors then ensuring that they are orthogonal
         * to all the ones previously created with reflectors.
         * </p>
         *
         * <p>
         * NOTE: This employs a brute force O(N<sup>3</sup>) algorithm.
         * </p>
         *
         * @param dimen dimension of the space which the vectors will span.
         * @param numVectors How many vectors it should generate.
         * @param rand Used to create random vectors.
         * @return Array of N random orthogonal vectors of unit length.
         */
        // is there a faster algorithm out there? This one is a bit sluggish
        public static DMatrixRMaj[] span(int dimen, int numVectors, IMersenneTwister rand)
        {
            if (dimen < numVectors)
            {
                throw new ArgumentException("The number of vectors must be less than or equal to the dimension");
            }

            DMatrixRMaj[] u = new DMatrixRMaj[numVectors];

            u[0] = RandomMatrices_DDRM.rectangle(dimen, 1, -1, 1, rand);
            NormOps_DDRM.normalizeF(u[0]);

            for (int i = 1; i < numVectors; i++)
            {
//            Console.WriteLine(" i = "+i);
                DMatrixRMaj a = new DMatrixRMaj(dimen, 1);
                DMatrixRMaj r = null;

                for (int j = 0; j < i; j++)
                {
//                Console.WriteLine("j = "+j);
                    if (j == 0)
                    {
                        r = RandomMatrices_DDRM.rectangle(dimen, 1, -1, 1, rand);
                    }

                    // find a vector that is normal to vector j
                    // u[i] = (1/2)*(r + Q[j]*r)
                    a.set(r);
                    VectorVectorMult_DDRM.householder(-2.0, u[j], r, a);
                    CommonOps_DDRM.add(r, a, a);
                    CommonOps_DDRM.scale(0.5, a);

//                UtilEjml.print(a);

                    DMatrixRMaj t = a;
                    a = r;
                    r = t;

                    // normalize it so it doesn't get too small
                    double val = NormOps_DDRM.normF(r);
                    if (val == 0 || double.IsNaN(val) || double.IsInfinity(val))
                    {
                        throw new InvalidOperationException("Failed sanity check");
                    }
                    CommonOps_DDRM.divide(r, val);
                }

                u[i] = r;
            }

            return(u);
        }
Beispiel #4
0
        /**
         * Creates aJava.Util.Random vector that is inside the specified span.
         *
         * @param span The span theJava.Util.Random vector belongs in.
         * @param rand RNG
         * @return AJava.Util.Random vector within the specified span.
         */
        public static DMatrixRMaj insideSpan(DMatrixRMaj[] span, double min, double max, Java.Util.Random rand)
        {
            DMatrixRMaj A = new DMatrixRMaj(span.Count(), 1);

            DMatrixRMaj B = new DMatrixRMaj(span[0].NumElements, 1);

            for (int i = 0; i < span.Count(); i++)
            {
                B.setTo(span[i]);
                double val = rand.NextDouble() * (max - min) + min;
                CommonOps_DDRM.scale(val, B);

                CommonOps_DDRM.add(A, B, A);
            }

            return(A);
        }
Beispiel #5
0
        /**
         * Creates a random vector that is inside the specified span.
         *
         * @param span The span the random vector belongs in.
         * @param rand RNG
         * @return A random vector within the specified span.
         */
        public static DMatrixRMaj insideSpan(DMatrixRMaj[] span, double min, double max, IMersenneTwister rand)
        {
            DMatrixRMaj A = new DMatrixRMaj(span.Length, 1);

            DMatrixRMaj B = new DMatrixRMaj(span[0].getNumElements(), 1);

            for (int i = 0; i < span.Length; i++)
            {
                B.set(span[i]);
                double val = rand.NextDouble() * (max - min) + min;
                CommonOps_DDRM.scale(val, B);

                CommonOps_DDRM.add(A, B, A);
            }

            return(A);
        }
        /**
         * Converts a vector from eigen space into sample space.
         *
         * @param eigenData Eigen space data.
         * @return Sample space projection.
         */
        public double[] eigenToSampleSpace(double[] eigenData)
        {
            if (eigenData.Length != numComponents)
            {
                throw new ArgumentException("Unexpected sample length");
            }

            DMatrixRMaj s = new DMatrixRMaj(A.getNumCols(), 1);
            DMatrixRMaj r = DMatrixRMaj.wrap(numComponents, 1, eigenData);

            CommonOps_DDRM.multTransA(V_t, r, s);

            DMatrixRMaj mean = DMatrixRMaj.wrap(A.getNumCols(), 1, this.mean);

            CommonOps_DDRM.add(s, mean, s);

            return(s.data);
        }
        /**
         * Computes a simple numerical Jacobian.
         *
         * @param param The set of parameters that the Jacobian is to be computed at.
         * @param pt The point around which the Jacobian is to be computed.
         * @param deriv Where the jacobian will be stored
         */
        public void computeNumericalJacobian(DMatrixRMaj param,
                                             DMatrixRMaj pt,
                                             DMatrixRMaj deriv)
        {
            double invDelta = 1.0 / DELTA;

            func.compute(param, pt, temp0);

            // compute the jacobian by perturbing the parameters slightly
            // then seeing how it effects the results.
            for (int i = 0; i < param.numRows; i++)
            {
                param.data[i] += DELTA;
                func.compute(param, pt, temp1);
                // compute the difference between the two parameters and divide by the delta
                CommonOps_DDRM.add(invDelta, temp1, -invDelta, temp0, temp1);
                // copy the results into the jacobian matrix
                Array.Copy(temp1.data, 0, deriv.data, i * pt.numRows, pt.numRows);

                param.data[i] -= DELTA;
            }
        }
 public virtual void plus(DMatrixRMaj A, DMatrixRMaj B, DMatrixRMaj output)
 {
     CommonOps_DDRM.add(A, B, output);
 }
Beispiel #9
0
        public override Individual NewIndividual(IEvolutionState state, int thread)
        {
            Individual       newind = base.NewIndividual(state, thread);
            IMersenneTwister random = state.Random[thread];

            if (!(newind is DoubleVectorIndividual)) // uh oh
            {
                state.Output.Fatal(
                    "To use CMAESSpecies, the species must be initialized with a DoubleVectorIndividual.  But it contains a " +
                    newind);
            }

            DoubleVectorIndividual dvind = (DoubleVectorIndividual)(newind);

            DMatrixRMaj genome = DMatrixRMaj.wrap(GenomeSize, 1, dvind.genome);
            DMatrixRMaj temp   = new DMatrixRMaj(GenomeSize, 1);

            // arz(:,k) = randn(N,1); % standard normally distributed vector
            // arx(:,k) = xmean + sigma*(B*D*arz(:,k));
            int tries = 0;

            while (true)
            {
                for (int i = 0; i < GenomeSize; i++)
                {
                    dvind.genome[i] = random.NextGaussian();
                }

                CommonOps_DDRM.mult(sbd, genome, temp);              // temp = sigma*b*d*genome;
                CommonOps_DDRM.add(temp, xmean.getMatrix(), genome); // genome = temp + xmean;

                bool invalid_value = false;
                for (int i = 0; i < GenomeSize; i++)
                {
                    if (dvind.genome[i] < MinGenes[i] || dvind.genome[i] > MaxGenes[i])
                    {
                        if (useAltGenerator && tries > altGeneratorTries)
                        {
                            // instead of just failing, we're going to select uniformly from
                            // possible values for this particular gene.
                            dvind.genome[i] = state.Random[thread].NextDouble() * (MaxGenes[i] - MinGenes[i]) +
                                              MinGenes[i];
                        }
                        else
                        {
                            invalid_value = true;
                            break;
                        }
                    }
                }

                if (invalid_value)
                {
                    if (++tries > MAX_TRIES_BEFORE_WARNING)
                    {
                        state.Output.WarnOnce(
                            "CMA-ES may be slow because many individuals are being generated which\n" +
                            "are outside the min/max gene bounds.  If an individual violates a single\n" +
                            "gene bounds, it is rejected, so as the number of genes grows, the\n" +
                            "probability of this happens increases exponentially.  You can deal\n" +
                            "with this by decreasing sigma.  Alternatively you can use set\n" +
                            "pop.subpop.0.alternative-generation=true (see the manual).\n" +
                            "Finally, if this is happening during initialization, you might also\n" +
                            "change pop.subpop.0.species.covariance=scaled.\n");
                    }
                    continue;
                }

                return(newind);
            }
        }
Beispiel #10
0
 public void plus(double alpha, Matrix A, double beta, Matrix b, Matrix output)
 {
     CommonOps_DDRM.add((double)alpha, (DMatrixRMaj)A, (double)beta, (DMatrixRMaj)b, (DMatrixRMaj)output);
 }
Beispiel #11
0
 public void plus(Matrix A, double b, Matrix output)
 {
     CommonOps_DDRM.add((DMatrixRMaj)A, (double)b, (DMatrixRMaj)output);
 }
Beispiel #12
0
 public void plus(Matrix A, Matrix B, Matrix output)
 {
     CommonOps_DDRM.add((DMatrixRMaj)A, (DMatrixRMaj)B, (DMatrixRMaj)output);
 }
        public static void main(String[] args)
        {
            IMersenneTwister rand = new MersenneTwisterFast(234);

            // easy to work with sparse format, but hard to do computations with
            DMatrixSparseTriplet work = new DMatrixSparseTriplet(5, 4, 5);

            work.addItem(0, 1, 1.2);
            work.addItem(3, 0, 3);
            work.addItem(1, 1, 22.21234);
            work.addItem(2, 3, 6);

            // convert into a format that's easier to perform math with
            DMatrixSparseCSC Z = ConvertDMatrixStruct.convert(work, (DMatrixSparseCSC)null);

            // print the matrix to standard out in two different formats
            Z.print();
            Console.WriteLine();
            Z.printNonZero();
            Console.WriteLine();

            // Create a large matrix that is 5% filled
            DMatrixSparseCSC A = RandomMatrices_DSCC.rectangle(ROWS, COLS, (int)(ROWS * COLS * 0.05), rand);
            //          large vector that is 70% filled
            DMatrixSparseCSC x = RandomMatrices_DSCC.rectangle(COLS, XCOLS, (int)(XCOLS * COLS * 0.7), rand);

            Console.WriteLine("Done generating random matrices");
            // storage for the initial solution
            DMatrixSparseCSC y = new DMatrixSparseCSC(ROWS, XCOLS, 0);
            DMatrixSparseCSC z = new DMatrixSparseCSC(ROWS, XCOLS, 0);

            // To demonstration how to perform sparse math let's multiply:
            //                  y=A*x
            // Optional storage is set to null so that it will declare it internally
            long       before = DateTimeHelper.CurrentTimeMilliseconds;
            IGrowArray workA  = new IGrowArray(A.numRows);
            DGrowArray workB  = new DGrowArray(A.numRows);

            for (int i = 0; i < 100; i++)
            {
                CommonOps_DSCC.mult(A, x, y, workA, workB);
                CommonOps_DSCC.add(1.5, y, 0.75, y, z, workA, workB);
            }
            long after = DateTimeHelper.CurrentTimeMilliseconds;

            Console.WriteLine("norm = " + NormOps_DSCC.fastNormF(y) + "  sparse time = " + (after - before) + " ms");

            DMatrixRMaj Ad = ConvertDMatrixStruct.convert(A, (DMatrixRMaj)null);
            DMatrixRMaj xd = ConvertDMatrixStruct.convert(x, (DMatrixRMaj)null);
            DMatrixRMaj yd = new DMatrixRMaj(y.numRows, y.numCols);
            DMatrixRMaj zd = new DMatrixRMaj(y.numRows, y.numCols);

            before = DateTimeHelper.CurrentTimeMilliseconds;
            for (int i = 0; i < 100; i++)
            {
                CommonOps_DDRM.mult(Ad, xd, yd);
                CommonOps_DDRM.add(1.5, yd, 0.75, yd, zd);
            }
            after = DateTimeHelper.CurrentTimeMilliseconds;
            Console.WriteLine("norm = " + NormOps_DDRM.fastNormF(yd) + "  dense time  = " + (after - before) + " ms");
        }