[583] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Globalization;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using System.Xml;
|
---|
| 7 | using HeuristicLab.Constraints;
|
---|
| 8 | using HeuristicLab.Core;
|
---|
| 9 | using HeuristicLab.Data;
|
---|
| 10 |
|
---|
| 11 | namespace HeuristicLab.SimOpt {
|
---|
| 12 | public class DoubleParameterBoundConstraint : ConstraintBase {
|
---|
| 13 | private double lowerBound;
|
---|
| 14 | public double LowerBound {
|
---|
| 15 | get { return lowerBound; }
|
---|
| 16 | set {
|
---|
| 17 | lowerBound = value;
|
---|
| 18 | OnChanged();
|
---|
| 19 | }
|
---|
| 20 | }
|
---|
| 21 | private bool lowerBoundIncluded;
|
---|
| 22 | public bool LowerBoundIncluded {
|
---|
| 23 | get { return lowerBoundIncluded; }
|
---|
| 24 | set {
|
---|
| 25 | lowerBoundIncluded = value;
|
---|
| 26 | OnChanged();
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 | private bool lowerBoundEnabled;
|
---|
| 30 | public bool LowerBoundEnabled {
|
---|
| 31 | get { return lowerBoundEnabled; }
|
---|
| 32 | set {
|
---|
| 33 | lowerBoundEnabled = value;
|
---|
| 34 | OnChanged();
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 | private double upperBound;
|
---|
| 38 | public double UpperBound {
|
---|
| 39 | get { return upperBound; }
|
---|
| 40 | set {
|
---|
| 41 | upperBound = value;
|
---|
| 42 | OnChanged();
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | private bool upperBoundIncluded;
|
---|
| 46 | public bool UpperBoundIncluded {
|
---|
| 47 | get { return upperBoundIncluded; }
|
---|
| 48 | set {
|
---|
| 49 | upperBoundIncluded = value;
|
---|
| 50 | OnChanged();
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 | private bool upperBoundEnabled;
|
---|
| 54 | public bool UpperBoundEnabled {
|
---|
| 55 | get { return upperBoundEnabled; }
|
---|
| 56 | set {
|
---|
| 57 | upperBoundEnabled = value;
|
---|
| 58 | OnChanged();
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | private string parameterName;
|
---|
| 62 | public string ParameterName {
|
---|
| 63 | get { return parameterName; }
|
---|
| 64 | set {
|
---|
| 65 | parameterName = value;
|
---|
| 66 | OnChanged();
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public override string Description {
|
---|
| 71 | get { return "The double is limited one or two sided by a lower and/or upper boundary"; }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public DoubleParameterBoundConstraint()
|
---|
| 75 | : this(double.MinValue, double.MaxValue, "undefined") {
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public DoubleParameterBoundConstraint(double lowerBound, double upperBound, string parameterName)
|
---|
| 79 | : this(lowerBound, true, upperBound, true, parameterName) {
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public DoubleParameterBoundConstraint(double lowerBound, bool lowerBoundIncluded, double upperBound, bool upperBoundIncluded, string parameterName)
|
---|
| 83 | : base() {
|
---|
| 84 | this.lowerBound = lowerBound;
|
---|
| 85 | this.lowerBoundIncluded = lowerBoundIncluded;
|
---|
| 86 | this.lowerBoundEnabled = true;
|
---|
| 87 | this.upperBound = upperBound;
|
---|
| 88 | this.upperBoundIncluded = upperBoundIncluded;
|
---|
| 89 | this.upperBoundEnabled = true;
|
---|
| 90 | this.parameterName = parameterName;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | public override bool Check(IItem data) {
|
---|
| 95 | ConstrainedItemList list = (data as ConstrainedItemList);
|
---|
| 96 | if (list == null) return false;
|
---|
| 97 | for (int i = 0; i < list.Count; i++) {
|
---|
| 98 | if (list[i] is IVariable && ((IVariable)list[i]).Name.Equals(parameterName) && ((IVariable)list[i]).Value is DoubleData) {
|
---|
| 99 | if (LowerBoundEnabled && ((LowerBoundIncluded && ((IComparable)((IVariable)list[i]).Value).CompareTo(LowerBound) < 0)
|
---|
| 100 | || (!LowerBoundIncluded && ((IComparable)((IVariable)list[i]).Value).CompareTo(LowerBound) <= 0))) return false;
|
---|
| 101 | if (UpperBoundEnabled && ((UpperBoundIncluded && ((IComparable)((IVariable)list[i]).Value).CompareTo(UpperBound) > 0)
|
---|
| 102 | || (!UpperBoundIncluded && ((IComparable)((IVariable)list[i]).Value).CompareTo(UpperBound) >= 0))) return false;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | return true;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public override IView CreateView() {
|
---|
| 109 | return new DoubleParameterBoundConstraintView(this);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 113 | DoubleParameterBoundConstraint clone = new DoubleParameterBoundConstraint();
|
---|
| 114 | clonedObjects.Add(Guid, clone);
|
---|
| 115 | clone.parameterName = (string)parameterName.Clone();
|
---|
| 116 | clone.upperBound = UpperBound;
|
---|
| 117 | clone.upperBoundIncluded = UpperBoundIncluded;
|
---|
| 118 | clone.upperBoundEnabled = UpperBoundEnabled;
|
---|
| 119 | clone.lowerBound = LowerBound;
|
---|
| 120 | clone.lowerBoundIncluded = LowerBoundIncluded;
|
---|
| 121 | clone.lowerBoundEnabled = LowerBoundEnabled;
|
---|
| 122 | return clone;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | #region persistence
|
---|
| 126 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 127 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 128 | XmlAttribute pm = document.CreateAttribute("ParameterName");
|
---|
| 129 | pm.Value = parameterName;
|
---|
| 130 | XmlAttribute lb = document.CreateAttribute("LowerBound");
|
---|
| 131 | lb.Value = LowerBound.ToString("r", CultureInfo.InvariantCulture);
|
---|
| 132 | XmlAttribute lbi = document.CreateAttribute("LowerBoundIncluded");
|
---|
| 133 | lbi.Value = lowerBoundIncluded.ToString();
|
---|
| 134 | XmlAttribute lbe = document.CreateAttribute("LowerBoundEnabled");
|
---|
| 135 | lbe.Value = lowerBoundEnabled.ToString();
|
---|
| 136 | XmlAttribute ub = document.CreateAttribute("UpperBound");
|
---|
| 137 | ub.Value = upperBound.ToString("r", CultureInfo.InvariantCulture);
|
---|
| 138 | XmlAttribute ubi = document.CreateAttribute("UpperBoundIncluded");
|
---|
| 139 | ubi.Value = upperBoundIncluded.ToString();
|
---|
| 140 | XmlAttribute ube = document.CreateAttribute("UpperBoundEnabled");
|
---|
| 141 | ube.Value = upperBoundEnabled.ToString();
|
---|
| 142 | node.Attributes.Append(pm);
|
---|
| 143 | node.Attributes.Append(lb);
|
---|
| 144 | if (!lowerBoundIncluded) node.Attributes.Append(lbi);
|
---|
| 145 | if (!lowerBoundEnabled) node.Attributes.Append(lbe);
|
---|
| 146 | node.Attributes.Append(ub);
|
---|
| 147 | if (!upperBoundIncluded) node.Attributes.Append(ubi);
|
---|
| 148 | if (!upperBoundEnabled) node.Attributes.Append(ube);
|
---|
| 149 | return node;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 153 | base.Populate(node, restoredObjects);
|
---|
| 154 | parameterName = node.Attributes["ParameterName"].Value;
|
---|
| 155 | lowerBound = double.Parse(node.Attributes["LowerBound"].Value, CultureInfo.InvariantCulture);
|
---|
| 156 | if (node.Attributes["LowerBoundIncluded"] != null) {
|
---|
| 157 | lowerBoundIncluded = bool.Parse(node.Attributes["LowerBoundIncluded"].Value);
|
---|
| 158 | } else {
|
---|
| 159 | lowerBoundIncluded = true;
|
---|
| 160 | }
|
---|
| 161 | if (node.Attributes["LowerBoundEnabled"] != null) {
|
---|
| 162 | lowerBoundEnabled = bool.Parse(node.Attributes["LowerBoundEnabled"].Value);
|
---|
| 163 | } else {
|
---|
| 164 | lowerBoundEnabled = true;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | upperBound = double.Parse(node.Attributes["UpperBound"].Value, CultureInfo.InvariantCulture);
|
---|
| 168 | if (node.Attributes["UpperBoundIncluded"] != null) {
|
---|
| 169 | upperBoundIncluded = bool.Parse(node.Attributes["UpperBoundIncluded"].Value);
|
---|
| 170 | } else {
|
---|
| 171 | upperBoundIncluded = true;
|
---|
| 172 | }
|
---|
| 173 | if (node.Attributes["UpperBoundEnabled"] != null) {
|
---|
| 174 | upperBoundEnabled = bool.Parse(node.Attributes["UpperBoundEnabled"].Value);
|
---|
| 175 | } else {
|
---|
| 176 | upperBoundEnabled = true;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | #endregion
|
---|
| 180 | }
|
---|
| 181 | }
|
---|