Generic Genetic Optimization Algorythm (c#.NET)

Just decidet to try to write a generic genetic optimization algorythm class. The essential thing in genetic algorythm is individ. And this is its c# interface:
[code lang=”cpp”]
 interface IIndivid
 {
  object Value
  {
   get;
   set;
  }

  IIndivid Mutate();
  IIndivid Cross(object obj);
  IIndivid Optimize();
  void Validate();
  IIndivid Clone();
 }
[/code]

Now we need a set of delegates to make our generic class customaizable according to our needs:
[code lang=”cpp”]
//BulkCrosser - to cross individs in a colony
 public delegate object BulkCrosser(object obj);
//Sorter – to sort individs starting from the best
 public delegate object Sorter(object obj);
//Optimizer – to optimize a particular individ
 public delegate object Optimizer(object obj);
//Mutator – to mutate a particular individ or a colony
 public delegate object Mutator(object obj);
//Crosser – to cross two given individs or sets of individs or colonies, whatever
 public delegate object Crosser(object obj1,object obj2);
[/code]

Now comes Individ class:
[code lang=”cpp”]
 class Individ:IIndivid
 {
  public Individ(object value,Mutator mutatorDelegate,Crosser crosserDelegate,Optimizer optimizerDelegate)
  {
   _MutatorDelegate = mutatorDelegate;
   _CrosserDelegate = crosserDelegate;
   _OptimizerDelegate = optimizerDelegate;
   _value = value;
   Validate();
  }
  private object _value;
  public object Value
  {
   set
   {
    _value = value;
   }
   get
   {
    return _value;
   }
  }

  private readonly Mutator _MutatorDelegate;
  private readonly Crosser _CrosserDelegate;
  private readonly Optimizer _OptimizerDelegate;
  public IIndivid Mutate()
  {
   if (_MutatorDelegate != null) _value = _MutatorDelegate(this);
   return this;
  }
  public IIndivid Cross(object obj)
  {
   if (_CrosserDelegate != null) _value = _CrosserDelegate(this, obj);
   return this;
  }
  public IIndivid Optimize()
  {
   if (_OptimizerDelegate != null) _value = _OptimizerDelegate(this);
   return this;
  }
  public void Validate()
  {
   if (_MutatorDelegate == null) throw new Exception(“Mutator delegate is not set…”);
   if (_CrosserDelegate == null) throw new Exception(“Crosser delegate is not set…”);
   if (_OptimizerDelegate == null) throw new Exception(“Optimizer delegate is not set…”);
   if (_value == null) throw new Exception(“Value is not set…”);
  }
  public IIndivid Clone()
  {
   Validate();
   return new Individ(this.Value, this._MutatorDelegate, this._CrosserDelegate, this._OptimizerDelegate);
  }
 }
[/code]
And an optimizer class:
[code lang=”cpp”]
 class GeneticOptimizer
 {
  private IIndivid[] _individs;

  private readonly int _maxIterations;

  public IIndivid Optimal
  {
   get
   {
    Optimize();
    return _individs[0];
   }
  }
  public GeneticOptimizer(IIndivid value,int individColonySize, Mutator mutatorDelegate, BulkCrosser bulkCrosserDelegate, Sorter sorterDelegate, int maxIterations)
  {
   _MutatorDelegate = mutatorDelegate;
   _BulkCrosserDelegate = bulkCrosserDelegate;
   _SorterDelegate = sorterDelegate;
   _maxIterations = maxIterations;
   Initialize(value, individColonySize);
   Validate();
  }
  private void Initialize(IIndivid value,int individColonySize)
  {
   _individs = new IIndivid[individColonySize];
   for (int i = 0; i < individColonySize; i++)
   {
    _individs[i] = value.Clone().Mutate();
   }
  }
  private readonly Sorter _SorterDelegate;
  private readonly BulkCrosser _BulkCrosserDelegate;
  private readonly Mutator _MutatorDelegate;
  internal void Sort()
  {
   _individs = (IIndivid[])_SorterDelegate((object)_individs);
  }
  internal void Cross()
  {
   _individs = (IIndivid[])_BulkCrosserDelegate((object)_individs);
  }
  internal void Optimize()
  {
   for (int i=0;i<_maxIterations;i++)
   {
    this.Mutate();
    this.Cross();
    foreach (IIndivid individ in _individs)
    {
     //Local optimization
     individ.Optimize();
    }
    this.Sort();
   }
  }

  internal void Mutate()
  {
   _individs = (IIndivid[])_MutatorDelegate((object)_individs);
  }
  internal void Validate()
  {
   if (_MutatorDelegate == null) throw new Exception(“Mutator delegate is not set…”);
   if (_BulkCrosserDelegate == null) throw new Exception(“Crosser delegate is not set…”);
   if (_SorterDelegate == null) throw new Exception(“Sorter delegate is not set…”);
   foreach (IIndivid individ in _individs)
   {
    individ.Validate();
   }
  }

 }
[/code]

Leave a Reply