/// <summary> /// Crossover using a bitmask. /// </summary> /// <param name="first"></param> /// <param name="second"></param> /// <returns>A tuple of two Inhabitants.</returns> public static Tuple<Inhabitant, Inhabitant> UniformCrossover(bool[] first, bool[] second) { Random rnd = new Random(); if (rnd.NextDouble() < 0.2) return Tuple.Create(new Inhabitant(first), new Inhabitant(second)); bool[] chromosome1, chromosome2; Stock current_stock; current_stock = new Stock(); chromosome1 = new bool[first.Length]; chromosome2 = new bool[first.Length]; //This crossover uses a bitmask, it gonna be created IRT and not saved in any var. for(uint i = 0; i < first.Length; i++) { if(rnd.Next() % 2 == 0) { chromosome1[i] = first[i]; chromosome2[i] = second[i]; } else { chromosome1[i] = second[i]; chromosome2[i] = first[i]; } } //Creating and returning inhabitants from new chromosomes. return Tuple.Create(new Inhabitant(chromosome1), new Inhabitant(chromosome2)); }
/// <summary> /// Begin the whole population with random values. /// </summary> public void BeginPopulation() { Random rnd; Stock Items; Items = new Stock(); rnd = new Random(); for (uint i = 0; i < this.Size; i++) { Boolean[] new_chromosome; new_chromosome = new Boolean[this.chromosome_length]; for (uint j = 0; j < this.chromosome_length; j++) new_chromosome[j] = rnd.Next() % 2 == 0; Individual[i] = new Inhabitant(new_chromosome); //Setting values for fitness, value and weight. Individual[i].Value = Items.GetValue(Individual[i]); Individual[i].Weight = Items.GetWeight(Individual[i]); Individual[i].Fitness = Items.GetFitness(Individual[i]); } //Getting the sum of the fitness of the whole population. total_fitness = Individual.Sum(i => i.Fitness); }
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(); }