Changeset 18233
- Timestamp:
- 03/10/22 09:53:58 (3 years ago)
- Location:
- branches/3040_VectorBasedGP
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Manipulators/UniformOnePositionManipulator.cs
r17180 r18233 93 93 // but 99 is not a feasible value, so max needs to be adjusted => min = 0, max = 95 94 94 max = FloorFeasible(min, max, step, max - 1); 95 vector[index] = RoundFeasible(min, max, step, random.Next(min, max)); 95 if (min == max) 96 vector[index] = min; 97 else 98 vector[index] = RoundFeasible(min, max, step, random.Next(min, max)); 96 99 } 97 100 } -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/NestedOptimizerSubVectorImprovementManipulator.cs
r18230 r18233 26 26 [Item("SubVectorOptimizationProblem", "")] 27 27 [StorableType("EA3D3221-B274-4F2F-8B58-23CB2D091FD7")] 28 p rivateclass SubVectorOptimizationProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> {28 public class SubVectorOptimizationProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> { 29 29 #region Fixed Problem Parameters 30 30 [Storable] … … 45 45 #endregion 46 46 47 private IFixedValueParameter<BoolValue> UseCacheParameter { 48 get { return (IFixedValueParameter<BoolValue>)Parameters["UseCache"]; } 49 } 50 51 public bool UseCache { 52 get { return UseCacheParameter.Value.Value; } 53 set { UseCacheParameter.Value.Value = value; } 54 } 55 56 private readonly IDictionary<IntegerVector, double> cache; 57 47 58 public override bool Maximization { get { return false; } } 48 59 … … 50 61 Encoding = new IntegerVectorEncoding("bounds"); 51 62 Parameters.Add(new ResultParameter<IntegerVector>(BestSolutionParameterName, "")); 52 } 63 64 Parameters.Add(new FixedValueParameter<BoolValue>("UseCache", new BoolValue(true))); 65 cache = new Dictionary<IntegerVector, double>(new IntegerVectorEqualityComparer()); 66 } 67 53 68 private SubVectorOptimizationProblem(SubVectorOptimizationProblem original, Cloner cloner) 54 : base(original, cloner) { } 69 : base(original, cloner) { 70 this.cache = new Dictionary<IntegerVector, double>(original.cache, new IntegerVectorEqualityComparer()); 71 } 55 72 public override IDeepCloneable Clone(Cloner cloner) { 56 73 return new SubVectorOptimizationProblem(this, cloner); 57 74 } 75 58 76 [StorableConstructor] 59 private SubVectorOptimizationProblem(StorableConstructorFlag _) : base(_) { } 77 private SubVectorOptimizationProblem(StorableConstructorFlag _) : base(_) { 78 cache = new Dictionary<IntegerVector, double>(new IntegerVectorEqualityComparer()); 79 } 80 [StorableHook(HookType.AfterDeserialization)] 81 private void AfterDeserialization() { 82 if (!Parameters.ContainsKey("UseCache")) 83 Parameters.Add(new FixedValueParameter<BoolValue>("UseCache", new BoolValue(true))); 84 } 60 85 61 86 public override double Evaluate(Individual individual, IRandom random) { 62 var solution = individual.IntegerVector(Encoding.Name); 87 return Evaluate(individual.IntegerVector(Encoding.Name)); 88 } 89 90 public double Evaluate(IntegerVector solution) { 91 if (UseCache && cache.TryGetValue(solution, out double cachedQuality)) { 92 return cachedQuality; 93 } 63 94 64 95 var updatedTree = (ISymbolicExpressionTree)tree.Clone(); 65 96 UpdateFromVector(updatedTree, selectedSubVectorNodes, solution, Encoding.Bounds[0, 1]); 66 97 67 98 var quality = evaluator.Evaluate(executionContext, updatedTree, problemData, rows); 68 99 if (evaluator.Maximization) 69 100 quality = -quality; 101 102 if (UseCache) { 103 cache.Add(solution, quality); 104 } 105 70 106 return quality; 71 107 } … … 83 119 this.rows = rows; 84 120 this.executionContext = executionContext; 121 cache.Clear(); 85 122 } 86 123 public void SetInstanceData(ISymbolicExpressionTree tree, List<int> selectedSubVectorNodes, int vectorLength) { … … 88 125 this.selectedSubVectorNodes = selectedSubVectorNodes; 89 126 Encoding.Length = selectedSubVectorNodes.Count * 2; 90 Encoding.Bounds = new IntMatrix(new int[,] { { 0, vectorLength } }); 91 } 92 } 93 127 Encoding.Bounds = new IntMatrix(new int[,] { { 0, vectorLength + 1 } }); 128 cache.Clear(); 129 } 130 } 131 132 [Item("SubVectorGradientMutator", "")] 133 [StorableType("DC5EC7CE-AD51-4655-8F75-28601345B4C7")] 134 public abstract class SubVectorGradientMutator : BoundedIntegerVectorManipulator { 135 136 [Storable] 137 private readonly SubVectorOptimizationProblem problem; 138 139 protected SubVectorGradientMutator(SubVectorOptimizationProblem problem) { 140 this.problem = problem; 141 } 142 protected SubVectorGradientMutator(SubVectorGradientMutator original, Cloner cloner) 143 : base(original, cloner) { 144 this.problem = cloner.Clone(original.problem); 145 } 146 147 [StorableConstructor] 148 protected SubVectorGradientMutator(StorableConstructorFlag _) : base(_) { 149 } 150 [StorableHook(HookType.AfterDeserialization)] 151 private void AfterDeserialization() { 152 } 153 154 public double FivePointStencil(IntegerVector position, int dim, IntMatrix bounds, int h = 1) { 155 double f(int i) { 156 var modified = new IntegerVector(position); 157 modified[dim] = FloorFeasible(bounds[dim % bounds.Rows, 0], bounds[dim % bounds.Rows, 1], 1, i); 158 return problem.Evaluate(modified); 159 } 160 161 int x = position[dim]; 162 var slope = ( 163 + 1 * f(x - 2*h) 164 - 8 * f(x - 1*h) 165 + 8 * f(x + 1*h) 166 - 1 * f(x + 2*h) 167 ) / 12; 168 169 return slope; 170 } 171 172 public double[] CalculateGradient(IntegerVector position, IntMatrix bounds, int h = 1) { 173 return Enumerable.Range(0, position.Length) 174 .Select((x, dim) => FivePointStencil(position, dim, bounds, h)).ToArray(); 175 } 176 } 177 178 [Item("GuidedDirectionManipulator", "")] 179 [StorableType("8781F827-BB46-4041-AAC4-25E76C5EF1F5")] 180 public class GuidedDirectionManipulator : SubVectorGradientMutator { 181 182 [StorableType("AED631BC-C1A3-4408-AA39-18A81018E159")] 183 public enum MutationType { 184 SinglePosition, 185 AllPosition 186 } 187 188 public IFixedValueParameter<EnumValue<MutationType>> MutationTypeParameter { 189 get { return (IFixedValueParameter<EnumValue<MutationType>>)Parameters["MutationType"]; } 190 } 191 192 public GuidedDirectionManipulator(SubVectorOptimizationProblem problem) 193 : base (problem) { 194 Parameters.Add(new FixedValueParameter<EnumValue<MutationType>>("MutationType", new EnumValue<MutationType>(MutationType.AllPosition))); 195 } 196 197 protected GuidedDirectionManipulator(GuidedDirectionManipulator original, Cloner cloner) 198 : base(original, cloner) { 199 } 200 public override IDeepCloneable Clone(Cloner cloner) { 201 return new GuidedDirectionManipulator(this, cloner); 202 } 203 204 [StorableConstructor] 205 protected GuidedDirectionManipulator(StorableConstructorFlag _) : base(_) { 206 } 207 [StorableHook(HookType.AfterDeserialization)] 208 private void AfterDeserialization() { 209 } 210 211 protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) { 212 if (MutationTypeParameter.Value.Value == MutationType.AllPosition) { 213 var gradient = CalculateGradient(integerVector, bounds); 214 var limitedBounds = LimitBounds(bounds, integerVector, gradient); 215 UniformSomePositionsManipulator.Apply(random, integerVector, limitedBounds, probability: 1.0); 216 } else if(MutationTypeParameter.Value.Value == MutationType.SinglePosition) { 217 int dim = random.Next(integerVector.Length); 218 var gradient = Enumerable.Repeat(0.0, integerVector.Length).ToArray(); 219 gradient[dim] = FivePointStencil(integerVector, dim, bounds); 220 var limitedBounds = LimitBounds(bounds, integerVector, gradient); 221 UniformOnePositionManipulator.Manipulate(random, integerVector, limitedBounds, dim); 222 } 223 } 224 225 private static IntMatrix LimitBounds(IntMatrix bounds, IntegerVector position, double[] gradient) { 226 var limitedBounds = new IntMatrix(gradient.Length, 2); 227 for (int i = 0; i < gradient.Length; i++) { 228 limitedBounds[i, 0] = gradient[i] < 0 ? position[i] + 0 : bounds[i % bounds.Rows, 0]; 229 limitedBounds[i, 1] = gradient[i] > 0 ? position[i] + 1 : bounds[i % bounds.Rows, 1]; 230 } 231 return limitedBounds; 232 } 233 } 94 234 95 235 #region Parameter Properties … … 131 271 }; 132 272 es.Mutator = es.MutatorParameter.ValidValues.OfType<UniformSomePositionsManipulator>().Single(); 273 274 var gdes = new EvolutionStrategy() { 275 Problem = problem, 276 PlusSelection = new BoolValue(true), 277 PopulationSize = new IntValue(10), 278 Children = new IntValue(10), 279 MaximumGenerations = new IntValue(100) 280 }; 281 gdes.Name = "Guided Direction " + gdes.Name; 282 var gdMutator = new GuidedDirectionManipulator(problem); 283 problem.Encoding.ConfigureOperator(gdMutator); 284 gdes.MutatorParameter.ValidValues.Add(gdMutator); 285 gdes.Mutator = gdMutator; 133 286 134 287 var ga = new GeneticAlgorithm() { … … 156 309 #endregion 157 310 158 var optimizers = new ItemSet<IAlgorithm>() { rs, es, g a, osga };311 var optimizers = new ItemSet<IAlgorithm>() { rs, es, gdes, ga, osga }; 159 312 160 313 Parameters.Add(new OptionalConstrainedValueParameter<IAlgorithm>("NestedOptimizer", optimizers, rs)); -
branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances.DataAnalysis/3.3/SegmentOptimization/SegmentOptimizationInstanceProvider.cs
r18228 r18233 182 182 } 183 183 184 public class SegmentOptimizationLargeNoNoiseFileInstanceProvider : SegmentOptimizationLargeFileInstanceProvider { 185 public override string Name { 186 get { return "SOP Large File NoNoise"; } 187 } 188 189 public override string Description { 190 get { return "SOP Large File NoNoise"; } 191 } 192 193 public override IEnumerable<IDataDescriptor> GetDataDescriptors() { 194 return base.GetDataDescriptors().Select(id => { 195 var descriptor = (SOPDataDescriptor)id; 196 descriptor.Name += "_m"; 197 descriptor.VariableName += "_m"; 198 return descriptor; 199 }); 200 } 201 } 202 184 203 public class SegmentOptimizationCombinationsLargeFileInstanceProvider : SegmentOptimizationFileInstanceProvider { 185 204 public override string Name {
Note: See TracChangeset
for help on using the changeset viewer.