1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Collections;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Data;
|
---|
9 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
10 | using HeuristicLab.Problems.DataAnalysis;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.GoalSeeking {
|
---|
13 | internal static class GoalSeekingUtil {
|
---|
14 | internal static void UpdateTargets(ICheckedItemList<GoalParameter> goals, IEnumerable<IRegressionModel> models, EventHandler changedEventHandler) {
|
---|
15 | var targets = models.Select(x => x.TargetVariable).Distinct().OrderBy(x => x);
|
---|
16 | var dict = goals.ToDictionary(x => x.Name, x => x);
|
---|
17 | goals.Clear();
|
---|
18 | foreach (var t in targets) {
|
---|
19 | GoalParameter gp;
|
---|
20 | if (dict.ContainsKey(t)) {
|
---|
21 | gp = dict[t];
|
---|
22 | } else {
|
---|
23 | var goal = double.NaN;
|
---|
24 | var weight = 1.0;
|
---|
25 | var variance = double.NaN;
|
---|
26 | var step = 1e-6;
|
---|
27 | 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
|
---|
28 | gp.Changed += changedEventHandler;
|
---|
29 | }
|
---|
30 | goals.Add(gp, gp.Active);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | internal static void UpdateInputs(ICheckedItemList<InputParameter> inputs, IEnumerable<IRegressionModel> models, EventHandler changedEventHandler) {
|
---|
35 | var variables = models.SelectMany(x => x.VariablesUsedForPrediction).Distinct();
|
---|
36 | var dict = inputs.ToDictionary(x => x.Name, x => x); // save old parameter settings
|
---|
37 | inputs.Clear();
|
---|
38 | foreach (var v in variables) {
|
---|
39 | InputParameter ip;
|
---|
40 | if (dict.ContainsKey(v)) {
|
---|
41 | ip = dict[v];
|
---|
42 | } else {
|
---|
43 | 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
|
---|
44 | ip.Changed += changedEventHandler;
|
---|
45 | }
|
---|
46 | inputs.Add(ip, ip.Active);
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | internal static RealVectorEncoding CreateEncoding(IEnumerable<InputParameter> inputParameters) {
|
---|
51 | var encoding = new RealVectorEncoding(inputParameters.Count());
|
---|
52 | encoding.Bounds = new DoubleMatrix(encoding.Length, 2); // only two columns: min and max
|
---|
53 | encoding.Bounds.RowNames = inputParameters.Select(x => x.Name);
|
---|
54 | encoding.Bounds.ColumnNames = new[] { "Min.", "Max." };
|
---|
55 |
|
---|
56 | int i = 0;
|
---|
57 | foreach (var parameter in inputParameters) {
|
---|
58 | encoding.Bounds[i, 0] = parameter.Min;
|
---|
59 | encoding.Bounds[i, 1] = parameter.Max;
|
---|
60 | ++i;
|
---|
61 | }
|
---|
62 | return encoding;
|
---|
63 | }
|
---|
64 |
|
---|
65 | internal static void ValidateGoalsAndInputs(IEnumerable<GoalParameter> goals, IEnumerable<InputParameter> inputs) {
|
---|
66 | var notConfiguredGoals = goals.Where(x => x.Active && (double.IsNaN(x.Variance) || double.IsNaN(x.Goal))).ToList();
|
---|
67 | var notConfiguredInputs = inputs.Where(x => x.Active && (double.IsNaN(x.Min) || double.IsNaN(x.Max) || double.IsNaN(x.Value))).ToList();
|
---|
68 | StringBuilder sb = null;
|
---|
69 | if (notConfiguredGoals.Any()) {
|
---|
70 | sb = new StringBuilder();
|
---|
71 | sb.AppendLine("The following GoalParameters are not configured.");
|
---|
72 | foreach (var goal in notConfiguredGoals) {
|
---|
73 | sb.AppendLine(goal.Name);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | if (notConfiguredInputs.Any()) {
|
---|
77 | if (sb == null)
|
---|
78 | sb = new StringBuilder();
|
---|
79 | sb.AppendLine("The following InpuTParameters are not configured.");
|
---|
80 | foreach (var input in notConfiguredInputs) {
|
---|
81 | sb.AppendLine(input.Name);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | if (sb != null)
|
---|
85 | throw new InvalidOperationException(sb.ToString());
|
---|
86 | }
|
---|
87 |
|
---|
88 | internal static void Configure(IEnumerable<GoalParameter> goals, IEnumerable<InputParameter> inputs, IRegressionProblemData problemData, int row) {
|
---|
89 | var inputMap = inputs.ToDictionary(x => x.Name, x => x);
|
---|
90 | var goalMap = goals.ToDictionary(x => x.Name, x => x);
|
---|
91 |
|
---|
92 | foreach (var variable in problemData.Dataset.DoubleVariables) {
|
---|
93 | if (inputMap.ContainsKey(variable)) {
|
---|
94 | var values = problemData.Dataset.GetReadOnlyDoubleValues(variable);
|
---|
95 | var input = inputMap[variable];
|
---|
96 | input.Value = values[row];
|
---|
97 | double min = values[0], max = values[0];
|
---|
98 | foreach (var v in values) {
|
---|
99 | if (min > v) min = v;
|
---|
100 | if (max < v) max = v;
|
---|
101 | }
|
---|
102 | input.Min = min;
|
---|
103 | input.Max = max;
|
---|
104 | } else if (goalMap.ContainsKey(variable)) {
|
---|
105 | var values = problemData.Dataset.GetReadOnlyDoubleValues(variable);
|
---|
106 | var goal = goalMap[variable];
|
---|
107 | goal.Variance = values.Variance();
|
---|
108 | goal.Goal = values[row];
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | internal static void Goals_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<GoalParameter>> args) {
|
---|
114 | var goals = (CheckedItemList<GoalParameter>)sender;
|
---|
115 | foreach (var item in args.Items.Select(x => x.Value)) {
|
---|
116 | item.Active = goals.ItemChecked(item);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | internal static void Inputs_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<InputParameter>> args) {
|
---|
121 | var inputs = (CheckedItemList<InputParameter>)sender;
|
---|
122 | foreach (var item in args.Items.Select(x => x.Value)) {
|
---|
123 | item.Active = inputs.ItemChecked(item);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | internal static void RaiseEvent(object sender, EventHandler handler) {
|
---|
128 | if (handler == null) return;
|
---|
129 | handler(sender, EventArgs.Empty);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|