Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.Constraints/3.2/NumberOfSubOperatorsConstraint.cs @ 4593

Last change on this file since 4593 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 6.0 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 HeuristicLab.Core;
26using System.Diagnostics;
27using HeuristicLab.Data;
28using System.Xml;
29
30namespace HeuristicLab.Constraints {
31  /// <summary>
32  /// Constraint where the number of sub-operators must be within a specific range.
33  /// </summary>
34  public class NumberOfSubOperatorsConstraint : ConstraintBase {
35    private IntData minOperators;
36    private IntData maxOperators;
37
38    /// <summary>
39    /// Gets the maximum number of sub-operators.
40    /// </summary>
41    public IntData MaxOperators {
42      get { return maxOperators; }
43    }
44
45    /// <summary>
46    /// Gets the minimum number of sub-operators.
47    /// </summary>
48    public IntData MinOperators {
49      get { return minOperators; }
50    }
51
52    /// <inheritdoc select="summary"/>
53    public override string Description {
54      get { return "Number of sub-operators has to be between " + MinOperators.ToString() + " and " + MaxOperators.ToString() + "."; }
55    }
56
57    /// <summary>
58    /// Initializes a new instance of <see cref="NumberOfSubOperatorsConstraint"/>.
59    /// </summary>
60    public NumberOfSubOperatorsConstraint()
61      : this(0,0) {
62    }
63
64    /// <summary>
65    /// Initializes a new instance of <see cref="NumberOfSubOperatorsConstraint"/> with the minimum and
66    /// the maximum number of sub-operators.
67    /// </summary>
68    /// <param name="min">The minimum number of sub-operators.</param>
69    /// <param name="max">The maximum number of sub-operators.</param>
70    public NumberOfSubOperatorsConstraint(int min, int max) : base() {
71      minOperators = new IntData(min);
72      maxOperators = new IntData(max);
73    }
74
75    /// <summary>
76    /// Checks whether the given element fulfills the current constraint.
77    /// </summary>
78    /// <param name="data">The item to check.</param>
79    /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
80    public override bool Check(IItem data) {
81      IOperator op = data as IOperator;
82      if (data == null) return false;
83
84      return (op.SubOperators.Count >= minOperators.Data && op.SubOperators.Count <= maxOperators.Data);
85    }
86
87    /// <summary>
88    /// Clones the current instance (deep clone).
89    /// </summary>
90    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
91    /// <returns>The cloned object as <see cref="AndConstraint"/>.</returns>
92    public override object Clone(IDictionary<Guid, object> clonedObjects) {
93      NumberOfSubOperatorsConstraint clone = new NumberOfSubOperatorsConstraint();
94      clonedObjects.Add(Guid, clone);
95      clone.maxOperators.Data = maxOperators.Data;
96      clone.minOperators.Data = minOperators.Data;
97      return clone;
98    }
99
100    /// <summary>
101    /// Creates a new instance of <see cref="NumberOfSubOperatorsConstraintView"/> to represent the current
102    /// instance visually.
103    /// </summary>
104    /// <returns>The created view as <see cref="NumberOfSubOperatorsConstraintView"/>.</returns>
105    public override IView CreateView() {
106      return new NumberOfSubOperatorsConstraintView(this);
107    }
108
109    #region persistence
110    /// <summary>
111    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
112    /// </summary>
113    /// <remarks>The minimum and the maximum number of sub-operators are saved as child nodes with tag
114    /// names <c>min</c> and <c>max</c>.</remarks>
115    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
116    /// <param name="document">The <see cref="XmlDocument"/> where the data is saved.</param>
117    /// <param name="persistedObjects">The dictionary of all already persisted objects.
118    /// (Needed to avoid cycles.)</param>
119    /// <returns>The saved <see cref="XmlNode"/>.</returns>
120    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
121      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
122      XmlNode minNode = PersistenceManager.Persist("min", minOperators, document, persistedObjects);
123      XmlNode maxNode = PersistenceManager.Persist("max", maxOperators, document, persistedObjects);
124      node.AppendChild(minNode);
125      node.AppendChild(maxNode);
126      return node;
127    }
128
129    /// <summary>
130    /// Loads the persisted constraint from the specified <paramref name="node"/>.
131    /// </summary>
132    /// <remarks>The constraint must be saved in a specific way, see <see cref="GetXmlNode"/> for
133    /// more information.</remarks>
134    /// <param name="node">The <see cref="XmlNode"/> where the instance is saved.</param>
135    /// <param name="restoredObjects">The dictionary of all already restored objects.
136    /// (Needed to avoid cycles.)</param>
137    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
138      base.Populate(node, restoredObjects);
139      minOperators = (IntData)PersistenceManager.Restore(node.SelectSingleNode("min"), restoredObjects);
140      maxOperators = (IntData)PersistenceManager.Restore(node.SelectSingleNode("max"), restoredObjects);
141    }
142    #endregion persistence
143
144  }
145}
Note: See TracBrowser for help on using the repository browser.