[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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[3710] | 24 | using System.Linq;
|
---|
[2] | 25 | using HeuristicLab.Core;
|
---|
[3193] | 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[3000] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 29 |
|
---|
| 30 | namespace HeuristicLab.Operators {
|
---|
[801] | 31 | /// <summary>
|
---|
[3710] | 32 | /// 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] | 33 | /// </summary>
|
---|
[3710] | 34 | [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] | 35 | [StorableClass]
|
---|
[3407] | 36 | public sealed class SubScopesProcessor : MultiOperator<IOperator> {
|
---|
[3193] | 37 | public ValueLookupParameter<BoolValue> ParallelParameter {
|
---|
| 38 | get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
|
---|
| 39 | }
|
---|
[3710] | 40 | public ValueParameter<IntValue> DepthParameter {
|
---|
| 41 | get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
|
---|
| 42 | }
|
---|
[3193] | 43 |
|
---|
| 44 | public BoolValue Parallel {
|
---|
| 45 | get { return ParallelParameter.Value; }
|
---|
| 46 | set { ParallelParameter.Value = value; }
|
---|
| 47 | }
|
---|
[3710] | 48 | public IntValue Depth {
|
---|
| 49 | get { return DepthParameter.Value; }
|
---|
| 50 | set { DepthParameter.Value = value; }
|
---|
| 51 | }
|
---|
[3193] | 52 |
|
---|
| 53 | public SubScopesProcessor()
|
---|
[2773] | 54 | : base() {
|
---|
[3193] | 55 | 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] | 56 | Parameters.Add(new ValueParameter<IntValue>("Depth", "The number of steps to descend in the scope tree before applying operator.", new IntValue(1)));
|
---|
[2] | 57 | }
|
---|
| 58 |
|
---|
[2834] | 59 | public override IOperation Apply() {
|
---|
[3710] | 60 | List<IScope> scopes = GetScopesOnLevel(ExecutionContext.Scope, Depth.Value).ToList();
|
---|
[2834] | 61 | OperationCollection next = new OperationCollection(base.Apply());
|
---|
[3710] | 62 | if (scopes.Count != Operators.Count)
|
---|
| 63 | throw new ArgumentException("The number of operators doesn't match the number of sub-scopes at depth " + Depth.Value);
|
---|
| 64 | OperationCollection inner = new OperationCollection();
|
---|
| 65 | inner.Parallel = Parallel == null ? false : Parallel.Value;
|
---|
| 66 | for (int i = 0; i < scopes.Count(); i++) {
|
---|
| 67 | inner.Add(ExecutionContext.CreateOperation(Operators[i], scopes[i]));
|
---|
[2773] | 68 | }
|
---|
[3710] | 69 | next.Insert(0, inner);
|
---|
[2] | 70 | return next;
|
---|
| 71 | }
|
---|
[3710] | 72 |
|
---|
| 73 | private IEnumerable<IScope> GetScopesOnLevel(IScope scope, int d) {
|
---|
| 74 | if (d == 0) yield return scope;
|
---|
| 75 | else {
|
---|
| 76 | foreach (IScope subScope in scope.SubScopes) {
|
---|
| 77 | foreach (IScope scopesOfSubScope in GetScopesOnLevel(subScope, d - 1)) {
|
---|
| 78 | yield return scopesOfSubScope;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
[2] | 83 | }
|
---|
| 84 | }
|
---|