Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.SimOpt/3.2/DoubleParameterBoundConstraint.cs @ 2553

Last change on this file since 2553 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

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