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;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Constraints {
|
---|
30 | public class DoubleBoundedConstraint : ConstraintBase {
|
---|
31 | private double lowerBound;
|
---|
32 | public double LowerBound {
|
---|
33 | get { return lowerBound; }
|
---|
34 | set {
|
---|
35 | lowerBound = value;
|
---|
36 | OnChanged();
|
---|
37 | }
|
---|
38 | }
|
---|
39 | private bool lowerBoundIncluded;
|
---|
40 | public bool LowerBoundIncluded {
|
---|
41 | get { return lowerBoundIncluded; }
|
---|
42 | set {
|
---|
43 | lowerBoundIncluded = value;
|
---|
44 | OnChanged();
|
---|
45 | }
|
---|
46 | }
|
---|
47 | private bool lowerBoundEnabled;
|
---|
48 | public bool LowerBoundEnabled {
|
---|
49 | get { return lowerBoundEnabled; }
|
---|
50 | set {
|
---|
51 | lowerBoundEnabled = value;
|
---|
52 | OnChanged();
|
---|
53 | }
|
---|
54 | }
|
---|
55 | private double upperBound;
|
---|
56 | public double UpperBound {
|
---|
57 | get { return upperBound; }
|
---|
58 | set {
|
---|
59 | upperBound = value;
|
---|
60 | OnChanged();
|
---|
61 | }
|
---|
62 | }
|
---|
63 | private bool upperBoundIncluded;
|
---|
64 | public bool UpperBoundIncluded {
|
---|
65 | get { return upperBoundIncluded; }
|
---|
66 | set {
|
---|
67 | upperBoundIncluded = value;
|
---|
68 | OnChanged();
|
---|
69 | }
|
---|
70 | }
|
---|
71 | private bool upperBoundEnabled;
|
---|
72 | public bool UpperBoundEnabled {
|
---|
73 | get { return upperBoundEnabled; }
|
---|
74 | set {
|
---|
75 | upperBoundEnabled = value;
|
---|
76 | OnChanged();
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override string Description {
|
---|
81 | get { return "The double is limited one or two sided by a lower and/or upper boundary"; }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public DoubleBoundedConstraint()
|
---|
85 | : this(double.MinValue, double.MaxValue) {
|
---|
86 | }
|
---|
87 |
|
---|
88 | public DoubleBoundedConstraint(double lowerBound, double upperBound)
|
---|
89 | : this(lowerBound, true, upperBound, true) {
|
---|
90 | }
|
---|
91 |
|
---|
92 | public DoubleBoundedConstraint(double lowerBound, bool lowerBoundIncluded, double upperBound, bool upperBoundIncluded)
|
---|
93 | : base() {
|
---|
94 | this.lowerBound = lowerBound;
|
---|
95 | this.lowerBoundIncluded = lowerBoundIncluded;
|
---|
96 | this.lowerBoundEnabled = true;
|
---|
97 | this.upperBound = upperBound;
|
---|
98 | this.upperBoundIncluded = upperBoundIncluded;
|
---|
99 | this.upperBoundEnabled = true;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | public override bool Check(IItem data) {
|
---|
104 | ConstrainedDoubleData d = (data as ConstrainedDoubleData);
|
---|
105 | if (d == null) return false;
|
---|
106 | if (LowerBoundEnabled && ((LowerBoundIncluded && d.CompareTo(LowerBound) < 0)
|
---|
107 | || (!LowerBoundIncluded && d.CompareTo(LowerBound) <= 0))) return false;
|
---|
108 | if (UpperBoundEnabled && ((UpperBoundIncluded && d.CompareTo(UpperBound) > 0)
|
---|
109 | || (!UpperBoundIncluded && d.CompareTo(UpperBound) >= 0))) return false;
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public override IView CreateView() {
|
---|
114 | return new DoubleBoundedConstraintView(this);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
118 | DoubleBoundedConstraint clone = new DoubleBoundedConstraint();
|
---|
119 | clonedObjects.Add(Guid, clone);
|
---|
120 | clone.upperBound = UpperBound;
|
---|
121 | clone.upperBoundIncluded = UpperBoundIncluded;
|
---|
122 | clone.upperBoundEnabled = UpperBoundEnabled;
|
---|
123 | clone.lowerBound = LowerBound;
|
---|
124 | clone.lowerBoundIncluded = LowerBoundIncluded;
|
---|
125 | clone.lowerBoundEnabled = LowerBoundEnabled;
|
---|
126 | return clone;
|
---|
127 | }
|
---|
128 |
|
---|
129 | public override void Accept(IConstraintVisitor visitor) {
|
---|
130 | visitor.Visit(this);
|
---|
131 | }
|
---|
132 |
|
---|
133 | #region persistence
|
---|
134 | //public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
135 | // XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
136 | // XmlAttribute lb = document.CreateAttribute("LowerBound");
|
---|
137 | // lb.Value = LowerBound+"";
|
---|
138 | // XmlAttribute lbi = document.CreateAttribute("LowerBoundIncluded");
|
---|
139 | // lbi.Value = lowerBoundIncluded+"";
|
---|
140 | // XmlAttribute lbe = document.CreateAttribute("LowerBoundEnabled");
|
---|
141 | // lbe.Value = lowerBoundEnabled + "";
|
---|
142 | // XmlAttribute ub = document.CreateAttribute("UpperBound");
|
---|
143 | // ub.Value = upperBound + "";
|
---|
144 | // XmlAttribute ubi = document.CreateAttribute("UpperBoundIncluded");
|
---|
145 | // ubi.Value = upperBoundIncluded + "";
|
---|
146 | // XmlAttribute ube = document.CreateAttribute("UpperBoundEnabled");
|
---|
147 | // ube.Value = upperBoundEnabled + "";
|
---|
148 | // node.Attributes.Append(lb);
|
---|
149 | // if (!lowerBoundIncluded) node.Attributes.Append(lbi);
|
---|
150 | // if (!lowerBoundEnabled) node.Attributes.Append(lbe);
|
---|
151 | // node.Attributes.Append(ub);
|
---|
152 | // if (!upperBoundIncluded) node.Attributes.Append(ubi);
|
---|
153 | // if (!upperBoundEnabled) node.Attributes.Append(ube);
|
---|
154 | // return node;
|
---|
155 | //}
|
---|
156 | public override void Persist(string name, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
157 | base.Persist(name, writer, persistedObjects);
|
---|
158 | writer.WriteAttributeString("LowerBound", LowerBound.ToString());
|
---|
159 | if(!lowerBoundIncluded) writer.WriteAttributeString("LowerBoundIncluded", "false");
|
---|
160 | if(!lowerBoundEnabled) writer.WriteAttributeString("LowerBoundEnabled", "false");
|
---|
161 | writer.WriteAttributeString("UpperBound", UpperBound.ToString());
|
---|
162 | if(!upperBoundIncluded) writer.WriteAttributeString("UpperBoundIncluded", "false");
|
---|
163 | if(!upperBoundEnabled) writer.WriteAttributeString("UpperBoundEnabled", "false");
|
---|
164 | }
|
---|
165 |
|
---|
166 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
167 | base.Populate(node, restoredObjects);
|
---|
168 | lowerBound = double.Parse(node.Attributes["LowerBound"].Value);
|
---|
169 | if (node.Attributes["LowerBoundIncluded"] != null) {
|
---|
170 | lowerBoundIncluded = bool.Parse(node.Attributes["LowerBoundIncluded"].Value);
|
---|
171 | } else {
|
---|
172 | lowerBoundIncluded = true;
|
---|
173 | }
|
---|
174 | if (node.Attributes["LowerBoundEnabled"] != null) {
|
---|
175 | lowerBoundEnabled = bool.Parse(node.Attributes["LowerBoundEnabled"].Value);
|
---|
176 | } else {
|
---|
177 | lowerBoundEnabled = true;
|
---|
178 | }
|
---|
179 |
|
---|
180 | upperBound = double.Parse(node.Attributes["UpperBound"].Value);
|
---|
181 | if (node.Attributes["UpperBoundIncluded"] != null) {
|
---|
182 | upperBoundIncluded = bool.Parse(node.Attributes["UpperBoundIncluded"].Value);
|
---|
183 | } else {
|
---|
184 | upperBoundIncluded = true;
|
---|
185 | }
|
---|
186 | if (node.Attributes["UpperBoundEnabled"] != null) {
|
---|
187 | upperBoundEnabled = bool.Parse(node.Attributes["UpperBoundEnabled"].Value);
|
---|
188 | } else {
|
---|
189 | upperBoundEnabled = true;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | #endregion
|
---|
193 | }
|
---|
194 | }
|
---|