Exemplo n.º 1
0
Arquivo: ICA.cs Projeto: alics/Leopard
        public CompositionPlan Execute(CompositionRequest input, Action <string> display)
        {
            display("ICA started...\n");

            List <CompositionPlan> countries = input.CreateInitialPopulation().ToList();

            countries.ForEach(p => p.PBest = p);

            List <Empire <CompositionPlan> > empires = CreateInitialEmpires(countries, ((IcaConfig)(input.Config))).ToList();

            double iteration = 1;

            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(input.Config.OutputFile))
            {
                file.Flush();
                while (empires.Count > 1 && iteration < input.Config.MaxIteration)
                {
                    foreach (Empire <CompositionPlan> empire in empires)
                    {
                        CompositionPlan globalBest = empires.First().Imperialist;

                        empire
                        //.Assimilate(empire.Imperialist,input.Config.QualityAttributeWeights, input)
                        .CustomAssimilate(globalBest, input.Config.QualityAttributeWeights, input)

                        .UpdateAfterAssimilation()
                        .CalculateCost(((IcaConfig)(input.Config)).Zeta);
                    }

                    empires.NormalizePowers().Compete();

                    //countries.ForEach(c =>
                    //    {
                    //        c.Revolution(countries, ((IcaConfig)(input.Config)).RevolutionRate);
                    //    });

                    empires.EliminatePowerlessEmpires();

                    string output =
                        $"iteration {iteration}," +
                        $" empires: {empires.Count}," +
                        // $" best solution:{empires.First().Imperialist}," +
                        $" best cost:{empires.First().Imperialist.Cost}";

                    file.WriteLine($"{iteration},{empires.First().Imperialist.Cost}");

                    display(output);
                    iteration++;
                }
            }

            display($"ICA Best Cost: {empires.First().Imperialist.Cost}");

            return(empires.First().Imperialist);
        }
Exemplo n.º 2
0
        public CompositionPlan Execute(CompositionRequest input, Action <string> display)
        {
            display("DPSO started...\n");

            List <CompositionPlan> particles = input.CreateInitialPopulation().ToList();

            particles.ForEach(p => p.PBest = p);

            particles.ForEach(p => p.Cost = p.CalculateCost(input.Config.QualityAttributeWeights));

            CompositionPlan gBest = particles.GetGlobalBest();

            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(input.Config.OutputFile))
            {
                file.Flush();

                DpsoConfig psoConfig  = (DpsoConfig)input.Config;
                double     c1_primary = psoConfig.C1;
                double     c2_primary = psoConfig.C2;

                for (int i = 0; i < psoConfig.MaxIteration; i++)
                {
                    psoConfig.C1 = ((psoConfig.MaxIteration - i) * c1_primary + (i * c2_primary)) / psoConfig.MaxIteration;

                    psoConfig.C2 = ((psoConfig.MaxIteration - i) * c2_primary + (i * c1_primary)) / psoConfig.MaxIteration;

                    particles.ForEach(p =>
                    {
                        p.Move(gBest, psoConfig, input.TaskCandidateServices.ToList());
                        gBest = particles.GetGlobalBest();
                    });

                    display($"iteration: {i}, Cost: {gBest.Cost}");
                    file.WriteLine($"{i},{gBest.Cost}");
                }
            }

            display($"DPSO Best Solution: {gBest}");

            return(gBest);
        }