Changeset 17309
- Timestamp:
- 10/03/19 17:43:11 (5 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/Indicators/HypervolumeIndicator.cs
r17226 r17309 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Optimization; 28 using HeuristicLab.Problems.TestFunctions.MultiObjective;29 28 30 29 namespace HeuristicLab.Algorithms.MOCMAEvolutionStrategy { … … 53 52 54 53 #region Helpers 55 private static double Contribution(IList<double[]> front, int idx, bool[]maximization, double[] refPoint) {54 private static double Contribution(IList<double[]> front, int idx, IReadOnlyList<bool> maximization, double[] refPoint) { 56 55 var point = front[idx]; 57 56 front.RemoveAt(idx); -
branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMAEvolutionStrategy.cs
r17261 r17309 376 376 var bkf = problem.BestKnownFront == null ? null : problem.BestKnownFront.ToArray(); 377 377 if (bkf != null && problem.ReferencePoint != null) { 378 ResultsBestKnownHypervolume = HypervolumeCalculator.CalculateHypervolume(bkf, problem.ReferencePoint, Problem.Maximization );378 ResultsBestKnownHypervolume = HypervolumeCalculator.CalculateHypervolume(bkf, problem.ReferencePoint, Problem.Maximization.CloneAsArray()); 379 379 ResultsDifferenceBestKnownHypervolume = ResultsBestKnownHypervolume; 380 380 } … … 456 456 //perform a non-dominated sort to assign the rank to every element 457 457 int[] ranks; 458 var fronts = DominationCalculator.CalculateAllParetoFronts(parents.ToArray(), parents.Select(i => i.PenalizedFitness).ToArray(), Problem.Maximization , out ranks);458 var fronts = DominationCalculator.CalculateAllParetoFronts(parents.ToArray(), parents.Select(i => i.PenalizedFitness).ToArray(), Problem.Maximization.CloneAsArray(), out ranks); 459 459 460 460 //deselect the highest rank fronts until we would end up with less or equal mu elements -
branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/BoolArray.cs
r17253 r17309 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using System.Text; 23 24 using HEAL.Attic; … … 37 38 public BoolArray(int length) : base(length) { } 38 39 public BoolArray(bool[] elements, bool @readonly = false) : base(elements, @readonly) { } 40 public BoolArray(IReadOnlyList<bool> elements) : base(elements) { } 39 41 40 42 public override IDeepCloneable Clone(Cloner cloner) { -
branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/StringConvertibleArray.cs
r17253 r17309 22 22 #endregion 23 23 24 using System.Collections.Generic; 24 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; … … 38 39 protected StringConvertibleArray(int length) : base(length) { } 39 40 protected StringConvertibleArray(T[] elements, bool @readonly = false) : base(elements, @readonly) { } 41 protected StringConvertibleArray(IReadOnlyList<T> elements) : base(elements) { } 40 42 41 43 protected abstract bool Validate(string value, out string errorMessage); -
branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/ValueTypeArray.cs
r17253 r17309 138 138 } 139 139 protected ValueTypeArray(T[] elements, bool @readonly = false) { 140 if (elements == null) throw new ArgumentNullException( );140 if (elements == null) throw new ArgumentNullException(nameof(elements)); 141 141 array = (T[])elements.Clone(); 142 142 readOnly = @readonly; 143 143 resizable = true; 144 144 elementNames = new List<string>(); 145 } 146 147 protected ValueTypeArray(IReadOnlyList<T> elements) { 148 if (elements == null) throw new ArgumentNullException(nameof(elements)); 149 array = elements.ToArray(); // TODO: array should be an IList<T> 150 readOnly = true; 151 resizable = true; // TODO: really? 152 elementNames = new List<string>(); // TODO: wasteful 145 153 } 146 154 -
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeMultiObjectiveProblem.cs
r17230 r17309 54 54 base.Analyze(trees, qualities, results, random); 55 55 56 var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(trees, qualities, Maximization );56 var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(trees, qualities, Maximization.CloneAsArray()); 57 57 var plot = new ParetoFrontScatterPlot<ISymbolicExpressionTree>(fronts, trees, qualities, Objectives, BestKnownFront); 58 58 results.AddOrUpdateResult("Pareto Front Scatter Plot", plot); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiObjectiveProblemDefinition.cs
r17257 r17309 28 28 public interface IMultiObjectiveProblemDefinition { 29 29 int Objectives { get; } 30 bool[]Maximization { get; }30 IReadOnlyList<bool> Maximization { get; } 31 31 IReadOnlyList<double[]> BestKnownFront { get; } 32 32 double[] ReferencePoint { get; } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveProblem.cs
r17226 r17309 37 37 where TEncodedSolution : class, IEncodedSolution { 38 38 #region Parameternames 39 public const string MaximizationParameterName = "Maximization";40 39 public const string BestKnownFrontParameterName = "BestKnownFront"; 41 40 public const string ReferencePointParameterName = "ReferencePoint"; … … 43 42 44 43 #region Parameterproperties 45 public IValueParameter<BoolArray> MaximizationParameter { 46 get { return (IValueParameter<BoolArray>)Parameters[MaximizationParameterName]; } 47 } 44 [Storable] protected IValueParameter<BoolArray> MaximizationParameter { get; } 48 45 public IValueParameter<DoubleMatrix> BestKnownFrontParameter { 49 46 get { return (IValueParameter<DoubleMatrix>)Parameters[BestKnownFrontParameterName]; } … … 60 57 protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner) 61 58 : base(original, cloner) { 59 MaximizationParameter = cloner.Clone(original.MaximizationParameter); 62 60 ParameterizeOperators(); 63 61 } 64 62 65 63 protected MultiObjectiveProblem() : base() { 66 Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly())); 64 MaximizationParameter = new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", new BoolArray(new[] { false, false }).AsReadOnly()) { Hidden = true, ReadOnly = true }; 65 Parameters.Add(MaximizationParameter); 67 66 Parameters.Add(new OptionalValueParameter<DoubleMatrix>(BestKnownFrontParameterName, "A double matrix representing the best known qualites for this problem (aka points on the Pareto front). Points are to be given in a row-wise fashion.")); 68 67 Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem")); … … 73 72 74 73 protected MultiObjectiveProblem(TEncoding encoding) : base(encoding) { 75 Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly())); 74 MaximizationParameter = new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", new BoolArray(new[] { false, false }).AsReadOnly()) { Hidden = true, ReadOnly = true }; 75 Parameters.Add(MaximizationParameter); 76 76 Parameters.Add(new OptionalValueParameter<DoubleMatrix>(BestKnownFrontParameterName, "A double matrix representing the best known qualites for this problem (aka points on the Pareto front). Points are to be given in a row-wise fashion.")); 77 77 Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The refrence point for hypervolume calculations on this problem")); … … 89 89 get { return Maximization.Length; } 90 90 } 91 public abstract bool[] Maximization { get; } 91 IReadOnlyList<bool> IMultiObjectiveProblemDefinition.Maximization { 92 get { return Maximization.CloneAsArray(); } 93 } 94 public BoolArray Maximization { 95 get { return MaximizationParameter.Value; } 96 set { 97 if (Maximization == value) return; 98 MaximizationParameter.ReadOnly = false; 99 MaximizationParameter.Value = value.AsReadOnly(); 100 MaximizationParameter.ReadOnly = true; 101 } 102 } 92 103 93 104 public virtual IReadOnlyList<double[]> BestKnownFront { … … 168 179 #region IMultiObjectiveHeuristicOptimizationProblem Members 169 180 IParameter IMultiObjectiveHeuristicOptimizationProblem.MaximizationParameter { 170 get { return Parameters[MaximizationParameterName]; }181 get { return MaximizationParameter; } 171 182 } 172 183 IMultiObjectiveEvaluator IMultiObjectiveHeuristicOptimizationProblem.Evaluator { -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/MultiObjective/DominationCalculator.cs
r17230 r17309 47 47 /// <param name="dominateOnEqualQualities">Whether solutions of exactly equal quality should dominate one another.</param> 48 48 /// <returns>The pareto front containing the best solutions and their associated quality resp. fitness.</returns> 49 public static List<Tuple<T, double[]>> CalculateBestParetoFront<T>(T[] solutions, double[][] qualities, bool[]maximization, bool dominateOnEqualQualities = true) {49 public static List<Tuple<T, double[]>> CalculateBestParetoFront<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, bool dominateOnEqualQualities = true) { 50 50 var populationSize = solutions.Length; 51 51 Dictionary<T, List<int>> dominatedIndividuals; … … 72 72 /// <param name="dominateOnEqualQualities">Whether solutions of exactly equal quality should dominate one another.</param> 73 73 /// <returns>A sorted list of the pareto fronts from best to worst.</returns> 74 public static List<List<Tuple<T, double[]>>> CalculateAllParetoFronts<T>(T[] solutions, double[][] qualities, bool[]maximization, out int[] rank, bool dominateOnEqualQualities = true) {74 public static List<List<Tuple<T, double[]>>> CalculateAllParetoFronts<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, out int[] rank, bool dominateOnEqualQualities = true) { 75 75 var populationSize = solutions.Length; 76 76 … … 101 101 } 102 102 103 private static List<Tuple<T, double[]>> CalculateBestFront<T>(T[] solutions, double[][] qualities, bool[]maximization, bool dominateOnEqualQualities, int populationSize, out Dictionary<T, List<int>> dominatedIndividuals, out int[] dominationCounter, out int[] rank) {103 private static List<Tuple<T, double[]>> CalculateBestFront<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, bool dominateOnEqualQualities, int populationSize, out Dictionary<T, List<int>> dominatedIndividuals, out int[] dominationCounter, out int[] rank) { 104 104 var front = new List<Tuple<T, double[]>>(); 105 105 dominatedIndividuals = new Dictionary<T, List<int>>(); … … 155 155 /// <param name="dominateOnEqualQualities">Whether solutions of exactly equal quality should dominate one another.</param> 156 156 /// <returns>A sorted list of the pareto fronts where each front contains the indices of the <paramref name="solutions"/> and <paramref name="qualities"/>.</returns> 157 public static List<List<int>> CalculateAllParetoFrontsIndices<T>(T[] solutions, double[][] qualities, bool[]maximization, bool dominateOnEqualQualities = true) {157 public static List<List<int>> CalculateAllParetoFrontsIndices<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, bool dominateOnEqualQualities = true) { 158 158 return CalculateAllParetoFrontsIndices(solutions, qualities, maximization, out var rank, dominateOnEqualQualities); 159 159 } … … 178 178 /// <param name="dominateOnEqualQualities">Whether solutions of exactly equal quality should dominate one another.</param> 179 179 /// <returns>A sorted list of the pareto fronts where each front contains the indices of the <paramref name="solutions"/> and <paramref name="qualities"/>.</returns> 180 public static List<List<int>> CalculateAllParetoFrontsIndices<T>(T[] solutions, double[][] qualities, bool[]maximization, out int[] rank, bool dominateOnEqualQualities = true) {180 public static List<List<int>> CalculateAllParetoFrontsIndices<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, out int[] rank, bool dominateOnEqualQualities = true) { 181 181 var populationSize = solutions.Length; 182 182 … … 207 207 } 208 208 209 private static List<int> CalculateBestFrontIndices<T>(T[] solutions, double[][] qualities, bool[]maximization, bool dominateOnEqualQualities, int populationSize, List<int>[] dominatedIndividuals, int[] dominationCounter, int[] rank) {209 private static List<int> CalculateBestFrontIndices<T>(T[] solutions, double[][] qualities, IReadOnlyList<bool> maximization, bool dominateOnEqualQualities, int populationSize, List<int>[] dominatedIndividuals, int[] dominationCounter, int[] rank) { 210 210 var front = new List<int>(); 211 211 for (var pI = 0; pI < populationSize - 1; pI++) { … … 244 244 /// <param name="dominateOnEqualQualities">Whether the result should be Dominates in case both fitness vectors are exactly equal</param> 245 245 /// <returns>Dominates if left dominates right, IsDominated if right dominates left and IsNonDominated otherwise.</returns> 246 public static DominationResult Dominates(double[] left, double[] right, bool[]maximizations, bool dominateOnEqualQualities) {246 public static DominationResult Dominates(double[] left, double[] right, IReadOnlyList<bool> maximizations, bool dominateOnEqualQualities) { 247 247 //mkommend Caution: do not use LINQ.SequenceEqual for comparing the two quality arrays (left and right) due to performance reasons 248 248 if (dominateOnEqualQualities) { -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/MultiObjective/HypervolumeCalculator.cs
r17225 r17309 26 26 namespace HeuristicLab.Optimization { 27 27 public static class HypervolumeCalculator { 28 public static double[] CalculateNadirPoint(IEnumerable<double[]> qualities, bool[]maximization) {28 public static double[] CalculateNadirPoint(IEnumerable<double[]> qualities, IReadOnlyList<bool> maximization) { 29 29 var res = maximization.Select(m => m ? double.MaxValue : double.MinValue).ToArray(); 30 30 foreach (var quality in qualities) … … 59 59 /// </summary> 60 60 /// 61 public static double CalculateHypervolume(IList<double[]> qualities, double[] referencePoint, bool[]maximization) {61 public static double CalculateHypervolume(IList<double[]> qualities, double[] referencePoint, IReadOnlyList<bool> maximization) { 62 62 qualities = qualities.Where(vec => DominationCalculator.Dominates(vec, referencePoint, maximization, false) == DominationResult.Dominates).ToArray(); 63 63 if (qualities.Count== 0) return 0; //TODO computation for negative hypervolume? 64 if (maximization. Length== 2)64 if (maximization.Count == 2) 65 65 return Calculate2D(qualities, referencePoint, maximization); 66 66 67 if ( Array.TrueForAll(maximization,x => !x))67 if (maximization.All(x => !x)) 68 68 return CalculateMultiDimensional(qualities, referencePoint); 69 69 throw new NotImplementedException("HypervolumeCalculator calculation for more than two dimensions is supported only with minimization problems."); … … 78 78 /// <param name="maximization"></param> 79 79 /// <returns></returns> 80 public static double Calculate2D(IList<double[]> front, double[] referencePoint, bool[]maximization) {80 public static double Calculate2D(IList<double[]> front, double[] referencePoint, IReadOnlyList<bool> maximization) { 81 81 if (front == null) throw new ArgumentNullException("front"); 82 82 if (referencePoint == null) throw new ArgumentNullException("referencePoint"); -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/MultiObjectiveExternalEvaluationProblem.cs
r17226 r17309 90 90 public MultiObjectiveExternalEvaluationProblem(TEncoding encoding) 91 91 : base(encoding) { 92 Parameters.Remove("Maximization"); // readonly in base class93 Parameters.Add(new FixedValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", new BoolArray()));92 MaximizationParameter.ReadOnly = false; 93 MaximizationParameter.Value = new BoolArray(new [] { false, false }); 94 94 Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions.")); 95 95 Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() })); … … 99 99 100 100 #region Multi Objective Problem Overrides 101 public override bool[] Maximization {102 get {103 return Parameters.ContainsKey("Maximization") ? ((IValueParameter<BoolArray>)Parameters["Maximization"]).Value.ToArray() : new bool[0];104 }105 }106 107 101 public virtual void SetMaximization(bool[] maximization) { 108 102 ((IStringConvertibleArray)MaximizationParameter.Value).Length = maximization.Length; -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/CompiledProblemDefinition.cs
r17226 r17309 23 23 using System.Collections.Generic; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Data;26 25 using HeuristicLab.Optimization; 27 26 … … 78 77 79 78 #region ISingleObjectiveProblemDefinition<TEncoding,TEncodedSolution> Members 80 public int Objectives => Maximization. Length;81 public abstract bool[]Maximization { get; }79 public int Objectives => Maximization.Count; 80 public abstract IReadOnlyList<bool> Maximization { get; } 82 81 public abstract IReadOnlyList<double[]> BestKnownFront { get; } 83 82 public abstract double[] ReferencePoint { get; } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProblemDefinitionScript.cs
r17226 r17309 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data;27 26 using HeuristicLab.Optimization; 28 27 … … 52 51 public IReadOnlyList<double[]> BestKnownFront => CompiledProblemDefinition.BestKnownFront; 53 52 public double[] ReferencePoint => CompiledProblemDefinition.ReferencePoint; 54 public bool[]Maximization => CompiledProblemDefinition.Maximization;53 public IReadOnlyList<bool> Maximization => CompiledProblemDefinition.Maximization; 55 54 56 55 -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs
r17226 r17309 94 94 95 95 private void OnProblemDefinitionChanged() { 96 Parameters.Remove("Maximization"); 97 Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()) {Hidden = true}); 96 Maximization = new BoolArray(ProblemDefinition.Maximization); 98 97 Encoding = (TEncoding)ProblemScript.Encoding.Clone(); 99 98 … … 107 106 private void OnProblemScriptNameChanged() { 108 107 Name = ProblemScript.Name; 109 }110 111 public override bool[] Maximization {112 get { return Parameters.ContainsKey("ProblemScript") ? ProblemDefinition.Maximization : new[] {false}; }113 108 } 114 109 -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/Templates/MultiObjectiveCombinedEncodingProblem_Template.cs
r17225 r17309 16 16 namespace HeuristicLab.Problems.Programmable { 17 17 public class CompiledMultiObjectiveProblemDefinition : CompiledMultiObjectiveProblemDefinition<CombinedEncoding, CombinedSolution> { 18 public override bool[]Maximization { get { return new[] { true, false }; } }18 public override IReadOnlyList<bool> Maximization { get { return new[] { true, false }; } } 19 19 public override double[] ReferencePoint { get { return null; } } 20 20 public override IReadOnlyList<double[]> BestKnownFront { get { return null; } } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/MultiObjectiveTestFunctionProblem.cs
r17261 r17309 20 20 #endregion 21 21 using System; 22 using System.Linq;23 22 using HEAL.Attic; 24 23 using HeuristicLab.Analysis; … … 52 51 53 52 #region Properties 54 public override bool[] Maximization {55 get {56 //necessary because of virtual member call in base ctor57 if (!Parameters.ContainsKey("TestFunction")) return new bool[0];58 return TestFunction.Maximization(Objectives).ToArray();59 }60 }61 62 53 public int ProblemSize { 63 54 get { return ProblemSizeParameter.Value.Value; } … … 141 132 #region Events 142 133 private void UpdateParameterValues() { 143 Parameters.Remove(MaximizationParameterName); 144 Parameters.Add(new FixedValueParameter<BoolArray>(MaximizationParameterName, "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(TestFunction.Maximization(Objectives)).AsReadOnly())); 134 Maximization = new BoolArray(TestFunction.Maximization(Objectives)); 145 135 146 136 Parameters.Remove(BestKnownFrontParameterName);
Note: See TracChangeset
for help on using the changeset viewer.