Changeset 1914 for trunk/sources/HeuristicLab.GP
- Timestamp:
- 05/27/09 15:42:04 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.GP/3.4
- Files:
-
- 7 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP/3.4/BakedFunctionTree.cs
r1529 r1914 28 28 using System.Xml; 29 29 using System.Globalization; 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 31 31 32 namespace HeuristicLab.GP { 32 33 33 34 public class LightWeightFunction { 35 36 [Storable] 34 37 public byte arity = 0; 38 39 [Storable] 35 40 public IFunction functionType; 41 42 [Storable] 36 43 public List<double> data = new List<double>(); 37 44 … … 46 53 47 54 public class BakedFunctionTree : ItemBase, IFunctionTree { 55 48 56 private List<LightWeightFunction> linearRepresentation; 57 58 [Storable] 49 59 public List<LightWeightFunction> LinearRepresentation { 50 60 get { 51 61 FlattenVariables(); 52 FlattenTrees(); 62 FlattenTrees(); 53 63 return linearRepresentation; 54 64 } 55 } 65 private set { 66 linearRepresentation = value; 67 treesExpanded = false; 68 variablesExpanded = false; 69 } 70 } 71 56 72 private bool treesExpanded = false; 57 73 private List<IFunctionTree> subTrees; … … 72 88 variables = new List<IVariable>(); 73 89 variablesExpanded = true; 74 foreach (IVariableInfo variableInfo in function.VariableInfos) {75 if (variableInfo.Local) {90 foreach (IVariableInfo variableInfo in function.VariableInfos) { 91 if (variableInfo.Local) { 76 92 variables.Add((IVariable)function.GetVariable(variableInfo.FormalName).Clone()); 77 93 } … … 84 100 fun.functionType = tree.Function; 85 101 linearRepresentation.Add(fun); 86 foreach (IVariable variable in tree.LocalVariables) {102 foreach (IVariable variable in tree.LocalVariables) { 87 103 IItem value = variable.Value; 88 104 fun.data.Add(GetDoubleValue(value)); 89 105 } 90 foreach (IFunctionTree subTree in tree.SubTrees) {106 foreach (IFunctionTree subTree in tree.SubTrees) { 91 107 AddSubTree(new BakedFunctionTree(subTree)); 92 108 } … … 94 110 95 111 private double GetDoubleValue(IItem value) { 96 if (value is DoubleData) {112 if (value is DoubleData) { 97 113 return ((DoubleData)value).Data; 98 } else if (value is ConstrainedDoubleData) {114 } else if (value is ConstrainedDoubleData) { 99 115 return ((ConstrainedDoubleData)value).Data; 100 } else if (value is IntData) {116 } else if (value is IntData) { 101 117 return ((IntData)value).Data; 102 } else if (value is ConstrainedIntData) {118 } else if (value is ConstrainedIntData) { 103 119 return ((ConstrainedIntData)value).Data; 104 120 } else throw new NotSupportedException("Invalid datatype of local variable for GP"); … … 108 124 int arity = linearRepresentation[branchRoot].arity; 109 125 int length = 1; 110 for (int i = 0; i < arity; i++) {126 for (int i = 0; i < arity; i++) { 111 127 length += BranchLength(branchRoot + length); 112 128 } … … 115 131 116 132 private void FlattenTrees() { 117 if (treesExpanded) {133 if (treesExpanded) { 118 134 linearRepresentation[0].arity = (byte)subTrees.Count; 119 foreach (BakedFunctionTree subTree in subTrees) {135 foreach (BakedFunctionTree subTree in subTrees) { 120 136 subTree.FlattenVariables(); 121 137 subTree.FlattenTrees(); … … 128 144 129 145 private void FlattenVariables() { 130 if (variablesExpanded) {146 if (variablesExpanded) { 131 147 linearRepresentation[0].data.Clear(); 132 foreach (IVariable variable in variables) {148 foreach (IVariable variable in variables) { 133 149 linearRepresentation[0].data.Add(GetDoubleValue(variable.Value)); 134 150 } … … 140 156 public int Size { 141 157 get { 142 if (treesExpanded) {158 if (treesExpanded) { 143 159 int size = 1; 144 foreach (BakedFunctionTree tree in subTrees) {160 foreach (BakedFunctionTree tree in subTrees) { 145 161 size += tree.Size; 146 162 } 147 163 return size; 148 } else 149 return linearRepresentation.Count;164 } else 165 return linearRepresentation.Count; 150 166 } 151 167 } … … 153 169 public int Height { 154 170 get { 155 if (treesExpanded) {171 if (treesExpanded) { 156 172 int height = 0; 157 foreach (IFunctionTree subTree in subTrees) {173 foreach (IFunctionTree subTree in subTrees) { 158 174 int curHeight = subTree.Height; 159 if (curHeight > height) height = curHeight;175 if (curHeight > height) height = curHeight; 160 176 } 161 return height +1;177 return height + 1; 162 178 } else { 163 179 int nextBranchStart; … … 171 187 int height = 0; 172 188 branchStart++; 173 for (int i = 0; i < f.arity; i++) {189 for (int i = 0; i < f.arity; i++) { 174 190 int curHeight = BranchHeight(branchStart, out nextBranchStart); 175 if (curHeight > height) height = curHeight;191 if (curHeight > height) height = curHeight; 176 192 branchStart = nextBranchStart; 177 193 } … … 182 198 public IList<IFunctionTree> SubTrees { 183 199 get { 184 if (!treesExpanded) {200 if (!treesExpanded) { 185 201 subTrees = new List<IFunctionTree>(); 186 202 int arity = linearRepresentation[0].arity; 187 203 int branchIndex = 1; 188 for (int i = 0; i < arity; i++) {204 for (int i = 0; i < arity; i++) { 189 205 BakedFunctionTree subTree = new BakedFunctionTree(); 190 206 int length = BranchLength(branchIndex); 191 for (int j = branchIndex; j < branchIndex + length; j++) {207 for (int j = branchIndex; j < branchIndex + length; j++) { 192 208 subTree.linearRepresentation.Add(linearRepresentation[j]); 193 209 } … … 205 221 public ICollection<IVariable> LocalVariables { 206 222 get { 207 if (!variablesExpanded) {223 if (!variablesExpanded) { 208 224 variables = new List<IVariable>(); 209 225 IFunction function = Function; 210 226 int localVariableIndex = 0; 211 foreach (IVariableInfo variableInfo in function.VariableInfos) {212 if (variableInfo.Local) {227 foreach (IVariableInfo variableInfo in function.VariableInfos) { 228 if (variableInfo.Local) { 213 229 IVariable clone = (IVariable)function.GetVariable(variableInfo.FormalName).Clone(); 214 230 IItem value = clone.Value; 215 if (value is ConstrainedDoubleData) {231 if (value is ConstrainedDoubleData) { 216 232 ((ConstrainedDoubleData)value).Data = linearRepresentation[0].data[localVariableIndex]; 217 } else if (value is ConstrainedIntData) {233 } else if (value is ConstrainedIntData) { 218 234 ((ConstrainedIntData)value).Data = (int)linearRepresentation[0].data[localVariableIndex]; 219 } else if (value is DoubleData) {235 } else if (value is DoubleData) { 220 236 ((DoubleData)value).Data = linearRepresentation[0].data[localVariableIndex]; 221 } else if (value is IntData) {237 } else if (value is IntData) { 222 238 ((IntData)value).Data = (int)linearRepresentation[0].data[localVariableIndex]; 223 239 } else throw new NotSupportedException("Invalid local variable type for GP."); … … 238 254 239 255 public IVariable GetLocalVariable(string name) { 240 foreach (IVariable var in LocalVariables) {241 if (var.Name == name) return var;256 foreach (IVariable var in LocalVariables) { 257 if (var.Name == name) return var; 242 258 } 243 259 return null; … … 253 269 254 270 public void AddSubTree(IFunctionTree tree) { 255 if (!treesExpanded) throw new InvalidOperationException();271 if (!treesExpanded) throw new InvalidOperationException(); 256 272 subTrees.Add(tree); 257 273 } 258 274 259 275 public void InsertSubTree(int index, IFunctionTree tree) { 260 if (!treesExpanded) throw new InvalidOperationException();276 if (!treesExpanded) throw new InvalidOperationException(); 261 277 subTrees.Insert(index, tree); 262 278 } … … 264 280 public void RemoveSubTree(int index) { 265 281 // sanity check 266 if (!treesExpanded) throw new InvalidOperationException();282 if (!treesExpanded) throw new InvalidOperationException(); 267 283 subTrees.RemoveAt(index); 268 }269 270 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {271 FlattenVariables();272 FlattenTrees();273 XmlNode node = base.GetXmlNode(name, document, persistedObjects);274 XmlNode linearRepresentationNode = document.CreateElement("LinearRepresentation");275 foreach(LightWeightFunction f in linearRepresentation) {276 XmlNode entryNode = PersistenceManager.Persist("FunctionType", f.functionType, document, persistedObjects);277 XmlAttribute arityAttribute = document.CreateAttribute("Arity");278 arityAttribute.Value = f.arity+"";279 entryNode.Attributes.Append(arityAttribute);280 if(f.data.Count > 0) {281 XmlAttribute dataAttribute = document.CreateAttribute("Data");282 dataAttribute.Value = GetString(f.data);283 entryNode.Attributes.Append(dataAttribute);284 }285 linearRepresentationNode.AppendChild(entryNode);286 }287 288 node.AppendChild(linearRepresentationNode);289 return node;290 }291 292 public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {293 base.Populate(node, restoredObjects);294 XmlNode linearRepresentationNode = node.SelectSingleNode("LinearRepresentation");295 foreach(XmlNode entryNode in linearRepresentationNode.ChildNodes) {296 LightWeightFunction f = new LightWeightFunction();297 f.arity = byte.Parse(entryNode.Attributes["Arity"].Value, CultureInfo.InvariantCulture);298 if(entryNode.Attributes["Data"]!=null)299 f.data = GetList<double>(entryNode.Attributes["Data"].Value, s => double.Parse(s, CultureInfo.InvariantCulture));300 f.functionType = (IFunction)PersistenceManager.Restore(entryNode, restoredObjects);301 linearRepresentation.Add(f);302 }303 treesExpanded = false;304 variablesExpanded = false;305 }306 307 private string GetString(IEnumerable<double> xs) {308 StringBuilder builder = new StringBuilder();309 foreach(double x in xs) {310 builder.Append(x.ToString("r", CultureInfo.InvariantCulture) + "; ");311 }312 if(builder.Length > 0) builder.Remove(builder.Length - 2, 2);313 return builder.ToString();314 }315 316 private List<T> GetList<T>(string s, Converter<string, T> converter) {317 List<T> result = new List<T>();318 string[] tokens = s.Split(new char[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);319 foreach(string token in tokens) {320 T x = converter(token.Trim());321 result.Add(x);322 }323 return result;324 284 } 325 285 … … 327 287 BakedFunctionTree clone = new BakedFunctionTree(); 328 288 // in case the user (de)serialized the tree between evaluation and selection we have to flatten the tree again. 329 if (treesExpanded) FlattenTrees();330 if (variablesExpanded) FlattenVariables();331 foreach (LightWeightFunction f in linearRepresentation) {289 if (treesExpanded) FlattenTrees(); 290 if (variablesExpanded) FlattenVariables(); 291 foreach (LightWeightFunction f in linearRepresentation) { 332 292 clone.linearRepresentation.Add(f.Clone()); 333 293 } -
trunk/sources/HeuristicLab.GP/3.4/FunctionBase.cs
r1529 r1914 28 28 using HeuristicLab.Constraints; 29 29 using System.Diagnostics; 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 31 31 32 namespace HeuristicLab.GP { … … 34 35 /// is to evaluate all children first. 35 36 /// </summary> 37 [EmptyStorableClass] 36 38 public abstract class FunctionBase : OperatorBase, IFunction { 39 37 40 public const string INITIALIZATION = "Initialization"; 38 41 public const string MANIPULATION = "Manipulation"; -
trunk/sources/HeuristicLab.GP/3.4/GPOperatorGroup.cs
r1529 r1914 29 29 using HeuristicLab.Random; 30 30 using HeuristicLab.Constraints; 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 32 32 33 namespace HeuristicLab.GP { 34 35 [EmptyStorableClass] 33 36 public class GPOperatorGroup : OperatorGroup { 34 37 private Dictionary<IOperator, int> minTreeHeight = new Dictionary<IOperator, int>(); -
trunk/sources/HeuristicLab.GP/3.4/GPOperatorLibrary.cs
r1529 r1914 26 26 using HeuristicLab.Core; 27 27 using System.Xml; 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 29 30 namespace HeuristicLab.GP { 30 public class GPOperatorLibrary : ItemBase, IOperatorLibrary, IEditable 31 public class GPOperatorLibrary : ItemBase, IOperatorLibrary, IEditable { 31 32 // constants for variable names 32 33 internal const string MIN_TREE_HEIGHT = "MinTreeHeight"; … … 34 35 internal const string TICKETS = "Tickets"; 35 36 37 [Storable] 36 38 private GPOperatorGroup group; 37 39 … … 63 65 } 64 66 65 #region persistence66 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {67 XmlNode node = base.GetXmlNode(name, document, persistedObjects);68 node.AppendChild(PersistenceManager.Persist("Group", group, document, persistedObjects));69 return node;70 }71 72 public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {73 base.Populate(node, restoredObjects);74 group = (GPOperatorGroup) PersistenceManager.Restore(node.SelectSingleNode("Group"), restoredObjects);75 }76 #endregion77 78 67 #region IEditable Members 79 68 -
trunk/sources/HeuristicLab.GP/3.4/HeuristicLab.GP-3.4.csproj
r1907 r1914 6 6 <ProductVersion>9.0.30729</ProductVersion> 7 7 <SchemaVersion>2.0</SchemaVersion> 8 <ProjectGuid>{ 1F1CF3ED-374C-4288-995B-93F6B872F571}</ProjectGuid>8 <ProjectGuid>{DA0D505A-2149-4E3C-B220-5724DE26EB59}</ProjectGuid> 9 9 <OutputType>Library</OutputType> 10 10 <AppDesignerFolder>Properties</AppDesignerFolder> 11 11 <RootNamespace>HeuristicLab.GP</RootNamespace> 12 <AssemblyName>HeuristicLab.GP-3. 3</AssemblyName>12 <AssemblyName>HeuristicLab.GP-3.4</AssemblyName> 13 13 <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> 14 14 <FileAlignment>512</FileAlignment> … … 128 128 </ItemGroup> 129 129 <ItemGroup> 130 <ProjectReference Include="..\..\HeuristicLab.Constraints\3. 2\HeuristicLab.Constraints-3.2.csproj">131 <Project>{ FCD62C6F-4793-4593-AE9A-0BDCA256EE99}</Project>132 <Name>HeuristicLab.Constraints-3. 2</Name>130 <ProjectReference Include="..\..\HeuristicLab.Constraints\3.3\HeuristicLab.Constraints-3.3.csproj"> 131 <Project>{19C1E42A-4B48-4EFD-B697-899016F1C198}</Project> 132 <Name>HeuristicLab.Constraints-3.3</Name> 133 133 </ProjectReference> 134 <ProjectReference Include="..\..\HeuristicLab.Core\3. 2\HeuristicLab.Core-3.2.csproj">135 <Project>{ F43B59AB-2B8C-4570-BC1E-15592086517C}</Project>136 <Name>HeuristicLab.Core-3. 2</Name>134 <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj"> 135 <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project> 136 <Name>HeuristicLab.Core-3.3</Name> 137 137 </ProjectReference> 138 <ProjectReference Include="..\..\HeuristicLab.Data\3. 2\HeuristicLab.Data-3.2.csproj">139 <Project>{ F473D9AF-3F09-4296-9F28-3C65118DAFFA}</Project>140 <Name>HeuristicLab.Data-3. 2</Name>138 <ProjectReference Include="..\..\HeuristicLab.Data\3.3\HeuristicLab.Data-3.3.csproj"> 139 <Project>{BBAB9DF5-5EF3-4BA8-ADE9-B36E82114937}</Project> 140 <Name>HeuristicLab.Data-3.3</Name> 141 141 </ProjectReference> 142 <ProjectReference Include="..\..\HeuristicLab.Evolutionary\3. 2\HeuristicLab.Evolutionary-3.2.csproj">143 <Project>{ F5614C53-153C-4A37-A608-121E1C087F07}</Project>144 <Name>HeuristicLab.Evolutionary-3. 2</Name>142 <ProjectReference Include="..\..\HeuristicLab.Evolutionary\3.3\HeuristicLab.Evolutionary-3.3.csproj"> 143 <Project>{25087811-F74C-4128-BC86-8324271DA13E}</Project> 144 <Name>HeuristicLab.Evolutionary-3.3</Name> 145 145 </ProjectReference> 146 <ProjectReference Include="..\..\HeuristicLab.Operators\3.2\HeuristicLab.Operators-3.2.csproj"> 147 <Project>{A9983BA2-B3B2-475E-8E2C-62050B71D1C5}</Project> 148 <Name>HeuristicLab.Operators-3.2</Name> 146 <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj"> 147 <Project>{23DA7FF4-D5B8-41B6-AA96-F0561D24F3EE}</Project> 148 <Name>HeuristicLab.Operators-3.3</Name> 149 </ProjectReference> 150 <ProjectReference Include="..\..\HeuristicLab.Persistence\3.3\HeuristicLab.Persistence-3.3.csproj"> 151 <Project>{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}</Project> 152 <Name>HeuristicLab.Persistence-3.3</Name> 149 153 </ProjectReference> 150 154 <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\HeuristicLab.PluginInfrastructure.csproj"> … … 152 156 <Name>HeuristicLab.PluginInfrastructure</Name> 153 157 </ProjectReference> 154 <ProjectReference Include="..\..\HeuristicLab.Random\3. 2\HeuristicLab.Random-3.2.csproj">155 <Project>{ 47019A74-F7F7-482E-83AA-D3F4F777E879}</Project>156 <Name>HeuristicLab.Random-3. 2</Name>158 <ProjectReference Include="..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj"> 159 <Project>{F4539FB6-4708-40C9-BE64-0A1390AEA197}</Project> 160 <Name>HeuristicLab.Random-3.3</Name> 157 161 </ProjectReference> 158 <ProjectReference Include="..\..\HeuristicLab.Selection\3. 2\HeuristicLab.Selection-3.2.csproj">159 <Project>{ F7CF0571-25CB-43D5-8443-0843A1E2861A}</Project>160 <Name>HeuristicLab.Selection-3. 2</Name>162 <ProjectReference Include="..\..\HeuristicLab.Selection\3.3\HeuristicLab.Selection-3.3.csproj"> 163 <Project>{2C36CD4F-E5F5-43A4-801A-201EA895FE17}</Project> 164 <Name>HeuristicLab.Selection-3.3</Name> 161 165 </ProjectReference> 162 166 </ItemGroup> -
trunk/sources/HeuristicLab.GP/3.4/HeuristicLabGPPlugin.cs
r1529 r1914 26 26 27 27 namespace HeuristicLab.GP { 28 [ClassInfo(Name = "HeuristicLab.GP-3.3")] 29 [PluginFile(Filename = "HeuristicLab.GP-3.3.dll", Filetype = PluginFileType.Assembly)] 30 [Dependency(Dependency = "HeuristicLab.Constraints-3.2")] 31 [Dependency(Dependency = "HeuristicLab.Core-3.2")] 32 [Dependency(Dependency = "HeuristicLab.Data-3.2")] 33 [Dependency(Dependency = "HeuristicLab.Evolutionary-3.2")] 34 [Dependency(Dependency = "HeuristicLab.Operators-3.2")] 35 [Dependency(Dependency = "HeuristicLab.Random-3.2")] 36 [Dependency(Dependency = "HeuristicLab.Selection-3.2")] 28 [ClassInfo(Name = "HeuristicLab.GP-3.4")] 29 [PluginFile(Filename = "HeuristicLab.GP-3.4.dll", Filetype = PluginFileType.Assembly)] 30 [Dependency(Dependency = "HeuristicLab.Constraints-3.3")] 31 [Dependency(Dependency = "HeuristicLab.Core-3.3")] 32 [Dependency(Dependency = "HeuristicLab.Data-3.3")] 33 [Dependency(Dependency = "HeuristicLab.Evolutionary-3.3")] 34 [Dependency(Dependency = "HeuristicLab.Operators-3.3")] 35 [Dependency(Dependency = "HeuristicLab.Random-3.3")] 36 [Dependency(Dependency = "HeuristicLab.Selection-3.3")] 37 [Dependency(Dependency = "HeuristicLab.Persistence-3.3")] 37 38 public class HeuristicLabGPPlugin : PluginBase { 38 39 } -
trunk/sources/HeuristicLab.GP/3.4/ProbabilisticTreeCreator.cs
r1529 r1914 27 27 using HeuristicLab.Random; 28 28 using System.Diagnostics; 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 30 30 31 namespace HeuristicLab.GP { 32 31 33 public class ProbabilisticTreeCreator : OperatorBase { 32 34 private int MAX_TRIES { get { return 100; } } -
trunk/sources/HeuristicLab.GP/3.4/Properties/AssemblyInfo.frame
r1527 r1914 54 54 // You can specify all the values or you can default the Revision and Build Numbers 55 55 // by using the '*' as shown below: 56 [assembly: AssemblyVersion("3. 3.0.$WCREV$")]57 [assembly: AssemblyFileVersion("3. 3.0.$WCREV$")]56 [assembly: AssemblyVersion("3.4.0.$WCREV$")] 57 [assembly: AssemblyFileVersion("3.4.0.$WCREV$")] 58 58 [assembly: AssemblyBuildDate("$WCNOW$")]
Note: See TracChangeset
for help on using the changeset viewer.