Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • worked on algorithms, problems and parameters
File size: 10.1 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 OptionalValueParameter<IntData> PopulationSizeParameter {
46      get { return (OptionalValueParameter<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 ConstrainedValueParameter<IManipulator> MutatorParameter {
55      get { return (ConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
56    }
57    private OptionalValueParameter<IntData> ElitesParameter {
58      get { return (OptionalValueParameter<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 OptionalValueParameter<IntData>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntData(0)));
72      Parameters.Add(new OptionalValueParameter<BoolData>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolData(true)));
73      Parameters.Add(new OptionalValueParameter<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 OptionalValueParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new DoubleData(0.05)));
77      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
78      Parameters.Add(new OptionalValueParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation.", new IntData(1)));
79      Parameters.Add(new OptionalValueParameter<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      var selectors = ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector));
111      foreach (ISelector selector in selectors) {
112        selector.CopySelected = new BoolData(true);
113        selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
114        if (selector is IStochasticOperator) ((IStochasticOperator)selector).RandomParameter.ActualName = "Random";
115        SelectorParameter.ValidValues.Add(selector);
116      }
117    }
118
119    public override IDeepCloneable Clone(Cloner cloner) {
120      SGA clone = (SGA)base.Clone(cloner);
121      clone.populationCreator = (PopulationCreator)cloner.Clone(populationCreator);
122      clone.sgaOperator = (SGAOperator)cloner.Clone(sgaOperator);
123      return clone;
124    }
125
126    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
127      foreach (ISelector selector in SelectorParameter.ValidValues)
128        selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
129    }
130    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
131      foreach (ISelector selector in SelectorParameter.ValidValues)
132        selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
133    }
134
135    protected override void DeregisterProblemEvents() {
136      Problem.MaximizationChanged -= new EventHandler(Problem_MaximizationChanged);
137      base.DeregisterProblemEvents();
138    }
139    protected override void RegisterProblemEvents() {
140      base.RegisterProblemEvents();
141      Problem.MaximizationChanged += new EventHandler(Problem_MaximizationChanged);
142    }
143
144    protected override void OnProblemChanged() {
145      if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
146      if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random";
147      foreach (IStochasticOperator op in Problem.Operators.OfType<IStochasticOperator>())
148        op.RandomParameter.ActualName = "Random";
149
150      populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
151      populationCreator.EvaluatorParameter.Value = Problem.Evaluator;
152      sgaOperator.MaximizationParameter.Value = Problem.Maximization;
153      sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
154      sgaOperator.EvaluatorParameter.Value = Problem.Evaluator;
155
156      foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
157        op.MaximizationParameter.Value = Problem.Maximization;
158        op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
159      }
160
161      CrossoverParameter.ValidValues.Clear();
162      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>())
163        CrossoverParameter.ValidValues.Add(crossover);
164
165      MutatorParameter.ValidValues.Clear();
166      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>())
167        MutatorParameter.ValidValues.Add(mutator);
168
169      base.OnProblemChanged();
170    }
171    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
172      if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
173      populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
174      base.Problem_SolutionCreatorChanged(sender, e);
175    }
176    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
177      if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random";
178
179      foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
180        op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
181      }
182
183      populationCreator.EvaluatorParameter.Value = Problem.Evaluator;
184      sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
185      sgaOperator.EvaluatorParameter.Value = Problem.Evaluator;
186      base.Problem_EvaluatorChanged(sender, e);
187    }
188    private void Problem_MaximizationChanged(object sender, EventArgs e) {
189      sgaOperator.MaximizationParameter.Value = Problem.Maximization;
190      foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
191        op.MaximizationParameter.Value = Problem.Maximization;
192      }
193    }
194  }
195}
Note: See TracBrowser for help on using the repository browser.