Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Constraints/3.3/NumberOfSubOperatorsConstraint.cs @ 6049

Last change on this file since 6049 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 4.1 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;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Constraints {
32  /// <summary>
33  /// Constraint where the number of sub-operators must be within a specific range.
34  /// </summary>
35  public class NumberOfSubOperatorsConstraint : ConstraintBase {
36
37    [Storable]
38    private IntData minOperators;
39
40    [Storable]
41    private IntData maxOperators;
42
43    /// <summary>
44    /// Gets the maximum number of sub-operators.
45    /// </summary>
46    public IntData MaxOperators {
47      get { return maxOperators; }
48    }
49
50    /// <summary>
51    /// Gets the minimum number of sub-operators.
52    /// </summary>
53    public IntData MinOperators {
54      get { return minOperators; }
55    }
56
57    /// <inheritdoc select="summary"/>
58    public override string Description {
59      get { return "Number of sub-operators has to be between " + MinOperators.ToString() + " and " + MaxOperators.ToString() + "."; }
60    }
61
62    /// <summary>
63    /// Initializes a new instance of <see cref="NumberOfSubOperatorsConstraint"/>.
64    /// </summary>
65    public NumberOfSubOperatorsConstraint()
66      : this(0, 0) {
67    }
68
69    /// <summary>
70    /// Initializes a new instance of <see cref="NumberOfSubOperatorsConstraint"/> with the minimum and
71    /// the maximum number of sub-operators.
72    /// </summary>
73    /// <param name="min">The minimum number of sub-operators.</param>
74    /// <param name="max">The maximum number of sub-operators.</param>
75    public NumberOfSubOperatorsConstraint(int min, int max)
76      : base() {
77      minOperators = new IntData(min);
78      maxOperators = new IntData(max);
79    }
80
81    /// <summary>
82    /// Checks whether the given element fulfills the current constraint.
83    /// </summary>
84    /// <param name="data">The item to check.</param>
85    /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
86    public override bool Check(IItem data) {
87      IOperator op = data as IOperator;
88      if (data == null) return false;
89
90      return (op.SubOperators.Count >= minOperators.Data && op.SubOperators.Count <= maxOperators.Data);
91    }
92
93    /// <summary>
94    /// Clones the current instance (deep clone).
95    /// </summary>
96    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
97    /// <returns>The cloned object as <see cref="AndConstraint"/>.</returns>
98    public override object Clone(IDictionary<Guid, object> clonedObjects) {
99      NumberOfSubOperatorsConstraint clone = new NumberOfSubOperatorsConstraint();
100      clonedObjects.Add(Guid, clone);
101      clone.maxOperators.Data = maxOperators.Data;
102      clone.minOperators.Data = minOperators.Data;
103      return clone;
104    }
105
106    /// <summary>
107    /// Creates a new instance of <see cref="NumberOfSubOperatorsConstraintView"/> to represent the current
108    /// instance visually.
109    /// </summary>
110    /// <returns>The created view as <see cref="NumberOfSubOperatorsConstraintView"/>.</returns>
111    public override IView CreateView() {
112      return new NumberOfSubOperatorsConstraintView(this);
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.