Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/IntervalConstraint.cs @ 17506

Last change on this file since 17506 was 17506, checked in by mkommend, 4 years ago

#2971: Further minor refactorings and renaming of members.

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
21using System;
22using HEAL.Attic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25
26namespace HeuristicLab.Problems.DataAnalysis {
27  [StorableType("8109BE58-CCFB-4462-A2F4-EEE5DFADAFF7")]
28  [Item("Interval Constraint", "Constraint on intervals.")]
29  public sealed class IntervalConstraint : Item {
30    [Storable]
31    private string expression;
32    public string Expression {
33      get => expression;
34      private set {
35        if (expression == value) return;
36        expression = value;
37        OnChanged();
38        OnToStringChanged();
39      }
40    }
41
42    public string Definition {
43      get { return GetDefinitionString(); }
44    }
45
46    [Storable]
47    private string variable;
48    public string Variable {
49      get => variable;
50      private set {
51        if (variable == value) return;
52        variable = value;
53        UpdateExpression();
54        OnChanged();
55      }
56    }
57
58    [Storable]
59    private string target;
60    public string Target {
61      get => target;
62      private set {
63        if (target == value) return;
64        target = value;
65        UpdateExpression();
66        OnChanged();
67      }
68    }
69
70    public bool IsDerivative {
71      get => NumberOfDerivations > 0;
72    }
73
74    [Storable]
75    private int numberOfDerivations;
76    public int NumberOfDerivations {
77      get => numberOfDerivations;
78      set {
79        if (value < 0 || value > 3) throw new ArgumentException("Number of derivation has to be between 0 - 3.");
80        if (numberOfDerivations == value) return;
81        numberOfDerivations = value;
82        UpdateExpression();
83        OnChanged();
84      }
85    }
86
87    [Storable]
88    private Interval interval;
89    public Interval Interval {
90      get => interval;
91      set {
92        if (interval == value) return;
93        interval = value;
94        UpdateExpression();
95        OnChanged();
96      }
97    }
98
99    [Storable]
100    private bool enabled;
101    public bool Enabled {
102      get => enabled;
103      set {
104        if (enabled == value) return;
105        enabled = value;
106        OnChanged();
107      }
108    }
109
110    [StorableConstructor]
111    private IntervalConstraint(StorableConstructorFlag _) : base(_) { }
112
113    public IntervalConstraint(string expression, string variable, string target, int numberOfDerivations, Interval interval, bool enabled) : base(){
114      this.expression = expression;
115      this.variable = variable;
116      this.target = target;
117      this.numberOfDerivations = numberOfDerivations;
118      this.interval = interval;
119      this.enabled = enabled;
120    }
121
122    public override IDeepCloneable Clone(Cloner cloner) {
123      return new IntervalConstraint(this, cloner);
124    }
125
126    private IntervalConstraint(IntervalConstraint original, Cloner cloner)
127      : base(original, cloner) {
128      this.Expression = original.Expression;
129      this.Variable = original.Variable;
130      this.Target = original.Target;
131      this.NumberOfDerivations = original.NumberOfDerivations;
132      this.Interval = original.Interval;
133      this.Enabled = original.Enabled;
134    }
135
136    public event EventHandler Changed;
137    private void OnChanged() {
138      EventHandler handlers = Changed;
139      if (handlers != null)
140        handlers(this, EventArgs.Empty);
141    }
142
143    private static string GetDerivationString(int derivation) {
144      switch (derivation) {
145        case 1:
146          return "";
147        case 2:
148          return "²";
149        case 3:
150          return "³";
151        default:
152          return "";
153      }
154    }
155
156    private void UpdateExpression() {
157      var expression = "";
158
159      if (!IsDerivative) {
160        expression = string.Format("Target:{0} in {1}{2} .. {3}{4}",
161          Variable,
162          "[",
163          Interval?.LowerBound,
164          Interval?.UpperBound,
165          "]");
166        Expression = expression;
167        return;
168      }
169      expression = string.Format("∂{6}{1}/∂{0}{6} in {2}{3} .. {4}{5}",
170        Variable,
171        Target,
172        "[",
173        Interval?.LowerBound,
174        Interval?.UpperBound,
175        "]",
176        GetDerivationString(numberOfDerivations));
177        Expression = expression;
178    }
179
180    private string GetDefinitionString() {
181      if (!IsDerivative) {
182        return "Target " + Variable;
183      }
184      var definition = $"∂{GetDerivationString(numberOfDerivations)}Target/∂{Variable}{GetDerivationString(numberOfDerivations)}";
185      return definition;
186    }
187
188    public override string ToString() {
189      return Expression;
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.