Exemple #1
0
 /// <summary>
 /// Returns the sum of the weights of all items in the backpack of the inhabitant.
 /// </summary>
 /// <param name="inhabitant"></param>
 /// <returns></returns>
 public double GetWeight(Inhabitant inhabitant)
 {
     double weight = 0.0;
     for (uint i = 0; i < Size; i++)
         weight += element_array[i].Weight * (inhabitant.GetChromosome()[i] ? 1.0 : 0.0);
     return weight;
 }
 public static void Mutation(ref Inhabitant individual)
 {
     Random rnd = new Random();
     for(uint i = 0; i < individual.ChromosomeLength; i++)
         if (rnd.NextDouble() > 0.95)
             individual.SetGen(!individual.GetGen(i), i);
 }
Exemple #3
0
 /// <summary>
 /// Returns the sum of the values of all items in the backpack of the inhabitant.
 /// </summary>
 /// <param name="inhabitant"></param>
 /// <returns></returns>
 public double GetValue(Inhabitant inhabitant)
 {
     double value = 0.0;
     for (uint i = 0; i < Size; i++)
         value += element_array[i].Value * (inhabitant.GetChromosome()[i] ? 1.0 : 0.0);
     return value;
 }
Exemple #4
0
 public void PrintElementsInBackpack(Inhabitant inhabitant)
 {
     for(uint i = 0; i < Size; i++)
     {
         if (inhabitant.GetGen(i))
         {
             Console.WriteLine("\n\nName: {0}\nValue: {1}\nWeight: {2}", element_array[i].Name, 
                     element_array[i].Value, element_array[i].Weight);
         }
     }
 }
Exemple #5
0
        /// <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);
        }
Exemple #6
0
 /// <summary>
 /// Set an inhabitant in the given position.
 /// </summary>
 /// <param name="inhabitant"></param>
 /// <param name="position"></param>
 public void SetInhabitant(Inhabitant inhabitant, uint position)
 {
     Individual[position] = inhabitant;
 }
Exemple #7
0
 /// <summary>
 /// Returns the fitness of the inhabitant. It works as evaluation function.
 /// To use this function, you should have already got the value and weight of the inhabitant.
 /// </summary>
 /// <param name="inhabitant"></param>
 /// <returns></returns>
 public double GetFitness(Inhabitant inhabitant)
 {
     return inhabitant.Value / total_value * (inhabitant.Weight > Max_Weight ? 0.2 : 1.0);
 }