Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.3/GenderSpecificSelector.cs @ 4068

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

Sorted usings and removed unused usings in entire solution (#1094)

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