[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using System.Diagnostics;
|
---|
| 28 | using System.Xml;
|
---|
[1823] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 30 |
|
---|
| 31 | namespace HeuristicLab.Constraints {
|
---|
[1176] | 32 | /// <summary>
|
---|
| 33 | /// Constraint where the sub-operator at a specific index has to be an element of a pre-defined group.
|
---|
| 34 | /// </summary>
|
---|
[2] | 35 | public class SubOperatorTypeConstraint : ConstraintBase {
|
---|
[1672] | 36 |
|
---|
| 37 | [Storable]
|
---|
[2] | 38 | private IntData subOperatorIndex;
|
---|
[1176] | 39 | /// <summary>
|
---|
| 40 | /// Gets the index of the sub-operator.
|
---|
| 41 | /// </summary>
|
---|
[2] | 42 | public IntData SubOperatorIndex {
|
---|
| 43 | get { return subOperatorIndex; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[1672] | 46 | [Storable]
|
---|
[2] | 47 | private List<IOperator> subOperators;
|
---|
[1176] | 48 | /// <summary>
|
---|
| 49 | /// Gets all allowed sub-operators.
|
---|
| 50 | /// </summary>
|
---|
[2] | 51 | public IList<IOperator> AllowedSubOperators {
|
---|
| 52 | get {
|
---|
| 53 | return subOperators.AsReadOnly();
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[1176] | 57 | ///<inheritdoc select="summary"/>
|
---|
[2] | 58 | public override string Description {
|
---|
| 59 | get { return "The sub-operator at a specific index has to be an element of a pre-defined group."; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[1176] | 62 | /// <summary>
|
---|
| 63 | /// Initializes a new instance of <see cref="SubOperatorTypeConstraint"/>.
|
---|
| 64 | /// </summary>
|
---|
[2] | 65 | public SubOperatorTypeConstraint()
|
---|
| 66 | : base() {
|
---|
| 67 | subOperatorIndex = new IntData(0);
|
---|
| 68 | subOperators = new List<IOperator>();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[1176] | 71 | /// <summary>
|
---|
| 72 | /// Initializes a new instance of <see cref="SubOperatorTypeConstraint"/> with the given
|
---|
| 73 | /// <paramref name="index"/>.
|
---|
| 74 | /// </summary>
|
---|
| 75 | /// <param name="index">The index of the sub-operator.</param>
|
---|
[1672] | 76 | public SubOperatorTypeConstraint(int index)
|
---|
| 77 | : base() {
|
---|
[435] | 78 | subOperatorIndex = new IntData(index);
|
---|
| 79 | subOperators = new List<IOperator>();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[1176] | 82 | /// <summary>
|
---|
| 83 | /// Adds the given operator to the list of sub-operators.
|
---|
| 84 | /// </summary>
|
---|
| 85 | /// <remarks>Calls <see cref="ItemBase.FireChanged"/> of base class <see cref="ConstraintBase"/>.</remarks>
|
---|
| 86 | /// <param name="op">The operator to add.</param>
|
---|
[2] | 87 | public void AddOperator(IOperator op) {
|
---|
[1672] | 88 | if (!subOperators.Contains(op)) {
|
---|
[435] | 89 | subOperators.Add(op);
|
---|
| 90 | FireChanged();
|
---|
[2] | 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[1176] | 94 | /// <summary>
|
---|
| 95 | /// Removes the given operator from the list of sub-operators.
|
---|
| 96 | /// </summary>
|
---|
| 97 | /// <remarks>Calls <see cref="ItemBase.FireChanged"/> of base class <see cref="ConstraintBase"/>.</remarks>
|
---|
| 98 | /// <param name="op">The operator to remove.</param>
|
---|
[2] | 99 | public void RemoveOperator(IOperator op) {
|
---|
[1672] | 100 | if (subOperators.Contains(op)) {
|
---|
[435] | 101 | subOperators.Remove(op);
|
---|
| 102 | FireChanged();
|
---|
[2] | 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[1176] | 106 | /// <summary>
|
---|
| 107 | /// Empties the list of sub-operators.
|
---|
| 108 | /// </summary>
|
---|
[467] | 109 | public void Clear() {
|
---|
| 110 | subOperators.Clear();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[1176] | 113 | /// <summary>
|
---|
| 114 | /// Checks whether the given element fulfills the current constraint.
|
---|
| 115 | /// </summary>
|
---|
| 116 | /// <param name="data">The item to check.</param>
|
---|
| 117 | /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
|
---|
[2] | 118 | public override bool Check(IItem data) {
|
---|
| 119 | IOperator op = data as IOperator;
|
---|
[1672] | 120 | if (data == null) return false;
|
---|
[2] | 121 |
|
---|
[1672] | 122 | if (op.SubOperators.Count <= subOperatorIndex.Data) {
|
---|
[2] | 123 | return false;
|
---|
| 124 | }
|
---|
[435] | 125 | return subOperators.Contains(op.SubOperators[subOperatorIndex.Data]);
|
---|
[2] | 126 | }
|
---|
| 127 |
|
---|
[1176] | 128 | /// <summary>
|
---|
| 129 | /// Clones the current instance (deep clone).
|
---|
| 130 | /// </summary>
|
---|
| 131 | /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
|
---|
| 132 | /// <see cref="Auxiliary"/>.</remarks>
|
---|
| 133 | /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
|
---|
| 134 | /// <returns>The cloned object as <see cref="SubOperatorTypeConstraint"/>.</returns>
|
---|
[2] | 135 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 136 | SubOperatorTypeConstraint clone = new SubOperatorTypeConstraint();
|
---|
| 137 | clonedObjects.Add(Guid, clone);
|
---|
| 138 | clone.subOperatorIndex.Data = subOperatorIndex.Data;
|
---|
[1672] | 139 | foreach (IOperator op in subOperators) {
|
---|
[2] | 140 | clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
|
---|
| 141 | }
|
---|
| 142 | return clone;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[1176] | 145 | /// <summary>
|
---|
| 146 | /// Creates a new instance of <see cref="SubOperatorsTypeConstraintView"/> to represent the current
|
---|
| 147 | /// instance visually.
|
---|
| 148 | /// </summary>
|
---|
| 149 | /// <returns>The created view as <see cref="SubOperatorsTypeConstraintView"/>.</returns>
|
---|
[2] | 150 | public override IView CreateView() {
|
---|
| 151 | return new SubOperatorsTypeConstraintView(this);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|