[14321] | 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 |
|
---|
[16901] | 22 | using HEAL.Attic;
|
---|
[14321] | 23 | using HeuristicLab.Collections;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[16901] | 31 | using System;
|
---|
| 32 | using System.Collections.Generic;
|
---|
| 33 | using System.Linq;
|
---|
[14321] | 34 |
|
---|
[14324] | 35 | namespace 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.")]
|
---|
[14321] | 37 | [Creatable("Problems")]
|
---|
[16901] | 38 | [StorableType("76EA4627-FD52-41D0-A594-20C99F700F46")]
|
---|
[14321] | 39 | public sealed class MultiObjectiveGoalSeekingProblem : MultiObjectiveBasicProblem<RealVectorEncoding>, IGoalSeekingProblem {
|
---|
[14333] | 40 | #region parameter names
|
---|
| 41 | private const string InputsParameterName = "Inputs";
|
---|
| 42 | private const string GoalsParameterName = "Goals";
|
---|
| 43 | private const string ModelsParameterName = "Models";
|
---|
[14321] | 44 | private const string QualitySumCutoffParameterName = "QualitySumCutoff";
|
---|
[14333] | 45 | #endregion
|
---|
[14321] | 46 |
|
---|
| 47 | #region parameters
|
---|
[14333] | 48 | public IValueParameter<CheckedItemList<InputParameter>> InputsParameter {
|
---|
| 49 | get { return (IValueParameter<CheckedItemList<InputParameter>>)Parameters[InputsParameterName]; }
|
---|
[14321] | 50 | }
|
---|
[14333] | 51 | public IValueParameter<CheckedItemList<GoalParameter>> GoalsParameter {
|
---|
| 52 | get { return (IValueParameter<CheckedItemList<GoalParameter>>)Parameters[GoalsParameterName]; }
|
---|
[14321] | 53 | }
|
---|
[14379] | 54 | public IFixedValueParameter<ItemList<IRegressionModel>> ModelsParameter {
|
---|
| 55 | get { return (IFixedValueParameter<ItemList<IRegressionModel>>)Parameters[ModelsParameterName]; }
|
---|
[14321] | 56 | }
|
---|
[14333] | 57 | public IFixedValueParameter<DoubleValue> QualitySumCutoffParameter {
|
---|
| 58 | get { return (IFixedValueParameter<DoubleValue>)Parameters[QualitySumCutoffParameterName]; }
|
---|
[14321] | 59 | }
|
---|
| 60 | #endregion
|
---|
| 61 |
|
---|
[14333] | 62 | #region IGoalSeekingProblem implementation
|
---|
| 63 | public IEnumerable<IRegressionModel> Models {
|
---|
| 64 | get { return ModelsParameter.Value; }
|
---|
[14321] | 65 | }
|
---|
| 66 |
|
---|
[14333] | 67 | public IEnumerable<GoalParameter> Goals {
|
---|
| 68 | get { return GoalsParameter.Value; }
|
---|
[14321] | 69 | }
|
---|
| 70 |
|
---|
[14333] | 71 | public IEnumerable<InputParameter> Inputs {
|
---|
| 72 | get { return InputsParameter.Value; }
|
---|
[14321] | 73 | }
|
---|
| 74 |
|
---|
[14333] | 75 | public void AddModel(IRegressionModel model) {
|
---|
| 76 | var models = ModelsParameter.Value;
|
---|
| 77 | models.Add(model);
|
---|
| 78 | GoalSeekingUtil.RaiseEvent(this, ModelsChanged);
|
---|
[14321] | 79 | }
|
---|
| 80 |
|
---|
[14333] | 81 | public void RemoveModel(IRegressionModel model) {
|
---|
| 82 | var models = ModelsParameter.Value;
|
---|
| 83 | models.Remove(model);
|
---|
| 84 | GoalSeekingUtil.RaiseEvent(this, ModelsChanged);
|
---|
[14321] | 85 | }
|
---|
| 86 |
|
---|
[14333] | 87 | public void Configure(IRegressionProblemData problemData, int row) {
|
---|
| 88 | GoalSeekingUtil.Configure(Goals, Inputs, problemData, row);
|
---|
[14321] | 89 | }
|
---|
| 90 |
|
---|
[14324] | 91 | public IEnumerable<double> GetEstimatedGoalValues(IEnumerable<double> parameterValues, bool round = false) {
|
---|
[14321] | 92 | var ds = (ModifiableDataset)dataset.Clone();
|
---|
[16901] | 93 | foreach (var parameter in ActiveInputs.Zip(parameterValues, (p, v) => new { p.Name, Value = v })) {
|
---|
[14324] | 94 | ds.SetVariableValue(parameter.Value, parameter.Name, 0);
|
---|
| 95 | }
|
---|
[14321] | 96 | var rows = new[] { 0 }; // actually just one row
|
---|
[16901] | 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()));
|
---|
[14321] | 99 | }
|
---|
| 100 |
|
---|
| 101 | public event EventHandler ModelsChanged;
|
---|
[14333] | 102 | public event EventHandler TargetsChanged;
|
---|
| 103 | public event EventHandler ParametersChanged;
|
---|
[14321] | 104 | #endregion
|
---|
| 105 |
|
---|
[14333] | 106 | private IEnumerable<GoalParameter> ActiveGoals {
|
---|
| 107 | get { return Goals.Where(x => x.Active); }
|
---|
[14321] | 108 | }
|
---|
[14333] | 109 | private IEnumerable<InputParameter> ActiveInputs {
|
---|
| 110 | get { return Inputs.Where(x => x.Active); }
|
---|
[14321] | 111 | }
|
---|
[14333] | 112 | private double QualitySumCutoff {
|
---|
| 113 | get { return QualitySumCutoffParameter.Value.Value; }
|
---|
[14321] | 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]
|
---|
[16901] | 131 | private MultiObjectiveGoalSeekingProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[14321] | 132 |
|
---|
[14324] | 133 | private MultiObjectiveGoalSeekingProblem(MultiObjectiveGoalSeekingProblem original, Cloner cloner) : base(original, cloner) {
|
---|
[14321] | 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() {
|
---|
[14333] | 149 | dataset = new ModifiableDataset();
|
---|
| 150 | Parameters.Add(new ValueParameter<CheckedItemList<InputParameter>>(InputsParameterName));
|
---|
| 151 | Parameters.Add(new ValueParameter<CheckedItemList<GoalParameter>>(GoalsParameterName));
|
---|
[14379] | 152 | Parameters.Add(new FixedValueParameter<ItemList<IRegressionModel>>(ModelsParameterName, new ItemList<IRegressionModel>()));
|
---|
[14321] | 153 | Parameters.Add(new FixedValueParameter<DoubleValue>(QualitySumCutoffParameterName, new DoubleValue(0.2)));
|
---|
| 154 | QualitySumCutoffParameter.Hidden = true;
|
---|
[14333] | 155 | EncodingParameter.Hidden = true;
|
---|
| 156 | EvaluatorParameter.Hidden = true;
|
---|
| 157 | SolutionCreatorParameter.Hidden = true;
|
---|
| 158 | MaximizationParameter.Hidden = true;
|
---|
[14321] | 159 | RegisterEvents();
|
---|
| 160 | }
|
---|
| 161 | #endregion
|
---|
| 162 |
|
---|
| 163 | public override double[] Evaluate(Individual individual, IRandom random) {
|
---|
| 164 | var vector = individual.RealVector();
|
---|
[14333] | 165 | vector.ElementNames = ActiveInputs.Select(x => x.Name);
|
---|
[14321] | 166 | int i = 0;
|
---|
| 167 | // round vector according to parameter step sizes
|
---|
[14333] | 168 | foreach (var parameter in ActiveInputs) {
|
---|
| 169 | vector[i] = RoundToNearestStepMultiple(vector[i], parameter.Step);
|
---|
[14321] | 170 | ++i;
|
---|
| 171 | }
|
---|
| 172 | var estimatedValues = GetEstimatedGoalValues(vector, round: true);
|
---|
[14333] | 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);
|
---|
[14324] | 175 | return qualities.ToArray();
|
---|
[14321] | 176 | }
|
---|
| 177 |
|
---|
[14333] | 178 | #region pareto analyzer
|
---|
[14321] | 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) {
|
---|
[14333] | 191 | var activeParameters = ActiveInputs.ToList();
|
---|
| 192 | var activeGoals = ActiveGoals.ToList();
|
---|
[14321] | 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" };
|
---|
[14333] | 197 | foreach (var target in activeGoals) {
|
---|
| 198 | columnNames.Add(target.Name);
|
---|
| 199 | columnNames.Add(target.Name + " (estimated)");
|
---|
[14321] | 200 | }
|
---|
[14333] | 201 | foreach (var parameter in activeParameters) {
|
---|
| 202 | columnNames.Add(parameter.Name);
|
---|
| 203 | columnNames.Add(parameter.Name + " (estimated)");
|
---|
| 204 | columnNames.Add(parameter.Name + " (deviation)");
|
---|
[14321] | 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();
|
---|
[14324] | 213 | var estimatedValues = GetEstimatedGoalValues(vector).ToList();
|
---|
[14321] | 214 | var rowValues = new double[columnNames.Count];
|
---|
| 215 | rowValues[0] = qualitySum;
|
---|
| 216 | int offset = 1;
|
---|
[14333] | 217 | for (int j = 0; j < activeGoals.Count * 2; j += 2) {
|
---|
[14321] | 218 | int k = j + offset;
|
---|
[16901] | 219 | rowValues[k] = activeGoals[j / 2].Goal; // original value
|
---|
[14321] | 220 | rowValues[k + 1] = estimatedValues[j / 2]; // estimated value
|
---|
| 221 | }
|
---|
[14333] | 222 | offset += activeGoals.Count * 2;
|
---|
[14321] | 223 | for (int j = 0; j < activeParameters.Count * 3; j += 3) {
|
---|
| 224 | int k = j + offset;
|
---|
[14379] | 225 | rowValues[k] = activeParameters[j / 3].Value;
|
---|
[14321] | 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 | }
|
---|
[14333] | 242 | #endregion
|
---|
[14321] | 243 |
|
---|
| 244 | #region event handlers
|
---|
| 245 | private void RegisterEvents() {
|
---|
[14333] | 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;
|
---|
[14338] | 250 |
|
---|
| 251 | foreach (var input in Inputs)
|
---|
| 252 | input.Changed += InputParameterChanged;
|
---|
| 253 |
|
---|
| 254 | foreach (var goal in Goals)
|
---|
| 255 | goal.Changed += GoalParameterChanged;
|
---|
[14321] | 256 | }
|
---|
| 257 |
|
---|
[14379] | 258 | private void ModelCollection_ItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<IRegressionModel>> e) {
|
---|
[14333] | 259 | if (e.Items == null || !e.Items.Any()) return;
|
---|
| 260 | GoalSeekingUtil.UpdateInputs(InputsParameter.Value, Models, InputParameterChanged);
|
---|
[14379] | 261 | GoalSeekingUtil.UpdateEncoding(Encoding, ActiveInputs);
|
---|
[14338] | 262 | dataset = Inputs.Any() ? new ModifiableDataset(Inputs.Select(x => x.Name), Inputs.Select(x => new List<double> { x.Value })) : new ModifiableDataset();
|
---|
[14333] | 263 | GoalSeekingUtil.UpdateTargets(GoalsParameter.Value, Models, GoalParameterChanged);
|
---|
| 264 | GoalSeekingUtil.RaiseEvent(this, ModelsChanged);
|
---|
[14321] | 265 | }
|
---|
| 266 |
|
---|
[14333] | 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);
|
---|
[14379] | 272 | GoalSeekingUtil.UpdateEncoding(Encoding, ActiveInputs);
|
---|
[14321] | 273 | }
|
---|
| 274 |
|
---|
[14333] | 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);
|
---|
[14321] | 280 | }
|
---|
| 281 | #endregion
|
---|
| 282 |
|
---|
| 283 | #region helper methods
|
---|
[14333] | 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));
|
---|
[14321] | 288 | }
|
---|
| 289 | private static double RoundToNearestStepMultiple(double value, double step) {
|
---|
| 290 | return step * (long)Math.Round(value / step);
|
---|
| 291 | }
|
---|
[14324] | 292 | private IEnumerable<IRegressionModel> GetModels(string target) {
|
---|
[14333] | 293 | return Models.Where(x => x.TargetVariable == target);
|
---|
[14324] | 294 | }
|
---|
[14321] | 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 | }
|
---|