Changeset 3710 for trunk/sources/HeuristicLab.Operators/3.3
- Timestamp:
- 05/07/10 21:10:46 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Operators/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Operators/3.3/SubScopesProcessor.cs
r3407 r3710 20 20 #endregion 21 21 22 using System.Linq; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 25 26 using HeuristicLab.Parameters; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using System.Collections.Generic; 29 using System; 27 30 28 31 namespace HeuristicLab.Operators { 29 32 /// <summary> 30 /// An operator which contains multiple operators of which each is applied on one sub-scope of the current scope. The first operator is applied on the first sub-scope, the second on the second, and so on.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. 31 34 /// </summary> 32 [Item("SubScopesProcessor", "An operator which contains multiple operators of which each is applied on one sub-scope of the current scope. The first operator is applied on the first sub-scope, the second on the second, and so on.")]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.")] 33 36 [StorableClass] 34 37 public sealed class SubScopesProcessor : MultiOperator<IOperator> { 35 38 public ValueLookupParameter<BoolValue> ParallelParameter { 36 39 get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; } 40 } 41 public ValueParameter<IntValue> DepthParameter { 42 get { return (ValueParameter<IntValue>)Parameters["Depth"]; } 37 43 } 38 44 … … 41 47 set { ParallelParameter.Value = value; } 42 48 } 49 public IntValue Depth { 50 get { return DepthParameter.Value; } 51 set { DepthParameter.Value = value; } 52 } 43 53 44 54 public SubScopesProcessor() 45 55 : base() { 46 56 Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operators should be applied in parallel on the sub-scopes, otherwise false.", new BoolValue(false))); 57 Parameters.Add(new ValueParameter<IntValue>("Depth", "The number of steps to descend in the scope tree before applying operator.", new IntValue(1))); 47 58 } 48 59 49 60 public override IOperation Apply() { 50 BoolValue parallel = ParallelParameter.ActualValue;61 List<IScope> scopes = GetScopesOnLevel(ExecutionContext.Scope, Depth.Value).ToList(); 51 62 OperationCollection next = new OperationCollection(base.Apply()); 52 if ( Operators.Count > 0) {53 OperationCollection inner = new OperationCollection();54 inner.Parallel = parallel == null ? false : parallel.Value;55 for (int i = 0; (i < ExecutionContext.Scope.SubScopes.Count) && (i < Operators.Count); i++)56 if (Operators[i] != null) inner.Add(ExecutionContext.CreateOperation(Operators[i], ExecutionContext.Scope.SubScopes[i]));57 next.Insert(0, inner);63 if (scopes.Count != Operators.Count) 64 throw new ArgumentException("The number of operators doesn't match the number of sub-scopes at depth " + Depth.Value); 65 OperationCollection inner = new OperationCollection(); 66 inner.Parallel = Parallel == null ? false : Parallel.Value; 67 for (int i = 0; i < scopes.Count(); i++) { 68 inner.Add(ExecutionContext.CreateOperation(Operators[i], scopes[i])); 58 69 } 70 next.Insert(0, inner); 59 71 return next; 72 } 73 74 private IEnumerable<IScope> GetScopesOnLevel(IScope scope, int d) { 75 if (d == 0) yield return scope; 76 else { 77 foreach (IScope subScope in scope.SubScopes) { 78 foreach (IScope scopesOfSubScope in GetScopesOnLevel(subScope, d - 1)) { 79 yield return scopesOfSubScope; 80 } 81 } 82 } 60 83 } 61 84 } -
trunk/sources/HeuristicLab.Operators/3.3/UniformSubScopesProcessor.cs
r3376 r3710 25 25 using HeuristicLab.Parameters; 26 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using System.Collections.Generic; 28 using System; 29 using System.Linq; 27 30 28 31 namespace HeuristicLab.Operators { 29 32 /// <summary> 30 /// An operator which applies a specified operator on all sub-scopes of the current scope.33 /// An operator which applies a specified operator on all sub-scopes at the given depth of the current scope. 31 34 /// </summary> 32 [Item("UniformSubScopesProcessor", "An operator which applies a specified operator on all sub-scopes of the current scope.")]35 [Item("UniformSubScopesProcessor", "An operator which applies a specified operator on all sub-scopes at the given depth of the current scope.")] 33 36 [StorableClass] 34 37 public sealed class UniformSubScopesProcessor : SingleSuccessorOperator { … … 38 41 public ValueLookupParameter<BoolValue> ParallelParameter { 39 42 get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; } 43 } 44 public ValueParameter<IntValue> DepthParameter { 45 get { return (ValueParameter<IntValue>)Parameters["Depth"]; } 40 46 } 41 47 … … 48 54 set { ParallelParameter.Value = value; } 49 55 } 56 public IntValue Depth { 57 get { return DepthParameter.Value; } 58 set { DepthParameter.Value = value; } 59 } 50 60 51 61 public UniformSubScopesProcessor() … … 53 63 Parameters.Add(new OperatorParameter("Operator", "The operator which should be applied on all sub-scopes of the current scope.")); 54 64 Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(false))); 65 Parameters.Add(new ValueParameter<IntValue>("Depth", "The number of steps to descend in the scope tree before applying operator.", new IntValue(1))); 55 66 } 56 67 57 68 public override IOperation Apply() { 58 BoolValue parallel = ParallelParameter.ActualValue;59 69 OperationCollection next = new OperationCollection(base.Apply()); 60 70 if (Operator != null) { 71 List<IScope> scopes = GetScopesOnLevel(ExecutionContext.Scope, Depth.Value).ToList(); 61 72 OperationCollection inner = new OperationCollection(); 62 inner.Parallel = parallel == null ? false : parallel.Value; 63 for (int i = 0; i < ExecutionContext.Scope.SubScopes.Count; i++) 64 inner.Add(ExecutionContext.CreateOperation(Operator, ExecutionContext.Scope.SubScopes[i])); 73 inner.Parallel = Parallel == null ? false : Parallel.Value; 74 for (int i = 0; i < scopes.Count; i++) { 75 inner.Add(ExecutionContext.CreateOperation(Operator, scopes[i])); 76 } 65 77 next.Insert(0, inner); 66 78 } 67 79 return next; 68 80 } 81 82 private IEnumerable<IScope> GetScopesOnLevel(IScope scope, int d) { 83 if (d == 0) yield return scope; 84 else { 85 foreach (IScope subScope in scope.SubScopes) { 86 foreach (IScope scopesOfSubScope in GetScopesOnLevel(subScope, d - 1)) { 87 yield return scopesOfSubScope; 88 } 89 } 90 } 91 } 69 92 } 70 93 }
Note: See TracChangeset
for help on using the changeset viewer.