Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/3.3/DoubleBoundedConstraint.cs @ 2280

Last change on this file since 2280 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 7.6 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;
28using System.Globalization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Constraints {
32  /// <summary>
33  /// Constraint where a double value is limited by a one or two sided boundary.
34  /// </summary>
35  public class DoubleBoundedConstraint : ConstraintBase {
36
37    [Storable]
38    private double lowerBound;
39    /// <summary>
40    /// Gets or sets the lower bound of the limit.
41    /// </summary>
42    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
43    /// in the setter.</remarks>
44    public double LowerBound {
45      get { return lowerBound; }
46      set {
47        lowerBound = value;
48        OnChanged();
49      }
50    }
51
52    [Storable]
53    private bool lowerBoundIncluded;
54    /// <summary>
55    /// Gets or sets the boolean flag whether the lower bound should be included.
56    /// </summary>
57    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
58    /// in the setter.</remarks>
59    public bool LowerBoundIncluded {
60      get { return lowerBoundIncluded; }
61      set {
62        lowerBoundIncluded = value;
63        OnChanged();
64      }
65    }
66
67    [Storable]
68    private bool lowerBoundEnabled;
69    /// <summary>
70    /// Gets or sets the boolean flag whether the lower bound should be enabled.
71    /// </summary>
72    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
73    /// in the setter.</remarks>
74    public bool LowerBoundEnabled {
75      get { return lowerBoundEnabled; }
76      set {
77        lowerBoundEnabled = value;
78        OnChanged();
79      }
80    }
81
82    [Storable]
83    private double upperBound;
84    /// <summary>
85    /// Gets or sets the upper bound of the limit.
86    /// </summary>
87    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
88    /// in the setter.</remarks>
89    public double UpperBound {
90      get { return upperBound; }
91      set {
92        upperBound = value;
93        OnChanged();
94      }
95    }
96
97    [Storable]
98    private bool upperBoundIncluded;
99    /// <summary>
100    /// Gets or sets the boolean flag whether the upper bound should be included.
101    /// </summary>
102    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
103    /// in the setter.</remarks>
104    public bool UpperBoundIncluded {
105      get { return upperBoundIncluded; }
106      set {
107        upperBoundIncluded = value;
108        OnChanged();
109      }
110    }
111
112    [Storable]
113    private bool upperBoundEnabled;
114    /// <summary>
115    /// Gets or sets the boolean flag whether the upper bound should be enabled.
116    /// </summary>
117    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
118    /// in the setter.</remarks>
119    public bool UpperBoundEnabled {
120      get { return upperBoundEnabled; }
121      set {
122        upperBoundEnabled = value;
123        OnChanged();
124      }
125    }
126
127    /// <inheritdoc select="summary"/>
128    public override string Description {
129      get { return "The double is limited one or two sided by a lower and/or upper boundary"; }
130    }
131
132    /// <summary>
133    /// Initializes a new instance of <see cref="DoubleBoundedConstraint"/>.
134    /// </summary>
135    public DoubleBoundedConstraint()
136      : this(double.MinValue, double.MaxValue) {
137    }
138
139    /// <summary>
140    /// Initializes a new instance of <see cref="DoubleBoundedConstraint"/> with the two given boundaries.
141    /// </summary>
142    /// <param name="lowerBound">The lower bound of the constraint.</param>
143    /// <param name="upperBound">The upper bound of the constraint.</param>
144    public DoubleBoundedConstraint(double lowerBound, double upperBound)
145      : this(lowerBound, true, upperBound, true) {
146    }
147
148    /// <summary>
149    /// Initializes a new instance of <see cref="DoubleBoundedConstraint"/> with the given parameters.
150    /// </summary>
151    /// <param name="lowerBound">The lower bound of the constraint.</param>
152    /// <param name="lowerBoundIncluded">Boolean flag whether the lower bound should be included.</param>
153    /// <param name="upperBound">The upper bound of the constraint.</param>
154    /// <param name="upperBoundIncluded">Boolean flag whether the upper bound should be included.</param>
155    public DoubleBoundedConstraint(double lowerBound, bool lowerBoundIncluded, double upperBound, bool upperBoundIncluded)
156      : base() {
157      this.lowerBound = lowerBound;
158      this.lowerBoundIncluded = lowerBoundIncluded;
159      this.lowerBoundEnabled = true;
160      this.upperBound = upperBound;
161      this.upperBoundIncluded = upperBoundIncluded;
162      this.upperBoundEnabled = true;
163    }
164
165
166    /// <summary>
167    /// Checks whether the given element fulfills the current constraint.
168    /// </summary>
169    /// <param name="data">The item to check.</param>
170    /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
171    public override bool Check(IItem data) {
172      ConstrainedDoubleData d = (data as ConstrainedDoubleData);
173      if (d == null) return false;
174      if (LowerBoundEnabled && ((LowerBoundIncluded && d.CompareTo(LowerBound) < 0)
175        || (!LowerBoundIncluded && d.CompareTo(LowerBound) <= 0))) return false;
176      if (UpperBoundEnabled && ((UpperBoundIncluded && d.CompareTo(UpperBound) > 0)
177        || (!UpperBoundIncluded && d.CompareTo(UpperBound) >= 0))) return false;
178      return true;
179    }
180
181    /// <summary>
182    /// Creates a new instance of <see cref="DoubleBoundedConstraintView"/> to represent the current
183    /// instance visually.
184    /// </summary>
185    /// <returns>The created view as <see cref="DoubleBoundedConstraintView"/>.</returns>
186    public override IView CreateView() {
187      return new DoubleBoundedConstraintView(this);
188    }
189
190    /// <summary>
191    /// Clones the current instance (deep clone).
192    /// </summary>
193    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
194    /// <returns>The cloned object as <see cref="DoubleBoundedConstraint"/>.</returns>
195    public override object Clone(IDictionary<Guid, object> clonedObjects) {
196      DoubleBoundedConstraint clone = new DoubleBoundedConstraint();
197      clonedObjects.Add(Guid, clone);
198      clone.upperBound = UpperBound;
199      clone.upperBoundIncluded = UpperBoundIncluded;
200      clone.upperBoundEnabled = UpperBoundEnabled;
201      clone.lowerBound = LowerBound;
202      clone.lowerBoundIncluded = LowerBoundIncluded;
203      clone.lowerBoundEnabled = LowerBoundEnabled;
204      return clone;
205    }
206  }
207}
Note: See TracBrowser for help on using the repository browser.