Advertisement
Guest User

Kromosom.cs

a guest
Sep 23rd, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. // from pastebin
  7. namespace Kromo
  8. {
  9.     public class Kromosom:IComparable<Kromosom>
  10.     {
  11.         public List<double> Alele;
  12.         private int chromosomeLength=11;
  13.  
  14.         // CM -- added RawFitness for reference (diagnostics)
  15.         public double RawFitnes { get; set; }
  16.  
  17.         // CM -- a niche identifier for applying niche scaling
  18.         public int Niche { get; set; }
  19.  
  20.         // CM - added Niche scaled version of the fitness, which is a penalized
  21.         // version of the normalized fitness
  22.         public double NicheScaledFitness { get; set; }
  23.        
  24.         // CM - Fitness result of calculation, currently 1/Raw Fitness
  25.         public double ScaledFitness { get; set; }
  26.  
  27.         // CM - Normalized fitness value
  28.         // Sum of this in a population equals 1.0
  29.         public double NormalizedFitness { get; set; }
  30.  
  31.         public Kromosom(Random rand)
  32.         {
  33.             Alele = new List<double>();
  34.             for (int i = 0; i < chromosomeLength; i++)
  35.             {
  36.                 Alele.Add(rand.NextDouble() * 2000 - 1000);
  37.             }
  38.         }
  39.        
  40.         public Kromosom(Kromosom kromosom)
  41.         {
  42.             this.Alele = new List<double>();
  43.             this.RawFitnes = kromosom.RawFitnes;
  44.             this.ScaledFitness = kromosom.ScaledFitness;
  45.            
  46.             for (int i = 0; i < chromosomeLength; i++)
  47.             {
  48.                 Alele.Add(kromosom.Alele[i]);
  49.             }
  50.         }
  51.        
  52.         public Kromosom(List<double> Alele)
  53.         {
  54.             this.Alele = new List<double>();
  55.            
  56.             for (int i = 0; i < Alele.Count; i++)
  57.                 this.Alele.Add(Alele[i]);
  58.         }
  59.  
  60.         // For sorting, use "active_fitness" which returns, currently, the
  61.         // niche scaled fitness
  62.         public int CompareTo(Kromosom kromosom)
  63.         {
  64.             // CM - compare uses TempSelection which is the normalized fitness
  65.             return NormalizedFitness.CompareTo(kromosom.NormalizedFitness);
  66.         }
  67.  
  68.         // CM - added standard deviation calculation/comparison of content
  69.         // for niching
  70.         public double StdDev(Kromosom other) {
  71.             double sum = 0.0f;
  72.             for(int i=0; i<Alele.Count; i++) {
  73.                 double diff = other.Alele[i] - Alele[i];
  74.                 sum += diff*diff;
  75.             }
  76.             return Math.Sqrt(sum);
  77.         }
  78.  
  79.         // CM --
  80.         // utility for dumping to string (MonoDevelop doesn't like
  81.         // ToString() in the debug windows for some reason it crashes)
  82.         public string AsString() {
  83.        
  84.             StringBuilder sb = new StringBuilder();
  85.             sb.AppendFormat("raw; {0}; ", RawFitnes);
  86.             sb.AppendFormat("scale; {0}; ", ScaledFitness);
  87.             sb.AppendFormat("niche; {0}; nicheFit; {1}; ", Niche, NicheScaledFitness);
  88.             sb.AppendFormat("norm; {0}; ", NormalizedFitness);
  89.             sb.Append(" --- ");
  90.  
  91.             for(int i=0; i<Alele.Count; i++) {
  92.                 if(i>0) sb.Append(",");
  93.                 sb.AppendFormat("{0:0.000}", Alele[i] );
  94.             }
  95.             return sb.ToString();
  96.         }
  97.     }
  98.    
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement