Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Constraints/IntBoundedConstraint.cs @ 2515

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

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

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