Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/3.2/IntBoundedConstraint.cs @ 1587

Last change on this file since 1587 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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