Esempio n. 1
0
 public PSOSolution(PSOSolution original) : base(original)
 {
     Velocity = new double[MyContainer.MyOSSP.NumOperations];
     for (var d = 0; d < MyContainer.MyOSSP.NumOperations; d++)
     {
         Velocity[d] = original.Velocity[d];
     }
     BestPosition = new int[MyContainer.MyOSSP.NumOperations];
     for (var d = 0; d < MyContainer.MyOSSP.NumOperations; d++)
     {
         BestPosition[d] = original.BestPosition[d];
     }
     BestFitness = original.BestFitness;
 }
Esempio n. 2
0
        public override void Execute(OSSP theOSSP, Random theAleatory)
        {
            MyOSSP      = theOSSP;
            MyAleatory  = theAleatory;
            CurrentEFOs = 0;
            Curve       = new List <double>();

            var swarm = new List <PSOSolution>();

            for (var i = 0; i < SwarmSize; i++)
            {
                var newParticle = new PSOSolution(this);
                newParticle.RandomInitialization();
                swarm.Add(newParticle);
                if (Math.Abs(newParticle.Fitness - MyOSSP.OptimalKnown) < 1e-10)
                {
                    break;
                }
            }

            var maxFitness = swarm.Min(x => x.Fitness);
            var best       = swarm.Find(x => Math.Abs(x.Fitness - maxFitness) < 1e-10);

            MyBestSolution = new PSOSolution(best);

            while (CurrentEFOs < MaxEFOs && Math.Abs(MyBestSolution.Fitness - MyOSSP.OptimalKnown) > 1e-10)
            {
                for (var i = 0; i < SwarmSize; i++)
                {
                    swarm[i].UpdateVelocity((PSOSolution)MyBestSolution);
                }

                for (var i = 0; i < SwarmSize; i++)
                {
                    swarm[i].UpdatePosition();
                    if (Math.Abs(swarm[i].Fitness - MyOSSP.OptimalKnown) < 1e-10)
                    {
                        break;
                    }
                }

                maxFitness = swarm.Min(x => x.Fitness);
                best       = swarm.Find(x => Math.Abs(x.Fitness - maxFitness) < 1e-10);
                if (maxFitness > MyBestSolution.Fitness)
                {
                    MyBestSolution = new PSOSolution(best);
                }
            }
        }
Esempio n. 3
0
        public void UpdateVelocity(PSOSolution best)
        {
            for (var d = 0; d < MyContainer.MyOSSP.NumOperations; d++)
            {
                var w  = MyContainer.MyAleatory.NextDouble() * ((PSO)MyContainer).W;
                var c1 = MyContainer.MyAleatory.NextDouble() * ((PSO)MyContainer).C1;
                var c2 = MyContainer.MyAleatory.NextDouble() * ((PSO)MyContainer).C2;

                Velocity[d] = w * Velocity[d] +
                              c1 * (BestPosition[d] - Position[d]) +
                              c2 * (best.Position[d] - Position[d]);

                if (Velocity[d] < -4)
                {
                    Velocity[d] = -4;
                }
                if (Velocity[d] > 4)
                {
                    Velocity[d] = 4;
                }
            }
        }