Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/07/10 21:10:46 (14 years ago)
Author:
gkronber
Message:

Implemented reviewer comments. #893 (HeuristicLab 3.3.0 application review)

Location:
trunk/sources/HeuristicLab.Operators/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Operators/3.3/SubScopesProcessor.cs

    r3407 r3710  
    2020#endregion
    2121
     22using System.Linq;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    2526using HeuristicLab.Parameters;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using System.Collections.Generic;
     29using System;
    2730
    2831namespace HeuristicLab.Operators {
    2932  /// <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.
    3134  /// </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.")]
    3336  [StorableClass]
    3437  public sealed class SubScopesProcessor : MultiOperator<IOperator> {
    3538    public ValueLookupParameter<BoolValue> ParallelParameter {
    3639      get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
     40    }
     41    public ValueParameter<IntValue> DepthParameter {
     42      get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
    3743    }
    3844
     
    4147      set { ParallelParameter.Value = value; }
    4248    }
     49    public IntValue Depth {
     50      get { return DepthParameter.Value; }
     51      set { DepthParameter.Value = value; }
     52    }
    4353
    4454    public SubScopesProcessor()
    4555      : base() {
    4656      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)));
    4758    }
    4859
    4960    public override IOperation Apply() {
    50       BoolValue parallel = ParallelParameter.ActualValue;
     61      List<IScope> scopes = GetScopesOnLevel(ExecutionContext.Scope, Depth.Value).ToList();
    5162      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]));
    5869      }
     70      next.Insert(0, inner);
    5971      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      }
    6083    }
    6184  }
  • trunk/sources/HeuristicLab.Operators/3.3/UniformSubScopesProcessor.cs

    r3376 r3710  
    2525using HeuristicLab.Parameters;
    2626using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     27using System.Collections.Generic;
     28using System;
     29using System.Linq;
    2730
    2831namespace HeuristicLab.Operators {
    2932  /// <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.
    3134  /// </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.")]
    3336  [StorableClass]
    3437  public sealed class UniformSubScopesProcessor : SingleSuccessorOperator {
     
    3841    public ValueLookupParameter<BoolValue> ParallelParameter {
    3942      get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
     43    }
     44    public ValueParameter<IntValue> DepthParameter {
     45      get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
    4046    }
    4147
     
    4854      set { ParallelParameter.Value = value; }
    4955    }
     56    public IntValue Depth {
     57      get { return DepthParameter.Value; }
     58      set { DepthParameter.Value = value; }
     59    }
    5060
    5161    public UniformSubScopesProcessor()
     
    5363      Parameters.Add(new OperatorParameter("Operator", "The operator which should be applied on all sub-scopes of the current scope."));
    5464      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)));
    5566    }
    5667
    5768    public override IOperation Apply() {
    58       BoolValue parallel = ParallelParameter.ActualValue;
    5969      OperationCollection next = new OperationCollection(base.Apply());
    6070      if (Operator != null) {
     71        List<IScope> scopes = GetScopesOnLevel(ExecutionContext.Scope, Depth.Value).ToList();
    6172        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        }
    6577        next.Insert(0, inner);
    6678      }
    6779      return next;
    6880    }
     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    }
    6992  }
    7093}
Note: See TracChangeset for help on using the changeset viewer.