Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Operators/3.3/UniformSubScopesProcessor.cs @ 14711

Last change on this file since 14711 was 14711, checked in by gkronber, 7 years ago

#2520

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