[14379] | 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 |
|
---|
| 22 | using System;
|
---|
[14333] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Collections;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.GoalSeeking {
|
---|
| 34 | internal static class GoalSeekingUtil {
|
---|
| 35 | internal static void UpdateTargets(ICheckedItemList<GoalParameter> goals, IEnumerable<IRegressionModel> models, EventHandler changedEventHandler) {
|
---|
| 36 | var targets = models.Select(x => x.TargetVariable).Distinct().OrderBy(x => x);
|
---|
| 37 | var dict = goals.ToDictionary(x => x.Name, x => x);
|
---|
| 38 | goals.Clear();
|
---|
| 39 | foreach (var t in targets) {
|
---|
| 40 | GoalParameter gp;
|
---|
| 41 | if (dict.ContainsKey(t)) {
|
---|
| 42 | gp = dict[t];
|
---|
| 43 | } else {
|
---|
| 44 | var goal = double.NaN;
|
---|
| 45 | var weight = 1.0;
|
---|
| 46 | var variance = double.NaN;
|
---|
| 47 | var step = 1e-6;
|
---|
| 48 | gp = new GoalParameter(t, goal, weight, variance, step, active: false); // new goals are not active by default as they need to be configured by the user
|
---|
| 49 | gp.Changed += changedEventHandler;
|
---|
| 50 | }
|
---|
| 51 | goals.Add(gp, gp.Active);
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | internal static void UpdateInputs(ICheckedItemList<InputParameter> inputs, IEnumerable<IRegressionModel> models, EventHandler changedEventHandler) {
|
---|
| 56 | var variables = models.SelectMany(x => x.VariablesUsedForPrediction).Distinct();
|
---|
| 57 | var dict = inputs.ToDictionary(x => x.Name, x => x); // save old parameter settings
|
---|
| 58 | inputs.Clear();
|
---|
| 59 | foreach (var v in variables) {
|
---|
| 60 | InputParameter ip;
|
---|
| 61 | if (dict.ContainsKey(v)) {
|
---|
| 62 | ip = dict[v];
|
---|
| 63 | } else {
|
---|
| 64 | ip = new InputParameter(v, double.NaN, -1, 1, 1e-6, active: false); // new inputs are not active by default. the user needs to configure them first
|
---|
| 65 | ip.Changed += changedEventHandler;
|
---|
| 66 | }
|
---|
| 67 | inputs.Add(ip, ip.Active);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | internal static RealVectorEncoding CreateEncoding(IEnumerable<InputParameter> inputParameters) {
|
---|
| 72 | var encoding = new RealVectorEncoding(inputParameters.Count());
|
---|
| 73 | encoding.Bounds = new DoubleMatrix(encoding.Length, 2); // only two columns: min and max
|
---|
| 74 | encoding.Bounds.RowNames = inputParameters.Select(x => x.Name);
|
---|
| 75 | encoding.Bounds.ColumnNames = new[] { "Min.", "Max." };
|
---|
| 76 |
|
---|
| 77 | int i = 0;
|
---|
| 78 | foreach (var parameter in inputParameters) {
|
---|
| 79 | encoding.Bounds[i, 0] = parameter.Min;
|
---|
| 80 | encoding.Bounds[i, 1] = parameter.Max;
|
---|
| 81 | ++i;
|
---|
| 82 | }
|
---|
| 83 | return encoding;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[14379] | 86 | internal static void UpdateEncoding(RealVectorEncoding encoding, IEnumerable<InputParameter> inputParameters) {
|
---|
| 87 | encoding.Length = inputParameters.Count();
|
---|
| 88 | encoding.Bounds = new DoubleMatrix(encoding.Length, 2);
|
---|
| 89 | encoding.Bounds.RowNames = inputParameters.Select(x => x.Name);
|
---|
| 90 | encoding.Bounds.ColumnNames = new[] { "Min.", "Max." };
|
---|
| 91 |
|
---|
| 92 | int i = 0;
|
---|
| 93 | foreach (var parameter in inputParameters) {
|
---|
| 94 | encoding.Bounds[i, 0] = parameter.Min;
|
---|
| 95 | encoding.Bounds[i, 1] = parameter.Max;
|
---|
| 96 | ++i;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[14333] | 100 | internal static void ValidateGoalsAndInputs(IEnumerable<GoalParameter> goals, IEnumerable<InputParameter> inputs) {
|
---|
| 101 | var notConfiguredGoals = goals.Where(x => x.Active && (double.IsNaN(x.Variance) || double.IsNaN(x.Goal))).ToList();
|
---|
| 102 | var notConfiguredInputs = inputs.Where(x => x.Active && (double.IsNaN(x.Min) || double.IsNaN(x.Max) || double.IsNaN(x.Value))).ToList();
|
---|
| 103 | StringBuilder sb = null;
|
---|
| 104 | if (notConfiguredGoals.Any()) {
|
---|
| 105 | sb = new StringBuilder();
|
---|
| 106 | sb.AppendLine("The following GoalParameters are not configured.");
|
---|
| 107 | foreach (var goal in notConfiguredGoals) {
|
---|
| 108 | sb.AppendLine(goal.Name);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | if (notConfiguredInputs.Any()) {
|
---|
| 112 | if (sb == null)
|
---|
| 113 | sb = new StringBuilder();
|
---|
| 114 | sb.AppendLine("The following InpuTParameters are not configured.");
|
---|
| 115 | foreach (var input in notConfiguredInputs) {
|
---|
| 116 | sb.AppendLine(input.Name);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | if (sb != null)
|
---|
| 120 | throw new InvalidOperationException(sb.ToString());
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | internal static void Configure(IEnumerable<GoalParameter> goals, IEnumerable<InputParameter> inputs, IRegressionProblemData problemData, int row) {
|
---|
| 124 | var inputMap = inputs.ToDictionary(x => x.Name, x => x);
|
---|
| 125 | var goalMap = goals.ToDictionary(x => x.Name, x => x);
|
---|
| 126 |
|
---|
[14336] | 127 | foreach (var variable in problemData.Dataset.DoubleVariables) {
|
---|
[14333] | 128 | if (inputMap.ContainsKey(variable)) {
|
---|
| 129 | var values = problemData.Dataset.GetReadOnlyDoubleValues(variable);
|
---|
| 130 | var input = inputMap[variable];
|
---|
| 131 | input.Value = values[row];
|
---|
| 132 | double min = values[0], max = values[0];
|
---|
| 133 | foreach (var v in values) {
|
---|
| 134 | if (min > v) min = v;
|
---|
| 135 | if (max < v) max = v;
|
---|
| 136 | }
|
---|
| 137 | input.Min = min;
|
---|
[14336] | 138 | input.Max = max;
|
---|
[14333] | 139 | } else if (goalMap.ContainsKey(variable)) {
|
---|
| 140 | var values = problemData.Dataset.GetReadOnlyDoubleValues(variable);
|
---|
| 141 | var goal = goalMap[variable];
|
---|
| 142 | goal.Variance = values.Variance();
|
---|
| 143 | goal.Goal = values[row];
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | internal static void Goals_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<GoalParameter>> args) {
|
---|
| 149 | var goals = (CheckedItemList<GoalParameter>)sender;
|
---|
| 150 | foreach (var item in args.Items.Select(x => x.Value)) {
|
---|
| 151 | item.Active = goals.ItemChecked(item);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | internal static void Inputs_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<InputParameter>> args) {
|
---|
| 156 | var inputs = (CheckedItemList<InputParameter>)sender;
|
---|
| 157 | foreach (var item in args.Items.Select(x => x.Value)) {
|
---|
| 158 | item.Active = inputs.ItemChecked(item);
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | internal static void RaiseEvent(object sender, EventHandler handler) {
|
---|
| 163 | if (handler == null) return;
|
---|
| 164 | handler(sender, EventArgs.Empty);
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | }
|
---|