[2] | 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;
|
---|
[1823] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 29 |
|
---|
| 30 | namespace HeuristicLab.Constraints {
|
---|
[1176] | 31 | /// <summary>
|
---|
| 32 | /// Constraint where an integer value is limited by a one or two sided boundary.
|
---|
| 33 | /// </summary>
|
---|
[2] | 34 | public class IntBoundedConstraint : ConstraintBase {
|
---|
[1672] | 35 |
|
---|
| 36 | [Storable]
|
---|
[2] | 37 | private int lowerBound;
|
---|
[1176] | 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>
|
---|
[2] | 43 | public int LowerBound {
|
---|
| 44 | get { return lowerBound; }
|
---|
| 45 | set {
|
---|
| 46 | lowerBound = value;
|
---|
| 47 | OnChanged();
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
[1672] | 50 |
|
---|
| 51 | [Storable]
|
---|
[2] | 52 | private bool lowerBoundIncluded;
|
---|
[1176] | 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>
|
---|
[2] | 58 | public bool LowerBoundIncluded {
|
---|
| 59 | get { return lowerBoundIncluded; }
|
---|
| 60 | set {
|
---|
| 61 | lowerBoundIncluded = value;
|
---|
| 62 | OnChanged();
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
[1672] | 65 |
|
---|
| 66 | [Storable]
|
---|
[2] | 67 | private bool lowerBoundEnabled;
|
---|
[1176] | 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>
|
---|
[2] | 73 | public bool LowerBoundEnabled {
|
---|
| 74 | get { return lowerBoundEnabled; }
|
---|
| 75 | set {
|
---|
| 76 | lowerBoundEnabled = value;
|
---|
| 77 | OnChanged();
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
[1672] | 80 |
|
---|
| 81 | [Storable]
|
---|
[2] | 82 | private int upperBound;
|
---|
[1176] | 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>
|
---|
[2] | 88 | public int UpperBound {
|
---|
| 89 | get { return upperBound; }
|
---|
| 90 | set {
|
---|
| 91 | upperBound = value;
|
---|
| 92 | OnChanged();
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
[1672] | 95 |
|
---|
| 96 | [Storable]
|
---|
[2] | 97 | private bool upperBoundIncluded;
|
---|
[1176] | 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>
|
---|
[2] | 103 | public bool UpperBoundIncluded {
|
---|
| 104 | get { return upperBoundIncluded; }
|
---|
| 105 | set {
|
---|
| 106 | upperBoundIncluded = value;
|
---|
| 107 | OnChanged();
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
[1672] | 110 |
|
---|
| 111 | [Storable]
|
---|
[2] | 112 | private bool upperBoundEnabled;
|
---|
[1176] | 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>
|
---|
[2] | 118 | public bool UpperBoundEnabled {
|
---|
| 119 | get { return upperBoundEnabled; }
|
---|
| 120 | set {
|
---|
| 121 | upperBoundEnabled = value;
|
---|
| 122 | OnChanged();
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[1176] | 126 | /// <inheritdoc select="summary"/>
|
---|
[2] | 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 |
|
---|
[1176] | 131 | /// <summary>
|
---|
| 132 | /// Initializes a new instance of <see cref="IntBoundedConstraint"/>.
|
---|
| 133 | /// </summary>
|
---|
[2] | 134 | public IntBoundedConstraint()
|
---|
| 135 | : this(int.MinValue, int.MaxValue) {
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[1176] | 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>
|
---|
[1672] | 143 | public IntBoundedConstraint(int low, int high)
|
---|
| 144 | : base() {
|
---|
[2] | 145 | lowerBound = low;
|
---|
| 146 | lowerBoundIncluded = false;
|
---|
| 147 | lowerBoundEnabled = true;
|
---|
| 148 | upperBound = high;
|
---|
| 149 | upperBoundIncluded = false;
|
---|
| 150 | upperBoundEnabled = true;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[1176] | 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>
|
---|
[2] | 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 |
|
---|
[1176] | 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>
|
---|
[2] | 173 | public override IView CreateView() {
|
---|
| 174 | return new IntBoundedConstraintView(this);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[1176] | 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>
|
---|
[2] | 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 | }
|
---|