Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 885 was 764, checked in by gkronber, 16 years ago

removed visitor classes and methods in HeuristicLab.Constraints and replaced visitor with manual dispatch. #343 (Rethink about usefulness of visitors for ObjectData and Constraints)

File size: 6.6 KB
RevLine 
[2]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;
[138]28using System.Globalization;
[2]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    #region persistence
131    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
132      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
133      XmlAttribute lb = document.CreateAttribute("LowerBound");
[344]134      lb.Value = LowerBound.ToString("r", CultureInfo.InvariantCulture);
[2]135      XmlAttribute lbi = document.CreateAttribute("LowerBoundIncluded");
[138]136      lbi.Value = lowerBoundIncluded.ToString();
[2]137      XmlAttribute lbe = document.CreateAttribute("LowerBoundEnabled");
[138]138      lbe.Value = lowerBoundEnabled.ToString();
[2]139      XmlAttribute ub = document.CreateAttribute("UpperBound");
[344]140      ub.Value = upperBound.ToString("r", CultureInfo.InvariantCulture);
[2]141      XmlAttribute ubi = document.CreateAttribute("UpperBoundIncluded");
[138]142      ubi.Value = upperBoundIncluded.ToString();
[2]143      XmlAttribute ube = document.CreateAttribute("UpperBoundEnabled");
[138]144      ube.Value = upperBoundEnabled.ToString();
[2]145      node.Attributes.Append(lb);
146      if (!lowerBoundIncluded) node.Attributes.Append(lbi);
147      if (!lowerBoundEnabled) node.Attributes.Append(lbe);
148      node.Attributes.Append(ub);
149      if (!upperBoundIncluded) node.Attributes.Append(ubi);
150      if (!upperBoundEnabled) node.Attributes.Append(ube);
151      return node;
152    }
153
154    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
155      base.Populate(node, restoredObjects);
[138]156      lowerBound = double.Parse(node.Attributes["LowerBound"].Value, CultureInfo.InvariantCulture);
[2]157      if (node.Attributes["LowerBoundIncluded"] != null) {
158        lowerBoundIncluded = bool.Parse(node.Attributes["LowerBoundIncluded"].Value);
159      } else {
160        lowerBoundIncluded = true;
161      }
162      if (node.Attributes["LowerBoundEnabled"] != null) {
163        lowerBoundEnabled = bool.Parse(node.Attributes["LowerBoundEnabled"].Value);
164      } else {
165        lowerBoundEnabled = true;
166      }
167
[138]168      upperBound = double.Parse(node.Attributes["UpperBound"].Value, CultureInfo.InvariantCulture);
[2]169      if (node.Attributes["UpperBoundIncluded"] != null) {
170        upperBoundIncluded = bool.Parse(node.Attributes["UpperBoundIncluded"].Value);
171      } else {
172        upperBoundIncluded = true;
173      }
174      if (node.Attributes["UpperBoundEnabled"] != null) {
175        upperBoundEnabled = bool.Parse(node.Attributes["UpperBoundEnabled"].Value);
176      } else {
177        upperBoundEnabled = true;
178      }
179    }
180    #endregion
181  }
182}
Note: See TracBrowser for help on using the repository browser.