1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using System.Globalization;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Constraints {
|
---|
31 | /// <summary>
|
---|
32 | /// Constraint where a double value is limited by a one or two sided boundary.
|
---|
33 | /// </summary>
|
---|
34 | public class DoubleBoundedConstraint : ConstraintBase {
|
---|
35 | private double lowerBound;
|
---|
36 | /// <summary>
|
---|
37 | /// Gets or sets the lower bound of the limit.
|
---|
38 | /// </summary>
|
---|
39 | /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
|
---|
40 | /// in the setter.</remarks>
|
---|
41 | public double LowerBound {
|
---|
42 | get { return lowerBound; }
|
---|
43 | set {
|
---|
44 | lowerBound = value;
|
---|
45 | OnChanged();
|
---|
46 | }
|
---|
47 | }
|
---|
48 | private bool lowerBoundIncluded;
|
---|
49 | /// <summary>
|
---|
50 | /// Gets or sets the boolean flag whether the lower bound should be included.
|
---|
51 | /// </summary>
|
---|
52 | /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
|
---|
53 | /// in the setter.</remarks>
|
---|
54 | public bool LowerBoundIncluded {
|
---|
55 | get { return lowerBoundIncluded; }
|
---|
56 | set {
|
---|
57 | lowerBoundIncluded = value;
|
---|
58 | OnChanged();
|
---|
59 | }
|
---|
60 | }
|
---|
61 | private bool lowerBoundEnabled;
|
---|
62 | /// <summary>
|
---|
63 | /// Gets or sets the boolean flag whether the lower bound should be enabled.
|
---|
64 | /// </summary>
|
---|
65 | /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
|
---|
66 | /// in the setter.</remarks>
|
---|
67 | public bool LowerBoundEnabled {
|
---|
68 | get { return lowerBoundEnabled; }
|
---|
69 | set {
|
---|
70 | lowerBoundEnabled = value;
|
---|
71 | OnChanged();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | private double upperBound;
|
---|
75 | /// <summary>
|
---|
76 | /// Gets or sets the upper bound of the limit.
|
---|
77 | /// </summary>
|
---|
78 | /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
|
---|
79 | /// in the setter.</remarks>
|
---|
80 | public double UpperBound {
|
---|
81 | get { return upperBound; }
|
---|
82 | set {
|
---|
83 | upperBound = value;
|
---|
84 | OnChanged();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | private bool upperBoundIncluded;
|
---|
88 | /// <summary>
|
---|
89 | /// Gets or sets the boolean flag whether the upper bound should be included.
|
---|
90 | /// </summary>
|
---|
91 | /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
|
---|
92 | /// in the setter.</remarks>
|
---|
93 | public bool UpperBoundIncluded {
|
---|
94 | get { return upperBoundIncluded; }
|
---|
95 | set {
|
---|
96 | upperBoundIncluded = value;
|
---|
97 | OnChanged();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | private bool upperBoundEnabled;
|
---|
101 | /// <summary>
|
---|
102 | /// Gets or sets the boolean flag whether the upper bound should be enabled.
|
---|
103 | /// </summary>
|
---|
104 | /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
|
---|
105 | /// in the setter.</remarks>
|
---|
106 | public bool UpperBoundEnabled {
|
---|
107 | get { return upperBoundEnabled; }
|
---|
108 | set {
|
---|
109 | upperBoundEnabled = value;
|
---|
110 | OnChanged();
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /// <inheritdoc select="summary"/>
|
---|
115 | public override string Description {
|
---|
116 | get { return "The double is limited one or two sided by a lower and/or upper boundary"; }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /// <summary>
|
---|
120 | /// Initializes a new instance of <see cref="DoubleBoundedConstraint"/>.
|
---|
121 | /// </summary>
|
---|
122 | public DoubleBoundedConstraint()
|
---|
123 | : this(double.MinValue, double.MaxValue) {
|
---|
124 | }
|
---|
125 |
|
---|
126 | /// <summary>
|
---|
127 | /// Initializes a new instance of <see cref="DoubleBoundedConstraint"/> with the two given boundaries.
|
---|
128 | /// </summary>
|
---|
129 | /// <param name="lowerBound">The lower bound of the constraint.</param>
|
---|
130 | /// <param name="upperBound">The upper bound of the constraint.</param>
|
---|
131 | public DoubleBoundedConstraint(double lowerBound, double upperBound)
|
---|
132 | : this(lowerBound, true, upperBound, true) {
|
---|
133 | }
|
---|
134 |
|
---|
135 | /// <summary>
|
---|
136 | /// Initializes a new instance of <see cref="DoubleBoundedConstraint"/> with the given parameters.
|
---|
137 | /// </summary>
|
---|
138 | /// <param name="lowerBound">The lower bound of the constraint.</param>
|
---|
139 | /// <param name="lowerBoundIncluded">Boolean flag whether the lower bound should be included.</param>
|
---|
140 | /// <param name="upperBound">The upper bound of the constraint.</param>
|
---|
141 | /// <param name="upperBoundIncluded">Boolean flag whether the upper bound should be included.</param>
|
---|
142 | public DoubleBoundedConstraint(double lowerBound, bool lowerBoundIncluded, double upperBound, bool upperBoundIncluded)
|
---|
143 | : base() {
|
---|
144 | this.lowerBound = lowerBound;
|
---|
145 | this.lowerBoundIncluded = lowerBoundIncluded;
|
---|
146 | this.lowerBoundEnabled = true;
|
---|
147 | this.upperBound = upperBound;
|
---|
148 | this.upperBoundIncluded = upperBoundIncluded;
|
---|
149 | this.upperBoundEnabled = true;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /// <summary>
|
---|
154 | /// Checks whether the given element fulfills the current constraint.
|
---|
155 | /// </summary>
|
---|
156 | /// <param name="data">The item to check.</param>
|
---|
157 | /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
|
---|
158 | public override bool Check(IItem data) {
|
---|
159 | ConstrainedDoubleData d = (data as ConstrainedDoubleData);
|
---|
160 | if (d == null) return false;
|
---|
161 | if (LowerBoundEnabled && ((LowerBoundIncluded && d.CompareTo(LowerBound) < 0)
|
---|
162 | || (!LowerBoundIncluded && d.CompareTo(LowerBound) <= 0))) return false;
|
---|
163 | if (UpperBoundEnabled && ((UpperBoundIncluded && d.CompareTo(UpperBound) > 0)
|
---|
164 | || (!UpperBoundIncluded && d.CompareTo(UpperBound) >= 0))) return false;
|
---|
165 | return true;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /// <summary>
|
---|
169 | /// Creates a new instance of <see cref="DoubleBoundedConstraintView"/> to represent the current
|
---|
170 | /// instance visually.
|
---|
171 | /// </summary>
|
---|
172 | /// <returns>The created view as <see cref="DoubleBoundedConstraintView"/>.</returns>
|
---|
173 | public override IView CreateView() {
|
---|
174 | return new DoubleBoundedConstraintView(this);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /// <summary>
|
---|
178 | /// Clones the current instance (deep clone).
|
---|
179 | /// </summary>
|
---|
180 | /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
|
---|
181 | /// <returns>The cloned object as <see cref="DoubleBoundedConstraint"/>.</returns>
|
---|
182 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
183 | DoubleBoundedConstraint clone = new DoubleBoundedConstraint();
|
---|
184 | clonedObjects.Add(Guid, clone);
|
---|
185 | clone.upperBound = UpperBound;
|
---|
186 | clone.upperBoundIncluded = UpperBoundIncluded;
|
---|
187 | clone.upperBoundEnabled = UpperBoundEnabled;
|
---|
188 | clone.lowerBound = LowerBound;
|
---|
189 | clone.lowerBoundIncluded = LowerBoundIncluded;
|
---|
190 | clone.lowerBoundEnabled = LowerBoundEnabled;
|
---|
191 | return clone;
|
---|
192 | }
|
---|
193 |
|
---|
194 | #region persistence
|
---|
195 | /// <summary>
|
---|
196 | /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
|
---|
197 | /// </summary>
|
---|
198 | /// <remarks>The properties of the current instance are saved as attributes with special tag names.</remarks>
|
---|
199 | /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
|
---|
200 | /// <param name="document">The <see cref="XmlDocument"/> where the data is saved.</param>
|
---|
201 | /// <param name="persistedObjects">The dictionary of all already persisted objects.
|
---|
202 | /// (Needed to avoid cycles.)</param>
|
---|
203 | /// <returns>The saved <see cref="XmlNode"/>.</returns>
|
---|
204 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
205 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
206 | XmlAttribute lb = document.CreateAttribute("LowerBound");
|
---|
207 | lb.Value = LowerBound.ToString("r", CultureInfo.InvariantCulture);
|
---|
208 | XmlAttribute lbi = document.CreateAttribute("LowerBoundIncluded");
|
---|
209 | lbi.Value = lowerBoundIncluded.ToString();
|
---|
210 | XmlAttribute lbe = document.CreateAttribute("LowerBoundEnabled");
|
---|
211 | lbe.Value = lowerBoundEnabled.ToString();
|
---|
212 | XmlAttribute ub = document.CreateAttribute("UpperBound");
|
---|
213 | ub.Value = upperBound.ToString("r", CultureInfo.InvariantCulture);
|
---|
214 | XmlAttribute ubi = document.CreateAttribute("UpperBoundIncluded");
|
---|
215 | ubi.Value = upperBoundIncluded.ToString();
|
---|
216 | XmlAttribute ube = document.CreateAttribute("UpperBoundEnabled");
|
---|
217 | ube.Value = upperBoundEnabled.ToString();
|
---|
218 | node.Attributes.Append(lb);
|
---|
219 | if (!lowerBoundIncluded) node.Attributes.Append(lbi);
|
---|
220 | if (!lowerBoundEnabled) node.Attributes.Append(lbe);
|
---|
221 | node.Attributes.Append(ub);
|
---|
222 | if (!upperBoundIncluded) node.Attributes.Append(ubi);
|
---|
223 | if (!upperBoundEnabled) node.Attributes.Append(ube);
|
---|
224 | return node;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /// <summary>
|
---|
228 | /// Loads the persisted constraint from the specified <paramref name="node"/>.
|
---|
229 | /// </summary>
|
---|
230 | /// <remarks>The constraint must be saved in a specific way, see <see cref="GetXmlNode"/> for
|
---|
231 | /// more information.</remarks>
|
---|
232 | /// <param name="node">The <see cref="XmlNode"/> where the instance is saved.</param>
|
---|
233 | /// <param name="restoredObjects">The dictionary of all already restored objects.
|
---|
234 | /// (Needed to avoid cycles.)</param>
|
---|
235 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
236 | base.Populate(node, restoredObjects);
|
---|
237 | lowerBound = double.Parse(node.Attributes["LowerBound"].Value, CultureInfo.InvariantCulture);
|
---|
238 | if (node.Attributes["LowerBoundIncluded"] != null) {
|
---|
239 | lowerBoundIncluded = bool.Parse(node.Attributes["LowerBoundIncluded"].Value);
|
---|
240 | } else {
|
---|
241 | lowerBoundIncluded = true;
|
---|
242 | }
|
---|
243 | if (node.Attributes["LowerBoundEnabled"] != null) {
|
---|
244 | lowerBoundEnabled = bool.Parse(node.Attributes["LowerBoundEnabled"].Value);
|
---|
245 | } else {
|
---|
246 | lowerBoundEnabled = true;
|
---|
247 | }
|
---|
248 |
|
---|
249 | upperBound = double.Parse(node.Attributes["UpperBound"].Value, CultureInfo.InvariantCulture);
|
---|
250 | if (node.Attributes["UpperBoundIncluded"] != null) {
|
---|
251 | upperBoundIncluded = bool.Parse(node.Attributes["UpperBoundIncluded"].Value);
|
---|
252 | } else {
|
---|
253 | upperBoundIncluded = true;
|
---|
254 | }
|
---|
255 | if (node.Attributes["UpperBoundEnabled"] != null) {
|
---|
256 | upperBoundEnabled = bool.Parse(node.Attributes["UpperBoundEnabled"].Value);
|
---|
257 | } else {
|
---|
258 | upperBoundEnabled = true;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | #endregion
|
---|
262 | }
|
---|
263 | }
|
---|