Changeset 15343 for branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/DiscreteEGO/DiscreteInfillProblem.cs
- Timestamp:
- 08/29/17 11:28:16 (7 years ago)
- Location:
- branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/DiscreteEGO
- Files:
-
- 1 added
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/DiscreteEGO/DiscreteInfillProblem.cs
r15340 r15343 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.IntegerVectorEncoding; 28 29 using HeuristicLab.Encodings.RealVectorEncoding; 29 30 using HeuristicLab.Optimization; … … 33 34 namespace HeuristicLab.Algorithms.EGO { 34 35 [StorableClass] 35 [Item(" InfillProblem", "A problem for finding the most interesing potential new sampling Points by optimizing some InfillCriterion")]36 public sealed class InfillProblem : SingleObjectiveBasicProblem<RealVectorEncoding> {36 [Item("DiscreteInfillProblem", "A problem for finding the most interesing potential new sampling Points by optimizing some InfillCriterion")] 37 public sealed class DiscreteInfillProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> { 37 38 38 39 public override bool Maximization => true; … … 47 48 private IInfillCriterion infillCriterion; 48 49 49 public IInfillCriterion InfillCriterion 50 { 50 public IInfillCriterion InfillCriterion { 51 51 get { return infillCriterion; } 52 set 53 { 52 set { 54 53 infillCriterion = value; 55 infillCriterion.Encoding = Encoding;54 infillCriterion.Encoding = GetRealVectorEncoding(Encoding); 56 55 } 57 56 } … … 60 59 #region Constructors 61 60 [StorableConstructor] 62 private InfillProblem(bool deserializing) : base(deserializing) { }63 private InfillProblem(InfillProblem original, Cloner cloner) : base(original, cloner) {61 private DiscreteInfillProblem(bool deserializing) : base(deserializing) { } 62 private DiscreteInfillProblem(DiscreteInfillProblem original, Cloner cloner) : base(original, cloner) { 64 63 infillCriterion = cloner.Clone(original.infillCriterion); 65 64 } 66 public InfillProblem() { }67 public override IDeepCloneable Clone(Cloner cloner) { return new InfillProblem(this, cloner); }65 public DiscreteInfillProblem() { } 66 public override IDeepCloneable Clone(Cloner cloner) { return new DiscreteInfillProblem(this, cloner); } 68 67 #endregion 69 68 70 69 public override double Evaluate(Individual individual, IRandom r) { 71 return !InBounds(individual. RealVector(), Encoding.Bounds) ? double.MinValue : InfillCriterion.Evaluate(individual.RealVector());70 return !InBounds(individual.IntegerVector(), Encoding.Bounds) ? double.MinValue : InfillCriterion.Evaluate(individual.IntegerVector().ToRealVector()); 72 71 } 73 72 public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { … … 76 75 var newQuality = qualities[best]; 77 76 if (!results.ContainsKey(BestInfillQualityResultName)) { 78 results.Add(new Result(BestInfillSolutionResultName, ( RealVector)individuals[best].RealVector().Clone()));77 results.Add(new Result(BestInfillSolutionResultName, (IntegerVector)individuals[best].IntegerVector().Clone())); 79 78 results.Add(new Result(BestInfillQualityResultName, new DoubleValue(newQuality))); 80 79 return; … … 83 82 if (qold == null) throw new ArgumentException("Old best quality is not a double value. Conflicting Analyzers?"); 84 83 if (qold.Value >= newQuality) return; 85 results[BestInfillSolutionResultName].Value = ( RealVector)individuals[best].RealVector().Clone();84 results[BestInfillSolutionResultName].Value = (IntegerVector)individuals[best].IntegerVector().Clone(); 86 85 qold.Value = newQuality; 87 86 } … … 89 88 var bounds = Encoding.Bounds; 90 89 var michalewiczIteration = 0; 90 var sigma = new DoubleArray(new double[] { 1.0 }); 91 91 92 while (true) { 92 93 var neighbour = individual.Copy(); 93 var r = neighbour.RealVector(); 94 switch (random.Next(5)) { 95 case 0: UniformOnePositionManipulator.Apply(random, r, bounds); break; 96 case 1: UniformOnePositionManipulator.Apply(random, r, bounds); break;//FixedNormalAllPositionsManipulator.Apply(random, r, new RealVector(new[] { 0.1 })); break; 97 case 2: MichalewiczNonUniformAllPositionsManipulator.Apply(random, r, bounds, new IntValue(michalewiczIteration++), new IntValue(10000), new DoubleValue(5.0)); break; 98 case 3: MichalewiczNonUniformOnePositionManipulator.Apply(random, r, bounds, new IntValue(michalewiczIteration++), new IntValue(10000), new DoubleValue(5.0)); break; 99 case 4: BreederGeneticAlgorithmManipulator.Apply(random, r, bounds, new DoubleValue(0.1)); break; 94 var r = neighbour.IntegerVector(); 95 switch (random.Next(3) % 3) { 96 case 0: HeuristicLab.Encodings.IntegerVectorEncoding.UniformOnePositionManipulator.Apply(random, r, bounds); break; 97 case 1: HeuristicLab.Encodings.IntegerVectorEncoding.RoundedNormalAllPositionsManipulator.Apply(random, r, bounds, sigma); break;//FixedNormalAllPositionsManipulator.Apply(random, r, new RealVector(new[] { 0.1 })); break; 98 case 2: HeuristicLab.Encodings.IntegerVectorEncoding.UniformSomePositionsManipulator.Apply(random, r, bounds, 0.1); break; 100 99 default: throw new NotImplementedException(); 101 100 } … … 108 107 infillCriterion.RegressionSolution = model; 109 108 infillCriterion.ExpensiveMaximization = expensiveMaximization; 110 infillCriterion.Encoding = Encoding;109 infillCriterion.Encoding = GetRealVectorEncoding(Encoding); 111 110 infillCriterion.Initialize(); 112 111 } 113 112 114 113 #region helpers 115 private static bool InBounds( RealVector r, DoubleMatrix bounds) {114 private static bool InBounds(IntegerVector r, IntMatrix bounds) { 116 115 return !r.Where((t, i) => t < bounds[i % bounds.Rows, 0] || t > bounds[i % bounds.Rows, 1]).Any(); 116 } 117 118 private static RealVectorEncoding GetRealVectorEncoding(IntegerVectorEncoding enc) { 119 var res = new RealVectorEncoding(enc.Length); 120 res.Bounds = new DoubleMatrix(enc.Bounds.Rows, enc.Bounds.Columns); 121 for (int r = 0; r < res.Bounds.Rows; r++) 122 for (int c = 0; c < res.Bounds.Columns; c++) 123 res.Bounds[r, c] = enc.Bounds[r, c]; 124 return res; 125 126 117 127 } 118 128 #endregion
Note: See TracChangeset
for help on using the changeset viewer.