Changeset 16196
- Timestamp:
- 09/28/18 16:52:05 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.PGE/3.3/PGE.cs
r16195 r16196 2 2 using System.Runtime.InteropServices; 3 3 using System.Threading; 4 using HeuristicLab.Common; // required for parameters collection5 using HeuristicLab.Core; // required for parameters collection6 using HeuristicLab.Data; // IntValue, ...7 using HeuristicLab.Optimization; // BasicAlgorithm4 using HeuristicLab.Common; 5 using HeuristicLab.Core; 6 using HeuristicLab.Data; 7 using HeuristicLab.Optimization; 8 8 using HeuristicLab.Parameters; 9 9 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 10 using HeuristicLab.Problems.DataAnalysis; 11 using HeuristicLab.Random; // MersenneTwister 10 using HeuristicLab.Problems.DataAnalysis; 12 11 13 12 namespace PGE { 14 // each HL item needs to have a name and a description (BasicAlgorithm is an Item)15 // The name and description of items is shown in the GUI16 13 [Item(Name = "Priorizied Grammar Enumeration (PGE)", Description = "Priorizied grammar enumeration algorithm. Worm, T. and Chiu K., 'Prioritized Grammar Enumeration: Symbolic Regression by Dynamic Programming'. GECCO 2013")] 17 14 18 // If the algorithm should be shown in the "New..." dialog it must be creatable. Entries in the new dialog are grouped to categories and ordered by priorities19 15 [Creatable(Category = CreatableAttribute.Categories.Algorithms, Priority = 999)] 20 16 21 [StorableClass] // for persistence (storing your algorithm to a files or transfer to HeuristicLab.Hive)17 [StorableClass] 22 18 public unsafe class PGE : BasicAlgorithm { 23 19 … … 46 42 public new RegressionProblem Problem { get { return (RegressionProblem)base.Problem; } } 47 43 48 #region parameters 49 // If an algorithm has parameters then we usually also add properties to access these parameters. 50 // This is not strictly required but considered good shape. 44 #region parameter names 45 private static readonly string MaxIterationsParameterName = "MaxIterations"; 46 private static readonly string NumberOfUsableVariablesParameterName = "umberOfUsableVariables"; 47 private static readonly string MaxGenParameterName = "MaxGen"; 48 private static readonly string EvalrCountParameterName = "EvalrCount"; 49 private static readonly string MaxSizeParameterName = "MaxSize"; 50 private static readonly string MinSizeParameterName = "MinSize"; 51 private static readonly string MaxDepthParameterName = "MaxDepth"; 52 private static readonly string SearchVarParameterName = "SearchVar"; 53 private static readonly string MinDepthParameterName = "MinDepth"; 54 private static readonly string PgeRptEpochParameterName = "PgeRptEpoch"; 55 private static readonly string PgeRptCountParameterName = "PgeRptCount"; 56 private static readonly string PgeArchiveCapParameterName = "PgeArchiveCap"; 57 private static readonly string PeelCntParameterName = "PeelCnt"; 58 private static readonly string ZeroEpsilonParameterName = "ZeroEpsilon"; 59 private static readonly string HitRatioParameterName = "HitRatio"; 60 private static readonly string IndependedNamesParameterName = "IndependedNames"; 61 private static readonly string DependedNamesParameterName = "DependedNames"; 62 private static readonly string InitMethodParameterName = "InitMethod"; 63 private static readonly string GrowMethodParameterName = "GrowMethod"; 64 private static readonly string RootsParameterName = "Roots"; 65 private static readonly string NodesParameterName = "Nodes"; 66 private static readonly string NonTrigParameterName = "NonTrig"; 67 private static readonly string LeafsParameterName = "Leafs"; 68 69 #endregion 70 71 #region parameters 51 72 private IFixedValueParameter<IntValue> MaxIterationsParameter { 52 get { return (IFixedValueParameter<IntValue>)Parameters[ "MaxIterations"]; }73 get { return (IFixedValueParameter<IntValue>)Parameters[MaxIterationsParameterName]; } 53 74 } 54 75 public int MaxIterations { … … 57 78 } 58 79 59 private IFixedValueParameter<IntValue> nUsableVarsParameter{60 get { return (IFixedValueParameter<IntValue>)Parameters[ "nUsableVars"]; }80 private IFixedValueParameter<IntValue> NumberOfUsableVariables { 81 get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfUsableVariablesParameterName]; } 61 82 } 62 public int nUsableVars {63 get { return nUsableVarsParameter.Value.Value; }64 set { nUsableVarsParameter.Value.Value = value; }83 public int NumberOfUseableVariables { 84 get { return NumberOfUsableVariables.Value.Value; } 85 set { NumberOfUsableVariables.Value.Value = value; } 65 86 } 66 87 67 88 private IFixedValueParameter<IntValue> MaxGenParameter { 68 get { return (IFixedValueParameter<IntValue>)Parameters[ "MaxGen"]; }89 get { return (IFixedValueParameter<IntValue>)Parameters[MaxGenParameterName]; } 69 90 } 70 91 public int MaxGen { … … 74 95 75 96 private IFixedValueParameter<IntValue> EvalrCountParameter { 76 get { return (IFixedValueParameter<IntValue>)Parameters[ "EvalrCount"]; }97 get { return (IFixedValueParameter<IntValue>)Parameters[EvalrCountParameterName]; } 77 98 } 78 99 public int EvalrCount { … … 82 103 83 104 private IFixedValueParameter<IntValue> MaxSizeParameter { 84 get { return (IFixedValueParameter<IntValue>)Parameters[ "MaxSize"]; }105 get { return (IFixedValueParameter<IntValue>)Parameters[MaxSizeParameterName]; } 85 106 } 86 107 public int MaxSize { … … 90 111 91 112 private IFixedValueParameter<IntValue> MinSizeParameter { 92 get { return (IFixedValueParameter<IntValue>)Parameters[ "MinSize"]; }113 get { return (IFixedValueParameter<IntValue>)Parameters[MinSizeParameterName]; } 93 114 } 94 115 public int MinSize { … … 98 119 99 120 private IFixedValueParameter<IntValue> MaxDepthParameter { 100 get { return (IFixedValueParameter<IntValue>)Parameters[ "MaxDepth"]; }121 get { return (IFixedValueParameter<IntValue>)Parameters[MaxDepthParameterName]; } 101 122 } 102 123 public int MaxDepth { … … 106 127 107 128 private IFixedValueParameter<IntValue> SearchVarParameter { 108 get { return (IFixedValueParameter<IntValue>)Parameters[ "SearchVar"]; }129 get { return (IFixedValueParameter<IntValue>)Parameters[SearchVarParameterName]; } 109 130 } 110 131 public int SearchVar { … … 114 135 115 136 private IFixedValueParameter<IntValue> MinDepthParameter { 116 get { return (IFixedValueParameter<IntValue>)Parameters[ "MinDepth"]; }137 get { return (IFixedValueParameter<IntValue>)Parameters[MinDepthParameterName]; } 117 138 } 118 139 public int MinDepth { … … 122 143 123 144 private IFixedValueParameter<IntValue> PgeRptEpochParameter { 124 get { return (IFixedValueParameter<IntValue>)Parameters[ "PgeRptEpoch"]; }145 get { return (IFixedValueParameter<IntValue>)Parameters[PgeRptEpochParameterName]; } 125 146 } 126 147 public int PgeRptEpoch { … … 130 151 131 152 private IFixedValueParameter<IntValue> PgeRptCountParameter { 132 get { return (IFixedValueParameter<IntValue>)Parameters[ "PgeRptCount"]; }153 get { return (IFixedValueParameter<IntValue>)Parameters[PgeRptCountParameterName]; } 133 154 } 134 155 public int PgeRptCount { … … 138 159 139 160 private IFixedValueParameter<IntValue> PgeArchiveCapParameter { 140 get { return (IFixedValueParameter<IntValue>)Parameters[ "PgeArchiveCap"]; }161 get { return (IFixedValueParameter<IntValue>)Parameters[PgeArchiveCapParameterName]; } 141 162 } 142 163 public int PgeArchiveCap { … … 146 167 147 168 private IFixedValueParameter<IntValue> PeelCntParameter { 148 get { return (IFixedValueParameter<IntValue>)Parameters[ "PeelCnt"]; }169 get { return (IFixedValueParameter<IntValue>)Parameters[PeelCntParameterName]; } 149 170 } 150 171 public int PeelCnt { … … 154 175 155 176 private IFixedValueParameter<DoubleValue> ZeroEpsilonParameter { 156 get { return (IFixedValueParameter<DoubleValue>)Parameters[ "ZeroEpsilon"]; }177 get { return (IFixedValueParameter<DoubleValue>)Parameters[ZeroEpsilonParameterName]; } 157 178 } 158 179 public double ZeroEpsilon { … … 162 183 163 184 private IFixedValueParameter<DoubleValue> HitRatioParameter { 164 get { return (IFixedValueParameter<DoubleValue>)Parameters[ "HitRatio"]; }185 get { return (IFixedValueParameter<DoubleValue>)Parameters[HitRatioParameterName]; } 165 186 } 166 187 public double HitRatio { … … 170 191 171 192 private IFixedValueParameter<StringValue> IndependedNamesParameter { 172 get { return (IFixedValueParameter<StringValue>)Parameters[ "IndependedNames"]; }193 get { return (IFixedValueParameter<StringValue>)Parameters[IndependedNamesParameterName]; } 173 194 } 174 195 public string IndependedNames { … … 178 199 179 200 private IFixedValueParameter<StringValue> DependedNamesParameter { 180 get { return (IFixedValueParameter<StringValue>)Parameters[ "DependedNames"]; }201 get { return (IFixedValueParameter<StringValue>)Parameters[DependedNamesParameterName]; } 181 202 } 182 203 public string DependedNames { … … 186 207 187 208 private IFixedValueParameter<StringValue> InitMethodParameter { 188 get { return (IFixedValueParameter<StringValue>)Parameters[ "InitMethod"]; }209 get { return (IFixedValueParameter<StringValue>)Parameters[InitMethodParameterName]; } 189 210 } 190 211 public string InitMethod { … … 194 215 195 216 private IFixedValueParameter<StringValue> GrowMethodParameter { 196 get { return (IFixedValueParameter<StringValue>)Parameters[ "GrowMethod"]; }217 get { return (IFixedValueParameter<StringValue>)Parameters[GrowMethodParameterName]; } 197 218 } 198 219 public string GrowMethod { … … 202 223 203 224 private IFixedValueParameter<StringValue> RootsParameter { 204 get { return (IFixedValueParameter<StringValue>)Parameters[ "Roots"]; }225 get { return (IFixedValueParameter<StringValue>)Parameters[RootsParameterName]; } 205 226 } 206 227 public string Roots { … … 210 231 211 232 private IFixedValueParameter<StringValue> NodesParameter { 212 get { return (IFixedValueParameter<StringValue>)Parameters[ "Nodes"]; }233 get { return (IFixedValueParameter<StringValue>)Parameters[NodesParameterName]; } 213 234 } 214 235 public string Nodes { … … 218 239 219 240 private IFixedValueParameter<StringValue> NonTrigParameter { 220 get { return (IFixedValueParameter<StringValue>)Parameters[ "NonTrig"]; }241 get { return (IFixedValueParameter<StringValue>)Parameters[NonTrigParameterName]; } 221 242 } 222 243 public string NonTrig { … … 226 247 227 248 private IFixedValueParameter<StringValue> LeafsParameter { 228 get { return (IFixedValueParameter<StringValue>)Parameters[ "Leafs"]; }249 get { return (IFixedValueParameter<StringValue>)Parameters[LeafsParameterName]; } 229 250 } 230 251 public string Leafs { … … 234 255 #endregion 235 256 236 // createable items must have a default ctor237 257 public PGE() { 238 258 // algorithm parameters are shown in the GUI 239 Parameters.Add(new FixedValueParameter<IntValue>( "MaxIterations", new IntValue(50)));240 Parameters.Add(new FixedValueParameter<IntValue>( "SearchVar", new IntValue(0)));241 Parameters.Add(new FixedValueParameter<IntValue>( "MinDepth", new IntValue(1)));242 Parameters.Add(new FixedValueParameter<IntValue>( "MaxDepth", new IntValue(6)));243 Parameters.Add(new FixedValueParameter<IntValue>( "MinSize", new IntValue(4)));244 Parameters.Add(new FixedValueParameter<IntValue>( "MaxSize", new IntValue(50)));245 Parameters.Add(new FixedValueParameter<IntValue>( "EvalrCount", new IntValue(2)));246 Parameters.Add(new FixedValueParameter<IntValue>( "PeelCnt", new IntValue(3)));247 Parameters.Add(new FixedValueParameter<IntValue>( "PgeArchiveCap", new IntValue(256)));248 Parameters.Add(new FixedValueParameter<IntValue>( "PgeRptCount", new IntValue(20)));249 Parameters.Add(new FixedValueParameter<IntValue>( "PgeRptEpoch", new IntValue(1)));250 Parameters.Add(new FixedValueParameter<IntValue>( "MaxGen", new IntValue(200)));251 Parameters.Add(new FixedValueParameter<IntValue>( "nUsableVars", new IntValue(5)));259 Parameters.Add(new FixedValueParameter<IntValue>(MaxIterationsParameterName, new IntValue(50))); 260 Parameters.Add(new FixedValueParameter<IntValue>(SearchVarParameterName, new IntValue(0))); 261 Parameters.Add(new FixedValueParameter<IntValue>(MinDepthParameterName, new IntValue(1))); 262 Parameters.Add(new FixedValueParameter<IntValue>(MaxDepthParameterName, new IntValue(6))); 263 Parameters.Add(new FixedValueParameter<IntValue>(MinSizeParameterName, new IntValue(4))); 264 Parameters.Add(new FixedValueParameter<IntValue>(MaxSizeParameterName, new IntValue(50))); 265 Parameters.Add(new FixedValueParameter<IntValue>(EvalrCountParameterName, new IntValue(2))); 266 Parameters.Add(new FixedValueParameter<IntValue>(PeelCntParameterName, new IntValue(3))); 267 Parameters.Add(new FixedValueParameter<IntValue>(PgeArchiveCapParameterName, new IntValue(256))); 268 Parameters.Add(new FixedValueParameter<IntValue>(PgeRptCountParameterName, new IntValue(20))); 269 Parameters.Add(new FixedValueParameter<IntValue>(PgeRptEpochParameterName, new IntValue(1))); 270 Parameters.Add(new FixedValueParameter<IntValue>(MaxGenParameterName, new IntValue(200))); 271 Parameters.Add(new FixedValueParameter<IntValue>(NumberOfUsableVariablesParameterName, new IntValue(5))); 252 272 253 Parameters.Add(new FixedValueParameter<StringValue>( "IndependedNames", new StringValue("x y z v w")));254 Parameters.Add(new FixedValueParameter<StringValue>( "DependedNames", new StringValue("f(xs)")));255 Parameters.Add(new FixedValueParameter<StringValue>( "InitMethod", new StringValue("method1")));256 Parameters.Add(new FixedValueParameter<StringValue>( "GrowMethod", new StringValue("method1")));273 Parameters.Add(new FixedValueParameter<StringValue>(IndependedNamesParameterName, new StringValue("x y z v w"))); // TODO 274 Parameters.Add(new FixedValueParameter<StringValue>(DependedNamesParameterName, new StringValue("f(xs)"))); // TODO 275 Parameters.Add(new FixedValueParameter<StringValue>(InitMethodParameterName, new StringValue("method1"))); // TODO Dropdown 276 Parameters.Add(new FixedValueParameter<StringValue>(GrowMethodParameterName, new StringValue("method1"))); 257 277 258 Parameters.Add(new FixedValueParameter<StringValue>( "Roots", new StringValue("Add")));259 Parameters.Add(new FixedValueParameter<StringValue>( "Nodes", new StringValue("Add Mul")));260 Parameters.Add(new FixedValueParameter<StringValue>( "NonTrig", new StringValue("Add Mul")));261 Parameters.Add(new FixedValueParameter<StringValue>( "Leafs", new StringValue("Var ConstantF")));278 Parameters.Add(new FixedValueParameter<StringValue>(RootsParameterName, new StringValue("Add"))); 279 Parameters.Add(new FixedValueParameter<StringValue>(NodesParameterName, new StringValue("Add Mul"))); 280 Parameters.Add(new FixedValueParameter<StringValue>(NonTrigParameterName, new StringValue("Add Mul"))); 281 Parameters.Add(new FixedValueParameter<StringValue>(LeafsParameterName, new StringValue("Var ConstantF"))); 262 282 263 Parameters.Add(new FixedValueParameter<DoubleValue>( "ZeroEpsilon", new DoubleValue(0.00001)));264 Parameters.Add(new FixedValueParameter<DoubleValue>( "HitRatio", new DoubleValue(0.01)));283 Parameters.Add(new FixedValueParameter<DoubleValue>(ZeroEpsilonParameterName, new DoubleValue(0.00001))); 284 Parameters.Add(new FixedValueParameter<DoubleValue>(HitRatioParameterName, new DoubleValue(0.01))); 265 285 } 266 286 … … 329 349 addTrainData(CindepNames, CdepndNames, TrainData, nTrainData); 330 350 331 IntPtr UsableVars = GetUsableVars( nUsableVars);351 IntPtr UsableVars = GetUsableVars(NumberOfUseableVariables); 332 352 333 353 Console.WriteLine("InitSearch"); … … 335 355 336 356 Console.WriteLine("InitTree"); 337 initTreeParams(Croots, Cnodes, CnonTrig, Cleafs, UsableVars, nUsableVars, MaxSize, MinSize, MaxDepth, MinDepth);357 initTreeParams(Croots, Cnodes, CnonTrig, Cleafs, UsableVars, NumberOfUseableVariables, MaxSize, MinSize, MaxDepth, MinDepth); 338 358 339 359 Console.WriteLine("InitProblem"); … … 344 364 var curItersItem = new IntValue(); 345 365 346 double[] coeffmem = new double[ nUsableVars];347 IntPtr Coeff = Marshal.AllocHGlobal(sizeof(Double) * nUsableVars);348 Marshal.Copy(coeffmem, 0, Coeff, nUsableVars);366 double[] coeffmem = new double[NumberOfUseableVariables]; 367 IntPtr Coeff = Marshal.AllocHGlobal(sizeof(Double) * NumberOfUseableVariables); 368 Marshal.Copy(coeffmem, 0, Coeff, NumberOfUseableVariables); 349 369 350 370 for (int iter = 1; iter <= MaxIter; iter++) {
Note: See TracChangeset
for help on using the changeset viewer.