00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using GEP.Core.Common;
00006 using System.Runtime.Serialization;
00007 using System.Runtime.Serialization.Formatters.Binary;
00008
00009 namespace GEP.Core.Float
00010 {
00021 [Serializable]
00022 public class Genotype : Common.Genotype
00023 {
00032 protected float[] _linearStructureValues;
00036 public float[] _geneValues;
00041 public float _value;
00042
00046 protected Genotype() { }
00047
00060 public Genotype(Common.Population population)
00061 : base(population)
00062 {
00063 _linearStructureValues = ((Population)population)._linearStructureValues;
00064 }
00065
00066 protected override void InitializeGeneValuesArray()
00067 {
00068 _geneValues = new float[_n_Genes];
00069 }
00070
00071
00072 protected override void CalculateValue()
00073 {
00074 if (_genesLinkingFunction != null)
00075 _value = ((Symbol)_genesLinkingFunction).Value(_geneValues, 0);
00076 }
00077
00078 protected override void CalculateGene(int geneIndex, int variableValuesIndex)
00079 {
00080 Common.Symbol[] gene = _linearStructure[geneIndex];
00081 int randomNumericalConstantIndex = _headLength + _tailLength;
00082 if (gene[0] == null)
00083 {
00084 _linearStructureValues[0] =
00085 ((Symbol)gene[randomNumericalConstantIndex]).Value(_linearStructureValues, 0);
00086 }
00087 else if (gene[0].N_Parameters == 0)
00088 {
00089 _linearStructureValues[0] =
00090 ((Symbol)gene[0]).Value(_linearStructureValues, variableValuesIndex);
00091 }
00092 else
00093 {
00094 _functionStack[0] = 0;
00095 int functionStackPointer = 2;
00096 int functionStackInnerPointer = 0;
00097 int n_UnprocessedParameters = gene[0].N_Parameters;
00098 _functionStack[1] = 1;
00099 int parametersLoopEnd;
00100
00101
00102 do
00103 {
00104 parametersLoopEnd = _functionStack[functionStackInnerPointer + 1]
00105 + gene[_functionStack[functionStackInnerPointer]].N_Parameters;
00106 for (int j = _functionStack[functionStackInnerPointer + 1]; j < parametersLoopEnd; j++)
00107 {
00108 if (gene[j] == null)
00109 {
00110 _linearStructureValues[j] =
00111 ((Symbol)gene[randomNumericalConstantIndex++]).Value(_linearStructureValues, 0);
00112 }
00113 else if (gene[j].N_Parameters == 0)
00114 {
00115 _linearStructureValues[j] =
00116 ((Symbol)gene[j]).Value(_linearStructureValues, variableValuesIndex);
00117 }
00118 else
00119 {
00120 _functionStack[functionStackPointer] = j;
00121 _functionStack[functionStackPointer + 1] = _functionStack[functionStackPointer - 1]
00122 + gene[_functionStack[functionStackPointer - 2]].N_Parameters;
00123 n_UnprocessedParameters += gene[j].N_Parameters;
00124 functionStackPointer += 2;
00125 }
00126 n_UnprocessedParameters--;
00127 }
00128 functionStackInnerPointer += 2;
00129 } while (n_UnprocessedParameters > 0);
00130 functionStackPointer -= 2;
00131
00132 while (functionStackPointer >= 0)
00133 {
00134 _linearStructureValues[_functionStack[functionStackPointer]] =
00135 ((Symbol)gene[_functionStack[functionStackPointer]])
00136 .Value(_linearStructureValues, _functionStack[functionStackPointer + 1]);
00137 functionStackPointer -= 2;
00138 }
00139 }
00140 _geneValues[geneIndex] = _linearStructureValues[_functionStack[0]];
00141 }
00142
00143 public override Common.Genotype Clone()
00144 {
00145 Genotype result = new Genotype();
00146 result._functionStack = _functionStack;
00147 result._geneLength = _geneLength;
00148 result._genesLinkingFunction = _genesLinkingFunction;
00149 result._geneValues = new float[_n_Genes];
00150 result._headLength = _headLength;
00151 result._linearStructure = new Common.Symbol[_n_Genes][];
00152 for (int i = 0; i < _n_Genes; i++)
00153 {
00154 result._linearStructure[i] = new Common.Symbol[_geneLength];
00155 for (int j = 0; j < _geneLength; j++)
00156 {
00157 result._linearStructure[i][j] = _linearStructure[i][j];
00158 }
00159 }
00160 result._linearStructureValues = _linearStructureValues;
00161 result._n_Genes = _n_Genes;
00162 result._tailLength = _tailLength;
00163 return result;
00164 }
00165
00166 public override Common.Genotype CloneWithoutLinearStructure()
00167 {
00168 Genotype result = new Genotype();
00169 result._functionStack = _functionStack;
00170 result._geneLength = _geneLength;
00171 result._genesLinkingFunction = _genesLinkingFunction;
00172 result._geneValues = _geneValues;
00173 result._headLength = _headLength;
00174 result._linearStructure = new Common.Symbol[_n_Genes][];
00175 for (int i = 0; i < _n_Genes; i++)
00176 {
00177 result._linearStructure[i] = new Common.Symbol[_geneLength];
00178 }
00179 result._linearStructureValues = _linearStructureValues;
00180 result._n_Genes = _n_Genes;
00181 result._tailLength = _tailLength;
00182 return result;
00183 }
00184
00185 public override int IsValid()
00186 {
00187 for (int i = 0; i < _n_Genes; i++)
00188 {
00189 for (int j = _headLength + _tailLength - 1; _headLength <= j; j--)
00190 {
00191 if (_linearStructure[i][j] != null && _linearStructure[i][j].N_Parameters != 0)
00192 return i;
00193 }
00194 for (int j = _headLength + _tailLength; j < _geneLength; j++)
00195 {
00196 if (_linearStructure[i][j] != null && _linearStructure[i][j].N_Parameters != 0)
00197 return i;
00198 }
00199 }
00200 return -1;
00201 }
00202 }
00203 }