Changeset 14746 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Simplifier
- Timestamp:
- 03/11/17 20:07:13 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Simplifier/RandomSimplifier.cs
r14745 r14746 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier {2 using System;3 using HeuristicLab.Core;4 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;1 //namespace HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier { 2 // using System; 3 // using HeuristicLab.Core; 4 // using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 5 5 6 public class RandomSimplifier : ISimplifier {7 public int Trys { get; set; }6 // public class RandomSimplifier : ISimplifier { 7 // public int Trys { get; set; } 8 8 9 public PushProgram Simplify(PushProgram program, IRandom random, Predicate<PushProgram> isBetter) {9 // public PushProgram Simplify(PushProgram program, IRandom random, Predicate<PushProgram> isBetter) { 10 10 11 if (program.TotalCount == 1) {12 return isBetter(PushProgram.Empty) ? PushProgram.Empty : program;13 }11 // if (program.TotalCount == 1) { 12 // return isBetter(PushProgram.Empty) ? PushProgram.Empty : program; 13 // } 14 14 15 var copy = program.Copy();16 var maxTries = Math.Min(Trys, program.TotalCount - 2);17 var successfulRemoves = 0;15 // var copy = program.Copy(); 16 // var maxTries = Math.Min(Trys, program.TotalCount - 2); 17 // var successfulRemoves = 0; 18 18 19 for (var i = 0; i < maxTries; i++) {20 var rndIndex = random.Next(1, program.TotalCount - 1 - successfulRemoves);21 var node = copy.GetFromTree(22 rndIndex,23 (super, parent, child, childIndex, parentIndex) => new {24 Super = super,25 Parent = parent,26 ChildIndex = childIndex,27 ParentIndex = parentIndex28 });19 // for (var i = 0; i < maxTries; i++) { 20 // var rndIndex = random.Next(1, program.TotalCount - 1 - successfulRemoves); 21 // var node = copy.GetFromTree( 22 // rndIndex, 23 // (super, parent, child, childIndex, parentIndex) => new { 24 // Super = super, 25 // Parent = parent, 26 // ChildIndex = childIndex, 27 // ParentIndex = parentIndex 28 // }); 29 29 30 var oldParentExpressions = node.Parent.State;31 var newParentExpressions = RemoveAt(oldParentExpressions, node.ChildIndex);32 var newParent = new PushProgram(newParentExpressions);30 // var oldParentExpressions = node.Parent.State; 31 // var newParentExpressions = RemoveAt(oldParentExpressions, node.ChildIndex); 32 // var newParent = new PushProgram(newParentExpressions); 33 33 34 var superExpressions = node.Super == null ? copy.State : node.Super.State;35 superExpressions[node.ParentIndex] = newParent;34 // var superExpressions = node.Super == null ? copy.State : node.Super.State; 35 // superExpressions[node.ParentIndex] = newParent; 36 36 37 if (isBetter(copy)) {38 successfulRemoves++;39 } else {40 superExpressions[node.ParentIndex] = node.Parent;41 }42 }37 // if (isBetter(copy)) { 38 // successfulRemoves++; 39 // } else { 40 // superExpressions[node.ParentIndex] = node.Parent; 41 // } 42 // } 43 43 44 return copy;45 }44 // return copy; 45 // } 46 46 47 private static T[] RemoveAt<T>(T[] source, int index) {48 var dest = new T[source.Length - 1];49 if (index > 0)50 Array.Copy(source, 0, dest, 0, index);47 // private static T[] RemoveAt<T>(T[] source, int index) { 48 // var dest = new T[source.Length - 1]; 49 // if (index > 0) 50 // Array.Copy(source, 0, dest, 0, index); 51 51 52 if (index < source.Length - 1)53 Array.Copy(source, index + 1, dest, index, source.Length - index - 1);52 // if (index < source.Length - 1) 53 // Array.Copy(source, index + 1, dest, index, source.Length - index - 1); 54 54 55 return dest;56 }57 }58 }55 // return dest; 56 // } 57 // } 58 //}
Note: See TracChangeset
for help on using the changeset viewer.