Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 574 was 344, checked in by gkronber, 16 years ago

merged changes r338 r339 r340 r341 r342 r343 from the ticket-specific branch into the main trunk

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