Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/LayerUniformSubScopesProcessor.cs @ 12039

Last change on this file since 12039 was 12039, checked in by pfleck, 9 years ago

#2269
Per-layer parameter can be used to define all multiple layers.
If less values are specified in the array the last value is used for all subsequent layers.
I.e. when the array has the length 1, all layers use that same one value.
From a usability point of view, this behavior may has to be discussed more in detail.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Algorithms.ALPS {
34  [Item("LayerUniformSubScopesProcessor", "")]
35  [StorableClass]
36  public class LayerUniformSubScopesProcessor : UniformSubScopesProcessor {
37
38    private ILookupParameter<IntArray> PopulationSizeParameter {
39      get { return (ILookupParameter<IntArray>)Parameters["PopulationSize"]; }
40    }
41
42    [StorableConstructor]
43    private LayerUniformSubScopesProcessor(bool deserializing) : base(deserializing) { }
44    private LayerUniformSubScopesProcessor(LayerUniformSubScopesProcessor original, Cloner cloner)
45      : base(original, cloner) {
46    }
47    public LayerUniformSubScopesProcessor()
48      : base() {
49      Parameters.Add(new LookupParameter<IntArray>("PopulationSize"));
50    }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new LayerUniformSubScopesProcessor(this, cloner);
54    }
55
56    public override IOperation Apply() {
57      OperationCollection next = new OperationCollection(Successor != null ? ExecutionContext.CreateOperation(Successor) : null); // base.base.Apply not possible
58      if (Operator != null) {
59        List<IScope> scopes = GetScopesOnLevel(ExecutionContext.Scope, Depth.Value).ToList();
60        OperationCollection inner = new OperationCollection();
61        inner.Parallel = Parallel == null ? false : Parallel.Value;
62        for (int i = 0; i < scopes.Count; i++) {
63          var layerParameters = new LayerIntermediateParameter {
64            new ValueParameter<IntValue>(PopulationSizeParameter.ActualName, new IntValue(PopulationSizeParameter.ActualValue[Math.Min(i, PopulationSizeParameter.ActualValue.Length - 1)]))
65          };
66          var immediateContext = new ExecutionContext(ExecutionContext.Parent, layerParameters, scopes[i]);
67          inner.Add(immediateContext.CreateChildOperation(Operator, scopes[i]));
68        }
69        next.Insert(0, inner);
70      }
71      return next;
72    }
73  }
74
75  [Item("LayerIntermediateParameter", "")]
76  [StorableClass]
77  public class LayerIntermediateParameter : ParameterizedNamedItem, IEnumerable<IParameter> {
78    [StorableConstructor]
79    private LayerIntermediateParameter(bool deserializing) : base(deserializing) { }
80    private LayerIntermediateParameter(LayerIntermediateParameter original, Cloner cloner)
81      : base(original, cloner) {
82    }
83    public LayerIntermediateParameter()
84      : base("LayerIntermediateParameter") {
85    }
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new LayerIntermediateParameter(this, cloner);
88    }
89
90    public void Add(IParameter parameter) {
91      Parameters.Add(parameter);
92    }
93
94    public IEnumerator<IParameter> GetEnumerator() {
95      return Parameters.GetEnumerator();
96    }
97    IEnumerator IEnumerable.GetEnumerator() {
98      return GetEnumerator();
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.