コード例 #1
0
ファイル: Stock.cs プロジェクト: ferSoto/GA_Backpack
 /// <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;
 }
コード例 #2
0
 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);
 }
コード例 #3
0
ファイル: Stock.cs プロジェクト: ferSoto/GA_Backpack
 /// <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;
 }
コード例 #4
0
ファイル: Stock.cs プロジェクト: ferSoto/GA_Backpack
 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);
         }
     }
 }
コード例 #5
0
ファイル: Population.cs プロジェクト: ferSoto/GA_Backpack
        /// <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);
        }
コード例 #6
0
ファイル: Population.cs プロジェクト: ferSoto/GA_Backpack
 /// <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;
 }
コード例 #7
0
ファイル: Stock.cs プロジェクト: ferSoto/GA_Backpack
 /// <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);
 }