Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3413 was 3413, checked in by abeham, 14 years ago

Updated Gender Specific Selection, Offspring Selector and other changes related to the OSGA #976
Removed old OS plugin, removed SelectedSubScopesLookupParameter

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