Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/ShapeConstraint.cs @ 17887

Last change on this file since 17887 was 17887, checked in by gkronber, 3 years ago

#3073: intermediate comment while refactoring the branch

File size: 6.6 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23using System;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
28namespace HeuristicLab.Problems.DataAnalysis {
29  [StorableType("8109BE58-CCFB-4462-A2F4-EEE5DFADAFF7")]
30  [Item("ShapeConstraint", "Constraint on the shape of a function e.g. monotonicity.")]
31  public sealed class ShapeConstraint : Item {
32    // [Storable]
33    // private string expression;
34    //
35    // public string Expression {
36    //   get => expression;
37    //   private set {
38    //     if (expression == value)
39    //       return;
40    //     expression = value;
41    //     OnChanged();
42    //     OnToStringChanged();
43    //   }
44    // }
45
46    // public string Definition => GetDefinitionString();
47
48    [Storable]
49    private string variable;
50
51    public string Variable {
52      get => variable;
53      private set {
54        if (variable == value)
55          return;
56        variable = value;
57        OnChanged();
58      }
59    }
60
61    // [Storable]
62    // private string target;
63    //
64    // public string Target {
65    //   get => target;
66    //   private set {
67    //     if (target == value)
68    //       return;
69    //     target = value;
70    //     OnChanged();
71    //   }
72    // }
73
74    public bool IsDerivative => NumberOfDerivations > 0;
75
76    [Storable]
77    private int numberOfDerivations;
78
79    public int NumberOfDerivations {
80      get => numberOfDerivations;
81      set {
82        if (value < 0 || value > 3)
83          throw new ArgumentException("Number of derivation has to be between 0 - 3.");
84        if (numberOfDerivations == value)
85          return;
86        numberOfDerivations = value;
87        OnChanged();
88      }
89    }
90
91    [Storable]
92    private Interval interval;
93
94    public Interval Interval {
95      get => interval;
96      set {
97        if (interval == value)
98          return;
99        interval = value;
100        OnChanged();
101      }
102    }
103
104    [Storable]
105    private bool enabled;
106
107    public bool Enabled {
108      get => enabled;
109      set {
110        if (enabled == value)
111          return;
112        enabled = value;
113        OnChanged();
114      }
115    }
116
117    [Storable]
118    private IntervalCollection regions;
119    public IntervalCollection Regions {
120      get => regions;
121      set {
122        if (regions != value) {
123          regions = value;
124          OnChanged();
125        }
126      }
127    }
128
129    [Storable]
130    private double weight = 1.0;
131    public double Weight {
132      get => weight;
133      set {
134        if (weight != value) {
135          weight = value;
136          OnChanged();
137        }
138      }
139    }
140
141    [StorableConstructor]
142    private ShapeConstraint(StorableConstructorFlag _) : base(_) { }
143
144    // without derivation
145    public ShapeConstraint(Interval interval, double weight, bool enabled)
146      : this(string.Empty, 0,
147         interval, new IntervalCollection(), weight, enabled) { }
148
149    public ShapeConstraint(Interval interval, IntervalCollection regions, double weight, bool enabled)
150      : this(string.Empty, 0,
151         interval, regions, weight, enabled) { }
152
153    public ShapeConstraint(string variable, int numberOfDerivations,
154                              Interval interval, double weight, bool enabled)
155      : this(variable, numberOfDerivations,
156             interval, new IntervalCollection(), weight, enabled) { }
157
158    public ShapeConstraint(string variable, int numberOfDerivations,
159                              Interval interval, IntervalCollection regions, double weight, bool enabled) {
160      this.regions = regions;
161      this.weight = weight;
162      this.variable = variable;
163      this.numberOfDerivations = numberOfDerivations;
164      this.interval = interval;
165      this.enabled = enabled;
166    }
167
168    public override IDeepCloneable Clone(Cloner cloner) {
169      return new ShapeConstraint(this, cloner);
170    }
171
172    private ShapeConstraint(ShapeConstraint original, Cloner cloner)
173      : base(original, cloner) {
174      Regions = original.Regions;
175      Variable = original.Variable;
176      NumberOfDerivations = original.NumberOfDerivations;
177      Interval = original.Interval;
178      Enabled = original.Enabled;
179    }
180
181    public event EventHandler Changed;
182
183    private void OnChanged() {
184      var handlers = Changed;
185      if (handlers != null)
186        handlers(this, EventArgs.Empty);
187    }
188
189
190    // private string GetDefinitionString() {
191    //   if (!IsDerivative) return "Target " + Variable;
192    //
193    //   var definition =
194    //     $"∂{GetDerivationString(numberOfDerivations)}Target/∂{Variable}{GetDerivationString(numberOfDerivations)}";
195    //   return definition;
196    // }
197
198    public override string ToString() {
199      return GenerateExpressionString();
200    }
201
202
203    private string GenerateExpressionString() {
204      string expression;
205
206      if (!IsDerivative) {
207        expression = string.Format($"f in [{Interval?.LowerBound} .. {Interval?.UpperBound}]");
208        if (Regions != null) {
209          foreach (var region in Regions.GetReadonlyDictionary())
210            expression += $", {region.Key}=({region.Value.LowerBound} .. {region.Value.UpperBound})";
211        }
212
213        return expression;
214      }
215
216      var derivationString = string.Empty;
217      switch (numberOfDerivations) {
218        case 1:
219          derivationString = ""; break;
220        case 2:
221          derivationString = "²"; break;
222        case 3:
223          derivationString = "³"; break;
224      }
225      expression = string.Format($"∂{derivationString}f/∂{Variable}{derivationString} in [{Interval?.LowerBound} .. {Interval?.UpperBound}]");
226      if (Regions != null) {
227        foreach (var region in Regions.GetReadonlyDictionary())
228          expression += $", {region.Key}=({region.Value.LowerBound} .. {region.Value.UpperBound})";
229      }
230      return expression;
231    }
232  }
233}
Note: See TracBrowser for help on using the repository browser.