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
|
---|
23 | using System;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
30 | [StorableType("8109BE58-CCFB-4462-A2F4-EEE5DFADAFF7")]
|
---|
31 | [Item("Interval Constraint", "Constraint on intervals.")]
|
---|
32 | public sealed class IntervalConstraint : Item {
|
---|
33 | [Storable] 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] private string variable;
|
---|
49 |
|
---|
50 | public string Variable {
|
---|
51 | get => variable;
|
---|
52 | private set {
|
---|
53 | if (variable == value)
|
---|
54 | return;
|
---|
55 | variable = value;
|
---|
56 | UpdateExpression();
|
---|
57 | OnChanged();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [Storable] private string target;
|
---|
62 |
|
---|
63 | public string Target {
|
---|
64 | get => target;
|
---|
65 | private set {
|
---|
66 | if (target == value)
|
---|
67 | return;
|
---|
68 | target = value;
|
---|
69 | UpdateExpression();
|
---|
70 | OnChanged();
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public bool IsDerivative => NumberOfDerivations > 0;
|
---|
75 |
|
---|
76 | [Storable] private int numberOfDerivations;
|
---|
77 |
|
---|
78 | public int NumberOfDerivations {
|
---|
79 | get => numberOfDerivations;
|
---|
80 | set {
|
---|
81 | if (value < 0 || value > 3)
|
---|
82 | throw new ArgumentException("Number of derivation has to be between 0 - 3.");
|
---|
83 | if (numberOfDerivations == value)
|
---|
84 | return;
|
---|
85 | numberOfDerivations = value;
|
---|
86 | UpdateExpression();
|
---|
87 | OnChanged();
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | [Storable] private Interval interval;
|
---|
92 |
|
---|
93 | public Interval Interval {
|
---|
94 | get => interval;
|
---|
95 | set {
|
---|
96 | if (interval == value)
|
---|
97 | return;
|
---|
98 | interval = value;
|
---|
99 | UpdateExpression();
|
---|
100 | OnChanged();
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | [Storable] private bool enabled;
|
---|
105 |
|
---|
106 | public bool Enabled {
|
---|
107 | get => enabled;
|
---|
108 | set {
|
---|
109 | if (enabled == value)
|
---|
110 | return;
|
---|
111 | enabled = value;
|
---|
112 | OnChanged();
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | [Storable]
|
---|
117 | private /*IDictionary<string, Interval>*/ IEnumerable<Region> regions; //= new Dictionary<string, Interval>();
|
---|
118 | public IEnumerable<Region> Regions
|
---|
119 | {
|
---|
120 | get => regions;
|
---|
121 | set
|
---|
122 | {
|
---|
123 | if (regions != value)
|
---|
124 | {
|
---|
125 | regions = value;
|
---|
126 | UpdateExpression();
|
---|
127 | OnChanged();
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | [Storable]
|
---|
133 | private double weight = 1.0;
|
---|
134 | public double Weight
|
---|
135 | {
|
---|
136 | get => weight;
|
---|
137 | set
|
---|
138 | {
|
---|
139 | if(weight != value)
|
---|
140 | {
|
---|
141 | weight = value;
|
---|
142 | UpdateExpression();
|
---|
143 | OnChanged();
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | [StorableConstructor]
|
---|
149 | private IntervalConstraint(StorableConstructorFlag _) : base(_) { }
|
---|
150 |
|
---|
151 | public IntervalConstraint(string expression, string variable, string target, int numberOfDerivations,
|
---|
152 | Interval interval, double weight, bool enabled)
|
---|
153 | : this(expression, variable, target, numberOfDerivations,
|
---|
154 | interval, new List<Region>(), weight, enabled) { }
|
---|
155 |
|
---|
156 | public IntervalConstraint(string expression, string variable, string target, int numberOfDerivations,
|
---|
157 | Interval interval, IEnumerable<Region> regions, double weight, bool enabled)
|
---|
158 | {
|
---|
159 | this.expression = expression;
|
---|
160 | this.variable = variable;
|
---|
161 | this.target = target;
|
---|
162 | this.numberOfDerivations = numberOfDerivations;
|
---|
163 | this.interval = interval;
|
---|
164 | this.regions = regions;
|
---|
165 | this.weight = weight;
|
---|
166 | this.enabled = enabled;
|
---|
167 | }
|
---|
168 |
|
---|
169 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
170 | return new IntervalConstraint(this, cloner);
|
---|
171 | }
|
---|
172 |
|
---|
173 | private IntervalConstraint(IntervalConstraint original, Cloner cloner)
|
---|
174 | : base(original, cloner) {
|
---|
175 | Expression = original.Expression;
|
---|
176 | Variable = original.Variable;
|
---|
177 | Target = original.Target;
|
---|
178 | NumberOfDerivations = original.NumberOfDerivations;
|
---|
179 | Interval = original.Interval;
|
---|
180 | Regions = original.Regions;
|
---|
181 | Enabled = original.Enabled;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public event EventHandler Changed;
|
---|
185 |
|
---|
186 | private void OnChanged() {
|
---|
187 | var handlers = Changed;
|
---|
188 | if (handlers != null)
|
---|
189 | handlers(this, EventArgs.Empty);
|
---|
190 | }
|
---|
191 |
|
---|
192 | private static string GetDerivationString(int derivation) {
|
---|
193 | switch (derivation) {
|
---|
194 | case 1:
|
---|
195 | return "";
|
---|
196 | case 2:
|
---|
197 | return "²";
|
---|
198 | case 3:
|
---|
199 | return "³";
|
---|
200 | default:
|
---|
201 | return "";
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void UpdateExpression() {
|
---|
206 | var expression = "";
|
---|
207 |
|
---|
208 | if (!IsDerivative) {
|
---|
209 | expression = string.Format("Target:{0} in {1}{2} .. {3}{4}",
|
---|
210 | Variable,
|
---|
211 | "[",
|
---|
212 | Interval?.LowerBound,
|
---|
213 | Interval?.UpperBound,
|
---|
214 | "]");
|
---|
215 | foreach(var region in Regions)
|
---|
216 | expression += $", {region.VariableName}=({region.Interval.LowerBound} .. {region.Interval.UpperBound})";
|
---|
217 | Expression = expression;
|
---|
218 | return;
|
---|
219 | }
|
---|
220 |
|
---|
221 | expression = string.Format("∂{6}{1}/∂{0}{6} in {2}{3} .. {4}{5}",
|
---|
222 | Variable,
|
---|
223 | Target,
|
---|
224 | "[",
|
---|
225 | Interval?.LowerBound,
|
---|
226 | Interval?.UpperBound,
|
---|
227 | "]",
|
---|
228 | GetDerivationString(numberOfDerivations));
|
---|
229 | foreach (var region in Regions)
|
---|
230 | expression += $", {region.VariableName}=({region.Interval.LowerBound} .. {region.Interval.UpperBound})";
|
---|
231 | Expression = expression;
|
---|
232 | }
|
---|
233 |
|
---|
234 | private string GetDefinitionString() {
|
---|
235 | if (!IsDerivative) return "Target " + Variable;
|
---|
236 |
|
---|
237 | var definition =
|
---|
238 | $"∂{GetDerivationString(numberOfDerivations)}Target/∂{Variable}{GetDerivationString(numberOfDerivations)}";
|
---|
239 | return definition;
|
---|
240 | }
|
---|
241 |
|
---|
242 | public override string ToString() {
|
---|
243 | return Expression;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | } |
---|