Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/3.3/IntBoundedConstraint.cs @ 2285

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

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

File size: 6.7 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Constraints {
31  /// <summary>
32  /// Constraint where an integer value is limited by a one or two sided boundary.
33  /// </summary>
34  public class IntBoundedConstraint : ConstraintBase {
35
36    [Storable]
37    private int lowerBound;
38    /// <summary>
39    /// Gets or sets the lower bound of the limit.
40    /// </summary>
41    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
42    /// in the setter.</remarks>
43    public int LowerBound {
44      get { return lowerBound; }
45      set {
46        lowerBound = value;
47        OnChanged();
48      }
49    }
50
51    [Storable]
52    private bool lowerBoundIncluded;
53    /// <summary>
54    /// Gets or sets the boolean flag whether the lower bound should be included.
55    /// </summary>
56    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
57    /// in the setter.</remarks>
58    public bool LowerBoundIncluded {
59      get { return lowerBoundIncluded; }
60      set {
61        lowerBoundIncluded = value;
62        OnChanged();
63      }
64    }
65
66    [Storable]
67    private bool lowerBoundEnabled;
68    /// <summary>
69    /// Gets or sets the boolean flag whether the lower bound should be enabled.
70    /// </summary>
71    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
72    /// in the setter.</remarks>
73    public bool LowerBoundEnabled {
74      get { return lowerBoundEnabled; }
75      set {
76        lowerBoundEnabled = value;
77        OnChanged();
78      }
79    }
80
81    [Storable]
82    private int upperBound;
83    /// <summary>
84    /// Gets or sets the upper bound of the limit.
85    /// </summary>
86    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
87    /// in the setter.</remarks>
88    public int UpperBound {
89      get { return upperBound; }
90      set {
91        upperBound = value;
92        OnChanged();
93      }
94    }
95
96    [Storable]
97    private bool upperBoundIncluded;
98    /// <summary>
99    /// Gets or sets the boolean flag whether the upper bound should be included.
100    /// </summary>
101    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
102    /// in the setter.</remarks>
103    public bool UpperBoundIncluded {
104      get { return upperBoundIncluded; }
105      set {
106        upperBoundIncluded = value;
107        OnChanged();
108      }
109    }
110
111    [Storable]
112    private bool upperBoundEnabled;
113    /// <summary>
114    /// Gets or sets the boolean flag whether the upper bound should be enabled.
115    /// </summary>
116    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
117    /// in the setter.</remarks>
118    public bool UpperBoundEnabled {
119      get { return upperBoundEnabled; }
120      set {
121        upperBoundEnabled = value;
122        OnChanged();
123      }
124    }
125
126    /// <inheritdoc select="summary"/>
127    public override string Description {
128      get { return "The integer is limited one or two sided by a lower and/or upper boundary"; }
129    }
130
131    /// <summary>
132    /// Initializes a new instance of <see cref="IntBoundedConstraint"/>.
133    /// </summary>
134    public IntBoundedConstraint()
135      : this(int.MinValue, int.MaxValue) {
136    }
137
138    /// <summary>
139    /// Initializes a new instance of <see cref="IntBoundedConstraint"/> with the two given boundaries.
140    /// </summary>
141    /// <param name="low">The lower bound of the constraint.</param>
142    /// <param name="high">The upper bound of the constraint.</param>
143    public IntBoundedConstraint(int low, int high)
144      : base() {
145      lowerBound = low;
146      lowerBoundIncluded = false;
147      lowerBoundEnabled = true;
148      upperBound = high;
149      upperBoundIncluded = false;
150      upperBoundEnabled = true;
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      ConstrainedIntData d = (data as ConstrainedIntData);
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="IntBoundedConstraintView"/> to represent the current
170    /// instance visually.
171    /// </summary>
172    /// <returns>The created view as <see cref="IntBoundedConstraintView"/>.</returns>
173    public override IView CreateView() {
174      return new IntBoundedConstraintView(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="IntBoundedConstraint"/>.</returns>
182    public override object Clone(IDictionary<Guid, object> clonedObjects) {
183      IntBoundedConstraint clone = new IntBoundedConstraint();
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}
Note: See TracBrowser for help on using the repository browser.