Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.SGA/3.3/SGA.cs @ 2924

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

Operator architecture refactoring (#95)

  • implemented reviewers' comments on version r2917.
File size: 10.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 System.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Evolutionary;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Algorithms.SGA {
34  /// <summary>
35  /// A standard genetic algorithm.
36  /// </summary>
37  [Item("SGA", "A standard genetic algorithm.")]
38  [Creatable("Algorithms")]
39  public sealed class SGA : EngineAlgorithm {
40    [Storable]
41    private PopulationCreator populationCreator;
42    [Storable]
43    private SGAOperator sgaOperator;
44
45    private ValueParameter<IntData> PopulationSizeParameter {
46      get { return (ValueParameter<IntData>)Parameters["PopulationSize"]; }
47    }
48    private ConstrainedValueParameter<ISelector> SelectorParameter {
49      get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
50    }
51    private ConstrainedValueParameter<ICrossover> CrossoverParameter {
52      get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
53    }
54    private OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
55      get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
56    }
57    private ValueParameter<IntData> ElitesParameter {
58      get { return (ValueParameter<IntData>)Parameters["Elites"]; }
59    }
60
61    public override Type ProblemType {
62      get { return typeof(ISingleObjectiveProblem); }
63    }
64    public new ISingleObjectiveProblem Problem {
65      get { return (ISingleObjectiveProblem)base.Problem; }
66      set { base.Problem = value; }
67    }
68
69    public SGA()
70      : base() {
71      Parameters.Add(new ValueParameter<IntData>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntData(0)));
72      Parameters.Add(new ValueParameter<BoolData>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolData(true)));
73      Parameters.Add(new ValueParameter<IntData>("PopulationSize", "The size of the population of solutions.", new IntData(100)));
74      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
75      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
76      Parameters.Add(new ValueParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new DoubleData(0.05)));
77      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
78      Parameters.Add(new ValueParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation.", new IntData(1)));
79      Parameters.Add(new ValueParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntData(1000)));
80
81      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
82      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
83
84      RandomCreator randomCreator = new RandomCreator();
85      populationCreator = new PopulationCreator();
86      sgaOperator = new SGAOperator();
87
88      randomCreator.RandomParameter.ActualName = "Random";
89      randomCreator.SeedParameter.ActualName = "Seed";
90      randomCreator.SeedParameter.Value = null;
91      randomCreator.SetSeedRandomlyParameter.ActualName = "SetSeedRandomly";
92      randomCreator.SetSeedRandomlyParameter.Value = null;
93      randomCreator.Successor = populationCreator;
94
95      populationCreator.PopulationSizeParameter.ActualName = "PopulationSize";
96      populationCreator.PopulationSizeParameter.Value = null;
97      populationCreator.Successor = sgaOperator;
98
99      sgaOperator.SelectorParameter.ActualName = "Selector";
100      sgaOperator.CrossoverParameter.ActualName = "Crossover";
101      sgaOperator.ElitesParameter.ActualName = "Elites";
102      sgaOperator.MaximumGenerationsParameter.ActualName = "MaximumGenerations";
103      sgaOperator.MutatorParameter.ActualName = "Mutator";
104      sgaOperator.MutationProbabilityParameter.ActualName = "MutationProbability";
105      sgaOperator.RandomParameter.ActualName = "Random";
106      sgaOperator.ResultsParameter.ActualName = "Results";
107
108      OperatorGraph.InitialOperator = randomCreator;
109
110      if (ApplicationManager.Manager != null) {
111        var selectors = ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name);
112        foreach (ISelector selector in selectors) {
113          selector.CopySelected = new BoolData(true);
114          selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
115          if (selector is IStochasticOperator) ((IStochasticOperator)selector).RandomParameter.ActualName = "Random";
116          SelectorParameter.ValidValues.Add(selector);
117        }
118      }
119    }
120
121    public override IDeepCloneable Clone(Cloner cloner) {
122      SGA clone = (SGA)base.Clone(cloner);
123      clone.populationCreator = (PopulationCreator)cloner.Clone(populationCreator);
124      clone.sgaOperator = (SGAOperator)cloner.Clone(sgaOperator);
125      return clone;
126    }
127
128    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
129      foreach (ISelector selector in SelectorParameter.ValidValues)
130        selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
131    }
132    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
133      foreach (ISelector selector in SelectorParameter.ValidValues)
134        selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
135    }
136
137    protected override void DeregisterProblemEvents() {
138      Problem.MaximizationChanged -= new EventHandler(Problem_MaximizationChanged);
139      base.DeregisterProblemEvents();
140    }
141    protected override void RegisterProblemEvents() {
142      base.RegisterProblemEvents();
143      Problem.MaximizationChanged += new EventHandler(Problem_MaximizationChanged);
144    }
145
146    protected override void OnProblemChanged() {
147      if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
148      if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random";
149      foreach (IStochasticOperator op in Problem.Operators.OfType<IStochasticOperator>())
150        op.RandomParameter.ActualName = "Random";
151
152      populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
153      populationCreator.EvaluatorParameter.Value = Problem.Evaluator;
154      sgaOperator.MaximizationParameter.Value = Problem.Maximization;
155      sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
156      sgaOperator.EvaluatorParameter.Value = Problem.Evaluator;
157
158      foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
159        op.MaximizationParameter.Value = Problem.Maximization;
160        op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
161      }
162
163      ICrossover oldCrossover = CrossoverParameter.Value;
164      CrossoverParameter.ValidValues.Clear();
165      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
166        CrossoverParameter.ValidValues.Add(crossover);
167      if (oldCrossover != null) {
168        CrossoverParameter.Value = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
169      }
170
171      IManipulator oldMutator = MutatorParameter.Value;
172      MutatorParameter.ValidValues.Clear();
173      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
174        MutatorParameter.ValidValues.Add(mutator);
175      if (oldMutator != null) {
176        MutatorParameter.Value = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
177      }
178
179      base.OnProblemChanged();
180    }
181    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
182      if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
183      populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
184      base.Problem_SolutionCreatorChanged(sender, e);
185    }
186    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
187      if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random";
188
189      foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
190        op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
191      }
192
193      populationCreator.EvaluatorParameter.Value = Problem.Evaluator;
194      sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
195      sgaOperator.EvaluatorParameter.Value = Problem.Evaluator;
196      base.Problem_EvaluatorChanged(sender, e);
197    }
198    private void Problem_MaximizationChanged(object sender, EventArgs e) {
199      sgaOperator.MaximizationParameter.Value = Problem.Maximization;
200      foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
201        op.MaximizationParameter.Value = Problem.Maximization;
202      }
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.