Changeset 14577
- Timestamp:
- 01/16/17 14:25:56 (8 years ago)
- Location:
- branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/CrowdingIndicator.cs
r14404 r14577 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Encodings.RealVectorEncoding; 28 using HeuristicLab.Optimization; 27 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Problems.TestFunctions.MultiObjective;29 30 30 31 namespace HeuristicLab.Algorithms.MOCMAEvolutionStrategy { … … 33 34 internal class CrowdingIndicator : Item, IIndicator { 34 35 35 public int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjective TestFunctionProblemproblem) {36 var bounds = problem. Bounds;36 public int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjectiveBasicProblem<RealVectorEncoding> problem) { 37 var bounds = problem.Encoding.Bounds; 37 38 var extracted = front.Select(extractor.Invoke).ToArray(); 39 if (extracted.Length <= 2) return 0; 38 40 var pointsums = new double[extracted.Length]; 39 41 40 for (var dim = 0; dim < problem. Objectives; dim++) {42 for (var dim = 0; dim < problem.Maximization.Length; dim++) { 41 43 var arr = extracted.Select(x => x[dim]).ToArray(); 42 44 Array.Sort(arr); 43 var fmax = problem. Bounds[dim % bounds.Rows, 1];45 var fmax = problem.Encoding.Bounds[dim % bounds.Rows, 1]; 44 46 var fmin = bounds[dim % bounds.Rows, 0]; 45 47 var pointIdx = 0; … … 52 54 } 53 55 //find min 54 return pointsums. Where(x => !double.IsInfinity(x)).ArgMin();56 return pointsums.ArgMin(); 55 57 } 56 58 -
branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/HeuristicLab.Algorithms.MOCMAEvolutionStrategy-3.3.csproj
r14404 r14577 106 106 </ItemGroup> 107 107 <ItemGroup> 108 <Compile Include="MinimalDistanceIndicator.cs" /> 108 109 <Compile Include="CrowdingIndicator.cs" /> 109 110 <Compile Include="HypervolumeIndicator.cs" /> -
branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/HypervolumeIndicator.cs
r14404 r14577 26 26 using HeuristicLab.Common; 27 27 using HeuristicLab.Core; 28 using HeuristicLab.Encodings.RealVectorEncoding; 29 using HeuristicLab.Optimization; 28 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 31 using HeuristicLab.Problems.TestFunctions.MultiObjective; … … 33 35 internal class HypervolumeIndicator : Item, IIndicator { 34 36 35 public int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjective TestFunctionProblemproblem) {37 public int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjectiveBasicProblem<RealVectorEncoding> problem) { 36 38 var frontArr = front.Select(extractor.Invoke).ToList(); 37 var refPoint = BuildReference(frontArr.Concat(new[] { problem.TestFunction.ReferencePoint(problem.Objectives) }), problem.Maximization); 39 if (frontArr.Count <= 1) return 0; 40 var p = problem as MultiObjectiveTestFunctionProblem; 41 var refPoint = BuildReference(p != null ? frontArr.Concat(new[] { p.TestFunction.ReferencePoint(p.Objectives) }) : frontArr, problem.Maximization); 38 42 var hv = Hypervolume.Calculate(frontArr, refPoint, problem.Maximization); 39 return Enumerable.Range(0, frontArr.Count).Select(i => Contribution(frontArr, i, problem.Maximization, refPoint, hv)).ArgMin(); 43 var contributions = Enumerable.Range(0, frontArr.Count).Select(i => Contribution(frontArr, i, problem.Maximization, refPoint, hv)); 44 return contributions.ArgMin(); 40 45 } 41 46 -
branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/IIndicator.cs
r14404 r14577 22 22 using System.Collections.Generic; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Problems.TestFunctions.MultiObjective; 24 using HeuristicLab.Encodings.RealVectorEncoding; 25 using HeuristicLab.Optimization; 25 26 26 27 namespace HeuristicLab.Algorithms.MOCMAEvolutionStrategy { … … 35 36 /// <param name="problem">The problem on which the front is evaluated (!! The function itself will NOT be evluated only bounds referencePoints & other metadate will be used</param> 36 37 /// <returns>the index of the least contributing point according to any type of quality criteria</returns> 37 int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjective TestFunctionProblemproblem);38 int LeastContributer<TR>(IEnumerable<TR> front, Func<TR, double[]> extractor, MultiObjectiveBasicProblem<RealVectorEncoding> problem); 38 39 } 39 40 } -
branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMASEvolutionStrategy.cs
r14404 r14577 44 44 public override Type ProblemType 45 45 { 46 get { return typeof(MultiObjective TestFunctionProblem); }47 } 48 public new MultiObjective TestFunctionProblemProblem49 { 50 get { return (MultiObjective TestFunctionProblem)base.Problem; }46 get { return typeof(MultiObjectiveBasicProblem<RealVectorEncoding>); } 47 } 48 public new MultiObjectiveBasicProblem<RealVectorEncoding> Problem 49 { 50 get { return (MultiObjectiveBasicProblem<RealVectorEncoding>)base.Problem; } 51 51 set { base.Problem = value; } 52 52 } 53 public override bool SupportsPause 54 { 55 get { return false; } 56 } 57 53 58 #region internal variables 54 59 private readonly IRandom random = new MersenneTwister(); … … 76 81 private const string CrowdingResultName = "Crowding"; 77 82 private const string SpacingResultName = "Spacing"; 78 private const string SolutionsResultName = "Pareto Front";83 private const string CurrentFrontResultName = "Pareto Front"; 79 84 private const string BestHypervolumeResultName = "Best Hypervolume"; 80 85 private const string BestKnownHypervolumeResultName = "Best known hypervolume"; … … 97 102 get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; } 98 103 } 99 p rivateIFixedValueParameter<IntValue> PopulationSizeParameter104 public IFixedValueParameter<IntValue> PopulationSizeParameter 100 105 { 101 106 get { return (IFixedValueParameter<IntValue>)Parameters[PopulationSizeName]; } 102 107 } 103 p rivateIFixedValueParameter<IntValue> MaximumGenerationsParameter108 public IFixedValueParameter<IntValue> MaximumGenerationsParameter 104 109 { 105 110 get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsName]; } 106 111 } 107 p rivateIFixedValueParameter<IntValue> MaximumEvaluatedSolutionsParameter112 public IFixedValueParameter<IntValue> MaximumEvaluatedSolutionsParameter 108 113 { 109 114 get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluatedSolutionsName]; } 110 115 } 111 p rivateIFixedValueParameter<DoubleValue> InitialSigmaParameter116 public IFixedValueParameter<DoubleValue> InitialSigmaParameter 112 117 { 113 118 get { return (IFixedValueParameter<DoubleValue>)Parameters[InitialSigmaName]; } 114 119 } 115 p rivateIConstrainedValueParameter<IIndicator> IndicatorParameter120 public IConstrainedValueParameter<IIndicator> IndicatorParameter 116 121 { 117 122 get { return (IConstrainedValueParameter<IIndicator>)Parameters[IndicatorName]; } … … 254 259 private DoubleMatrix ResultsSolutions 255 260 { 256 get { return ((DoubleMatrix)Results[ SolutionsResultName].Value); }257 set { Results[ SolutionsResultName].Value = value; }261 get { return ((DoubleMatrix)Results[CurrentFrontResultName].Value); } 262 set { Results[CurrentFrontResultName].Value = value; } 258 263 } 259 264 private ScatterPlotContent ResultsScatterPlot … … 273 278 Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsName, "The maximum number of generations which should be processed.", new IntValue(1000))); 274 279 Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluatedSolutionsName, "The maximum number of evaluated solutions that should be computed.", new IntValue(int.MaxValue))); 275 var set = new ItemSet<IIndicator> { new HypervolumeIndicator(), new CrowdingIndicator() };280 var set = new ItemSet<IIndicator> { new HypervolumeIndicator(), new CrowdingIndicator(), new MinimalDistanceIndicator() }; 276 281 Parameters.Add(new ConstrainedValueParameter<IIndicator>(IndicatorName, "The selection mechanism on non-dominated solutions", set, set.First())); 277 282 } … … 331 336 solutions = new MOCMAESIndividual[PopulationSize]; 332 337 for (var i = 0; i < PopulationSize; i++) { 333 var x = new RealVector(Problem. ProblemSize); // Uniform distibution in all dimensions assumed.334 // There is the UniformSolutionCreater associated with the Encoding, but it was considered not usable here335 var bounds = Problem. Bounds;336 for (var j = 0; j < Problem. ProblemSize; j++) {338 var x = new RealVector(Problem.Encoding.Length); // Uniform distibution in all dimensions assumed. 339 // There is the UniformSolutionCreater associated with the Encoding, but it was considered not usable here 340 var bounds = Problem.Encoding.Bounds; 341 for (var j = 0; j < Problem.Encoding.Length; j++) { 337 342 var dim = j % bounds.Rows; 338 343 x[j] = random.NextDouble() * (bounds[dim, 1] - bounds[dim, 0]) + bounds[dim, 0]; … … 344 349 private void InitStrategy() { 345 350 const int lambda = 1; 346 double n = Problem. ProblemSize;351 double n = Problem.Encoding.Length; 347 352 348 353 var targetSuccessProbability = 1.0 / (5.0 + Math.Sqrt(lambda) / 2.0); … … 377 382 table.Rows.Add(new DataRow(SpacingResultName)); 378 383 AddResult(TimetableResultName, "Different quality meassures in a timeseries", table); 379 AddResult( SolutionsResultName, "The current front", new DoubleMatrix());384 AddResult(CurrentFrontResultName, "The current front", new DoubleMatrix()); 380 385 AddResult(ScatterPlotResultName, "A scatterplot displaying the evaluated solutions and (if available) the analytically optimal front", new ScatterPlotContent(null, null, null, 2)); 381 386 382 if (Problem.BestKnownFront != null) { 383 ResultsBestKnownHypervolume = Hypervolume.Calculate(Utilities.ToArray(Problem.BestKnownFront), Problem.TestFunction.ReferencePoint(Problem.Objectives), Problem.Maximization); 387 var problem = Problem as MultiObjectiveTestFunctionProblem; 388 if (problem == null) return; 389 if (problem.BestKnownFront != null) { 390 ResultsBestKnownHypervolume = Hypervolume.Calculate(Utilities.ToArray(problem.BestKnownFront), problem.TestFunction.ReferencePoint(problem.Objectives), Problem.Maximization); 384 391 ResultsDifferenceBestKnownHypervolume = ResultsBestKnownHypervolume; 385 392 } 386 ResultsScatterPlot = new ScatterPlotContent(n ull, null, Utilities.ToArray(Problem.BestKnownFront), Problem.Objectives);393 ResultsScatterPlot = new ScatterPlotContent(new double[0][], new double[0][], Utilities.ToArray(problem.BestKnownFront), problem.Objectives); 387 394 } 388 395 #endregion … … 411 418 } 412 419 private double[] Evaluate(RealVector x) { 413 var res = Problem.Evaluate(x); 420 var s = new Scope(); 421 s.Variables.Add(new Variable(Problem.Encoding.Name, x)); 422 var ind = new SingleEncodingIndividual(Problem.Encoding, s); 423 var res = Problem.Evaluate(ind, random); 414 424 ResultsEvaluations++; 415 425 return res; 416 426 } 417 private staticdouble[] Penalize(RealVector x, RealVector t, IEnumerable<double> fitness) {418 var penalty = x. Select((t1, i) => t1 - t[i]).Sum(d => d * d);419 return fitness.Select( v =>v + penalty).ToArray();427 private double[] Penalize(RealVector x, RealVector t, IEnumerable<double> fitness) { 428 var penalty = x.Zip(t, (a, b) => (a - b) * (a - b)).Sum() * 1E-6; 429 return fitness.Select((v, i) => Problem.Maximization[i] ? v - penalty : v + penalty).ToArray(); 420 430 } 421 431 private RealVector ClosestFeasible(RealVector x) { 422 var bounds = Problem. Bounds;432 var bounds = Problem.Encoding.Bounds; 423 433 var r = new RealVector(x.Length); 424 434 for (var i = 0; i < x.Length; i++) { … … 429 439 } 430 440 private bool IsFeasable(RealVector offspring) { 431 var bounds = Problem. Bounds;441 var bounds = Problem.Encoding.Bounds; 432 442 for (var i = 0; i < offspring.Length; i++) { 433 443 var dim = i % bounds.Rows; … … 452 462 } 453 463 464 454 465 //now use the indicator to deselect the approximatingly worst elements of the last selected front 455 466 var front1 = fronts[rank]; 467 front1.Sort(new HelpComparer()); 456 468 for (; popSize > length; popSize--) { 457 469 var lc = Indicator.LeastContributer(front1.ToArray(), x => x.PenalizedFitness, Problem); … … 486 498 } 487 499 private void AnalyzeQualityIndicators() { 488 var front = NonDominatedSelect.GetDominatingVectors(solutions.Select(x => x.Fitness), Problem.TestFunction.ReferencePoint(Problem.Objectives), Problem.Maximization, true).ToArray(); 489 var bounds = Problem.Bounds.CloneAsMatrix(); 500 var problem = Problem as MultiObjectiveTestFunctionProblem; 501 if (problem == null) return; 502 503 var front = NonDominatedSelect.GetDominatingVectors(solutions.Select(x => x.Fitness), problem.TestFunction.ReferencePoint(problem.Objectives), Problem.Maximization, true).ToArray(); 504 if (front.Length == 0) return; 505 var bounds = problem.Bounds.CloneAsMatrix(); 490 506 ResultsCrowding = Crowding.Calculate(front, bounds); 491 507 ResultsSpacing = Spacing.Calculate(front); 492 ResultsGenerationalDistance = Problem.BestKnownFront != null ? GenerationalDistance.Calculate(front, Utilities.ToArray(Problem.BestKnownFront), 1) : double.NaN;493 494 ResultsInvertedGenerationalDistance = Problem.BestKnownFront != null ? InvertedGenerationalDistance.Calculate(front, Utilities.ToArray(Problem.BestKnownFront), 1) : double.NaN;495 496 ResultsHypervolume = Hypervolume.Calculate(front, Problem.TestFunction.ReferencePoint(Problem.Objectives), Problem.Maximization);508 ResultsGenerationalDistance = problem.BestKnownFront != null ? GenerationalDistance.Calculate(front, Utilities.ToArray(problem.BestKnownFront), 1) : double.NaN; 509 510 ResultsInvertedGenerationalDistance = problem.BestKnownFront != null ? InvertedGenerationalDistance.Calculate(front, Utilities.ToArray(problem.BestKnownFront), 1) : double.NaN; 511 512 ResultsHypervolume = Hypervolume.Calculate(front, problem.TestFunction.ReferencePoint(problem.Objectives), Problem.Maximization); 497 513 ResultsBestHypervolume = Math.Max(ResultsHypervolume, ResultsBestHypervolume); 498 514 ResultsDifferenceBestKnownHypervolume = ResultsBestKnownHypervolume - ResultsBestHypervolume; … … 510 526 #endregion 511 527 512 528 private class HelpComparer : IComparer<MOCMAESIndividual> { 529 public int Compare(MOCMAESIndividual x, MOCMAESIndividual y) { 530 return x.PenalizedFitness[0].CompareTo(y.PenalizedFitness[0]); 531 } 532 } 513 533 514 534 #region Helpers -
branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/Plugin.cs.frame
r14404 r14577 33 33 [PluginDependency("HeuristicLab.Common", "3.3")] 34 34 [PluginDependency("HeuristicLab.Core", "3.3")] 35 [PluginDependency("HeuristicLab.Core.Views", "3.3")]36 35 [PluginDependency("HeuristicLab.Data", "3.3")] 37 36 [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")] 38 [PluginDependency("HeuristicLab.Operators", "3.3")]39 37 [PluginDependency("HeuristicLab.Optimization", "3.3")] 40 [PluginDependency("HeuristicLab.Optimization.Operators", "3.3")]41 38 [PluginDependency("HeuristicLab.Parameters", "3.3")] 42 39 [PluginDependency("HeuristicLab.Persistence", "3.3")] 43 [PluginDependency("HeuristicLab.Problems.Instances", "3.3")]44 40 [PluginDependency("HeuristicLab.Problems.TestFunctions.MultiObjective", "3.3")] 45 41 [PluginDependency("HeuristicLab.Random", "3.3")] -
branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/Utilities.cs
r14404 r14577 5 5 internal static class Utilities { 6 6 internal static double[][] ToArray(DoubleMatrix m) { 7 if (m == null) return null; 7 8 var i = m.Rows - 1; 8 9 var a = new double[i][]; … … 16 17 17 18 public static int ArgMin<T>(IEnumerable<T> values, IComparer<T> comparer) { 18 var mindex = 0;19 var mindex = -1; 19 20 var min = default(T); 20 21 var i = 0; … … 30 31 } 31 32 public static int ArgMax<T>(IEnumerable<T> values, IComparer<T> comparer) { 32 var mindex = 0;33 var mindex = -1; 33 34 var min = default(T); 34 35 var i = 0;
Note: See TracChangeset
for help on using the changeset viewer.