Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9661 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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