- Timestamp:
- 06/26/17 09:10:56 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Problems/InfillProblem.cs
r14818 r15064 21 21 22 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 23 25 using HeuristicLab.Common; 24 26 using HeuristicLab.Core; … … 27 29 using HeuristicLab.Optimization; 28 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.DataAnalysis; 29 32 30 33 namespace HeuristicLab.Algorithms.EGO { … … 33 36 public sealed class InfillProblem : SingleObjectiveBasicProblem<RealVectorEncoding> { 34 37 35 public override bool Maximization => true; //This is necessary because algorithms do not expect the maximization to change38 public override bool Maximization => true; 36 39 37 #region Properties; 40 #region ProblemResultNames 41 public const string BestInfillSolutionResultName = "BestInfillSolution"; 42 public const string BestInfillQualityResultName = "BestInfillQuality"; 43 #endregion 44 45 #region Properties 38 46 [Storable] 39 47 private IInfillCriterion infillCriterion; 40 [Storable]41 private SingleObjectiveBasicProblem<IEncoding> problem;42 48 43 49 public IInfillCriterion InfillCriterion 44 50 { 45 51 get { return infillCriterion; } 46 set { infillCriterion = value; }47 }48 public SingleObjectiveBasicProblem<IEncoding> Problem49 {50 get { return problem; }51 52 set 52 53 { 53 problem = value; 54 if (problem == null) return; 55 var enc = problem.Encoding as RealVectorEncoding; 56 if (enc == null) throw new ArgumentException("EGO can not be performed on non-RealVectorEncodings"); 57 Encoding = enc; 58 SolutionCreator = new UniformRandomRealVectorCreator();//ignore Problem specific Solution Creation 59 54 infillCriterion = value; 55 infillCriterion.Encoding = Encoding; 60 56 } 61 57 } 62 58 #endregion 63 59 64 #region HLConstructors60 #region Constructors 65 61 [StorableConstructor] 66 62 private InfillProblem(bool deserializing) : base(deserializing) { } 67 63 private InfillProblem(InfillProblem original, Cloner cloner) : base(original, cloner) { 68 infillCriterion = cloner.Clone(original.InfillCriterion); 69 problem = cloner.Clone(original.Problem); 64 infillCriterion = cloner.Clone(original.infillCriterion); 70 65 } 71 66 public InfillProblem() { } … … 74 69 75 70 public override double Evaluate(Individual individual, IRandom r) { 76 var q = InfillCriterion.Evaluate(individual.RealVector()); 77 return InfillCriterion.Maximization() ? q : -q; 71 return !InBounds(individual.RealVector(), Encoding.Bounds) ? double.MinValue : InfillCriterion.Evaluate(individual.RealVector()); 78 72 } 79 73 public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 80 74 base.Analyze(individuals, qualities, results, random); 81 75 var best = qualities.ArgMax(x => x); 82 var qnew = InfillCriterion.Maximization() ? qualities[best] : -qualities[best]; 83 const string qname = EfficientGlobalOptimizationAlgorithm.BestInfillQualityResultName; 84 const string sname = EfficientGlobalOptimizationAlgorithm.BestInfillSolutionResultName; 85 if (!results.ContainsKey(EfficientGlobalOptimizationAlgorithm.BestInfillQualityResultName)) { 86 results.Add(new Result(sname, (RealVector)individuals[best].RealVector().Clone())); 87 results.Add(new Result(qname, new DoubleValue(qnew))); 76 var newQuality = qualities[best]; 77 if (!results.ContainsKey(BestInfillQualityResultName)) { 78 results.Add(new Result(BestInfillSolutionResultName, (RealVector)individuals[best].RealVector().Clone())); 79 results.Add(new Result(BestInfillQualityResultName, new DoubleValue(newQuality))); 88 80 return; 89 81 } 90 var qold = results[ qname].Value as DoubleValue;82 var qold = results[BestInfillQualityResultName].Value as DoubleValue; 91 83 if (qold == null) throw new ArgumentException("Old best quality is not a double value. Conflicting Analyzers?"); 92 if (qold.Value >= qnew == InfillCriterion.Maximization()) return;93 results[ sname].Value = (RealVector)individuals[best].RealVector().Clone();94 qold.Value = qnew;84 if (qold.Value >= newQuality) return; 85 results[BestInfillSolutionResultName].Value = (RealVector)individuals[best].RealVector().Clone(); 86 qold.Value = newQuality; 95 87 } 88 public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) { 89 var bounds = Encoding.Bounds; 90 var michalewiczIteration = 0; 91 while (true) { 92 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; 100 default: throw new NotImplementedException(); 101 } 102 yield return neighbour; 103 michalewiczIteration %= 10000; 104 } 105 } 106 107 public void Initialize(IRegressionSolution model, bool expensiveMaximization) { 108 infillCriterion.RegressionSolution = model; 109 infillCriterion.ExpensiveMaximization = expensiveMaximization; 110 infillCriterion.Encoding = Encoding; 111 infillCriterion.Initialize(); 112 } 113 114 #region helpers 115 private static bool InBounds(RealVector r, DoubleMatrix bounds) { 116 return !r.Where((t, i) => t < bounds[i % bounds.Rows, 0] || t > bounds[i % bounds.Rows, 1]).Any(); 117 } 118 #endregion 119 96 120 } 97 121 }
Note: See TracChangeset
for help on using the changeset viewer.