Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3729 was 3710, checked in by gkronber, 15 years ago

Implemented reviewer comments. #893 (HeuristicLab 3.3.0 application review)

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