Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2708_ScopedAlgorithms/HeuristicLab.Selection/3.3/GenderSpecificSelector.cs @ 18242

Last change on this file since 18242 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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    protected 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      CopySelectedParameter.Hidden = true;
100      #endregion
101
102      #region Create operators
103      Placeholder femaleSelector = new Placeholder();
104      SubScopesProcessor maleSelection = new SubScopesProcessor();
105      Placeholder maleSelector = new Placeholder();
106      EmptyOperator empty = new EmptyOperator();
107      RightChildReducer rightChildReducer = new RightChildReducer();
108      SubScopesMixer subScopesMixer = new SubScopesMixer();
109
110      femaleSelector.OperatorParameter.ActualName = "FemaleSelector";
111
112      maleSelector.OperatorParameter.ActualName = "MaleSelector";
113
114      subScopesMixer.Partitions = new IntValue(2);
115      #endregion
116
117      #region Create operator graph
118      OperatorGraph.InitialOperator = femaleSelector;
119      femaleSelector.Successor = maleSelection;
120      maleSelection.Operators.Add(maleSelector);
121      maleSelection.Operators.Add(empty);
122      maleSelection.Successor = rightChildReducer;
123      rightChildReducer.Successor = subScopesMixer;
124      #endregion
125
126      Initialize();
127    }
128
129    public override IDeepCloneable Clone(Cloner cloner) {
130      return new GenderSpecificSelector(this, cloner);
131    }
132
133    [StorableHook(HookType.AfterDeserialization)]
134    private void AfterDeserialization() {
135      Initialize();
136    }
137
138    /// <summary>
139    /// Sets how many sub-scopes male and female selectors should select.
140    /// </summary>
141    /// <exception cref="InvalidOperationException">Thrown when <see cref="NumberOfSelectedSubScopesParameter"/> returns an odd number.</exception>
142    /// <returns>Returns Apply of <see cref="AlgorithmOperator"/>.</returns>
143    public override IOperation Apply() {
144      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
145      if (count % 2 > 0) throw new InvalidOperationException(Name + ": There must be an equal number of sub-scopes to be selected.");
146      FemaleSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(count / 2);
147      MaleSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(count / 2);
148      return base.Apply();
149    }
150
151    #region Events
152    private void SelectorParameter_ValueChanged(object sender, EventArgs e) {
153      IValueParameter<ISelector> selectorParam = (sender as IValueParameter<ISelector>);
154      if (selectorParam != null)
155        ParameterizeSelector(selectorParam.Value);
156    }
157    #endregion
158
159    #region Helpers
160    private void Initialize() {
161      FemaleSelectorParameter.ValueChanged += new EventHandler(SelectorParameter_ValueChanged);
162      MaleSelectorParameter.ValueChanged += new EventHandler(SelectorParameter_ValueChanged);
163      if (FemaleSelector == null) FemaleSelector = new ProportionalSelector();
164      if (MaleSelector == null) MaleSelector = new RandomSelector();
165    }
166    private void ParameterizeSelector(ISelector selector) {
167      selector.CopySelected = new BoolValue(true);
168      IStochasticOperator stoOp = (selector as IStochasticOperator);
169      if (stoOp != null) stoOp.RandomParameter.ActualName = RandomParameter.Name;
170      ISingleObjectiveSelector soSelector = (selector as ISingleObjectiveSelector);
171      if (soSelector != null) {
172        soSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
173        soSelector.QualityParameter.ActualName = QualityParameter.Name;
174      }
175    }
176    #endregion
177  }
178}
Note: See TracBrowser for help on using the repository browser.