Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Constraints/VariableComparisonConstraint.cs @ 5234

Last change on this file since 5234 was 175, checked in by abeham, 17 years ago

Fixed #129 (right side of VariableComparisonConstraint)

File size: 4.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 VariableComparisonConstraint : ConstraintBase {
31    private StringData leftVarName;
32    public StringData LeftVarName {
33      get { return leftVarName; }
34      set { leftVarName = value; }
35    }
36
37    private StringData rightVarName;
38    public StringData RightVarName {
39      get { return rightVarName; }
40      set { rightVarName = value; }
41    }
42
43    private IntData comparer;
44    public IntData Comparer {
45      get { return comparer; }
46      set { comparer = value; }
47    }
48
49    public override string Description {
50      get {
51        return @"Compares variables in a ConstrainedItemList";
52      }
53    }
54
55    public VariableComparisonConstraint() {
56      leftVarName = new StringData();
57      rightVarName = new StringData();
58      comparer = new IntData(-1);
59    }
60
61    public override bool Check(IItem data) {
62      ConstrainedItemList list = (data as ConstrainedItemList);
63      if (list == null) throw new InvalidOperationException("ERROR in VariableComparisonConstraint: Can only be applied on ConstrainedItemLists");
64      IComparable left = null;
65      IItem right = null;
66      foreach (IItem item in list) {
67        Variable tmp = (item as Variable);
68        if (tmp != null && tmp.Name.Equals(leftVarName.Data)) {
69          left = (tmp.Value as IComparable);
70          if (left == null) throw new InvalidCastException("ERROR in VariableComparisonConstraint: Value of the variable on the left side needs to be of type IComparable");
71        } else if (tmp != null && tmp.Name.Equals(rightVarName.Data)) {
72          right = tmp.Value;
73        }
74      }
75      if (left != null && right != null) {
76        switch (comparer.Data) {
77          case 0:
78            return left.CompareTo(right) < 0;
79          case 1:
80            return left.CompareTo(right) <= 0;
81          case 2:
82            return left.CompareTo(right) == 0;
83          case 3:
84            return left.CompareTo(right) >= 0;
85          case 4:
86            return left.CompareTo(right) > 0;
87          default:
88            throw new InvalidOperationException("ERROR in VariableComparisonConstraint: Comparer undefined");
89        }
90      }
91      return true;
92    }
93
94    public override IView CreateView() {
95      return new VariableComparisonConstraintView(this);
96    }
97
98    #region clone & persistence
99    public override object Clone(IDictionary<Guid, object> clonedObjects) {
100      VariableComparisonConstraint clone = new VariableComparisonConstraint();
101      clonedObjects.Add(Guid, clone);
102      clone.LeftVarName = (StringData)Auxiliary.Clone(LeftVarName, clonedObjects);
103      clone.RightVarName = (StringData)Auxiliary.Clone(RightVarName, clonedObjects);
104      clone.Comparer = (IntData)Auxiliary.Clone(Comparer, clonedObjects);
105      return clone;
106    }
107
108    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
109      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
110      XmlNode leftNode = PersistenceManager.Persist("LeftVarName", LeftVarName, document, persistedObjects);
111      node.AppendChild(leftNode);
112      XmlNode rightNode = PersistenceManager.Persist("RightVarName", RightVarName, document, persistedObjects);
113      node.AppendChild(rightNode);
114      XmlNode comparerNode = PersistenceManager.Persist("Comparer", Comparer, document, persistedObjects);
115      node.AppendChild(comparerNode);
116      return node;
117    }
118
119    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
120      base.Populate(node, restoredObjects);
121      leftVarName = (StringData)PersistenceManager.Restore(node.SelectSingleNode("LeftVarName"), restoredObjects);
122      rightVarName = (StringData)PersistenceManager.Restore(node.SelectSingleNode("RightVarName"), restoredObjects);
123      comparer = (IntData)PersistenceManager.Restore(node.SelectSingleNode("Comparer"), restoredObjects);
124    }
125    #endregion
126  }
127}
Note: See TracBrowser for help on using the repository browser.