Changeset 15343 for branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/DiscreteEGO/DiscreteInfillSolver.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/DiscreteInfillSolver.cs
r15340 r15343 22 22 using System; 23 23 using System.Threading; 24 using System.Linq; 25 using System.Collections.Generic; 24 26 using HeuristicLab.Common; 25 27 using HeuristicLab.Core; 26 28 using HeuristicLab.Data; 27 using HeuristicLab.Encodings. RealVectorEncoding;29 using HeuristicLab.Encodings.IntegerVectorEncoding; 28 30 using HeuristicLab.Optimization; 29 31 using HeuristicLab.Parameters; … … 32 34 33 35 namespace HeuristicLab.Algorithms.EGO { 34 /// <summary> 35 /// A base class for operators that manipulate real-valued vectors. 36 /// </summary> 37 [Item("InfillSolver", "A RealVectorCreator that creates candidates by optimizing an infill-subproblem")] 36 [Item("DiscreteInfillSolver", "An IntegerVectorCreator that creates candidates by optimizing an infill-subproblem")] 38 37 [StorableClass] 39 public class InfillSolver : RealVectorCreator, ICancellableOperator {38 public class DiscreteInfillSolver : IntegerVectorCreator, ICancellableOperator { 40 39 41 40 public ILookupParameter<IAlgorithm> InfillOptimizationAlgorithmParamter => (ILookupParameter<IAlgorithm>)Parameters["InfillAlgorithm"]; … … 44 43 public ILookupParameter<BoolValue> RemoveDuplicatesParameter => (ILookupParameter<BoolValue>)Parameters["RemoveDuplicates"]; 45 44 public IFixedValueParameter<DoubleValue> DuplicateCutoffParameter => (IFixedValueParameter<DoubleValue>)Parameters["Duplicates Cutoff"]; 46 public ILookupParameter< DoubleMatrix> InfillBoundsParameter => (ILookupParameter<DoubleMatrix>)Parameters["InfillBounds"];45 public ILookupParameter<IntMatrix> InfillBoundsParameter => (ILookupParameter<IntMatrix>)Parameters["InfillBounds"]; 47 46 48 47 public CancellationToken Cancellation { get; set; } 49 48 50 49 [StorableConstructor] 51 protected InfillSolver(bool deserializing) : base(deserializing) { }52 protected InfillSolver(InfillSolver original, Cloner cloner) : base(original, cloner) { }53 public InfillSolver() {50 protected DiscreteInfillSolver(bool deserializing) : base(deserializing) { } 51 protected DiscreteInfillSolver(DiscreteInfillSolver original, Cloner cloner) : base(original, cloner) { } 52 public DiscreteInfillSolver() { 54 53 Parameters.Add(new LookupParameter<IAlgorithm>("InfillAlgorithm", "The algorithm used to optimize the infill problem") { Hidden = true }); 55 54 Parameters.Add(new LookupParameter<IRegressionSolution>("Model", "The RegressionSolution upon which the InfillProblem operates") { Hidden = true }); … … 57 56 Parameters.Add(new LookupParameter<BoolValue>("RemoveDuplicates", "Whether duplicates shall be removed") { Hidden = true }); 58 57 Parameters.Add(new FixedValueParameter<DoubleValue>("Duplicates Cutoff", "The cut off radius for", new DoubleValue(0.01)) { Hidden = false }); 59 Parameters.Add(new LookupParameter< DoubleMatrix>("InfillBounds", "The bounds applied for infill solving") { Hidden = true });58 Parameters.Add(new LookupParameter<IntMatrix>("InfillBounds", "The bounds applied for infill solving") { Hidden = true }); 60 59 } 61 60 62 61 public override IDeepCloneable Clone(Cloner cloner) { 63 return new InfillSolver(this, cloner);62 return new DiscreteInfillSolver(this, cloner); 64 63 } 65 64 66 protected override RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds) {65 protected override IntegerVector Create(IRandom random, IntValue length, IntMatrix bounds) { 67 66 var infillBounds = InfillBoundsParameter.ActualValue; 68 67 if (infillBounds != null && infillBounds.Rows > 0) { … … 75 74 var res = OptimizeInfillProblem(alg, model, max, bounds, length.Value, random); 76 75 var rad = DuplicateCutoffParameter.Value.Value; 77 if (!RemoveDuplicatesParameter.ActualValue.Value || !(GetMinDifference(model.ProblemData.Dataset, res) < rad * rad)) return res; 78 for (var i = 0; i < res.Length; i++) res[i] += random.NextDouble() * rad * 2; 76 if (!RemoveDuplicatesParameter.ActualValue.Value || GetMinDifference(model.ProblemData.Dataset, res) >= rad * rad) return res; 77 78 bool changed = false; 79 var steps = 0; 80 var dims = new List<int>(); 81 82 //TODO this may take a long time to compute if many samples have already been evaluated in the surrounding area 83 //as the preferred region can not be sampled denser and denser due to the disceretization, the variance between two sampled points may be impossible to decease 84 85 //TODO speed up GetMinDifferecnce via tree-structure 86 while (!changed || GetMinDifference(model.ProblemData.Dataset, res) < rad * rad) { 87 if (dims.Count == 0) { 88 if (!changed && steps > 0) throw new ArgumentException("Can not avoid duplicate"); 89 dims = Enumerable.Range(0, res.Length).ToList(); 90 steps++; 91 changed = false; 92 } 93 var i = random.Next(dims.Count); 94 var dim = dims[i]; 95 dims.RemoveAt(i); 96 var step = bounds[dim % bounds.Rows, 2] * steps; 97 var low = checkIntBounds(bounds, dim, res[dim] - step); 98 var high = checkIntBounds(bounds, dim, res[dim] + step); 99 if (!low && !high) continue; 100 else if (low && high) res[dim] += (random.NextDouble() < 0.5 ? -step : step); 101 else if (low) res[dim] -= step; 102 else res[dim] += step; 103 changed = true; 104 } 79 105 return res; 80 106 } 81 107 82 private RealVector OptimizeInfillProblem(IAlgorithm algorithm, IRegressionSolution model, bool maximization, DoubleMatrix bounds, int length, IRandom random) { 83 var infillProblem = algorithm.Problem as InfillProblem; 108 109 private bool checkIntBounds(IntMatrix b, int row, int value) { 110 var bi = row % b.Rows; 111 var l = b[bi, 0]; 112 var h = b[bi, 1]; 113 var s = b[bi, 2]; 114 return l <= value && h >= value && (value - l) % s == 0; 115 } 116 117 private IntegerVector OptimizeInfillProblem(IAlgorithm algorithm, IRegressionSolution model, bool maximization, IntMatrix bounds, int length, IRandom random) { 118 var infillProblem = algorithm.Problem as DiscreteInfillProblem; 84 119 if (infillProblem == null) throw new ArgumentException("The algortihm has no InfillProblem to solve"); 85 120 infillProblem.Encoding.Length = length; … … 87 122 infillProblem.Initialize(model, maximization); 88 123 var res = EgoUtilities.SyncRunSubAlgorithm(algorithm, random.Next(int.MaxValue), Cancellation); 89 var v = res[ InfillProblem.BestInfillSolutionResultName].Value as RealVector;124 var v = res[DiscreteInfillProblem.BestInfillSolutionResultName].Value as IntegerVector; 90 125 algorithm.Runs.Clear(); 91 126 return v; 92 127 } 93 128 94 private static double GetMinDifference(IDataset data, RealVector r) {129 private static double GetMinDifference(IDataset data, IntegerVector r) { 95 130 var mind = double.MaxValue; 96 131 for (var i = 0; i < data.Rows; i++) {
Note: See TracChangeset
for help on using the changeset viewer.