Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- // from pastebin
- namespace Kromo
- {
- public class Kromosom:IComparable<Kromosom>
- {
- public List<double> Alele;
- private int chromosomeLength=11;
- // CM -- added RawFitness for reference (diagnostics)
- public double RawFitnes { get; set; }
- // CM -- a niche identifier for applying niche scaling
- public int Niche { get; set; }
- // CM - added Niche scaled version of the fitness, which is a penalized
- // version of the normalized fitness
- public double NicheScaledFitness { get; set; }
- // CM - Fitness result of calculation, currently 1/Raw Fitness
- public double ScaledFitness { get; set; }
- // CM - Normalized fitness value
- // Sum of this in a population equals 1.0
- public double NormalizedFitness { get; set; }
- public Kromosom(Random rand)
- {
- Alele = new List<double>();
- for (int i = 0; i < chromosomeLength; i++)
- {
- Alele.Add(rand.NextDouble() * 2000 - 1000);
- }
- }
- public Kromosom(Kromosom kromosom)
- {
- this.Alele = new List<double>();
- this.RawFitnes = kromosom.RawFitnes;
- this.ScaledFitness = kromosom.ScaledFitness;
- for (int i = 0; i < chromosomeLength; i++)
- {
- Alele.Add(kromosom.Alele[i]);
- }
- }
- public Kromosom(List<double> Alele)
- {
- this.Alele = new List<double>();
- for (int i = 0; i < Alele.Count; i++)
- this.Alele.Add(Alele[i]);
- }
- // For sorting, use "active_fitness" which returns, currently, the
- // niche scaled fitness
- public int CompareTo(Kromosom kromosom)
- {
- // CM - compare uses TempSelection which is the normalized fitness
- return NormalizedFitness.CompareTo(kromosom.NormalizedFitness);
- }
- // CM - added standard deviation calculation/comparison of content
- // for niching
- public double StdDev(Kromosom other) {
- double sum = 0.0f;
- for(int i=0; i<Alele.Count; i++) {
- double diff = other.Alele[i] - Alele[i];
- sum += diff*diff;
- }
- return Math.Sqrt(sum);
- }
- // CM --
- // utility for dumping to string (MonoDevelop doesn't like
- // ToString() in the debug windows for some reason it crashes)
- public string AsString() {
- StringBuilder sb = new StringBuilder();
- sb.AppendFormat("raw; {0}; ", RawFitnes);
- sb.AppendFormat("scale; {0}; ", ScaledFitness);
- sb.AppendFormat("niche; {0}; nicheFit; {1}; ", Niche, NicheScaledFitness);
- sb.AppendFormat("norm; {0}; ", NormalizedFitness);
- sb.Append(" --- ");
- for(int i=0; i<Alele.Count; i++) {
- if(i>0) sb.Append(",");
- sb.AppendFormat("{0:0.000}", Alele[i] );
- }
- return sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement