Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2679_HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/MultiObjectiveGoalSeekingProblem.cs

Last change on this file was 16901, checked in by bburlacu, 6 years ago

#2679: Update branch: framework version 4.6.1, use heal.attic for persistence, some minor refactoring.

File size: 13.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HEAL.Attic;
23using HeuristicLab.Collections;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Problems.DataAnalysis;
31using System;
32using System.Collections.Generic;
33using System.Linq;
34
35namespace HeuristicLab.GoalSeeking {
36  [Item("Goal seeking problem (multi-objective)", "Represents a single objective optimization problem which uses configurable regression models to evaluate targets from a given dataset.")]
37  [Creatable("Problems")]
38  [StorableType("76EA4627-FD52-41D0-A594-20C99F700F46")]
39  public sealed class MultiObjectiveGoalSeekingProblem : MultiObjectiveBasicProblem<RealVectorEncoding>, IGoalSeekingProblem {
40    #region parameter names
41    private const string InputsParameterName = "Inputs";
42    private const string GoalsParameterName = "Goals";
43    private const string ModelsParameterName = "Models";
44    private const string QualitySumCutoffParameterName = "QualitySumCutoff";
45    #endregion
46
47    #region parameters
48    public IValueParameter<CheckedItemList<InputParameter>> InputsParameter {
49      get { return (IValueParameter<CheckedItemList<InputParameter>>)Parameters[InputsParameterName]; }
50    }
51    public IValueParameter<CheckedItemList<GoalParameter>> GoalsParameter {
52      get { return (IValueParameter<CheckedItemList<GoalParameter>>)Parameters[GoalsParameterName]; }
53    }
54    public IFixedValueParameter<ItemList<IRegressionModel>> ModelsParameter {
55      get { return (IFixedValueParameter<ItemList<IRegressionModel>>)Parameters[ModelsParameterName]; }
56    }
57    public IFixedValueParameter<DoubleValue> QualitySumCutoffParameter {
58      get { return (IFixedValueParameter<DoubleValue>)Parameters[QualitySumCutoffParameterName]; }
59    }
60    #endregion
61
62    #region IGoalSeekingProblem implementation
63    public IEnumerable<IRegressionModel> Models {
64      get { return ModelsParameter.Value; }
65    }
66
67    public IEnumerable<GoalParameter> Goals {
68      get { return GoalsParameter.Value; }
69    }
70
71    public IEnumerable<InputParameter> Inputs {
72      get { return InputsParameter.Value; }
73    }
74
75    public void AddModel(IRegressionModel model) {
76      var models = ModelsParameter.Value;
77      models.Add(model);
78      GoalSeekingUtil.RaiseEvent(this, ModelsChanged);
79    }
80
81    public void RemoveModel(IRegressionModel model) {
82      var models = ModelsParameter.Value;
83      models.Remove(model);
84      GoalSeekingUtil.RaiseEvent(this, ModelsChanged);
85    }
86
87    public void Configure(IRegressionProblemData problemData, int row) {
88      GoalSeekingUtil.Configure(Goals, Inputs, problemData, row);
89    }
90
91    public IEnumerable<double> GetEstimatedGoalValues(IEnumerable<double> parameterValues, bool round = false) {
92      var ds = (ModifiableDataset)dataset.Clone();
93      foreach (var parameter in ActiveInputs.Zip(parameterValues, (p, v) => new { p.Name, Value = v })) {
94        ds.SetVariableValue(parameter.Value, parameter.Name, 0);
95      }
96      var rows = new[] { 0 }; // actually just one row
97      return round ? ActiveGoals.Select(t => RoundToNearestStepMultiple(GetModels(t.Name).Average(m => m.GetEstimatedValues(ds, rows).Single()), t.Step))
98                   : ActiveGoals.Select(t => GetModels(t.Name).Average(m => m.GetEstimatedValues(ds, rows).Single()));
99    }
100
101    public event EventHandler ModelsChanged;
102    public event EventHandler TargetsChanged;
103    public event EventHandler ParametersChanged;
104    #endregion
105
106    private IEnumerable<GoalParameter> ActiveGoals {
107      get { return Goals.Where(x => x.Active); }
108    }
109    private IEnumerable<InputParameter> ActiveInputs {
110      get { return Inputs.Where(x => x.Active); }
111    }
112    private double QualitySumCutoff {
113      get { return QualitySumCutoffParameter.Value.Value; }
114    }
115
116    [Storable]
117    private ModifiableDataset dataset; // modifiable dataset
118
119    [Storable]
120    private bool[] maximization;
121    public override bool[] Maximization {
122      get { return maximization ?? new bool[] { false }; }
123    }
124
125    public ValueParameter<BoolArray> MaximizationParameter {
126      get { return (ValueParameter<BoolArray>)Parameters["Maximization"]; }
127    }
128
129    #region constructors
130    [StorableConstructor]
131    private MultiObjectiveGoalSeekingProblem(StorableConstructorFlag _) : base(_) { }
132
133    private MultiObjectiveGoalSeekingProblem(MultiObjectiveGoalSeekingProblem original, Cloner cloner) : base(original, cloner) {
134      this.dataset = cloner.Clone(original.dataset);
135
136      RegisterEvents();
137    }
138
139    public override IDeepCloneable Clone(Cloner cloner) {
140      return new MultiObjectiveGoalSeekingProblem(this, cloner);
141    }
142
143    [StorableHook(HookType.AfterDeserialization)]
144    private void AfterDeserialization() {
145      RegisterEvents();
146    }
147
148    public MultiObjectiveGoalSeekingProblem() {
149      dataset = new ModifiableDataset();
150      Parameters.Add(new ValueParameter<CheckedItemList<InputParameter>>(InputsParameterName));
151      Parameters.Add(new ValueParameter<CheckedItemList<GoalParameter>>(GoalsParameterName));
152      Parameters.Add(new FixedValueParameter<ItemList<IRegressionModel>>(ModelsParameterName, new ItemList<IRegressionModel>()));
153      Parameters.Add(new FixedValueParameter<DoubleValue>(QualitySumCutoffParameterName, new DoubleValue(0.2)));
154      QualitySumCutoffParameter.Hidden = true;
155      EncodingParameter.Hidden = true;
156      EvaluatorParameter.Hidden = true;
157      SolutionCreatorParameter.Hidden = true;
158      MaximizationParameter.Hidden = true;
159      RegisterEvents();
160    }
161    #endregion
162
163    public override double[] Evaluate(Individual individual, IRandom random) {
164      var vector = individual.RealVector();
165      vector.ElementNames = ActiveInputs.Select(x => x.Name);
166      int i = 0;
167      // round vector according to parameter step sizes
168      foreach (var parameter in ActiveInputs) {
169        vector[i] = RoundToNearestStepMultiple(vector[i], parameter.Step);
170        ++i;
171      }
172      var estimatedValues = GetEstimatedGoalValues(vector, round: true);
173      var qualities = ActiveGoals.Zip(estimatedValues, (t, v) => new { Target = t, EstimatedValue = v })
174                                 .Select(x => x.Target.Weight * Math.Pow(x.EstimatedValue - x.Target.Goal, 2) / x.Target.Variance);
175      return qualities.ToArray();
176    }
177
178    #region pareto analyzer
179    public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
180      var matrix = FilterFrontsByQualitySum(individuals, qualities, Math.Max(QualitySumCutoff, qualities.Min(x => x.Sum())));
181      const string resultName = "Pareto Front Solutions"; // disclaimer: not really a pareto front
182      if (!results.ContainsKey(resultName)) {
183        results.Add(new Result(resultName, matrix));
184      } else {
185        results[resultName].Value = matrix;
186      }
187      base.Analyze(individuals, qualities, results, random);
188    }
189
190    private DoubleMatrix FilterFrontsByQualitySum(Individual[] individuals, double[][] qualities, double qualitySumCutoff) {
191      var activeParameters = ActiveInputs.ToList();
192      var activeGoals = ActiveGoals.ToList();
193      var filteredModels = new List<double[]>();
194      var rowNames = new List<string>();
195      // build list of column names by combining target and parameter names (with their respective original and estimated values)
196      var columnNames = new List<string> { "Quality Sum" };
197      foreach (var target in activeGoals) {
198        columnNames.Add(target.Name);
199        columnNames.Add(target.Name + " (estimated)");
200      }
201      foreach (var parameter in activeParameters) {
202        columnNames.Add(parameter.Name);
203        columnNames.Add(parameter.Name + " (estimated)");
204        columnNames.Add(parameter.Name + " (deviation)");
205      }
206      // filter models based on their quality sum; remove duplicate models
207      var dec = new DoubleEqualityComparer(); // comparer which uses the IsAlmost method for comparing floating point numbers
208      for (int i = 0; i < individuals.Length; ++i) {
209        var qualitySum = qualities[i].Sum();
210        if (qualitySum > qualitySumCutoff)
211          continue;
212        var vector = individuals[i].RealVector();
213        var estimatedValues = GetEstimatedGoalValues(vector).ToList();
214        var rowValues = new double[columnNames.Count];
215        rowValues[0] = qualitySum;
216        int offset = 1;
217        for (int j = 0; j < activeGoals.Count * 2; j += 2) {
218          int k = j + offset;
219          rowValues[k] = activeGoals[j / 2].Goal; // original value
220          rowValues[k + 1] = estimatedValues[j / 2]; // estimated value
221        }
222        offset += activeGoals.Count * 2;
223        for (int j = 0; j < activeParameters.Count * 3; j += 3) {
224          int k = j + offset;
225          rowValues[k] = activeParameters[j / 3].Value;
226          rowValues[k + 1] = vector[j / 3];
227          rowValues[k + 2] = rowValues[k + 1] - rowValues[k];
228        }
229        if (!filteredModels.Any(x => x.SequenceEqual(rowValues, dec))) {
230          rowNames.Add((i + 1).ToString());
231          filteredModels.Add(rowValues);
232        }
233      }
234      var matrix = new DoubleMatrix(filteredModels.Count, columnNames.Count) { RowNames = rowNames, ColumnNames = columnNames, SortableView = true };
235      for (int i = 0; i < filteredModels.Count; ++i) {
236        for (int j = 0; j < filteredModels[i].Length; ++j) {
237          matrix[i, j] = filteredModels[i][j];
238        }
239      }
240      return matrix;
241    }
242    #endregion
243
244    #region event handlers
245    private void RegisterEvents() {
246      ModelsParameter.Value.ItemsAdded += ModelCollection_ItemsChanged;
247      ModelsParameter.Value.ItemsRemoved += ModelCollection_ItemsChanged;
248      GoalsParameter.Value.CheckedItemsChanged += GoalSeekingUtil.Goals_CheckedItemsChanged;
249      InputsParameter.Value.CheckedItemsChanged += GoalSeekingUtil.Inputs_CheckedItemsChanged;
250
251      foreach (var input in Inputs)
252        input.Changed += InputParameterChanged;
253
254      foreach (var goal in Goals)
255        goal.Changed += GoalParameterChanged;
256    }
257
258    private void ModelCollection_ItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<IRegressionModel>> e) {
259      if (e.Items == null || !e.Items.Any()) return;
260      GoalSeekingUtil.UpdateInputs(InputsParameter.Value, Models, InputParameterChanged);
261      GoalSeekingUtil.UpdateEncoding(Encoding, ActiveInputs);
262      dataset = Inputs.Any() ? new ModifiableDataset(Inputs.Select(x => x.Name), Inputs.Select(x => new List<double> { x.Value })) : new ModifiableDataset();
263      GoalSeekingUtil.UpdateTargets(GoalsParameter.Value, Models, GoalParameterChanged);
264      GoalSeekingUtil.RaiseEvent(this, ModelsChanged);
265    }
266
267    private void InputParameterChanged(object sender, EventArgs args) {
268      var inputParameter = (InputParameter)sender;
269      var inputs = InputsParameter.Value;
270      if (inputs.ItemChecked(inputParameter) != inputParameter.Active)
271        inputs.SetItemCheckedState(inputParameter, inputParameter.Active);
272      GoalSeekingUtil.UpdateEncoding(Encoding, ActiveInputs);
273    }
274
275    private void GoalParameterChanged(object sender, EventArgs args) {
276      var goalParameter = (GoalParameter)sender;
277      var goals = GoalsParameter.Value;
278      if (goals.ItemChecked(goalParameter) != goalParameter.Active)
279        goals.SetItemCheckedState(goalParameter, goalParameter.Active);
280    }
281    #endregion
282
283    #region helper methods
284    // method which throws an exception that can be caught in the event handler if the check fails
285    private void CheckIfDatasetContainsTarget(string target) {
286      if (dataset.DoubleVariables.All(x => x != target))
287        throw new ArgumentException(string.Format("Model target \"{0}\" does not exist in the dataset.", target));
288    }
289    private static double RoundToNearestStepMultiple(double value, double step) {
290      return step * (long)Math.Round(value / step);
291    }
292    private IEnumerable<IRegressionModel> GetModels(string target) {
293      return Models.Where(x => x.TargetVariable == target);
294    }
295    private class DoubleEqualityComparer : IEqualityComparer<double> {
296      public bool Equals(double x, double y) { return x.IsAlmost(y); }
297      public int GetHashCode(double obj) { return obj.GetHashCode(); }
298    }
299    #endregion
300  }
301}
Note: See TracBrowser for help on using the repository browser.