Esempio n. 1
0
        public void TestVectorOperations()
        {
            VectorOperations.Sub(_a, _b, ref _dest);
            Assert.AreEqual(-2.0, _dest[0], FloatComparison.DefaultEps);
            Assert.AreEqual(-2.0, _dest[1], FloatComparison.DefaultEps);

            VectorOperations.Add(_a, _b, ref _dest);
            Assert.AreEqual(4.0, _dest[0], FloatComparison.DefaultEps);
            Assert.AreEqual(6.0, _dest[1], FloatComparison.DefaultEps);

            VectorOperations.ScalarMul(_a, 2, ref _dest);
            Assert.AreEqual(2.0, _dest[0], FloatComparison.DefaultEps);
            Assert.AreEqual(4.0, _dest[1], FloatComparison.DefaultEps);

            VectorOperations.Copy(_a, ref _dest);
            Assert.AreEqual(1.0, _dest[0], FloatComparison.DefaultEps);
            Assert.AreEqual(2.0, _dest[1], FloatComparison.DefaultEps);

            VectorOperations.Normalize(_a, ref _dest);
            Assert.AreEqual(0.44721359549995793, _dest[0], FloatComparison.DefaultEps);
            Assert.AreEqual(0.89442719099991586, _dest[1], FloatComparison.DefaultEps);

            VectorOperations.Fill(ref _dest, 5.0);
            Assert.AreEqual(5.0, _dest[0], FloatComparison.DefaultEps);
            Assert.AreEqual(5.0, _dest[1], FloatComparison.DefaultEps);
        }
        public void TestAdd()
        {
            Vector a = Vector.Create(1.0, 2.0);
            Vector b = Vector.Create(-1.0, 3.0);

            VectorOperations.Add(a, b, ref a);
            Assert.IsTrue(VectorComparison.Close(a, Vector.Create(0.0, 5.0), FloatComparison.DefaultEps));
        }
 public void Apply(Individual <double>[] population)
 {
     foreach (var individual in population)
     {
         double[] chromosome = individual.Chromosome;
         individual.Chromosome = VectorOperations.Add(individual.Chromosome, ComputePerturbations());
     }
 }
Esempio n. 4
0
        private void UpdatePositionAndVelocity()
        {
            for (int i = 0; i < swarmSize; i++)
            {
                int rp = randomNumberGenerator.Next(dimension);
                int rg = randomNumberGenerator.Next(dimension);

                individuals[i].Velocity = VectorOperations.Add(VectorOperations.Add(VectorOperations.Scale(omega, individuals[i].Velocity),
                                                                                    VectorOperations.Scale(phip, VectorOperations.Scale(rp, VectorOperations.Subtract(BestPosition, individuals[i].Position)))),
                                                               VectorOperations.Scale(phig, VectorOperations.Scale(rg, VectorOperations.Subtract(individuals[i].PersonalBestPosition, individuals[i].Position))));

                individuals[i].Position = VectorOperations.Add(individuals[i].Position, individuals[i].Velocity);
            }
        }
        /// <summary>
        ///     Performs mutation of the population according to de/rand/1/bin scheme
        /// </summary>
        private void Mutation()
        {
            offsprings = new Individual[populationSize];

            for (int i = 0; i < populationSize; i++)
            {
                int r1 = randomNumberGenerator.Next(populationSize);
                int r2 = randomNumberGenerator.Next(populationSize);
                int r3 = randomNumberGenerator.Next(populationSize);


                double[] donorVector = VectorOperations.Add(individuals[r1].Position, VectorOperations.Scale(mutationFactor, VectorOperations.Subtract(individuals[r2].Position, individuals[r3].Position)));

                offsprings[i] = new Individual(donorVector, double.MaxValue);
            }
        }