/**
         * Computes the dot product of each basis vector against the sample.  Can be used as a measure
         * for membership in the training sample set.  High values correspond to a better fit.
         *
         * @param sample Sample of original data.
         * @return Higher value indicates it is more likely to be a member of input dataset.
         */
        public double response(double[] sample)
        {
            if (sample.Length != A.numCols)
            {
                throw new ArgumentException("Expected input vector to be in sample space");
            }

            DMatrixRMaj dots = new DMatrixRMaj(numComponents, 1);
            DMatrixRMaj s    = DMatrixRMaj.wrap(A.numCols, 1, sample);

            CommonOps_DDRM.mult(V_t, s, dots);

            return(NormOps_DDRM.normF(dots));
        }
        /**
         * Converts a vector from sample space into eigen space.
         *
         * @param sampleData Sample space data.
         * @return Eigen space projection.
         */
        public double[] sampleToEigenSpace(double[] sampleData)
        {
            if (sampleData.Length != A.getNumCols())
            {
                throw new ArgumentException("Unexpected sample length");
            }
            DMatrixRMaj mean = DMatrixRMaj.wrap(A.getNumCols(), 1, this.mean);

            DMatrixRMaj s = new DMatrixRMaj(A.getNumCols(), 1, true, sampleData);
            DMatrixRMaj r = new DMatrixRMaj(numComponents, 1);

            CommonOps_DDRM.subtract(s, mean, s);

            CommonOps_DDRM.mult(V_t, s, r);

            return(r.data);
        }
        /**
         * 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);
        }
Exemplo n.º 4
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);
            }
        }