Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlReaderWriterBranch/HeuristicLab.Constraints/IntBoundedConstraint.cs @ 175

Last change on this file since 175 was 125, checked in by gkronber, 17 years ago

created a branch that combines the XmlReader and XmlWriter branches

File size: 7.9 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    public override void Persist(string name, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
152      base.Persist(name, writer, persistedObjects);
153      writer.WriteAttributeString("LowerBound", LowerBound.ToString());
154      if(!lowerBoundIncluded) writer.WriteAttributeString("LowerBoundIncluded", "false");
155      if(!lowerBoundEnabled) writer.WriteAttributeString("LowerBoundEnabled", "false");
156      writer.WriteAttributeString("UpperBound", UpperBound.ToString());
157      if(!upperBoundIncluded) writer.WriteAttributeString("UpperBoundIncluded", "false");
158      if(!upperBoundEnabled) writer.WriteAttributeString("UpperBoundEnabled", "false");
159    }
160
161    //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
162    //  base.Populate(node, restoredObjects);
163    //  lowerBound = int.Parse(node.Attributes["LowerBound"].Value);
164    //  if (node.Attributes["LowerBoundIncluded"] != null) {
165    //    lowerBoundIncluded = bool.Parse(node.Attributes["LowerBoundIncluded"].Value);
166    //  } else {
167    //    lowerBoundIncluded = true;
168    //  }
169    //  if (node.Attributes["LowerBoundEnabled"] != null) {
170    //    lowerBoundEnabled = bool.Parse(node.Attributes["LowerBoundEnabled"].Value);
171    //  } else {
172    //    lowerBoundEnabled = true;
173    //  }
174
175    //  upperBound = int.Parse(node.Attributes["UpperBound"].Value);
176    //  if (node.Attributes["UpperBoundIncluded"] != null) {
177    //    upperBoundIncluded = bool.Parse(node.Attributes["UpperBoundIncluded"].Value);
178    //  } else {
179    //    upperBoundIncluded = true;
180    //  }
181    //  if (node.Attributes["UpperBoundEnabled"] != null) {
182    //    upperBoundEnabled = bool.Parse(node.Attributes["UpperBoundEnabled"].Value);
183    //  } else {
184    //    upperBoundEnabled = true;
185    //  }
186    //}
187    public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
188      lowerBound = int.Parse(reader["LowerBound"]);
189      if(reader["LowerBoundIncluded"] != null) {
190        lowerBoundIncluded = bool.Parse(reader["LowerBoundIncluded"]);
191      } else {
192        lowerBoundIncluded = true;
193      }
194      if(reader["LowerBoundEnabled"] != null) {
195        lowerBoundEnabled = bool.Parse(reader["LowerBoundEnabled"]);
196      } else {
197        lowerBoundEnabled = true;
198      }
199
200      upperBound = int.Parse(reader["UpperBound"]);
201      if(reader["UpperBoundIncluded"] != null) {
202        upperBoundIncluded = bool.Parse(reader["UpperBoundIncluded"]);
203      } else {
204        upperBoundIncluded = true;
205      }
206      if(reader["UpperBoundEnabled"] != null) {
207        upperBoundEnabled = bool.Parse(reader["UpperBoundEnabled"]);
208      } else {
209        upperBoundEnabled = true;
210      }
211      base.Populate(reader, restoredObjects);
212    }
213
214    #endregion
215  }
216}
Note: See TracBrowser for help on using the repository browser.