Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Constraints/SubOperatorsConstraintAnalyser.cs @ 10499

Last change on this file since 10499 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

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 HeuristicLab.Constraints;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.Constraints {
30  public class SubOperatorsConstraintAnalyser {
31    private ICollection<IOperator> allPossibleOperators;
32
33    public ICollection<IOperator> AllPossibleOperators {
34      get { return allPossibleOperators; }
35      set { allPossibleOperators = value; }
36    }
37
38    public IList<IOperator> GetAllowedOperators(IOperator op, int childIndex) {
39
40      AndConstraint andConstraint = new AndConstraint();
41      foreach(IConstraint constraint in op.Constraints) {
42        andConstraint.Clauses.Add(constraint);
43      }
44
45      GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(allPossibleOperators, childIndex);
46      andConstraint.Accept(visitor);
47
48      return visitor.AllowedOperators;
49    }
50
51    #region static set management methods
52    // better to use HashSets from .NET 3.5
53    // however we would need to switch the whole Constraints project to .NET 3.5 for that
54    private static IList<IOperator> Intersect(ICollection<IOperator> a, ICollection<IOperator> b) {
55      if(a.Count > b.Count) {
56        return Intersect(b, a);
57      }
58
59      List<IOperator> intersection = new List<IOperator>(a.Count);
60
61      foreach(IOperator element in a) {
62        if(InSet(element, b)) {
63          intersection.Add(element);
64        }
65      }
66      return intersection;
67    }
68
69    private static IList<IOperator> Union(ICollection<IOperator> a, ICollection<IOperator> b) {
70      List<IOperator> union = new List<IOperator>(a);
71      foreach(IOperator candidateElement in b) {
72        if(!InSet(candidateElement, union)) {
73          union.Add(candidateElement);
74        }
75      }
76
77      return union;
78    }
79
80    private static IList<IOperator> Substract(ICollection<IOperator> minuend, ICollection<IOperator> subtrahend) {
81      List<IOperator> difference = new List<IOperator>();
82      foreach(IOperator element in minuend) {
83        if(!InSet(element, subtrahend)) {
84          difference.Add(element);
85        }
86      }
87
88      return difference;
89    }
90
91    private static bool InSet(IOperator op, ICollection<IOperator> set) {
92      foreach(IOperator element in set) {
93        if(((StringData)element.GetVariable("TypeId").Value).Data ==
94          ((StringData)op.GetVariable("TypeId").Value).Data) {
95          return true;
96        }
97      }
98
99      return false;
100    }
101    #endregion
102
103    #region visitor
104    /// <summary>
105    /// The visitor builds a set of allowed operators based on a tree of constraints.
106    /// </summary>
107    private class GetAllowedOperatorsVisitor : ConstraintVisitorBase {
108      private IList<IOperator> allowedOperators;
109
110      public IList<IOperator> AllowedOperators {
111        get { return allowedOperators; }
112      }
113      private ICollection<IOperator> possibleOperators;
114      private int childIndex;
115
116      public GetAllowedOperatorsVisitor(ICollection<IOperator> possibleOperators, int childIndex) {
117        // default is that all possible operators are allowed
118        allowedOperators = new List<IOperator>(possibleOperators);
119        this.possibleOperators = possibleOperators;
120        this.childIndex = childIndex;
121      }
122
123      public override void Visit(AndConstraint constraint) {
124        base.Visit(constraint);
125
126        // keep only the intersection of all subconstraints
127        foreach(ConstraintBase clause in constraint.Clauses) {
128          GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(possibleOperators, childIndex);
129          clause.Accept(visitor);
130          allowedOperators = Intersect(allowedOperators, visitor.allowedOperators);
131        }
132      }
133     
134      public override void Visit(OrConstraint constraint) {
135        base.Visit(constraint);
136
137        // allowed operators is the union of all allowed operators as defined by the subconstraints
138        allowedOperators.Clear();
139
140        foreach(ConstraintBase clause in constraint.Clauses) {
141          GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(possibleOperators, childIndex);
142          clause.Accept(visitor);
143          allowedOperators = Union(allowedOperators, visitor.allowedOperators);
144        }
145      }
146
147      public override void Visit(NotConstraint constraint) {
148        base.Visit(constraint);
149        GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(possibleOperators, childIndex);
150        constraint.SubConstraint.Accept(visitor);
151
152        allowedOperators = Substract(possibleOperators, visitor.allowedOperators);
153      }
154
155      public override void Visit(AllSubOperatorsTypeConstraint constraint) {
156        base.Visit(constraint);
157
158        allowedOperators = Intersect(possibleOperators, constraint.AllowedSubOperators);
159      }
160
161      public override void Visit(SubOperatorTypeConstraint constraint) {
162        if(childIndex != constraint.SubOperatorIndex.Data) {
163          allowedOperators = new List<IOperator>(possibleOperators);
164        } else {
165          allowedOperators = Intersect(possibleOperators, constraint.AllowedSubOperators);
166        }
167      }
168    }
169    #endregion
170  }
171}
Note: See TracBrowser for help on using the repository browser.