Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.3/Replacer.cs @ 3714

Last change on this file since 3714 was 3601, checked in by abeham, 15 years ago

Added replacement operators and IReplacer interface #998

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Selection {
31  [Item("Replacer", "Generic replacer that replaces sub-scopes of the remaining scope with those from the selected scope.")]
32  [StorableClass]
33  public class Replacer : AlgorithmOperator, IReplacer {
34    public IValueLookupParameter<ISelector> ReplacedSelectorParameter {
35      get { return (IValueLookupParameter<ISelector>)Parameters["ReplacedSelector"]; }
36    }
37    public IValueLookupParameter<ISelector> SelectedSelectorParameter {
38      get { return (IValueLookupParameter<ISelector>)Parameters["SelectedSelector"]; }
39    }
40
41    [StorableConstructor]
42    protected Replacer(bool deserializing) : base() { }
43    public Replacer() {
44      Parameters.Add(new ValueLookupParameter<ISelector>("ReplacedSelector", "The selection operator to select those scopes that are to be replaced."));
45      Parameters.Add(new ValueLookupParameter<ISelector>("SelectedSelector", "The selection operator to select those scopes that are replacing the others."));
46
47      SubScopesProcessor ssp = new SubScopesProcessor();
48
49      Placeholder replacedSelector = new Placeholder();
50      replacedSelector.OperatorParameter.ActualName = ReplacedSelectorParameter.Name;
51
52      LeftReducer leftReducer = new LeftReducer();
53
54      Placeholder selectedSelector = new Placeholder();
55      selectedSelector.OperatorParameter.ActualName = SelectedSelectorParameter.Name;
56
57      RightReducer rightReducer = new RightReducer();
58
59      MergingReducer merger = new MergingReducer();
60
61      OperatorGraph.InitialOperator = ssp;
62      ssp.Operators.Add(replacedSelector);
63      ssp.Operators.Add(selectedSelector);
64      ssp.Successor = merger;
65      replacedSelector.Successor = leftReducer;
66      leftReducer.Successor = null;
67      selectedSelector.Successor = rightReducer;
68      rightReducer.Successor = null;
69      merger.Successor = null;
70    }
71
72    public override IOperation Apply() {
73      if (ExecutionContext.Scope.SubScopes.Count != 2) throw new InvalidOperationException(Name + ": There must be two sub-scopes which should be replaced/merged.");
74      int remaining = ExecutionContext.Scope.SubScopes[0].SubScopes.Count;
75      int selected = ExecutionContext.Scope.SubScopes[1].SubScopes.Count;
76     
77      ISelector replacedSelector = ReplacedSelectorParameter.ActualValue;
78      ISelector selectedSelector = SelectedSelectorParameter.ActualValue;
79     
80      if (replacedSelector != null) {
81        replacedSelector.CopySelected = new BoolValue(false);
82        replacedSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(Math.Min(remaining, selected));
83      }
84      if (selectedSelector != null) {
85        selectedSelector.CopySelected = new BoolValue(false);
86        selectedSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(Math.Min(remaining, selected));
87      }
88
89      return base.Apply();
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.