00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005
00006 namespace GEP.Core.Common.GeneticOperators.GenotypeSelection
00007 {
00015 [Serializable]
00016 public class RandomAccordingToMatingProabability : GenotypeSelection
00017 {
00021 protected int _selectionSize;
00025 protected int _n_selections;
00029 protected int[][] _selection;
00036 protected int[][][] _allSelections;
00042 protected int[] _allSelectionsSize;
00046 protected int[] _allSelectionsStartIndex;
00050 protected int[] _allSelectionsEndIndex;
00058 protected int _allSelectionIndex;
00071 double[] _probabilityDistribution;
00075 protected Random _random = new Random();
00076
00092 public RandomAccordingToMatingProabability(int selectionSize, int n_selections, double[] probabilityDistribution)
00093 {
00094 _selectionSize = selectionSize;
00095 _n_selections = n_selections;
00096 _selection = new int[n_selections][];
00097 _probabilityDistribution = probabilityDistribution;
00098 for (int i = 0; i < _n_selections; i++)
00099 {
00100 _selection[i] = new int[selectionSize];
00101 }
00102 _allSelections = new int[probabilityDistribution.Length + 1][][];
00103 _allSelectionsSize = new int[_allSelections.Length];
00104 _allSelectionsStartIndex = new int[_allSelectionsSize.Length];
00105 _allSelectionsEndIndex = new int[_allSelectionsSize.Length];
00106 for (int i = _allSelections.Length - 1; 0 <= i; i--)
00107 {
00108 _allSelections[i] = _selection;
00109 }
00110 _allSelectionIndex = _allSelections.Length;
00111 }
00112
00113 public int[][] Select(Population population)
00114 {
00115 throw new NotImplementedException();
00116 }
00117
00118 public int[][] Select(ref int selectionStartIndex, ref int selectionEndIndex, Population population)
00119 {
00120
00121 if (_allSelectionIndex == _allSelections.Length)
00122 {
00123 double rd;
00124 for (int i = _allSelectionsSize.Length - 1; 0 <= i; i--)
00125 {
00126 _allSelectionsSize[i] = 0;
00127 }
00128 bool isLastInProbDistribution;
00129 for (int i = 0; i < _n_selections; i++)
00130 {
00131 isLastInProbDistribution = true;
00132 rd = _random.NextDouble();
00133
00134 for (int j = 0; j < _probabilityDistribution.Length; j++)
00135 {
00136 if (rd < _probabilityDistribution[j])
00137 {
00138 _allSelectionsSize[j]++;
00139 isLastInProbDistribution = false;
00140 }
00141 }
00142 if (isLastInProbDistribution)
00143 _allSelectionsSize[_allSelectionsSize.Length - 1]++;
00144 }
00145 for (int i = 0, counter = 0; i < _allSelections.Length; i++)
00146 {
00147 _allSelectionsStartIndex[i] = counter;
00148 counter += _allSelectionsSize[i];
00149 _allSelectionsEndIndex[i] = counter - 1;
00150 }
00151 _allSelectionIndex = 0;
00152 }
00153 double r;
00154 double iteratedMatingProb;
00155 int currentSelectionsEndIndex = _allSelectionsEndIndex[_allSelectionIndex];
00156 for (int i = _allSelectionsStartIndex[_allSelectionIndex];
00157 i < currentSelectionsEndIndex; i++)
00158 {
00159 iteratedMatingProb = 0;
00160 for (int j = _allSelections[_allSelectionIndex][i].Length - 1; 0 <= j; j--)
00161 {
00162 r = _random.NextDouble() * population._totalMatingProbability;
00163 iteratedMatingProb = 0;
00164 for (int k = population.Size - 1; 0 <= k; k--)
00165 {
00166 iteratedMatingProb += population[k]._matingProbability;
00167 if (r < iteratedMatingProb)
00168 {
00169 _allSelections[_allSelectionIndex][i][j] = k;
00170 break;
00171 }
00172 }
00173 }
00174 }
00175 selectionStartIndex = _allSelectionsStartIndex[_allSelectionIndex];
00176 selectionEndIndex = _allSelectionsEndIndex[_allSelectionIndex];
00177 _allSelectionIndex++;
00178 return _selection;
00179 }
00180 }
00181 }