source: branches/3073_IA_constraint_splitting/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/IntervalConstraint.cs @ 17767

Last change on this file since 17767 was 17767, checked in by dpiringe, 14 months ago

#3073

  • fixed a bug which caused a NullPointerException
  • added a storable constructor in Region
File size: 7.1 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 System.Collections.Generic;
25using System.Linq;
26using HEAL.Attic;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  [StorableType("8109BE58-CCFB-4462-A2F4-EEE5DFADAFF7")]
32  [Item("Interval Constraint", "Constraint on intervals.")]
33  public sealed class IntervalConstraint : Item {
34    [Storable] private string expression;
35
36    public string Expression {
37      get => expression;
38      private set {
39        if (expression == value)
40          return;
41        expression = value;
42        OnChanged();
43        OnToStringChanged();
44      }
45    }
46
47    public string Definition => GetDefinitionString();
48
49    [Storable] private string variable;
50
51    public string Variable {
52      get => variable;
53      private set {
54        if (variable == value)
55          return;
56        variable = value;
57        UpdateExpression();
58        OnChanged();
59      }
60    }
61
62    [Storable] private string target;
63
64    public string Target {
65      get => target;
66      private set {
67        if (target == value)
68          return;
69        target = value;
70        UpdateExpression();
71        OnChanged();
72      }
73    }
74
75    public bool IsDerivative => NumberOfDerivations > 0;
76
77    [Storable] 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        UpdateExpression();
88        OnChanged();
89      }
90    }
91
92    [Storable] private Interval interval;
93
94    public Interval Interval {
95      get => interval;
96      set {
97        if (interval == value)
98          return;
99        interval = value;
100        UpdateExpression();
101        OnChanged();
102      }
103    }
104
105    [Storable] 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] private IEnumerable<Region> regions;
118    public IEnumerable<Region> Regions {
119      get => regions;
120      set {
121        if (regions != value) {
122          regions = value;
123          UpdateExpression();
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          UpdateExpression();
137          OnChanged();
138        }
139      }
140    }
141
142    [StorableConstructor]
143    private IntervalConstraint(StorableConstructorFlag _) : base(_) { }
144
145    public IntervalConstraint(string expression, string variable, string target, int numberOfDerivations,
146                              Interval interval, double weight, bool enabled)
147      : this(expression, variable, target, numberOfDerivations,
148             interval, Enumerable.Empty<Region>(), weight, enabled) { }
149
150    public IntervalConstraint(string expression, string variable, string target, int numberOfDerivations,
151                              Interval interval, IEnumerable<Region> regions, double weight, bool enabled) {
152      this.regions = regions;
153      this.weight = weight;
154      this.expression = expression;
155      this.variable = variable;
156      this.target = target;
157      this.numberOfDerivations = numberOfDerivations;
158      this.interval = interval;
159      this.enabled = enabled;
160    }
161
162    public override IDeepCloneable Clone(Cloner cloner) {
163      return new IntervalConstraint(this, cloner);
164    }
165
166    private IntervalConstraint(IntervalConstraint original, Cloner cloner)
167      : base(original, cloner) {
168      Regions = new List<Region>(original.Regions?.Select(r => cloner.Clone(r)) ?? Enumerable.Empty<Region>());
169      Expression = original.Expression;
170      Variable = original.Variable;
171      Target = original.Target;
172      NumberOfDerivations = original.NumberOfDerivations;
173      Interval = original.Interval;
174      Enabled = original.Enabled;
175    }
176
177    public event EventHandler Changed;
178
179    private void OnChanged() {
180      var handlers = Changed;
181      if (handlers != null)
182        handlers(this, EventArgs.Empty);
183    }
184
185    private static string GetDerivationString(int derivation) {
186      switch (derivation) {
187        case 1:
188          return "";
189        case 2:
190          return "²";
191        case 3:
192          return "³";
193        default:
194          return "";
195      }
196    }
197
198    private void UpdateExpression() {
199      var expression = "";
200
201      if (!IsDerivative) {
202        expression = string.Format("Target:{0} in {1}{2} .. {3}{4}",
203                                   Variable,
204                                   "[",
205                                   Interval?.LowerBound,
206                                   Interval?.UpperBound,
207                                   "]");
208        if(Regions != null) {
209          foreach (var region in Regions)
210            expression += $", {region.VariableName}=({region.Interval.LowerBound} .. {region.Interval.UpperBound})";
211        }
212       
213        Expression = expression;
214        return;
215      }
216
217      expression = string.Format("∂{6}{1}/∂{0}{6} in {2}{3} .. {4}{5}",
218                                 Variable,
219                                 Target,
220                                 "[",
221                                 Interval?.LowerBound,
222                                 Interval?.UpperBound,
223                                 "]",
224                                 GetDerivationString(numberOfDerivations));
225      if (Regions != null) {
226        foreach (var region in Regions)
227          expression += $", {region.VariableName}=({region.Interval.LowerBound} .. {region.Interval.UpperBound})";
228      }
229      Expression = expression;
230    }
231
232    private string GetDefinitionString() {
233      if (!IsDerivative) return "Target " + Variable;
234
235      var definition =
236        $"∂{GetDerivationString(numberOfDerivations)}Target/∂{Variable}{GetDerivationString(numberOfDerivations)}";
237      return definition;
238    }
239
240    public override string ToString() {
241      return Expression;
242    }
243  }
244}
Note: See TracBrowser for help on using the repository browser.