Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 3.7 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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
[3376]22using HeuristicLab.Common;
[2]23using HeuristicLab.Core;
[3193]24using HeuristicLab.Data;
[2757]25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[3710]27using System.Collections.Generic;
28using System;
29using System.Linq;
[2]30
31namespace HeuristicLab.Operators {
[801]32  /// <summary>
[3710]33  /// An operator which applies a specified operator on all sub-scopes at the given depth of the current scope.
[801]34  /// </summary>
[3710]35  [Item("UniformSubScopesProcessor", "An operator which applies a specified operator on all sub-scopes at the given depth of the current scope.")]
[3017]36  [StorableClass]
[3193]37  public sealed class UniformSubScopesProcessor : SingleSuccessorOperator {
[2818]38    private OperatorParameter OperatorParameter {
39      get { return (OperatorParameter)Parameters["Operator"]; }
40    }
[3193]41    public ValueLookupParameter<BoolValue> ParallelParameter {
42      get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
43    }
[3710]44    public ValueParameter<IntValue> DepthParameter {
45      get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
46    }
[3193]47
[2818]48    public IOperator Operator {
49      get { return OperatorParameter.Value; }
50      set { OperatorParameter.Value = value; }
51    }
[3193]52    public BoolValue Parallel {
53      get { return ParallelParameter.Value; }
54      set { ParallelParameter.Value = value; }
55    }
[3710]56    public IntValue Depth {
57      get { return DepthParameter.Value; }
58      set { DepthParameter.Value = value; }
59    }
[2818]60
[3193]61    public UniformSubScopesProcessor()
[2757]62      : base() {
[3193]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)));
[3710]65      Parameters.Add(new ValueParameter<IntValue>("Depth", "The number of steps to descend in the scope tree before applying operator.", new IntValue(1)));
[2757]66    }
67
[2834]68    public override IOperation Apply() {
69      OperationCollection next = new OperationCollection(base.Apply());
[2757]70      if (Operator != null) {
[3710]71        List<IScope> scopes = GetScopesOnLevel(ExecutionContext.Scope, Depth.Value).ToList();
[2834]72        OperationCollection inner = new OperationCollection();
[3710]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        }
[2757]77        next.Insert(0, inner);
78      }
[2]79      return next;
80    }
[3710]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    }
[2]92  }
93}
Note: See TracBrowser for help on using the repository browser.