Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Selection/3.3/GenderSpecificSelector.cs @ 10187

Last change on this file since 10187 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 8.0 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.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("GenderSpecificSelection", "Brings two parents together by sampling each with a different selection scheme (Wagner, S. and Affenzeller, M. 2005. SexualGA: Gender-Specific Selection for Genetic Algorithms. Proceedings of the 9th World Multi-Conference on Systemics, Cybernetics and Informatics (WMSCI), pp. 76-81).")]
33  [StorableClass]
34  public class GenderSpecificSelector : AlgorithmOperator, ISingleObjectiveSelector, IStochasticOperator {
35    #region Parameters
36    public IValueLookupParameter<BoolValue> MaximizationParameter {
37      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
38    }
39    public ILookupParameter<ItemArray<DoubleValue>> QualityParameter {
40      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["Quality"]; }
41    }
42    public IValueLookupParameter<IntValue> NumberOfSelectedSubScopesParameter {
43      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfSelectedSubScopes"]; }
44    }
45    public IValueLookupParameter<BoolValue> CopySelectedParameter {
46      get { return (IValueLookupParameter<BoolValue>)Parameters["CopySelected"]; }
47    }
48    public ILookupParameter<IRandom> RandomParameter {
49      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
50    }
51    public ValueParameter<ISelector> FemaleSelectorParameter {
52      get { return (ValueParameter<ISelector>)Parameters["FemaleSelector"]; }
53    }
54    public ValueParameter<ISelector> MaleSelectorParameter {
55      get { return (ValueParameter<ISelector>)Parameters["MaleSelector"]; }
56    }
57    #endregion
58
59    #region Properties
60    public BoolValue Maximization {
61      get { return MaximizationParameter.Value; }
62      set { MaximizationParameter.Value = value; }
63    }
64    public IntValue NumberOfSelectedSubScopes {
65      get { return NumberOfSelectedSubScopesParameter.Value; }
66      set { NumberOfSelectedSubScopesParameter.Value = value; }
67    }
68    public BoolValue CopySelected {
69      get { return CopySelectedParameter.Value; }
70      set { CopySelectedParameter.Value = value; }
71    }
72    public ISelector FemaleSelector {
73      get { return FemaleSelectorParameter.Value; }
74      set { FemaleSelectorParameter.Value = value; }
75    }
76    public ISelector MaleSelector {
77      get { return MaleSelectorParameter.Value; }
78      set { MaleSelectorParameter.Value = value; }
79    }
80    #endregion
81
82    [StorableConstructor]
83    protected GenderSpecificSelector(bool deserializing) : base(deserializing) { }
84    protected GenderSpecificSelector(GenderSpecificSelector original, Cloner cloner)
85      : base(original, cloner) {
86      Initialize();
87    }
88
89    public GenderSpecificSelector()
90      : base() {
91      #region Create parameters
92      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
93      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the solutions."));
94      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfSelectedSubScopes", "The number of scopes that should be selected."));
95      Parameters.Add(new ValueLookupParameter<BoolValue>("CopySelected", "True if the scopes should be copied, false if they should be moved.", new BoolValue(true)));
96      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
97      Parameters.Add(new ValueParameter<ISelector>("FemaleSelector", "The selection operator to select the first parent."));
98      Parameters.Add(new ValueParameter<ISelector>("MaleSelector", "The selection operator to select the second parent."));
99      #endregion
100
101      #region Create operators
102      Placeholder femaleSelector = new Placeholder();
103      SubScopesProcessor maleSelection = new SubScopesProcessor();
104      Placeholder maleSelector = new Placeholder();
105      EmptyOperator empty = new EmptyOperator();
106      RightChildReducer rightChildReducer = new RightChildReducer();
107      SubScopesMixer subScopesMixer = new SubScopesMixer();
108
109      femaleSelector.OperatorParameter.ActualName = "FemaleSelector";
110
111      maleSelector.OperatorParameter.ActualName = "MaleSelector";
112
113      subScopesMixer.Partitions = new IntValue(2);
114      #endregion
115
116      #region Create operator graph
117      OperatorGraph.InitialOperator = femaleSelector;
118      femaleSelector.Successor = maleSelection;
119      maleSelection.Operators.Add(maleSelector);
120      maleSelection.Operators.Add(empty);
121      maleSelection.Successor = rightChildReducer;
122      rightChildReducer.Successor = subScopesMixer;
123      #endregion
124
125      Initialize();
126    }
127
128    public override IDeepCloneable Clone(Cloner cloner) {
129      return new GenderSpecificSelector(this, cloner);
130    }
131
132    [StorableHook(HookType.AfterDeserialization)]
133    private void AfterDeserialization() {
134      Initialize();
135    }
136
137    /// <summary>
138    /// Sets how many sub-scopes male and female selectors should select.
139    /// </summary>
140    /// <exception cref="InvalidOperationException">Thrown when <see cref="NumberOfSelectedSubScopesParameter"/> returns an odd number.</exception>
141    /// <returns>Returns Apply of <see cref="AlgorithmOperator"/>.</returns>
142    public override IOperation Apply() {
143      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
144      if (count % 2 > 0) throw new InvalidOperationException(Name + ": There must be an equal number of sub-scopes to be selected.");
145      FemaleSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(count / 2);
146      MaleSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(count / 2);
147      return base.Apply();
148    }
149
150    #region Events
151    private void SelectorParameter_ValueChanged(object sender, EventArgs e) {
152      IValueParameter<ISelector> selectorParam = (sender as IValueParameter<ISelector>);
153      if (selectorParam != null)
154        ParameterizeSelector(selectorParam.Value);
155    }
156    #endregion
157
158    #region Helpers
159    private void Initialize() {
160      FemaleSelectorParameter.ValueChanged += new EventHandler(SelectorParameter_ValueChanged);
161      MaleSelectorParameter.ValueChanged += new EventHandler(SelectorParameter_ValueChanged);
162      if (FemaleSelector == null) FemaleSelector = new ProportionalSelector();
163      if (MaleSelector == null) MaleSelector = new RandomSelector();
164    }
165    private void ParameterizeSelector(ISelector selector) {
166      selector.CopySelected = new BoolValue(true);
167      IStochasticOperator stoOp = (selector as IStochasticOperator);
168      if (stoOp != null) stoOp.RandomParameter.ActualName = RandomParameter.Name;
169      ISingleObjectiveSelector soSelector = (selector as ISingleObjectiveSelector);
170      if (soSelector != null) {
171        soSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
172        soSelector.QualityParameter.ActualName = QualityParameter.Name;
173      }
174    }
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.