Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.3/UniformSubScopesProcessor.cs @ 4682

Last change on this file since 4682 was 4477, checked in by gkronber, 14 years ago

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Operators {
30  /// <summary>
31  /// An operator which applies a specified operator on all sub-scopes at the given depth of the current scope.
32  /// </summary>
33  [Item("UniformSubScopesProcessor", "An operator which applies a specified operator on all sub-scopes at the given depth of the current scope.")]
34  [StorableClass]
35  public sealed class UniformSubScopesProcessor : SingleSuccessorOperator {
36    private OperatorParameter OperatorParameter {
37      get { return (OperatorParameter)Parameters["Operator"]; }
38    }
39    public ValueLookupParameter<BoolValue> ParallelParameter {
40      get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
41    }
42    public ValueParameter<IntValue> DepthParameter {
43      get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
44    }
45
46    public IOperator Operator {
47      get { return OperatorParameter.Value; }
48      set { OperatorParameter.Value = value; }
49    }
50    public BoolValue Parallel {
51      get { return ParallelParameter.Value; }
52      set { ParallelParameter.Value = value; }
53    }
54    public IntValue Depth {
55      get { return DepthParameter.Value; }
56      set { DepthParameter.Value = value; }
57    }
58
59    public UniformSubScopesProcessor()
60      : base() {
61      Parameters.Add(new OperatorParameter("Operator", "The operator which should be applied on all sub-scopes of the current scope."));
62      Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(false)));
63      Parameters.Add(new ValueParameter<IntValue>("Depth", "The number of steps to descend in the scope tree before applying operator.", new IntValue(1)));
64    }
65
66    public override IOperation Apply() {
67      OperationCollection next = new OperationCollection(base.Apply());
68      if (Operator != null) {
69        List<IScope> scopes = GetScopesOnLevel(ExecutionContext.Scope, Depth.Value).ToList();
70        OperationCollection inner = new OperationCollection();
71        inner.Parallel = Parallel == null ? false : Parallel.Value;
72        for (int i = 0; i < scopes.Count; i++) {
73          inner.Add(ExecutionContext.CreateOperation(Operator, scopes[i]));
74        }
75        next.Insert(0, inner);
76      }
77      return next;
78    }
79
80    private IEnumerable<IScope> GetScopesOnLevel(IScope scope, int d) {
81      if (d == 0) yield return scope;
82      else {
83        foreach (IScope subScope in scope.SubScopes) {
84          foreach (IScope scopesOfSubScope in GetScopesOnLevel(subScope, d - 1)) {
85            yield return scopesOfSubScope;
86          }
87        }
88      }
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.