[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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;
|
---|
[4722] | 24 | using HeuristicLab.Common;
|
---|
[2] | 25 | using HeuristicLab.Core;
|
---|
[3193] | 26 | using HeuristicLab.Data;
|
---|
[2757] | 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 29 |
|
---|
| 30 | namespace HeuristicLab.Operators {
|
---|
[801] | 31 | /// <summary>
|
---|
[3710] | 32 | /// An operator which applies a specified operator on all sub-scopes at the given depth of the current scope.
|
---|
[801] | 33 | /// </summary>
|
---|
[3710] | 34 | [Item("UniformSubScopesProcessor", "An operator which applies a specified operator on all sub-scopes at the given depth of the current scope.")]
|
---|
[3017] | 35 | [StorableClass]
|
---|
[3193] | 36 | public sealed class UniformSubScopesProcessor : SingleSuccessorOperator {
|
---|
[2818] | 37 | private OperatorParameter OperatorParameter {
|
---|
| 38 | get { return (OperatorParameter)Parameters["Operator"]; }
|
---|
| 39 | }
|
---|
[3193] | 40 | public ValueLookupParameter<BoolValue> ParallelParameter {
|
---|
| 41 | get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
|
---|
| 42 | }
|
---|
[3710] | 43 | public ValueParameter<IntValue> DepthParameter {
|
---|
| 44 | get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
|
---|
| 45 | }
|
---|
[3193] | 46 |
|
---|
[2818] | 47 | public IOperator Operator {
|
---|
| 48 | get { return OperatorParameter.Value; }
|
---|
| 49 | set { OperatorParameter.Value = value; }
|
---|
| 50 | }
|
---|
[3193] | 51 | public BoolValue Parallel {
|
---|
| 52 | get { return ParallelParameter.Value; }
|
---|
| 53 | set { ParallelParameter.Value = value; }
|
---|
| 54 | }
|
---|
[3710] | 55 | public IntValue Depth {
|
---|
| 56 | get { return DepthParameter.Value; }
|
---|
| 57 | set { DepthParameter.Value = value; }
|
---|
| 58 | }
|
---|
[2818] | 59 |
|
---|
[4722] | 60 | [StorableConstructor]
|
---|
| 61 | private UniformSubScopesProcessor(bool deserializing) : base(deserializing) { }
|
---|
| 62 | private UniformSubScopesProcessor(UniformSubScopesProcessor original, Cloner cloner)
|
---|
| 63 | : base(original, cloner) {
|
---|
| 64 | }
|
---|
[3193] | 65 | public UniformSubScopesProcessor()
|
---|
[2757] | 66 | : base() {
|
---|
[3193] | 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)));
|
---|
[3710] | 69 | Parameters.Add(new ValueParameter<IntValue>("Depth", "The number of steps to descend in the scope tree before applying operator.", new IntValue(1)));
|
---|
[2757] | 70 | }
|
---|
| 71 |
|
---|
[4722] | 72 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 73 | return new UniformSubScopesProcessor(this, cloner);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[2834] | 76 | public override IOperation Apply() {
|
---|
| 77 | OperationCollection next = new OperationCollection(base.Apply());
|
---|
[2757] | 78 | if (Operator != null) {
|
---|
[3710] | 79 | List<IScope> scopes = GetScopesOnLevel(ExecutionContext.Scope, Depth.Value).ToList();
|
---|
[2834] | 80 | OperationCollection inner = new OperationCollection();
|
---|
[3710] | 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 | }
|
---|
[2757] | 85 | next.Insert(0, inner);
|
---|
| 86 | }
|
---|
[2] | 87 | return next;
|
---|
| 88 | }
|
---|
[3710] | 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 | }
|
---|
[2] | 100 | }
|
---|
| 101 | }
|
---|