Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3073 Added classes/views to define constraints

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