Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/SubOperatorsConstraintAnalyser.cs @ 1181

Last change on this file since 1181 was 1176, checked in by vdorfer, 16 years ago

Created API documentation for HeuristicLab.BitVector and HeuristicLab.Constraints namespace and changed a comment in HeuristicLab.IntVector namespace(#331)

File size: 7.7 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 HeuristicLab.Constraints;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.Constraints {
30  /// <summary>
31  /// Analyzes the sub-operators for specific constraints.
32  /// </summary>
33  public class SubOperatorsConstraintAnalyser {
34    private ICollection<IOperator> allPossibleOperators;
35
36    /// <summary>
37    /// Gets or sets all possible operators.
38    /// </summary>
39    public ICollection<IOperator> AllPossibleOperators {
40      get { return allPossibleOperators; }
41      set { allPossibleOperators = value; }
42    }
43   
44    /// <summary>
45    /// Gets all operators that fulfill the expression of the constraints of the given operator.
46    /// </summary>
47    /// <param name="op">The operator whose constraints to check.</param>
48    /// <param name="childIndex">The index of the child.</param>
49    /// <returns>All allowed operators.</returns>
50    public IList<IOperator> GetAllowedOperators(IOperator op, int childIndex) {
51      AndConstraint andConstraint = new AndConstraint();
52      foreach (IConstraint constraint in op.Constraints) {
53        andConstraint.Clauses.Add(constraint);
54      }
55
56      return GetAllowedOperators(andConstraint, childIndex);
57    }
58
59    private IList<IOperator> GetAllowedOperators(IConstraint constraint, int childIndex) {
60      // manual dispatch on dynamic type
61      if (constraint is AndConstraint)
62        return GetAllowedOperators((AndConstraint)constraint, childIndex);
63      else if (constraint is OrConstraint)
64        return GetAllowedOperators((OrConstraint)constraint, childIndex);
65      else if (constraint is NotConstraint)
66        return GetAllowedOperators((NotConstraint)constraint, childIndex);
67      else if (constraint is AllSubOperatorsTypeConstraint)
68        return GetAllowedOperators((AllSubOperatorsTypeConstraint)constraint, childIndex);
69      else if (constraint is SubOperatorTypeConstraint)
70        return GetAllowedOperators((SubOperatorTypeConstraint)constraint, childIndex);
71      else return new List<IOperator>(allPossibleOperators); // ignore all other constraints
72    }
73
74    #region static set management methods
75    // better to use HashSets from .NET 3.5
76    // however we would need to switch the whole Constraints project to .NET 3.5 for that
77    private static IList<IOperator> Intersect(ICollection<IOperator> a, ICollection<IOperator> b) {
78      if (a.Count > b.Count) {
79        return Intersect(b, a);
80      }
81
82      List<IOperator> intersection = new List<IOperator>(a.Count);
83
84      foreach (IOperator element in a) {
85        if (InSet(element, b)) {
86          intersection.Add(element);
87        }
88      }
89      return intersection;
90    }
91
92    private static IList<IOperator> Union(ICollection<IOperator> a, ICollection<IOperator> b) {
93      List<IOperator> union = new List<IOperator>(a);
94      foreach (IOperator candidateElement in b) {
95        if (!InSet(candidateElement, union)) {
96          union.Add(candidateElement);
97        }
98      }
99
100      return union;
101    }
102
103    private static IList<IOperator> Substract(ICollection<IOperator> minuend, ICollection<IOperator> subtrahend) {
104      List<IOperator> difference = new List<IOperator>();
105      foreach (IOperator element in minuend) {
106        if (!InSet(element, subtrahend)) {
107          difference.Add(element);
108        }
109      }
110
111      return difference;
112    }
113
114    private static bool InSet(IOperator op, ICollection<IOperator> set) {
115      foreach (IOperator element in set) {
116        if (element == op)
117          return true;
118      }
119      return false;
120    }
121    #endregion
122
123    /// <summary>
124    /// Gets all allowed operators that fulfill the expression of the given <c>AndConstraint</c>.
125    /// </summary>
126    /// <param name="constraint">The constraint that must be fulfilled.</param>
127    /// <param name="childIndex">The index of the child.</param>
128    /// <returns>All allowed operators.</returns>
129    public IList<IOperator> GetAllowedOperators(AndConstraint constraint, int childIndex) {
130      IList<IOperator> allowedOperators = new List<IOperator>(allPossibleOperators);
131      // keep only the intersection of all subconstraints
132      foreach (ConstraintBase clause in constraint.Clauses) {
133        allowedOperators = Intersect(allowedOperators, GetAllowedOperators(clause, childIndex));
134      }
135      return allowedOperators;
136    }
137
138    /// <summary>
139    /// Gets all allowed operators that fulfill the expression of the given <c>OrConstraint</c>.
140    /// </summary>
141    /// <param name="constraint">The constraint that must be fulfilled.</param>
142    /// <param name="childIndex">The index of the child.</param>
143    /// <returns>All allowed operators.</returns>
144    public IList<IOperator> GetAllowedOperators(OrConstraint constraint, int childIndex) {
145      IList<IOperator> allowedOperators = new List<IOperator>();
146      foreach (ConstraintBase clause in constraint.Clauses) {
147        allowedOperators = Union(allowedOperators, GetAllowedOperators(clause, childIndex));
148      }
149      return allowedOperators;
150    }
151
152    /// <summary>
153    /// Gets all allowed operators that fulfill the expression of the given <c>NotConstraint</c>.
154    /// </summary>
155    /// <param name="constraint">The constraint that must be fulfilled.</param>
156    /// <param name="childIndex">The index of the child.</param>
157    /// <returns>All allowed operators.</returns>
158    public IList<IOperator> GetAllowedOperators(NotConstraint constraint, int childIndex) {
159      return Substract(allPossibleOperators, GetAllowedOperators(constraint.SubConstraint, childIndex));
160    }
161
162    /// <summary>
163    /// Gets all allowed operators that fulfill the expression of the given <c>AllSubOperatorsTypeConstraint</c>.
164    /// </summary>
165    /// <param name="constraint">The constraint that must be fulfilled.</param>
166    /// <param name="childIndex">The index of the child.</param>
167    /// <returns>All allowed operators.</returns>
168    public IList<IOperator> GetAllowedOperators(AllSubOperatorsTypeConstraint constraint, int childIndex) {
169      return Intersect(allPossibleOperators, constraint.AllowedSubOperators);
170    }
171
172    /// <summary>
173    /// Gets all allowed operators that fulfill the expression of the given <c>SubOperatorTypeConstraint</c>.
174    /// </summary>
175    /// <param name="constraint">The constraint that must be fulfilled.</param>
176    /// <param name="childIndex">The index of the child.</param>
177    /// <returns>All allowed operators.</returns>
178    public IList<IOperator> GetAllowedOperators(SubOperatorTypeConstraint constraint, int childIndex) {
179      if (childIndex != constraint.SubOperatorIndex.Data) {
180        return new List<IOperator>(allPossibleOperators);
181      } else {
182        return Intersect(allPossibleOperators, constraint.AllowedSubOperators);
183      }
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.