00001 using System; 00002 using System.Collections.Generic; 00003 using System.Linq; 00004 using System.Text; 00005 using GEP.Core.Common; 00006 00007 namespace GEP.Core.Common 00008 { 00019 [Serializable] 00020 public abstract class Genotype 00021 { 00022 internal double _matingProbability; 00033 public double MatingProbability 00034 { 00035 get 00036 { 00037 return _matingProbability; 00038 } 00039 } 00040 internal double _testFitness; 00044 public double TestFitness 00045 { 00046 get 00047 { 00048 return _testFitness; 00049 } 00050 } 00051 internal double _fitness; 00055 public double Fitness 00056 { 00057 get 00058 { 00059 return _fitness; 00060 } 00061 } 00077 protected int[] _functionStack; 00078 protected int _n_Genes; 00082 public int N_Genes 00083 { 00084 get 00085 { 00086 return _n_Genes; 00087 } 00088 } 00089 protected int _geneLength; 00093 public int GeneLength 00094 { 00095 get 00096 { 00097 return _geneLength; 00098 } 00099 } 00100 protected int _headLength; 00104 public int HeadLength 00105 { 00106 get 00107 { 00108 return _headLength; 00109 } 00110 } 00111 protected int _tailLength; 00115 public int TailLength 00116 { 00117 get 00118 { 00119 return _tailLength; 00120 } 00121 } 00122 00132 protected Symbol _genesLinkingFunction; 00133 00143 public Symbol[][] _linearStructure; 00144 00148 public int GenotypeLength 00149 { 00150 get 00151 { 00152 return _linearStructure.Length; 00153 } 00154 } 00155 00156 protected Genotype() { } 00157 00168 public Genotype(Population population) 00169 { 00170 _n_Genes = population.N_Genes; 00171 _geneLength = population.GeneLength; 00172 _headLength = population.HeadLength; 00173 _tailLength = population.TailLength; 00174 _genesLinkingFunction = population.GenesLinkingFunction; 00175 _functionStack = population._functionStack; 00176 } 00177 00196 public void Initialize(Random random, Alphabet functionsSet, Alphabet variablesSet, Alphabet constantsSet, 00197 double[] symbolSetsProbabilityDistribution) 00198 { 00199 _linearStructure = new Symbol[_n_Genes][]; 00200 for (int i = 0; i < _n_Genes; i++) 00201 { 00202 _linearStructure[i] = new Symbol[_geneLength]; 00203 } 00204 Symbol[] gene; 00205 float varConstRatioInTail = 00206 (float)((symbolSetsProbabilityDistribution[1] - symbolSetsProbabilityDistribution[0]) 00207 / (1 - symbolSetsProbabilityDistribution[0] - (symbolSetsProbabilityDistribution[1] - symbolSetsProbabilityDistribution[0]))); 00208 double varConstProbabilityDistribution = varConstRatioInTail / (varConstRatioInTail + 1); 00209 int headAndTailLength = _tailLength + _headLength; 00210 for (int i = 0; i < _n_Genes; i++) 00211 { 00212 gene = _linearStructure[i]; 00213 for (int j = 0; j < _headLength; j++) 00214 { 00215 double r = random.NextDouble(); 00216 if (r < symbolSetsProbabilityDistribution[0]) 00217 gene[j] = functionsSet[random.Next(functionsSet.Length)]; 00218 else if (r < symbolSetsProbabilityDistribution[1]) 00219 gene[j] = variablesSet[random.Next(variablesSet.Length)]; 00220 else 00221 gene[j] = null; 00222 } 00223 for (int j = _headLength; j < headAndTailLength; j++) 00224 { 00225 double r = random.NextDouble(); 00226 if (r < varConstProbabilityDistribution) 00227 gene[j] = variablesSet[random.Next(variablesSet.Length)]; 00228 else 00229 gene[j] = null; 00230 } 00231 int headAndTwoTailsLength = _tailLength + headAndTailLength; 00232 for (int j = headAndTailLength; j < headAndTwoTailsLength; j++) 00233 { 00234 gene[j] = constantsSet[random.Next(constantsSet.Length)]; ; 00235 } 00236 } 00237 InitializeGeneValuesArray(); 00238 } 00239 00245 protected abstract void InitializeGeneValuesArray(); 00246 00253 public void CalculateAll(int variableValuesIndex) 00254 { 00255 CalculateAllGenes(variableValuesIndex); 00256 CalculateValue(); 00257 } 00258 00263 protected abstract void CalculateValue(); 00264 00270 protected void CalculateAllGenes(int variableValuesIndex) 00271 { 00272 for (int i = 0; i < _n_Genes; i++) 00273 { 00274 CalculateGene(i, variableValuesIndex); 00275 } 00276 } 00277 00287 public static void CopyLinearStructure(Genotype source, Genotype destination, int startGene, 00288 int endGene, int startIndex, int endIndex) 00289 { 00290 int sourceGeneLength = source.GeneLength; 00291 int startIndexBound = (startGene == endGene ? endIndex + 1 : sourceGeneLength); 00292 00293 if (startGene < 0 || endGene >= source._n_Genes) 00294 return; 00295 for (int j = startIndex; j < startIndexBound; j++) 00296 { 00297 destination._linearStructure[startGene][j] = source._linearStructure[startGene][j]; 00298 } 00299 if (startIndexBound == sourceGeneLength) 00300 { 00301 for (int j = 0; j <= endIndex; j++) 00302 { 00303 destination._linearStructure[endGene][j] = source._linearStructure[endGene][j]; 00304 } 00305 } 00306 for (int i = startGene + 1; i < endGene; i++) 00307 { 00308 for (int j = 0; j < sourceGeneLength; j++) 00309 { 00310 destination._linearStructure[i][j] = source._linearStructure[i][j]; 00311 } 00312 } 00313 } 00314 00326 public void UpdateSymbolReferences(Alphabet oldAlphabet, Alphabet newAlphabet, int startIndex, int endIndex) 00327 { 00328 int indexInAlphabet; 00329 for (int i = 0; i < _n_Genes; i++) 00330 { 00331 for (int j = startIndex; j <= endIndex; j++) 00332 { 00333 indexInAlphabet = oldAlphabet[_linearStructure[i][j]]; 00334 if (indexInAlphabet != -1) 00335 _linearStructure[i][j] = newAlphabet[indexInAlphabet]; 00336 } 00337 } 00338 } 00339 00349 public void UpdateFunctionReferences(Alphabet oldFunctions, Alphabet newFunctions) 00350 { 00351 UpdateSymbolReferences(oldFunctions, newFunctions, 0, _headLength - 1); 00352 } 00353 00363 public void UpdateVariableReferences(Alphabet oldVariables, Alphabet newVariables) 00364 { 00365 UpdateSymbolReferences(oldVariables, newVariables, 0, _headLength + _tailLength - 1); 00366 } 00367 00377 public void UpdateConstantReferences(Alphabet oldConstants, Alphabet newConstants) 00378 { 00379 UpdateSymbolReferences(oldConstants, newConstants, _headLength + _tailLength, _geneLength - 1); 00380 } 00381 00393 public abstract Genotype Clone(); 00394 00399 public abstract Common.Genotype CloneWithoutLinearStructure(); 00400 00407 protected abstract void CalculateGene(int geneIndex, int variableValuesIndex); 00408 00409 public override string ToString() 00410 { 00411 return "F = " + _fitness + ",TF = " + _testFitness + ",M = " + _matingProbability; 00412 } 00413 00422 public abstract int IsValid(); 00423 00433 public void CloneConstants() 00434 { 00435 for (int i = 0; i < _n_Genes; i++) 00436 { 00437 for (int j = _headLength + _tailLength; j < _geneLength; j++) 00438 { 00439 _linearStructure[i][j] = _linearStructure[i][j].Clone(); 00440 } 00441 } 00442 } 00443 } 00444 }