00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005
00009 namespace GEP.Core.Common.GeneticOperators
00010 {
00016 [Serializable]
00017 public abstract class GeneticOperator : Symbol
00018 {
00024 protected Random _random;
00025
00026 protected GenotypeSelection.GenotypeSelection _selection;
00030 public GenotypeSelection.GenotypeSelection Selection
00031 {
00032 get
00033 {
00034 return _selection;
00035 }
00036 set
00037 {
00038 Selection = _selection;
00039 }
00040 }
00041
00042 protected bool _producesNewOffspring;
00049 public bool ProducesNewOffspring
00050 {
00051 get
00052 {
00053 return _producesNewOffspring;
00054 }
00055 }
00056
00057 internal bool _applyOnNewGeneration;
00064 public bool ApplyOnNewGeneration
00065 {
00066 get
00067 {
00068 return _applyOnNewGeneration;
00069 }
00070 }
00071
00072 protected int _n_children;
00077 public int N_children
00078 {
00079 get
00080 {
00081 return _n_children;
00082 }
00083 }
00084
00100 public GeneticOperator(int n_Parameters, int n_children, bool producesNewOffspring,
00101 bool applyOnNewGeneration,
00102 GenotypeSelection.GenotypeSelection selection, string name)
00103 : base(n_Parameters, name)
00104 {
00105 _applyOnNewGeneration = applyOnNewGeneration;
00106 _selection = selection;
00107 _producesNewOffspring = producesNewOffspring;
00108 _n_children = n_children;
00109 _random = new Random();
00110 }
00111
00123 public abstract void Apply(Genotype[] parameters, int[] selectionIndexes, Genotype[] destination, ref int destIndex);
00124
00132 public abstract void Apply(Genotype[] parameters, int[] selectionIndexes);
00133
00139 public void Apply(Population population)
00140 {
00141 int[][] selection = _selection.Select(population);
00142 for (int i = selection.Length - 1; 0 <= i; i--)
00143 {
00144 Apply(population._genotypes, selection[i]);
00145 }
00146 }
00147
00157 public void Apply(Population population, Genotype[] destination, ref int destIndex)
00158 {
00159 int selectionStartIndex = 0;
00160 int selectionEndIndex = 0;
00161
00162 int[][] selection = _selection.Select(ref selectionStartIndex, ref selectionEndIndex, population);
00163 for (int i = selectionStartIndex; i <= selectionEndIndex; i++)
00164 {
00165 Apply(population._genotypes, selection[i], destination, ref destIndex);
00166 }
00167 }
00168
00174 public GeneticOperator(GeneticOperator symbol)
00175 :base(symbol)
00176 {
00177 _applyOnNewGeneration = symbol._applyOnNewGeneration;
00178 _selection = symbol._selection;
00179 _producesNewOffspring = symbol._producesNewOffspring;
00180 _n_children = symbol._n_children;
00181 _random = new Random();
00182 }
00183 }
00184 }