Changeset 17690
- Timestamp:
- 07/21/20 17:06:15 (4 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/DoubleMatrix.cs
r17253 r17690 42 42 public DoubleMatrix(double[,] elements, IEnumerable<string> columnNames, bool @readonly = false) : base(elements, columnNames, @readonly) { } 43 43 public DoubleMatrix(double[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames, bool @readonly = false) : base(elements, columnNames, rowNames, @readonly) { } 44 45 public static DoubleMatrix FromRows(IList<double[]> elements, bool @readonly = false, IEnumerable<string> columnNames = null, IEnumerable<string> rowNames = null) { 46 if (elements.Count == 0) return new DoubleMatrix(0, 0); 47 var mat = new double[elements.Count, elements[0].Length]; 48 for (var r = 0; r < mat.GetLength(0); r++) 49 for (var c = 0; c < mat.GetLength(1); c++) 50 mat[r, c] = elements[r][c]; 51 // TODO: We should avoid the memory copy in this case 52 return new DoubleMatrix(mat, columnNames, rowNames, @readonly); 53 } 54 public static DoubleMatrix FromColumns(IList<double[]> elements, bool @readonly = false, IEnumerable<string> columnNames = null, IEnumerable<string> rowNames = null) { 55 if (elements.Count == 0) return new DoubleMatrix(0, 0); 56 var mat = new double[elements[0].Length, elements.Count]; 57 for (var c = 0; c < mat.GetLength(1); c++) 58 for (var r = 0; r < mat.GetLength(0); r++) 59 mat[r, c] = elements[c][r]; 60 // TODO: We should avoid the memory copy in this case 61 return new DoubleMatrix(mat, columnNames, rowNames, @readonly); 62 } 44 63 45 64 public override IDeepCloneable Clone(Cloner cloner) { -
branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/ValueTypeMatrix.cs
r17253 r17690 202 202 } 203 203 204 public IEnumerable<T[]> CloneByRows() { 205 for (var r = 0; r < matrix.GetLength(0); r++) 206 yield return GetRow(r).ToArray(); 207 } 208 209 public IEnumerable<T[]> CloneByColumn() { 210 for (var c = 0; c < matrix.GetLength(1); c++) 211 yield return GetColumn(c).ToArray(); 212 } 213 204 214 public virtual IEnumerable<T> GetRow(int row) { 205 215 for (var col = 0; col < Columns; col++) { -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveProblem.cs
r17620 r17690 38 38 where TEncoding : class, IEncoding<TEncodedSolution> 39 39 where TEncodedSolution : class, IEncodedSolution { 40 #region Parameter names41 public const string BestKnownFrontParameterName = "BestKnownFront";42 public const string ReferencePointParameterName = "ReferencePoint";43 #endregion44 40 45 41 #region Parameter properties 46 42 [Storable] public IValueParameter<BoolArray> MaximizationParameter { get; } 47 public IValueParameter<DoubleMatrix> BestKnownFrontParameter { 48 get { return (IValueParameter<DoubleMatrix>)Parameters[BestKnownFrontParameterName]; } 49 } 50 public IValueParameter<DoubleArray> ReferencePointParameter { 51 get { return (IValueParameter<DoubleArray>)Parameters[ReferencePointParameterName]; } 52 } 43 [Storable] public IValueParameter<DoubleMatrix> BestKnownFrontParameter { get; } 44 [Storable] public IValueParameter<DoubleArray> ReferencePointParameter { get; } 53 45 #endregion 54 46 … … 60 52 : base(original, cloner) { 61 53 MaximizationParameter = cloner.Clone(original.MaximizationParameter); 54 BestKnownFrontParameter = cloner.Clone(original.BestKnownFrontParameter); 55 ReferencePointParameter = cloner.Clone(original.ReferencePointParameter); 62 56 ParameterizeOperators(); 63 57 } 64 58 65 59 protected MultiObjectiveProblem(TEncoding encoding) : base(encoding) { 66 MaximizationParameter = new ValueParameter<BoolArray>("Maximization", "The dimensions correspond to the different objectives: False if the objective should be minimized, true if it should be maximized..", new BoolArray(new bool[] { }, @readonly: true)); 67 Parameters.Add(MaximizationParameter); 68 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.")); 69 Parameters.Add(new OptionalValueParameter<DoubleArray>(ReferencePointParameterName, "The reference point for hypervolume calculations on this problem")); 60 Parameters.Add(MaximizationParameter = new ValueParameter<BoolArray>("Maximization", "The dimensions correspond to the different objectives: False if the objective should be minimized, true if it should be maximized..", new BoolArray(new bool[] { }, @readonly: true))); 61 Parameters.Add(BestKnownFrontParameter = new OptionalValueParameter<DoubleMatrix>("Best Known Front", "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.")); 62 Parameters.Add(ReferencePointParameter = new OptionalValueParameter<DoubleArray>("Reference Point", "The reference point for hypervolume calculations on this problem")); 70 63 Operators.Add(Evaluator); 71 64 Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>()); … … 92 85 public virtual IReadOnlyList<double[]> BestKnownFront { 93 86 get { 94 if (!Parameters.ContainsKey(BestKnownFrontParameterName)) return null;95 87 var mat = BestKnownFrontParameter.Value; 96 88 if (mat == null) return null; 97 var v = new double[mat.Rows][]; 98 for (var i = 0; i < mat.Rows; i++) { 99 var r = v[i] = new double[mat.Columns]; 100 for (var j = 0; j < mat.Columns; j++) { 101 r[j] = mat[i, j]; 102 } 103 } 104 return v; 105 } 106 set { 107 if (value == null || value.Count == 0) { 108 BestKnownFrontParameter.Value = new DoubleMatrix(); 109 return; 110 } 111 var mat = new DoubleMatrix(value.Count, value[0].Length); 112 for (int i = 0; i < value.Count; i++) { 113 for (int j = 0; j < value[i].Length; j++) { 114 mat[i, j] = value[i][j]; 115 } 116 } 117 118 BestKnownFrontParameter.Value = mat; 89 return mat.CloneByRows().ToList(); 119 90 } 120 91 } 92 public virtual void SetBestKnownFront(IList<double[]> front) { 93 if (front == null || front.Count == 0) { 94 BestKnownFrontParameter.Value = null; 95 return; 96 } 97 BestKnownFrontParameter.Value = DoubleMatrix.FromRows(front); 98 } 121 99 public virtual double[] ReferencePoint { 122 get { return ReferencePointParameter.Value != null ? ReferencePointParameter.Value.CloneAsArray() : null; }100 get { return ReferencePointParameter.Value?.CloneAsArray(); } 123 101 set { ReferencePointParameter.Value = new DoubleArray(value); } 124 102 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/MultiObjectiveTestFunctionProblem.cs
r17587 r17690 19 19 */ 20 20 #endregion 21 21 22 using System; 22 23 using System.Threading; … … 75 76 76 77 BestKnownFrontParameter.Hidden = true; 78 BestKnownFrontParameter.ReadOnly = true; 79 ReferencePointParameter.ReadOnly = true; 77 80 78 81 UpdateParameterValues(); … … 82 85 83 86 private void RegisterEventHandlers() { 84 TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;85 ObjectivesParameter.Value.ValueChanged += ObjectivesOnValueChanged;87 IntValueParameterChangeHandler.Create(ObjectivesParameter, ObjectivesOnChanged); 88 ParameterChangeHandler<IMultiObjectiveTestFunction>.Create(TestFunctionParameter, TestFunctionOnChanged); 86 89 } 87 90 … … 113 116 114 117 #region Events 115 private void UpdateParameterValues() {116 Maximization = TestFunction.Maximization(Objectives);117 118 Parameters.Remove(BestKnownFrontParameterName);119 var front = TestFunction.OptimalParetoFront(Objectives);120 var bkf = front != null ? (DoubleMatrix)Utilities.ToMatrix(front).AsReadOnly() : null;121 Parameters.Add(new FixedValueParameter<DoubleMatrix>(BestKnownFrontParameterName, "A double matrix representing the best known qualities for this problem (aka points on the Pareto front). Points are to be given in a row-wise fashion.", bkf));122 123 Parameters.Remove(ReferencePointParameterName);124 Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The reference point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));125 126 BoundsRefParameter.Value = new DoubleMatrix(TestFunction.Bounds(Objectives));127 }128 129 protected override void OnEncodingChanged() {130 base.OnEncodingChanged();131 UpdateParameterValues();132 }133 134 protected override void OnEvaluatorChanged() {135 base.OnEvaluatorChanged();136 UpdateParameterValues();137 }138 139 118 protected override void DimensionOnChanged() { 140 119 base.DimensionOnChanged(); … … 142 121 Dimension = Math.Min(TestFunction.MaximumSolutionLength, Math.Max(TestFunction.MinimumSolutionLength, Dimension)); 143 122 UpdateParameterValues(); 123 OnReset(); 144 124 } 145 125 146 pr ivate void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {126 protected virtual void TestFunctionOnChanged() { 147 127 Dimension = Math.Max(TestFunction.MinimumSolutionLength, Math.Min(Dimension, TestFunction.MaximumSolutionLength)); 148 128 Objectives = Math.Max(TestFunction.MinimumObjectives, Math.Min(Objectives, TestFunction.MaximumObjectives)); 149 Parameters.Remove(ReferencePointParameterName);150 Parameters.Add(new FixedValueParameter<DoubleArray>(ReferencePointParameterName, "The reference point for hypervolume calculations on this problem", new DoubleArray(TestFunction.ReferencePoint(Objectives))));151 129 UpdateParameterValues(); 152 130 OnReset(); 153 131 } 154 132 155 pr ivate void ObjectivesOnValueChanged(object sender, EventArgs eventArgs) {133 protected virtual void ObjectivesOnChanged() { 156 134 Objectives = Math.Min(TestFunction.MaximumObjectives, Math.Max(TestFunction.MinimumObjectives, Objectives)); 157 135 UpdateParameterValues(); 136 OnReset(); 158 137 } 159 138 #endregion 160 139 161 140 #region Helpers 141 private void UpdateParameterValues() { 142 Maximization = TestFunction.Maximization(Objectives); 143 144 BestKnownFrontParameter.Value = DoubleMatrix.FromRows(TestFunction.OptimalParetoFront(Objectives)); 145 ReferencePoint = TestFunction.ReferencePoint(Objectives); 146 147 BoundsRefParameter.Value = new DoubleMatrix(TestFunction.Bounds(Objectives)); 148 } 149 162 150 private void InitializeOperators() { 163 151 Operators.Add(new CrowdingAnalyzer());
Note: See TracChangeset
for help on using the changeset viewer.