Changeset 14875 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem
- Timestamp:
- 04/18/17 01:15:25 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/IndividualMapper.cs
r14834 r14875 44 44 private static PushProgram ToPushProgram(this IntegerVector vector, IReadOnlyPushConfiguration config, IRandom random) { 45 45 var expressions = new Expression[vector.Length]; 46 var enabledExpressions = config.EnabledExpressions; 47 var ercOptions = config.ErcOptions; 46 48 47 for (var i = 0; i < vector.Length; i++) { 48 expressions[i] = CodeGeneratorUtils.CreateExpressionOrErc(vector[i], random, config.EnabledExpressions, config.ErcOptions); 49 } 49 for (var i = 0; i < vector.Length; i++) 50 expressions[i] = CodeGeneratorUtils.CreateExpressionOrErc(vector[i], random, enabledExpressions, ercOptions); 50 51 51 52 return new PushProgram(expressions); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushEvaluator.cs
r14834 r14875 3 3 4 4 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem { 5 5 6 using HeuristicLab.BenchmarkSuite; 6 7 using HeuristicLab.BenchmarkSuite.Problems; 8 using HeuristicLab.Common; 7 9 using HeuristicLab.Core; 8 10 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; … … 13 15 public static double Evaluate(IPushInterpreter interpreter, PushProgram program, Example example, double worstResult) { 14 16 interpreter.BooleanStack.Push(example.InputBoolean); 15 interpreter.IntegerStack.Push(example.InputInt );17 interpreter.IntegerStack.Push(example.InputInteger); 16 18 interpreter.FloatStack.Push(example.InputFloat); 17 19 18 20 interpreter.Run(program); 19 21 20 var result = GetDiff(example.OutputInt, interpreter.IntegerStack, worstResult, LongDiffer) 21 + GetDiff(example.OutputFloat, interpreter.FloatStack, worstResult, DoubleDiffer) 22 + GetDiff(example.OutputBoolean, interpreter.BooleanStack, worstResult, BooleanDiffer); 22 var result = GetDiff(example.OutputInteger, interpreter.IntegerStack, worstResult, IntegerDiffer) 23 + GetDiff(example.OutputFloat, interpreter.FloatStack, worstResult, FloatDiffer) 24 + GetDiff(example.OutputBoolean, interpreter.BooleanStack, worstResult, BooleanDiffer) 25 + GetDiff(example.OutputString, interpreter.StringStack, worstResult, StringDiffer) 26 + GetDiff(example.OutputChar, interpreter.CharStack, worstResult, CharDiffer) 27 + GetVectorDiff(example.OutputIntegerVector, interpreter.IntegerVectorStack, worstResult, (a, b) => VectorDiffer(a, b, IntegerDiffer)) 28 + GetVectorDiff(example.OutputFloatVector, interpreter.FloatVectorStack, worstResult, (a, b) => VectorDiffer(a, b, FloatDiffer)) 29 + GetVectorDiff(example.OutputStringVector, interpreter.StringVectorStack, worstResult, (a, b) => VectorDiffer(a, b, StringDiffer)); 23 30 24 31 return result; 25 32 } 26 33 27 public static EvaluationResult Evaluate(PushProgram program, PushInterpreterPool pool, IRandom random, Data data, int startIndex, int endIndex) {34 public static EvaluationResult Evaluate(PushProgram program, PushInterpreterPool pool, IRandom random, ProblemData data, int startIndex, int endIndex) { 28 35 var length = endIndex - startIndex; 29 36 if (length <= 0) return null; … … 45 52 } 46 53 47 //public static EvaluationResult Evaluate(Individual individual, PushInterpreterPool pool, IRandom random, Data data, int startIndex, int endIndex) { 48 // var program = individual.ToPushProgram(pool.PushGpConfiguration); 49 // return Evaluate(program, pool, random, data, startIndex, endIndex); 50 //} 51 52 private static double DoubleDiffer(double a, double b) { 54 private static double FloatDiffer(double a, double b) { 53 55 var result = a - b; 54 56 55 if (result == double.MinValue|| double.IsPositiveInfinity(result) || double.IsNaN(result))57 if (result.IsAlmost(double.MinValue) || double.IsPositiveInfinity(result) || double.IsNaN(result)) 56 58 return double.MaxValue; 57 59 58 if (result == double.MaxValue|| double.IsNegativeInfinity(result))60 if (result.IsAlmost(double.MaxValue) || double.IsNegativeInfinity(result)) 59 61 return double.MinValue; 60 62 … … 62 64 } 63 65 64 private static double LongDiffer(long a, long b) {66 private static double IntegerDiffer(long a, long b) { 65 67 var result = a - b; 66 68 … … 70 72 private static double BooleanDiffer(bool a, bool b) { 71 73 return a && b ? 0 : a || b ? 1 : 2; 74 } 75 76 private static double StringDiffer(string a, string b) { 77 return LevenshteinDistance(a, b); 78 } 79 80 private static double CharDiffer(char a, char b) { 81 return IntegerDiffer(a, b); 82 } 83 84 private static double VectorDiffer<T>(IReadOnlyList<T> a, IReadOnlyList<T> b, Func<T, T, double> differ) { 85 var length = Math.Min(a.Count, b.Count); 86 var result = 0d; 87 88 for (var i = 0; i < length; i++) { 89 result += differ(a[i], b[i]); 90 } 91 92 if (a.Count > b.Count) { 93 var remainingLength = a.Count - length; 94 for (var i = 0; i < remainingLength; i++) 95 result += differ(a[i], default(T)); 96 } 97 98 return result; 99 } 100 101 private static double GetVectorDiff<T>(IReadOnlyList<IReadOnlyList<T>> estimated, IPushStack<List<T>> resultStack, double worstResult, Func<IReadOnlyList<T>, IReadOnlyList<T>, double> differ) 102 where T : IComparable { 103 if (estimated.Count == 0) return 0d; 104 105 var diff = 0d; 106 var comparableLength = 0; 107 108 if (!resultStack.IsEmpty) { 109 var count = Math.Min(estimated.Count, resultStack.Count); 110 var result = resultStack.Peek(count); 111 comparableLength = Math.Min(estimated.Count, result.Length); 112 113 for (var i = 0; i < comparableLength; i++) { 114 diff += Math.Min(differ(estimated[i], result[i]), worstResult); 115 } 116 } 117 118 var emptyTArray = new T[0]; 119 for (var i = comparableLength; i < estimated.Count - comparableLength; i++) { 120 diff += differ(estimated[i], emptyTArray); 121 } 122 123 return diff; 72 124 } 73 125 … … 95 147 return diff; 96 148 } 149 150 /// <summary> 151 /// https://www.dotnetperls.com/levenshtein 152 /// </summary> 153 /// <param name="s"></param> 154 /// <param name="t"></param> 155 /// <returns></returns> 156 private static int LevenshteinDistance(string s, string t) { 157 if (s == null && t == null) return 0; 158 if (s == null) return t.Length; 159 if (t == null) return s.Length; 160 161 int n = s.Length; 162 int m = t.Length; 163 int[,] d = new int[n + 1, m + 1]; 164 165 // Step 1 166 if (n == 0) { 167 return m; 168 } 169 170 if (m == 0) { 171 return n; 172 } 173 174 // Step 2 175 for (int i = 0; i <= n; d[i, 0] = i++) { 176 } 177 178 for (int j = 0; j <= m; d[0, j] = j++) { 179 } 180 181 // Step 3 182 for (int i = 1; i <= n; i++) { 183 //Step 4 184 for (int j = 1; j <= m; j++) { 185 // Step 5 186 int cost = (t[j - 1] == s[i - 1]) ? 0 : 1; 187 188 // Step 6 189 d[i, j] = Math.Min( 190 Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), 191 d[i - 1, j - 1] + cost); 192 } 193 } 194 // Step 7 195 return d[n, m]; 196 } 97 197 } 98 198 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblem.cs
r14834 r14875 12 12 using Expressions; 13 13 using HeuristicLab.Data; 14 using HeuristicLab.Problems.ProgramSynthesis. Push.Erc;14 using HeuristicLab.Problems.ProgramSynthesis.Base.Erc; 15 15 16 16 using Instances; … … 25 25 [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 180)] 26 26 [Item("Push Problem", "")] 27 public class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IProblemInstanceConsumer< Data> {27 public class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IProblemInstanceConsumer<ProblemData> { 28 28 [Storable] 29 29 private readonly PushConfiguration config; … … 32 32 33 33 public const string CaseQualitiesScopeParameterName = "CaseQualities"; 34 private const string BestTrainingSolutionResultName = "Best Solution"; 35 private const string TestQualityResultName = "Test Quality"; 34 36 35 37 public PushProblem() { … … 63 65 private void InitData() { 64 66 pool = new PushInterpreterPool(Environment.ProcessorCount * 2, 4096, 1024, config); 65 66 //var solutionCreator = new PointsBasedPushProgramCreator(config.ErcOptions);67 //SolutionCreator = solutionCreator;68 //Encoding.SolutionCreator = solutionCreator;69 67 } 70 68 … … 93 91 private const string TopLevelPopCodeParameterName = "TopLevelPopCode"; 94 92 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."; 95 private const string MinRandomIntegerParameterName = "MinRandomInteger";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.";97 private const string MaxRandomIntegerParameterName = "MaxRandomInteger";98 private const string MaxRandomIntegerParameterDescription = "The maximum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.";99 private const string MinRandomFloatParameterName = "MinRandomFloat";100 private const string MinRandomFloatParameterDescription = "The minimum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.";101 private const string MaxRandomFloatParameterName = "MaxRandomFloat";102 private const string MaxRandomFloatParameterDescription = "The maximum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.";103 private const string NewErcNameProbabilityParameterName = "NewErcNameProbability";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.";105 93 private const string MaxPointsInRandomInstructionParameterName = "MaxPointsInRandomInstruction"; 106 94 private const string MaxPointsInRandomInstructionParameterDescription = "MaxPointsInRandomInstruction"; … … 117 105 config)); 118 106 119 Parameters.Add(new ValueParameter< Data>(107 Parameters.Add(new ValueParameter<ProblemData>( 120 108 DataParameterName, 121 109 DataParameterDescription)); 122 110 123 Parameters.Add(new FixedValueParameter<ErcOptions>(ErcOptionsParameterName, config.ErcOptions));111 Parameters.Add(new ValueParameter<ErcOptions>(ErcOptionsParameterName, config.ErcOptions)); 124 112 125 113 Parameters.Add(new FixedValueParameter<IntValue>( … … 188 176 } 189 177 190 public IValueParameter< Data> DataParameter191 { 192 get { return (IValueParameter< Data>)Parameters[DataParameterName]; }193 } 194 195 public Data Data178 public IValueParameter<ProblemData> DataParameter 179 { 180 get { return (IValueParameter<ProblemData>)Parameters[DataParameterName]; } 181 } 182 183 public ProblemData Data 196 184 { 197 185 get { return DataParameter.Value; } … … 356 344 357 345 public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 358 const string bestSolutionResultName = "Best Solution";359 346 var bestQuality = Maximization ? qualities.Max() : qualities.Min(); 360 347 var bestIdx = Array.IndexOf(qualities, bestQuality); 361 var bestIndividual = individuals[bestIdx]; 362 var solution = new PushSolution(bestIndividual.IntegerVector(), bestQuality, Data, random, config, DataBounds.TrainingRange.Start, DataBounds.TrainingRange.End); 363 364 if (!results.ContainsKey(bestSolutionResultName)) { 365 results.Add(new Result(bestSolutionResultName, solution)); 348 var bestIndividual = individuals[bestIdx].IntegerVector(); 349 350 var isIndividualBetter = AnalyzeBestTrainingSolution(bestIndividual, bestQuality, results, random); 351 352 if (isIndividualBetter) { 353 AnalyzeBestTestSolution(bestIndividual, results, random); 354 } 355 } 356 357 private void AnalyzeBestTestSolution(IntegerVector bestIndividual, ResultCollection results, IRandom random) { 358 var program = bestIndividual.ToPushProgram(config, randomPool); 359 var trainingResult = PushEvaluator.Evaluate(program, pool, random, Data, DataBounds.TestRange.Start, DataBounds.TestRange.End); 360 361 if (!results.ContainsKey(TestQualityResultName)) { 362 results.Add(new Result(TestQualityResultName, new DoubleValue(trainingResult.TotalQuality))); 366 363 } 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 } 373 } 364 ((DoubleValue)results[TestQualityResultName].Value).Value = trainingResult.TotalQuality; 365 } 366 } 367 368 private bool AnalyzeBestTrainingSolution(IntegerVector bestIndividual, double bestQuality, ResultCollection results, IRandom random) { 369 var solution = new PushSolution(bestIndividual, bestQuality, Data, random, config, DataBounds.TrainingRange.Start, DataBounds.TrainingRange.End); 370 371 if (!results.ContainsKey(BestTrainingSolutionResultName)) { 372 results.Add(new Result(BestTrainingSolutionResultName, solution)); 373 return true; 374 } 375 376 var currentBestQuality = ((PushSolution)results[BestTrainingSolutionResultName].Value).Quality; 377 378 if (Maximization && currentBestQuality < bestQuality || 379 !Maximization && currentBestQuality > bestQuality) { 380 results[BestTrainingSolutionResultName].Value = solution; 381 return true; 382 } 383 384 return false; 374 385 } 375 386 … … 389 400 } 390 401 391 public void Load( Data data) {402 public void Load(ProblemData data) { 392 403 Data = data; 393 404 BestKnownQuality = data.BestResult; 394 405 MaxPointsInProgram = data.MaxSize; 395 406 EvalPushLimit = data.EvalLimit; 407 ErcOptions = data.ErcOptions; 396 408 397 409 config.EnabledExpressions = (IList<string>)ExpressionTable.GetExpressionsByStackTypes((StackTypes)data.EnabledDataTypes); … … 408 420 409 421 DataBounds.TrainingRange.Start = 0; 410 DataBounds.TrainingRange.End = Data. OriginalTrainingCount;411 DataBounds.TestRange.Start = Data. OriginalTrainingCount;412 DataBounds.TestRange.End = Data. OriginalTrainingCount + Data.OriginalTestCount;422 DataBounds.TrainingRange.End = Data.TrainingCount; 423 DataBounds.TestRange.Start = Data.TrainingCount; 424 DataBounds.TestRange.End = Data.TrainingCount + Data.TestCount; 413 425 } 414 426 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushSolution.cs
r14834 r14875 14 14 public readonly IntegerVector IntegerVector; 15 15 [Storable] 16 public readonly Data Data;16 public readonly ProblemData Data; 17 17 [Storable] 18 18 public readonly IRandom Random; … … 23 23 [Storable] 24 24 public readonly int DataEnd; 25 26 25 [Storable] 27 26 public readonly bool Simplify; 28 27 29 public PushSolution(IntegerVector integerVector, double quality, Data data, IRandom random, IReadOnlyPushConfiguration config, int dataStart, int dataEnd, bool simplify = false)28 public PushSolution(IntegerVector integerVector, double quality, ProblemData data, IRandom random, IReadOnlyPushConfiguration config, int dataStart, int dataEnd, bool simplify = false) 30 29 : base("Solution", "A push solution.") { 31 30 IntegerVector = integerVector;
Note: See TracChangeset
for help on using the changeset viewer.