Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16932 was 16932, checked in by chaider, 5 years ago

#2971 Use partial differential directly in strings instead of unicode

File size: 6.4 KB
RevLine 
[16830]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;
[16772]22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HEAL.Attic;
[16587]25
26namespace HeuristicLab.Problems.DataAnalysis {
[16628]27  [StorableType("8109BE58-CCFB-4462-A2F4-EEE5DFADAFF7")]
[16772]28  [Item("Interval Constraint", "Constraint on intervals.")]
[16895]29  public sealed class IntervalConstraint : Item {
[16776]30    private string expression;
[16886]31    [Storable]
[16776]32    public string Expression {
33      get => expression;
[16895]34      private set {
35        if (expression != value) {
[16776]36          expression = value;
[16895]37          OnChanged();
38          OnToStringChanged();
[16776]39        }
40      }
41    }
[16895]42
[16880]43    public string Definition {
[16895]44      get { return GetDefinitionString(); }
[16880]45    }
[16774]46
47    private string variable;
[16886]48    [Storable]
[16774]49    public string Variable {
50      get => variable;
[16895]51      private set {
52        if (variable != value) {
[16774]53          variable = value;
[16895]54          UpdateExpression();
55          OnChanged();
[16774]56        }
57      }
58    }
[16596]59
[16900]60    private string target;
61    [Storable]
62    public string Target {
63      get => target;
64      set {
65        if (target != value) {
66          target = value;
67          UpdateExpression();
68          OnChanged();
69        }
70      }
71    }
72
[16895]73    public bool IsDerivation {
74      get => numberOfDerivation > 0;
75    }
76
[16898]77    private int numberOfDerivation;
[16886]78    [Storable]
[16778]79    public int NumberOfDerivation {
80      get => numberOfDerivation;
81      set {
[16895]82        if (value < 0 || value > 3)
83          throw new ArgumentException("Number of derivation has to be between 0 - 3.");
[16898]84        if (numberOfDerivation != value) {
[16778]85          numberOfDerivation = value;
[16895]86          UpdateExpression();
87          OnChanged();
[16778]88        }
89      }
90    }
91
[16895]92    private Interval interval;
93    [Storable]
94    public Interval Interval {
95      get => interval;
96      set {
97        if (interval != value) {
98          interval = value;
99          UpdateExpression();
100          OnChanged();
101        }
102      }
103    }
104    private bool inclusiveLowerBound;
105    [Storable]
106    public bool InclusiveLowerBound {
107      get => inclusiveLowerBound;
108      set {
109        if (inclusiveLowerBound != value) {
110          inclusiveLowerBound = value;
111          UpdateExpression();
112          OnChanged();
113        }
114      }
115    }
116
117    private bool inclusiveUpperBound;
118    [Storable]
119    public bool InclusiveUpperBound {
120      get => inclusiveUpperBound;
121      set {
122        if (inclusiveUpperBound != value) {
123          inclusiveUpperBound = value;
124          UpdateExpression();
125          OnChanged();
126        }
127      }
128    }
129
[16800]130    private bool enabled;
[16886]131    [Storable]
[16800]132    public bool Enabled {
133      get => enabled;
[16777]134      set {
[16800]135        if (value != enabled) {
136          enabled = value;
[16895]137          OnChanged();
[16777]138        }
139      }
140    }
[16596]141
[16886]142    [StorableConstructor]
[16895]143    private IntervalConstraint(StorableConstructorFlag _) : base(_) { }
[16886]144
[16900]145    public IntervalConstraint(string expression, string variable, string target, int numberOfDerivation, Interval interval, bool inclusiveLowerBound,
[16895]146      bool inclusiveUpperBound, bool enabled) : base(){
147      this.expression = expression;
148      this.variable = variable;
[16900]149      this.target = target;
[16895]150      this.numberOfDerivation = numberOfDerivation;
151      this.interval = interval;
152      this.inclusiveLowerBound = inclusiveLowerBound;
153      this.inclusiveUpperBound = inclusiveUpperBound;
154      this.enabled = enabled;
[16596]155    }
[16772]156
157    public override IDeepCloneable Clone(Cloner cloner) {
158      return new IntervalConstraint(this, cloner);
159    }
160
[16895]161    private IntervalConstraint(IntervalConstraint original, Cloner cloner)
[16772]162      : base(original, cloner) {
163      this.Expression = original.Expression;
[16900]164      this.Variable = original.Variable;
165      this.Target = original.Target;
166      this.NumberOfDerivation = original.NumberOfDerivation;
[16772]167      this.Interval = original.Interval;
168      this.InclusiveLowerBound = original.InclusiveLowerBound;
169      this.InclusiveUpperBound = original.InclusiveUpperBound;
[16917]170      this.Enabled = original.Enabled;
[16772]171    }
172
173    public event EventHandler Changed;
[16895]174    private void OnChanged() {
[16772]175      EventHandler handlers = Changed;
176      if (handlers != null)
[16895]177        handlers(this, EventArgs.Empty);
[16772]178    }
[16895]179
180    private static string GetDerivationString(int derivation) {
181      switch (derivation) {
182        case 1:
183          return "";
184        case 2:
185          return "²";
186        case 3:
187          return "³";
188        default:
189          return "";
190      }
191    }
192
193    private void UpdateExpression() {
194      var expression = "";
195
196      if (!IsDerivation) {
[16900]197        expression = string.Format("Target:{0} in {1}{2} .. {3}{4}",
[16895]198          Variable,
199          (InclusiveLowerBound) ? "[" : "]",
[16917]200          Interval?.LowerBound,
201          Interval?.UpperBound,
[16895]202          (InclusiveUpperBound) ? "]" : "[");
203        Expression = expression;
204        return;
205      }
[16932]206      expression = string.Format("∂{6}{1}/∂{0}{6} in {2}{3} .. {4}{5}",
[16895]207        Variable,
[16900]208        Target,
[16895]209        (InclusiveLowerBound) ? "[" : "]",
[16917]210        Interval?.LowerBound,
211        Interval?.UpperBound,
[16895]212        (InclusiveUpperBound) ? "]" : "[",
213        GetDerivationString(numberOfDerivation));
214        Expression = expression;
215    }
216
217    private string GetDefinitionString() {
218      if (!IsDerivation) {
219        return "Target " + Variable;
220      }
[16932]221      var definition = $"∂{GetDerivationString(numberOfDerivation)}Target/∂{Variable}{GetDerivationString(numberOfDerivation)}";
[16895]222      return definition;
223    }
224
225    public override string ToString() {
226      return Expression;
227    }
[16587]228  }
229}
Note: See TracBrowser for help on using the repository browser.