Changeset 14834 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblem.cs
- Timestamp:
- 04/10/17 00:27:31 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblem.cs
r14777 r14834 4 4 using System.Collections.Generic; 5 5 using System.Linq; 6 using BenchmarkSuite.Problems; 6 7 using Common; 7 8 using Configuration; 8 9 using Core; 10 using Data.Pool; 9 11 using Encodings.IntegerVectorEncoding; 10 11 using HeuristicLab.BenchmarkSuite; 12 using HeuristicLab.BenchmarkSuite.Problems; 12 using Expressions; 13 13 using HeuristicLab.Data; 14 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 15 using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; 14 using HeuristicLab.Problems.ProgramSynthesis.Push.Erc; 16 15 17 16 using Instances; … … 20 19 using Parameters; 21 20 using Persistence.Default.CompositeSerializers.Storable; 21 using Random; 22 using Stack; 22 23 23 24 [StorableClass] … … 28 29 private readonly PushConfiguration config; 29 30 private PushInterpreterPool pool; 30 private IBenchmarkSuiteDataDescriptor dataDescriptor; 31 private readonly ObjectPool<IRandom> randomPool = new ObjectPool<IRandom>(() => new MersenneTwister()); 32 33 public const string CaseQualitiesScopeParameterName = "CaseQualities"; 31 34 32 35 public PushProblem() { 33 36 config = new PushConfiguration(); 34 pool = new PushInterpreterPool(10000, 4096, 65536, config); 35 37 38 InitData(); 36 39 InitEvents(); 37 40 InitParameters(); 38 Instructions = config;39 41 } 40 42 … … 47 49 : base(original, cloner) { 48 50 config = cloner.Clone(original.config); 49 pool = new PushInterpreterPool(65536, 1024, 65536, config); 50 51 Instructions = config; 52 53 this.InitEvents(); 51 52 InitData(); 53 InitEvents(); 54 54 } 55 55 … … 57 57 // ReSharper disable once UnusedMember.Local 58 58 private void AfterDeserialization() { 59 pool = new PushInterpreterPool(config); 60 Instructions = config; 61 59 InitData(); 62 60 InitEvents(); 61 } 62 63 private void InitData() { 64 pool = new PushInterpreterPool(Environment.ProcessorCount * 2, 4096, 1024, config); 65 66 //var solutionCreator = new PointsBasedPushProgramCreator(config.ErcOptions); 67 //SolutionCreator = solutionCreator; 68 //Encoding.SolutionCreator = solutionCreator; 63 69 } 64 70 … … 68 74 69 75 private void EnabledExpressionsChanged(object sender, EnabledExpressionsChangedEventArgs e) { 70 this.Encoding.Bounds[0, 1] = config.EnabledExpressions.Count - 1;71 this.Encoding.BoundsParameter.Value[0, 1] = config.EnabledExpressions.Count - 1;76 this.Encoding.Bounds[0, 1] = config.EnabledExpressions.Count; 77 this.Encoding.BoundsParameter.Value[0, 1] = config.EnabledExpressions.Count; 72 78 } 73 79 74 80 #region Parameters 75 81 82 private const string DataBoundsParameterName = "DataBounds"; 83 private const string DataParameterName = "Data"; 84 private const string DataParameterDescription = "Program Synthesis"; 85 private const string InstructionsParameterName = "Instructions"; 86 private const string InstructionsParameterDescription = "Enables/Disables Instructions"; 76 87 private const string EvalPushLimitParameterName = "EvalPushLimit"; 77 88 private const string EvalPushLimitParameterDescription = "This is the maximum allowed number of \"executions\" in a single top-level call to the interpreter. The execution of a single Push instruction counts as one execution, as does the processing of a single literal, as does the descent into one layer of parentheses (that is, the processing of the \"(\" counts as one execution)."; 78 private const string MaxP rogramLengthParameterName = "MaxProgramLength";89 private const string MaxPointsInProgramParameterName = "MaxProgramLength"; 79 90 private const string MaxProgramLengthParameterDescription = "This is the maximum size of an item on the CODE stack, expressed as a number of points. A point is an instruction, a literal, or a pair of parentheses."; 80 91 private const string TopLevelPushCodeParameterName = "TopLevelPushCode"; … … 82 93 private const string TopLevelPopCodeParameterName = "TopLevelPopCode"; 83 94 private const string TopLevelPopCodeParameterDescription = "When TRUE, the CODE stack will be popped at the end of top level calls to the interpreter. The default is FALSE."; 84 private const string InstructionsParameterName = "Instructions";85 private const string InstructionsParameterDescription = "Enables/Disables Instructions";86 private const string DataParameterName = "Data";87 private const string DataParameterDescription = "Program Synthesis";88 95 private const string MinRandomIntegerParameterName = "MinRandomInteger"; 89 96 private const string MinRandomIntegerParameterDescription = "The minimum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND."; … … 96 103 private const string NewErcNameProbabilityParameterName = "NewErcNameProbability"; 97 104 private const string NewErcNameProbabilityParameterDescription = "The probability that the selection of the ephemeral random NAME constant for inclusion in randomly generated code will produce a new name."; 98 private const string ErcProbabilityParameterName = "ErcProbability";99 private const string ErcProbabilityParameterDescription = "The probability that the selection of a epheral random literal constant for inclusion in randomly generated code will produce a new literal.";100 105 private const string MaxPointsInRandomInstructionParameterName = "MaxPointsInRandomInstruction"; 101 106 private const string MaxPointsInRandomInstructionParameterDescription = "MaxPointsInRandomInstruction"; 102 private const string DataBoundsParameterName = "DataBounds";103 private const string Max ProgramDepthParameterName = "MaxProgramDepth";104 private const string Max ProgramDepthParameterDescription = "";107 private const string ErcOptionsParameterName = "ERC options"; 108 private const string MaxStringLengthParameterName = "Max. string length"; 109 private const string MaxDepthParameterName = "Max. program recursion"; 105 110 106 111 private void InitParameters() { 107 108 112 Parameters.Add(new FixedValueParameter<DataBounds>(DataBoundsParameterName)); 113 114 Parameters.Add(new ValueParameter<IEnabledExpressionsConfiguration>( 115 InstructionsParameterName, 116 InstructionsParameterDescription, 117 config)); 118 119 Parameters.Add(new ValueParameter<Data>( 120 DataParameterName, 121 DataParameterDescription)); 122 123 Parameters.Add(new FixedValueParameter<ErcOptions>(ErcOptionsParameterName, config.ErcOptions)); 109 124 110 125 Parameters.Add(new FixedValueParameter<IntValue>( … … 114 129 115 130 Parameters.Add(new FixedValueParameter<IntValue>( 116 MaxP rogramLengthParameterName,131 MaxPointsInProgramParameterName, 117 132 MaxProgramLengthParameterDescription, 118 133 new IntValue(config.MaxPointsInProgram))); 134 Encoding.LengthParameter = Parameters[MaxPointsInProgramParameterName] as IFixedValueParameter<IntValue>; 119 135 120 136 Parameters.Add(new FixedValueParameter<BoolValue>( … … 128 144 new BoolValue(config.TopLevelPopCode)) { Hidden = true }); 129 145 130 Parameters.Add(new ValueParameter<IEnabledExpressionsConfiguration>(131 InstructionsParameterName,132 InstructionsParameterDescription));133 134 Parameters.Add(new FixedValueParameter<IntValue>(135 MinRandomIntegerParameterName,136 MinRandomIntegerParameterDescription,137 new IntValue(config.MinRandomInteger)) { Hidden = true });138 139 Parameters.Add(new FixedValueParameter<IntValue>(140 MaxRandomIntegerParameterName,141 MaxRandomIntegerParameterDescription,142 new IntValue(config.MaxRandomInteger)) { Hidden = true });143 144 Parameters.Add(new FixedValueParameter<DoubleValue>(145 MinRandomFloatParameterName,146 MinRandomFloatParameterDescription,147 new DoubleValue(config.MinRandomFloat)) { Hidden = true });148 149 Parameters.Add(new FixedValueParameter<DoubleValue>(150 MaxRandomFloatParameterName,151 MaxRandomFloatParameterDescription,152 new DoubleValue(config.MaxRandomFloat)) { Hidden = true });153 154 Parameters.Add(new FixedValueParameter<PercentValue>(155 NewErcNameProbabilityParameterName,156 NewErcNameProbabilityParameterDescription,157 new PercentValue(config.NewErcNameProbability)) { Hidden = true });158 159 Parameters.Add(new FixedValueParameter<PercentValue>(160 ErcProbabilityParameterName,161 ErcProbabilityParameterDescription,162 new PercentValue(config.ErcProbability)) { Hidden = true });163 164 146 Parameters.Add(new FixedValueParameter<IntValue>( 165 147 MaxPointsInRandomInstructionParameterName, … … 167 149 new IntValue(config.MaxPointsInRandomExpression)) { Hidden = true }); 168 150 169 Parameters.Add( 170 new FixedValueParameter<IntValue>( 171 MaxProgramDepthParameterName, 172 MaxProgramDepthParameterDescription, 173 new IntValue(config.MaxDepth))); 174 175 Parameters.Add(new ValueParameter<Data>( 176 DataParameterName, 177 DataParameterDescription)); 151 Parameters.Add(new FixedValueParameter<IntValue>( 152 MaxStringLengthParameterName, 153 new IntValue(config.MaxStringLength)) { Hidden = true }); 154 155 Parameters.Add(new FixedValueParameter<IntValue>( 156 MaxDepthParameterName, 157 new IntValue(config.MaxDepth)) { Hidden = true }); 158 159 Parameters.Add(new LookupParameter<BoolArray>("Cases", "The training cases that have been successfully executed.")); 160 Parameters.Add(new LookupParameter<DoubleArray>(CaseQualitiesScopeParameterName, "The quality of every single training case for each individual")); 178 161 179 162 Encoding.Bounds[0, 0] = 0; 180 Encoding.Bounds[0, 1] = config.EnabledExpressions.Count - 1;163 Encoding.Bounds[0, 1] = config.EnabledExpressions.Count; 181 164 Encoding.Length = config.MaxPointsInProgram; 182 165 } … … 216 199 } 217 200 201 public IValueParameter<ErcOptions> ErcOptionsParameter 202 { 203 get { return (IValueParameter<ErcOptions>)Parameters[ErcOptionsParameterName]; } 204 } 205 206 public ErcOptions ErcOptions 207 { 208 get { return config.ErcOptions; } 209 set 210 { 211 ErcOptionsParameter.Value = value; 212 config.ErcOptions = value; 213 } 214 } 215 216 /// <summary> 217 /// This is the maximum allowed number of "executions" in a single top-level call to the interpreter. 218 /// The execution of a single Push instruction counts as one execution, as does the processing of a single literal, 219 /// as does the descent into one layer of parentheses (that is, the processing of the "(" counts as one execution). 220 /// When this limit is exceeded the interpreter aborts immediately, leaving its stacks in the states they were in prior 221 /// to the abort (so they may still be examined by a calling program). Whether or not this counts as an "abnormal" 222 /// termination 223 /// is up to the calling program. 224 /// </summary> 218 225 public IValueParameter<IntValue> EvalPushLimitParameter 219 226 { … … 226 233 set 227 234 { 228 this.EvalPushLimitParameter.Value.Value = value;235 EvalPushLimitParameter.Value.Value = value; 229 236 config.EvalPushLimit = value; 230 237 } 231 238 } 232 239 240 /// <summary> 241 /// This is the maximum of depth a push program can have. Expressions, which lead to exceed this limit are interpreted as NOOP. 242 /// </summary> 233 243 public IValueParameter<IntValue> MaxDepthParameter 234 244 { 235 get { return (IValueParameter<IntValue>) this.Parameters[MaxProgramDepthParameterName]; }245 get { return (IValueParameter<IntValue>)Parameters[MaxDepthParameterName]; } 236 246 } 237 247 … … 241 251 set 242 252 { 243 this.MaxDepthParameter.Value.Value = value;253 MaxDepthParameter.Value.Value = value; 244 254 config.MaxDepth = value; 245 255 } 246 256 } 247 257 248 public IValueParameter<IntValue> MaxProgramLengthParameter 249 { 250 get { return (IValueParameter<IntValue>)this.Parameters[MaxProgramLengthParameterName]; } 251 } 252 253 public int MaxProgramLength 258 /// <summary> 259 /// This is the maximum size of an item on the CODE stack, expressed as a number of points. 260 /// A point is an instruction, a literal, or a pair of parentheses. Any instruction that would cause this limit to be 261 /// exceeded 262 /// should instead act as a NOOP, leaving all stacks in the states that they were in before the execution of the 263 /// instruction. 264 /// </summary> 265 public IValueParameter<IntValue> MaxPointsInProgramParameter 266 { 267 get { return (IValueParameter<IntValue>)this.Parameters[MaxPointsInProgramParameterName]; } 268 } 269 270 public int MaxPointsInProgram 254 271 { 255 272 get { return config.MaxPointsInProgram; } 256 273 set 257 274 { 258 this.MaxProgramLengthParameter.Value.Value = value; 259 this.Encoding.LengthParameter.Value.Value = value; 275 MaxPointsInProgramParameter.Value.Value = value; 260 276 config.MaxPointsInProgram = value; 261 277 } 262 278 } 263 279 264 public IValueParameter<BoolValue> TopLevelPushParameter 280 /// <summary> 281 /// The maximum number of points in an expression produced by the CODE.RAND instruction. 282 /// </summary> 283 public IValueParameter<IntValue> MaxPointsInRandomExpressionParameter 284 { 285 get { return (IValueParameter<IntValue>)Parameters[MaxPointsInRandomInstructionParameterName]; } 286 } 287 288 public int MaxPointsInRandomExpression 289 { 290 get { return config.MaxPointsInRandomExpression; } 291 set 292 { 293 MaxPointsInRandomExpressionParameter.Value.Value = value; 294 config.MaxPointsInRandomExpression = value; 295 } 296 } 297 298 /// <summary> 299 /// When TRUE (which is the default), code passed to the top level of the interpreter 300 /// will be pushed onto the CODE stack prior to execution. 301 /// </summary> 302 public IValueParameter<BoolValue> TopLevelPushCodeParameter 265 303 { 266 304 get { return (IValueParameter<BoolValue>)this.Parameters[TopLevelPushCodeParameterName]; } … … 272 310 set 273 311 { 274 this.TopLevelPushParameter.Value.Value = value;312 TopLevelPushCodeParameter.Value.Value = value; 275 313 config.TopLevelPushCode = value; 276 314 } 277 315 } 278 316 279 public IValueParameter<BoolValue> TopLevelPopParameter 317 /// <summary> 318 /// When TRUE, the CODE stack will be popped at the end of top level calls to the interpreter. The default is FALSE. 319 /// </summary> 320 public IValueParameter<BoolValue> TopLevelPopCodeParameter 280 321 { 281 322 get { return (IValueParameter<BoolValue>)this.Parameters[TopLevelPopCodeParameterName]; } … … 287 328 set 288 329 { 289 this.TopLevelPushParameter.Value.Value = value;330 TopLevelPopCodeParameter.Value.Value = value; 290 331 config.TopLevelPopCode = value; 291 332 } 292 333 } 293 334 294 public IValueParameter<IntValue> MinRandomIntegerParameter 295 { 296 get { return (IValueParameter<IntValue>)this.Parameters[MinRandomIntegerParameterName]; } 297 } 298 299 public int MinRandomInteger 300 { 301 get { return config.MinRandomInteger; } 302 set 303 { 304 this.MinRandomIntegerParameter.Value.Value = value; 305 config.MinRandomInteger = value; 306 } 307 } 308 309 public IValueParameter<IntValue> MaxRandomIntegerParameter 310 { 311 get { return (IValueParameter<IntValue>)this.Parameters[MaxRandomIntegerParameterName]; } 312 } 313 314 public int MaxRandomInteger 315 { 316 get { return config.MaxRandomInteger; } 317 set 318 { 319 this.MaxRandomIntegerParameter.Value.Value = value; 320 config.MaxRandomInteger = value; 321 } 322 } 323 324 public IValueParameter<DoubleValue> MinRandomFloatParameter 325 { 326 get { return (IValueParameter<DoubleValue>)this.Parameters[MinRandomFloatParameterName]; } 327 } 328 329 public double MinRandomFloat 330 { 331 get { return config.MinRandomFloat; } 332 set 333 { 334 this.MinRandomFloatParameter.Value.Value = value; 335 config.MinRandomFloat = value; 336 } 337 } 338 339 public IValueParameter<DoubleValue> MaxRandomFloatParameter 340 { 341 get { return (IValueParameter<DoubleValue>)this.Parameters[MaxRandomFloatParameterName]; } 342 } 343 344 public double MaxRandomFloat 345 { 346 get { return config.MaxRandomFloat; } 347 set 348 { 349 this.MaxRandomFloatParameter.Value.Value = value; 350 config.MaxRandomFloat = value; 351 } 352 } 353 354 public IValueParameter<PercentValue> NewErcNameProbabilityParameter 355 { 356 get { return (IValueParameter<PercentValue>)this.Parameters[NewErcNameProbabilityParameterName]; } 357 } 358 359 public double NewErcNameProbability 360 { 361 get { return config.NewErcNameProbability; } 362 set 363 { 364 this.NewErcNameProbabilityParameter.Value.Value = value; 365 config.NewErcNameProbability = value; 366 } 367 } 368 369 public IValueParameter<PercentValue> ErcProbabilityParameter 370 { 371 get { return (IValueParameter<PercentValue>)this.Parameters[ErcProbabilityParameterName]; } 372 } 373 374 public double ErcProbability 375 { 376 get { return config.ErcProbability; } 377 set 378 { 379 this.ErcProbabilityParameter.Value.Value = value; 380 config.ErcProbability = value; 381 } 382 } 383 384 public IValueParameter<IntValue> MaxPointsInRandomInstructionParameter 385 { 386 get { return (IValueParameter<IntValue>)Parameters[MaxPointsInRandomInstructionParameterName]; } 387 } 388 389 public int MaxPointsInRandomInstruction 390 { 391 get { return config.MaxPointsInRandomExpression; } 392 set 393 { 394 this.MaxPointsInRandomInstructionParameter.Value.Value = value; 395 config.MaxPointsInRandomExpression = value; 396 } 397 } 398 335 public IValueParameter<IntValue> MaxStringLengthParameter 336 { 337 get { return (IValueParameter<IntValue>)Parameters[MaxStringLengthParameterName]; } 338 } 339 340 public int MaxStringLength 341 { 342 get { return config.MaxStringLength; } 343 set 344 { 345 MaxStringLengthParameter.Value.Value = value; 346 config.MaxStringLength = value; 347 } 348 } 399 349 #endregion 400 350 … … 414 364 if (!results.ContainsKey(bestSolutionResultName)) { 415 365 results.Add(new Result(bestSolutionResultName, solution)); 416 } else if (((PushSolution)results[bestSolutionResultName].Value).Quality < qualities[bestIdx]) { 417 results[bestSolutionResultName].Value = solution; 366 } else { 367 var currentBestQuality = ((PushSolution)results[bestSolutionResultName].Value).Quality; 368 369 if (Maximization && currentBestQuality < bestQuality || 370 !Maximization && currentBestQuality > bestQuality) { 371 results[bestSolutionResultName].Value = solution; 372 } 418 373 } 419 374 } 420 375 421 376 public override double Evaluate(Individual individual, IRandom random) { 422 return PushEvaluator.Evaluate(individual, pool, random, Data, DataBounds.TrainingRange.Start, DataBounds.TrainingRange.End); 377 var program = individual.ToPushProgram(config, randomPool); 378 var result = PushEvaluator.Evaluate( 379 program, 380 pool, 381 random, 382 Data, 383 DataBounds.TrainingRange.Start, 384 DataBounds.TrainingRange.End); 385 386 individual.SetScopeValue(CaseQualitiesScopeParameterName, new DoubleArray(result.ExampleQualities)); 387 388 return result.TotalQuality; 423 389 } 424 390 … … 426 392 Data = data; 427 393 BestKnownQuality = data.BestResult; 428 MaxP rogramLength= data.MaxSize;394 MaxPointsInProgram = data.MaxSize; 429 395 EvalPushLimit = data.EvalLimit; 430 396 431 config.EnabledExpressions = (IList<string>)ExpressionTable.GetE nabledExpressionsByStackTypes((StackTypes)data.EnabledDataTypes);397 config.EnabledExpressions = (IList<string>)ExpressionTable.GetExpressionsByStackTypes((StackTypes)data.EnabledDataTypes); 432 398 433 399 // update enabled stack types 434 400 foreach (var stackType in ExpressionTable.StackTypeToNamesTable.Keys) { 435 var enable dStackExpressions = config.EnabledExpressions.Intersect(ExpressionTable.StackTypeToNamesTable[stackType]);436 config.SetStack(stackType, enable dStackExpressions.Any());401 var enable = config.EnabledExpressions.Intersect(ExpressionTable.StackTypeToNamesTable[stackType]).Any(); 402 config.SetStack(stackType, enable); 437 403 } 438 404 439 405 Encoding.Bounds[0, 0] = 0; 440 Encoding.Bounds[0, 1] = config.EnabledExpressions.Count - 1;406 Encoding.Bounds[0, 1] = config.EnabledExpressions.Count; 441 407 Encoding.Length = config.MaxPointsInProgram; 442 408 443 // TODO444 // data.MaxGenerations445 // data.ProgEvalBudget446 447 409 DataBounds.TrainingRange.Start = 0; 448 DataBounds.TrainingRange.End = Data.OriginalTrainingCount - 1;410 DataBounds.TrainingRange.End = Data.OriginalTrainingCount; 449 411 DataBounds.TestRange.Start = Data.OriginalTrainingCount; 450 412 DataBounds.TestRange.End = Data.OriginalTrainingCount + Data.OriginalTestCount;
Note: See TracChangeset
for help on using the changeset viewer.