コード例 #1
0
        public override void Execute(OSSP theOSSP, Random theAleatory)
        {
            MyOSSP      = theOSSP;
            MyAleatory  = theAleatory;
            CurrentEFOs = 0;
            Curve       = new List <double>();

            /*Generar e inicializar gatos*/
            for (var i = 0; i < SwarmSize; i++)
            {
                var newCat = new Cat(this);
                newCat.RandomInitialization();
                Swarm.Add(newCat);
                if (Math.Abs(newCat.Fitness - MyOSSP.OptimalKnown) < 1e-10)
                {
                    break;
                }
            }
            DistributeCats();

            /*Escoger el mejor del enjambre*/
            var maxFitness = Swarm.Min(x => x.Fitness);
            var best       = Swarm.Find(x => Math.Abs(x.Fitness - maxFitness) < 1e-10);

            MyBestSolution = new Cat(best);

            while (CurrentEFOs < MaxEFOs && Math.Abs(MyBestSolution.Fitness - MyOSSP.OptimalKnown) > 1e-10)
            {
                for (var i = 0; i < SwarmSize; i++)
                {
                    if (!Swarm[i].TMFlag)
                    {
                        Swarm[i] = new Cat(SeekingMode(Swarm[i]));
                    }
                    else
                    {
                        Swarm[i] = new Cat(TracingMode(Swarm[i]));
                    }
                }

                /*Actualizar el mejor*/
                maxFitness = Swarm.Min(x => x.Fitness);
                best       = Swarm.Find(x => Math.Abs(x.Fitness - maxFitness) < 1e-10);
                if (maxFitness > MyBestSolution.Fitness)
                {
                    MyBestSolution = new Cat(best);
                }

                /*Redistribuir los gatos*/
                DistributeCats();
            }
        }
コード例 #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);
                }
            }
        }
コード例 #3
0
 public abstract void Execute(OSSP theOSSP, Random theAleatory);
コード例 #4
0
        //    public override void Execute(OSSP theTsp, Random theAleatory)
        //    {
        //       // MyTsp = theTsp;
        //        MyAleatory = theAleatory;
        //        CurrentEFOs = 0;
        //        var s = new Solution(this);
        //        s.Evaluate();
        //        MyBestSolution = new Solution(s);
        //        for (var i = 1; i < Math.Pow(2, theTsp.TotalNodes); i++)
        //        {
        //            // ---
        //            if (s.Fitness > MyBestSolution.Fitness)
        //                MyBestSolution = new Solution(s);
        //        }
        //    }

        //    public override string ToString()
        //    {
        //        return "Exhaustive";
        //    }
        public override void Execute(OSSP theOSSP, Random theAleatory)
        {
            throw new NotImplementedException();
        }
コード例 #5
0
        public override void Execute(OSSP theOSSP, Random theAleatory)
        {
            MyOSSP      = theOSSP;
            MyAleatory  = theAleatory;
            CurrentEFOs = 0;
            Curve       = new List <double>();

            var population = new List <Chromosome>();

            for (var i = 0; i < PopulationSize; i++)
            {
                var s = new Chromosome(this);
                s.RandomInitialization();
                population.Add(s);
                if (Math.Abs(s.Fitness - MyOSSP.OptimalKnown) < 1e-10)
                {
                    break;
                }
            }

            population.Sort((x, y) => x.Fitness.CompareTo(y.Fitness));
            Curve.Add(population[0].Fitness);

            while (CurrentEFOs < MaxEFOs && Math.Abs(population[0].Fitness - MyOSSP.OptimalKnown) > 1e-10)
            {
                var alloffspring = new List <Chromosome>();
                for (var h = 0; h < PopulationSize / 2; h++)
                {
                    // Parents Select
                    int p1;
                    int p2;
                    do
                    {
                        p1 = Tournament(population, TournamentSize);
                        p2 = Tournament(population, TournamentSize);
                    } while (p1 == p2);

                    // Generate Offspring (Crossover)
                    var offsoring = Croosover(population[p1], population[p2]);

                    // Mutate Offspring
                    offsoring[0].Tweak();
                    offsoring[1].Tweak();

                    alloffspring.AddRange(offsoring);

                    if (CurrentEFOs > MaxEFOs)
                    {
                        break;
                    }
                }

                // Replace - Define the new population
                population.AddRange(alloffspring);
                population.Sort((x, y) => x.Fitness.CompareTo(y.Fitness));

                population.RemoveRange(PopulationSize, alloffspring.Count);
            }

            MyBestSolution = population[0];
        }