Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.3/SubScopesProcessor.cs @ 6689

Last change on this file since 6689 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.0 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 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
[4068]22using System;
23using System.Collections.Generic;
[3710]24using System.Linq;
[4722]25using HeuristicLab.Common;
[2]26using HeuristicLab.Core;
[3193]27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
[3000]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]30
31namespace HeuristicLab.Operators {
[801]32  /// <summary>
[3710]33  /// An operator which contains multiple operators of which each is applied on one sub-scope at the given depth of the current scope. The first operator is applied on the first sub-scope, the second on the second, and so on.
[801]34  /// </summary>
[3710]35  [Item("SubScopesProcessor", "An operator which contains multiple operators of which each is applied on one sub-scope at the given depth of the current scope. The first operator is applied on the first sub-scope, the second on the second, and so on.")]
[3017]36  [StorableClass]
[3407]37  public sealed class SubScopesProcessor : MultiOperator<IOperator> {
[3193]38    public ValueLookupParameter<BoolValue> ParallelParameter {
39      get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
40    }
[3710]41    public ValueParameter<IntValue> DepthParameter {
42      get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
43    }
[3193]44
45    public BoolValue Parallel {
46      get { return ParallelParameter.Value; }
47      set { ParallelParameter.Value = value; }
48    }
[3710]49    public IntValue Depth {
50      get { return DepthParameter.Value; }
51      set { DepthParameter.Value = value; }
52    }
[3193]53
[4722]54    [StorableConstructor]
55    private SubScopesProcessor(bool deserializing) : base(deserializing) { }
56    private SubScopesProcessor(SubScopesProcessor original, Cloner cloner)
57      : base(original, cloner) {
58    }
[3193]59    public SubScopesProcessor()
[2773]60      : base() {
[3193]61      Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operators should be applied in parallel on the sub-scopes, otherwise false.", new BoolValue(false)));
[3710]62      Parameters.Add(new ValueParameter<IntValue>("Depth", "The number of steps to descend in the scope tree before applying operator.", new IntValue(1)));
[2]63    }
64
[4722]65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new SubScopesProcessor(this, cloner);
67    }
68
[2834]69    public override IOperation Apply() {
[3710]70      List<IScope> scopes = GetScopesOnLevel(ExecutionContext.Scope, Depth.Value).ToList();
[2834]71      OperationCollection next = new OperationCollection(base.Apply());
[3710]72      if (scopes.Count != Operators.Count)
73        throw new ArgumentException("The number of operators doesn't match the number of sub-scopes at depth " + Depth.Value);
74      OperationCollection inner = new OperationCollection();
75      inner.Parallel = Parallel == null ? false : Parallel.Value;
76      for (int i = 0; i < scopes.Count(); i++) {
77        inner.Add(ExecutionContext.CreateOperation(Operators[i], scopes[i]));
[2773]78      }
[3710]79      next.Insert(0, inner);
[2]80      return next;
81    }
[3710]82
83    private IEnumerable<IScope> GetScopesOnLevel(IScope scope, int d) {
84      if (d == 0) yield return scope;
85      else {
86        foreach (IScope subScope in scope.SubScopes) {
87          foreach (IScope scopesOfSubScope in GetScopesOnLevel(subScope, d - 1)) {
88            yield return scopesOfSubScope;
89          }
90        }
91      }
92    }
[2]93  }
94}
Note: See TracBrowser for help on using the repository browser.