Exemplo n.º 1
0
 public Population(Population population)
 {
     this.Size = population.Size;
     this.chromosome_length = population.chromosome_length;
     this.total_fitness = population.total_fitness;
     this.Individual = population.Individual;
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {

            double backpack_capacity;
            List<Element> myElements;
            Stock current_stock;
            Population current_population, next_population;

            myElements = new List<Element>();

            #region xmlreader
            //Loading xml file.
            XmlReader reader = XmlReader.Create("C:/Users/Fernando/Google Drive/UAA/8vo Semestre/Metaheurísticas II/AG_Backpack/Cookie_Test.xml");
            reader.MoveToContent();

            //Reading backpack capacity
            reader.ReadToFollowing("backpacksize");
            backpack_capacity = reader.ReadElementContentAsDouble();
            reader.ReadToFollowing("items");
            while (reader.Read())
            {
                if(reader.Name == "item" && reader.IsStartElement())
                {
                    reader.ReadToFollowing("name");
                    string name = reader.ReadElementContentAsString();
                    reader.ReadToFollowing("value");
                    double value = reader.ReadElementContentAsDouble();
                    reader.ReadToFollowing("weight");
                    double weight = reader.ReadElementContentAsDouble();
                    myElements.Add(new Element(name, value, weight));
                }
            }
            reader.Close();
            #endregion

            current_stock = new Stock(myElements, backpack_capacity);

            //Creating first population.
            current_population = new Population(Stock.Size * 4, Stock.Size);
            current_population.BeginPopulation();
            current_population.SortByFitness();

            for (uint i = 0; i < 200; i++)
            {
                //Creating new population.
                next_population = new Population(Stock.Size * 4, Stock.Size);

                for(uint j = 0; j < next_population.Size; j+= 2)
                {
                    //Selecting parents.
                    var parent1 = current_population.RouletteSelection();
                    var parent2 = current_population.RouletteSelection();

                    //Crossing over parents.
                    var offsprings = GeneticOperators.UniformCrossover(parent1.GetChromosome(), parent2.GetChromosome());
                    var offspring1 = offsprings.Item1;
                    var offspring2 = offsprings.Item2;

                    //Applying mutation to both offsprings.
                    GeneticOperators.Mutation(ref offspring1);
                    GeneticOperators.Mutation(ref offspring2);

                    //Getting value, fitness and weight of offsprings.
                    offspring1.Value   = current_stock.GetValue(offspring1);
                    offspring1.Weight  = current_stock.GetWeight(offspring1);
                    offspring1.Fitness = current_stock.GetFitness(offspring1);

                    offspring2.Value   = current_stock.GetValue(offspring2);
                    offspring2.Weight  = current_stock.GetWeight(offspring2);
                    offspring2.Fitness = current_stock.GetFitness(offspring2);

                    //Sending new offsprings to next generation.
                    next_population.SetInhabitant(offspring1, j);
                    next_population.SetInhabitant(offspring2, j + 1);
                }
                //Applying elitism
                next_population.SortByFitness();
                next_population.SetInhabitant(current_population.GetFittest(), 0);
                next_population.SortByFitness();
                next_population.CalculatePopulationFitness();

                //Making next population the actual population.
                current_population = new Population(next_population);
            }

            Console.WriteLine("Value: {0}\nWeight: {1}\nFitness: {2}\n", current_population.GetFittest().Value,
                    current_population.GetFittest().Weight, current_population.GetFittest().Fitness);
            current_stock.PrintElementsInBackpack(current_population.GetFittest());
            Console.ReadKey();
        }