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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
34 | [Item("LayerUniformSubScopesProcessor", "")]
|
---|
35 | [StorableClass]
|
---|
36 | public class LayerUniformSubScopesProcessor : SingleSuccessorOperator {
|
---|
37 | private OperatorParameter OperatorParameter {
|
---|
38 | get { return (OperatorParameter)Parameters["Operator"]; }
|
---|
39 | }
|
---|
40 | public ValueLookupParameter<BoolValue> ParallelParameter {
|
---|
41 | get { return (ValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | private ILookupParameter<IntArray> PopulationSizeParameter {
|
---|
45 | get { return (ILookupParameter<IntArray>)Parameters["PopulationSize"]; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public IOperator Operator {
|
---|
49 | get { return OperatorParameter.Value; }
|
---|
50 | set { OperatorParameter.Value = value; }
|
---|
51 | }
|
---|
52 | public BoolValue Parallel {
|
---|
53 | get { return ParallelParameter.Value; }
|
---|
54 | set { ParallelParameter.Value = value; }
|
---|
55 | }
|
---|
56 |
|
---|
57 | [StorableConstructor]
|
---|
58 | private LayerUniformSubScopesProcessor(bool deserializing) : base(deserializing) { }
|
---|
59 | private LayerUniformSubScopesProcessor(LayerUniformSubScopesProcessor original, Cloner cloner)
|
---|
60 | : base(original, cloner) {
|
---|
61 | }
|
---|
62 | public LayerUniformSubScopesProcessor()
|
---|
63 | : base() {
|
---|
64 | Parameters.Add(new OperatorParameter("Operator", "The operator which should be applied on all sub-scopes of the current scope."));
|
---|
65 | Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(false)));
|
---|
66 |
|
---|
67 | Parameters.Add(new LookupParameter<IntArray>("PopulationSize"));
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
71 | return new LayerUniformSubScopesProcessor(this, cloner);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override IOperation Apply() {
|
---|
75 | var next = new OperationCollection(Successor != null ? ExecutionContext.CreateOperation(Successor) : null);
|
---|
76 | if (Operator != null) {
|
---|
77 | var scopes = ExecutionContext.Scope.SubScopes;
|
---|
78 | var inner = new OperationCollection() {
|
---|
79 | Parallel = Parallel == null ? false : Parallel.Value
|
---|
80 | };
|
---|
81 | for (int i = 0; i < scopes.Count; i++) {
|
---|
82 | var layerParameters = new LayerIntermediateParameter {
|
---|
83 | new ValueParameter<IntValue>(PopulationSizeParameter.ActualName, new IntValue(PopulationSizeParameter.ActualValue[Math.Min(i, PopulationSizeParameter.ActualValue.Length - 1)]))
|
---|
84 | };
|
---|
85 | var immediateContext = new ExecutionContext(ExecutionContext.Parent, layerParameters, scopes[i]);
|
---|
86 | inner.Add(immediateContext.CreateChildOperation(Operator, scopes[i]));
|
---|
87 | }
|
---|
88 | next.Insert(0, inner);
|
---|
89 | }
|
---|
90 | return next;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | [Item("LayerIntermediateParameter", "")]
|
---|
95 | [StorableClass]
|
---|
96 | public class LayerIntermediateParameter : ParameterizedNamedItem, IEnumerable<IParameter> {
|
---|
97 | [StorableConstructor]
|
---|
98 | private LayerIntermediateParameter(bool deserializing) : base(deserializing) { }
|
---|
99 | private LayerIntermediateParameter(LayerIntermediateParameter original, Cloner cloner)
|
---|
100 | : base(original, cloner) {
|
---|
101 | }
|
---|
102 | public LayerIntermediateParameter()
|
---|
103 | : base("LayerIntermediateParameter") {
|
---|
104 | }
|
---|
105 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
106 | return new LayerIntermediateParameter(this, cloner);
|
---|
107 | }
|
---|
108 |
|
---|
109 | public void Add(IParameter parameter) {
|
---|
110 | Parameters.Add(parameter);
|
---|
111 | }
|
---|
112 |
|
---|
113 | public IEnumerator<IParameter> GetEnumerator() {
|
---|
114 | return Parameters.GetEnumerator();
|
---|
115 | }
|
---|
116 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
117 | return GetEnumerator();
|
---|
118 | }
|
---|
119 | }
|
---|
120 | } |
---|