[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 |
|
---|
[4068] | 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
[2] | 24 | using HeuristicLab.Core;
|
---|
[3193] | 25 | using HeuristicLab.Data;
|
---|
[2757] | 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 28 |
|
---|
| 29 | namespace HeuristicLab.Operators {
|
---|
[801] | 30 | /// <summary>
|
---|
[3710] | 31 | /// An operator which applies a specified operator on all sub-scopes at the given depth of the current scope.
|
---|
[801] | 32 | /// </summary>
|
---|
[3710] | 33 | [Item("UniformSubScopesProcessor", "An operator which applies a specified operator on all sub-scopes at the given depth of the current scope.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[3193] | 35 | public sealed class UniformSubScopesProcessor : SingleSuccessorOperator {
|
---|
[2818] | 36 | private OperatorParameter OperatorParameter {
|
---|
| 37 | get { return (OperatorParameter)Parameters["Operator"]; }
|
---|
| 38 | }
|
---|
[3193] | 39 | public ValueLookupParameter<BoolValue> ParallelParameter {
|
---|
| 40 | get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
|
---|
| 41 | }
|
---|
[3710] | 42 | public ValueParameter<IntValue> DepthParameter {
|
---|
| 43 | get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
|
---|
| 44 | }
|
---|
[3193] | 45 |
|
---|
[2818] | 46 | public IOperator Operator {
|
---|
| 47 | get { return OperatorParameter.Value; }
|
---|
| 48 | set { OperatorParameter.Value = value; }
|
---|
| 49 | }
|
---|
[3193] | 50 | public BoolValue Parallel {
|
---|
| 51 | get { return ParallelParameter.Value; }
|
---|
| 52 | set { ParallelParameter.Value = value; }
|
---|
| 53 | }
|
---|
[3710] | 54 | public IntValue Depth {
|
---|
| 55 | get { return DepthParameter.Value; }
|
---|
| 56 | set { DepthParameter.Value = value; }
|
---|
| 57 | }
|
---|
[2818] | 58 |
|
---|
[3193] | 59 | public UniformSubScopesProcessor()
|
---|
[2757] | 60 | : base() {
|
---|
[3193] | 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)));
|
---|
[3710] | 63 | Parameters.Add(new ValueParameter<IntValue>("Depth", "The number of steps to descend in the scope tree before applying operator.", new IntValue(1)));
|
---|
[2757] | 64 | }
|
---|
| 65 |
|
---|
[2834] | 66 | public override IOperation Apply() {
|
---|
| 67 | OperationCollection next = new OperationCollection(base.Apply());
|
---|
[2757] | 68 | if (Operator != null) {
|
---|
[3710] | 69 | List<IScope> scopes = GetScopesOnLevel(ExecutionContext.Scope, Depth.Value).ToList();
|
---|
[2834] | 70 | OperationCollection inner = new OperationCollection();
|
---|
[3710] | 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 | }
|
---|
[2757] | 75 | next.Insert(0, inner);
|
---|
| 76 | }
|
---|
[2] | 77 | return next;
|
---|
| 78 | }
|
---|
[3710] | 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 | }
|
---|
[2] | 90 | }
|
---|
| 91 | }
|
---|