Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/IntBoundedConstraint.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.1 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 IntBoundedConstraint : ConstraintBase {
31    private int lowerBound;
32    public int 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 int upperBound;
56    public int 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 integer is limited one or two sided by a lower and/or upper boundary"; }
82    }
83
84    public IntBoundedConstraint()
85      : this(int.MinValue, int.MaxValue) {
86    }
87
88    public IntBoundedConstraint(int low, int high) : base() {
89      lowerBound = low;
90      lowerBoundIncluded = false;
91      lowerBoundEnabled = true;
92      upperBound = high;
93      upperBoundIncluded = false;
94      upperBoundEnabled = true;
95    }
96
97    public override bool Check(IItem data) {
98      ConstrainedIntData d = (data as ConstrainedIntData);
99      if (d == null) return false;
100      if (LowerBoundEnabled && ((LowerBoundIncluded && d.CompareTo(LowerBound) < 0)
101        || (!LowerBoundIncluded && d.CompareTo(LowerBound) <= 0))) return false;
102      if (UpperBoundEnabled && ((UpperBoundIncluded && d.CompareTo(UpperBound) > 0)
103        || (!UpperBoundIncluded && d.CompareTo(UpperBound) >= 0))) return false;
104      return true;
105    }
106
107    public override IView CreateView() {
108      return new IntBoundedConstraintView(this);
109    }
110
111    public override object Clone(IDictionary<Guid, object> clonedObjects) {
112      IntBoundedConstraint clone = new IntBoundedConstraint();
113      clonedObjects.Add(Guid, clone);
114      clone.upperBound = UpperBound;
115      clone.upperBoundIncluded = UpperBoundIncluded;
116      clone.upperBoundEnabled = UpperBoundEnabled;
117      clone.lowerBound = LowerBound;
118      clone.lowerBoundIncluded = LowerBoundIncluded;
119      clone.lowerBoundEnabled = LowerBoundEnabled;
120      return clone;
121    }
122
123    #region persistence
124    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
125      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
126      XmlAttribute lb = document.CreateAttribute("LowerBound");
127      lb.Value = LowerBound + "";
128      XmlAttribute lbi = document.CreateAttribute("LowerBoundIncluded");
129      lbi.Value = lowerBoundIncluded + "";
130      XmlAttribute lbe = document.CreateAttribute("LowerBoundEnabled");
131      lbe.Value = lowerBoundEnabled + "";
132      XmlAttribute ub = document.CreateAttribute("UpperBound");
133      ub.Value = upperBound + "";
134      XmlAttribute ubi = document.CreateAttribute("UpperBoundIncluded");
135      ubi.Value = upperBoundIncluded + "";
136      XmlAttribute ube = document.CreateAttribute("UpperBoundEnabled");
137      ube.Value = upperBoundEnabled + "";
138      node.Attributes.Append(lb);
139      if (!lowerBoundIncluded) node.Attributes.Append(lbi);
140      if (!lowerBoundEnabled) node.Attributes.Append(lbe);
141      node.Attributes.Append(ub);
142      if (!upperBoundIncluded) node.Attributes.Append(ubi);
143      if (!upperBoundEnabled) node.Attributes.Append(ube);
144      return node;
145    }
146
147    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
148      base.Populate(node, restoredObjects);
149      lowerBound = int.Parse(node.Attributes["LowerBound"].Value);
150      if (node.Attributes["LowerBoundIncluded"] != null) {
151        lowerBoundIncluded = bool.Parse(node.Attributes["LowerBoundIncluded"].Value);
152      } else {
153        lowerBoundIncluded = true;
154      }
155      if (node.Attributes["LowerBoundEnabled"] != null) {
156        lowerBoundEnabled = bool.Parse(node.Attributes["LowerBoundEnabled"].Value);
157      } else {
158        lowerBoundEnabled = true;
159      }
160
161      upperBound = int.Parse(node.Attributes["UpperBound"].Value);
162      if (node.Attributes["UpperBoundIncluded"] != null) {
163        upperBoundIncluded = bool.Parse(node.Attributes["UpperBoundIncluded"].Value);
164      } else {
165        upperBoundIncluded = true;
166      }
167      if (node.Attributes["UpperBoundEnabled"] != null) {
168        upperBoundEnabled = bool.Parse(node.Attributes["UpperBoundEnabled"].Value);
169      } else {
170        upperBoundEnabled = true;
171      }
172    }
173    #endregion
174  }
175}
Note: See TracBrowser for help on using the repository browser.