#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using HeuristicLab.Core;
using HeuristicLab.Data;
using System.Globalization;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Constraints {
///
/// Constraint where a double value is limited by a one or two sided boundary.
///
public class DoubleBoundedConstraint : ConstraintBase {
[Storable]
private double lowerBound;
///
/// Gets or sets the lower bound of the limit.
///
/// Calls of base class
/// in the setter.
public double LowerBound {
get { return lowerBound; }
set {
lowerBound = value;
OnChanged();
}
}
[Storable]
private bool lowerBoundIncluded;
///
/// Gets or sets the boolean flag whether the lower bound should be included.
///
/// Calls of base class
/// in the setter.
public bool LowerBoundIncluded {
get { return lowerBoundIncluded; }
set {
lowerBoundIncluded = value;
OnChanged();
}
}
[Storable]
private bool lowerBoundEnabled;
///
/// Gets or sets the boolean flag whether the lower bound should be enabled.
///
/// Calls of base class
/// in the setter.
public bool LowerBoundEnabled {
get { return lowerBoundEnabled; }
set {
lowerBoundEnabled = value;
OnChanged();
}
}
[Storable]
private double upperBound;
///
/// Gets or sets the upper bound of the limit.
///
/// Calls of base class
/// in the setter.
public double UpperBound {
get { return upperBound; }
set {
upperBound = value;
OnChanged();
}
}
[Storable]
private bool upperBoundIncluded;
///
/// Gets or sets the boolean flag whether the upper bound should be included.
///
/// Calls of base class
/// in the setter.
public bool UpperBoundIncluded {
get { return upperBoundIncluded; }
set {
upperBoundIncluded = value;
OnChanged();
}
}
[Storable]
private bool upperBoundEnabled;
///
/// Gets or sets the boolean flag whether the upper bound should be enabled.
///
/// Calls of base class
/// in the setter.
public bool UpperBoundEnabled {
get { return upperBoundEnabled; }
set {
upperBoundEnabled = value;
OnChanged();
}
}
///
public override string Description {
get { return "The double is limited one or two sided by a lower and/or upper boundary"; }
}
///
/// Initializes a new instance of .
///
public DoubleBoundedConstraint()
: this(double.MinValue, double.MaxValue) {
}
///
/// Initializes a new instance of with the two given boundaries.
///
/// The lower bound of the constraint.
/// The upper bound of the constraint.
public DoubleBoundedConstraint(double lowerBound, double upperBound)
: this(lowerBound, true, upperBound, true) {
}
///
/// Initializes a new instance of with the given parameters.
///
/// The lower bound of the constraint.
/// Boolean flag whether the lower bound should be included.
/// The upper bound of the constraint.
/// Boolean flag whether the upper bound should be included.
public DoubleBoundedConstraint(double lowerBound, bool lowerBoundIncluded, double upperBound, bool upperBoundIncluded)
: base() {
this.lowerBound = lowerBound;
this.lowerBoundIncluded = lowerBoundIncluded;
this.lowerBoundEnabled = true;
this.upperBound = upperBound;
this.upperBoundIncluded = upperBoundIncluded;
this.upperBoundEnabled = true;
}
///
/// Checks whether the given element fulfills the current constraint.
///
/// The item to check.
/// true if the constraint could be fulfilled, false otherwise.
public override bool Check(IItem data) {
ConstrainedDoubleData d = (data as ConstrainedDoubleData);
if (d == null) return false;
if (LowerBoundEnabled && ((LowerBoundIncluded && d.CompareTo(LowerBound) < 0)
|| (!LowerBoundIncluded && d.CompareTo(LowerBound) <= 0))) return false;
if (UpperBoundEnabled && ((UpperBoundIncluded && d.CompareTo(UpperBound) > 0)
|| (!UpperBoundIncluded && d.CompareTo(UpperBound) >= 0))) return false;
return true;
}
///
/// Creates a new instance of to represent the current
/// instance visually.
///
/// The created view as .
public override IView CreateView() {
return new DoubleBoundedConstraintView(this);
}
///
/// Clones the current instance (deep clone).
///
/// Dictionary of all already clone objects. (Needed to avoid cycles.)
/// The cloned object as .
public override object Clone(IDictionary clonedObjects) {
DoubleBoundedConstraint clone = new DoubleBoundedConstraint();
clonedObjects.Add(Guid, clone);
clone.upperBound = UpperBound;
clone.upperBoundIncluded = UpperBoundIncluded;
clone.upperBoundEnabled = UpperBoundEnabled;
clone.lowerBound = LowerBound;
clone.lowerBoundIncluded = LowerBoundIncluded;
clone.lowerBoundEnabled = LowerBoundEnabled;
return clone;
}
}
}