Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/DoubleBoundedConstraint.cs @ 16

Last change on this file since 16 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 6.5 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;
28
29namespace 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
157    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
158      base.Populate(node, restoredObjects);
159      lowerBound = double.Parse(node.Attributes["LowerBound"].Value);
160      if (node.Attributes["LowerBoundIncluded"] != null) {
161        lowerBoundIncluded = bool.Parse(node.Attributes["LowerBoundIncluded"].Value);
162      } else {
163        lowerBoundIncluded = true;
164      }
165      if (node.Attributes["LowerBoundEnabled"] != null) {
166        lowerBoundEnabled = bool.Parse(node.Attributes["LowerBoundEnabled"].Value);
167      } else {
168        lowerBoundEnabled = true;
169      }
170
171      upperBound = double.Parse(node.Attributes["UpperBound"].Value);
172      if (node.Attributes["UpperBoundIncluded"] != null) {
173        upperBoundIncluded = bool.Parse(node.Attributes["UpperBoundIncluded"].Value);
174      } else {
175        upperBoundIncluded = true;
176      }
177      if (node.Attributes["UpperBoundEnabled"] != null) {
178        upperBoundEnabled = bool.Parse(node.Attributes["UpperBoundEnabled"].Value);
179      } else {
180        upperBoundEnabled = true;
181      }
182    }
183    #endregion
184  }
185}
Note: See TracBrowser for help on using the repository browser.