Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2884 was 2884, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • renamed HeuristicLab.SGA to HeuristicLab.Algorithms.SGA
File size: 9.8 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 ConstrainedValueParameter<IManipulator> MutatorParameter {
55      get { return (ConstrainedValueParameter<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 ConstrainedValueParameter<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      var selectors = ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector));
111      selectors.Select(x => x.CopySelected = new BoolData(true));
112      selectors.Select(x => x.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value)));
113      selectors.OfType<IStochasticOperator>().Select(x => x.RandomParameter.ActualName = "Random");
114      foreach (ISelector selector in selectors)
115        SelectorParameter.ValidValues.Add(selector);
116    }
117
118    public override IDeepCloneable Clone(Cloner cloner) {
119      SGA clone = (SGA)base.Clone(cloner);
120      clone.populationCreator = (PopulationCreator)cloner.Clone(populationCreator);
121      clone.sgaOperator = (SGAOperator)cloner.Clone(sgaOperator);
122      return clone;
123    }
124
125    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
126      SelectorParameter.ValidValues.Select(x => x.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value)));
127    }
128    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
129      SelectorParameter.ValidValues.Select(x => x.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value)));
130    }
131
132    protected override void DeregisterProblemEvents() {
133      Problem.MaximizationChanged -= new EventHandler(Problem_MaximizationChanged);
134      base.DeregisterProblemEvents();
135    }
136    protected override void RegisterProblemEvents() {
137      base.RegisterProblemEvents();
138      Problem.MaximizationChanged += new EventHandler(Problem_MaximizationChanged);
139    }
140
141    protected override void OnProblemChanged() {
142      if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
143      if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random";
144      Problem.Operators.OfType<IStochasticOperator>().Select(x => x.RandomParameter.ActualName = "Random");
145
146      populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
147      populationCreator.EvaluatorParameter.Value = Problem.Evaluator;
148      sgaOperator.MaximizationParameter.Value = Problem.Maximization;
149      sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
150      sgaOperator.EvaluatorParameter.Value = Problem.Evaluator;
151
152      SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>().Select(x => x.MaximizationParameter.Value = Problem.Maximization);
153      SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>().Select(x => x.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName);
154
155      CrossoverParameter.ValidValues.Clear();
156      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>())
157        CrossoverParameter.ValidValues.Add(crossover);
158
159      MutatorParameter.ValidValues.Clear();
160      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>())
161        MutatorParameter.ValidValues.Add(mutator);
162
163      base.OnProblemChanged();
164    }
165    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
166      if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
167      populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
168      base.Problem_SolutionCreatorChanged(sender, e);
169    }
170    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
171      if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random";
172      SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>().Select(x => x.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName);
173      populationCreator.EvaluatorParameter.Value = Problem.Evaluator;
174      sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
175      sgaOperator.EvaluatorParameter.Value = Problem.Evaluator;
176      base.Problem_EvaluatorChanged(sender, e);
177    }
178    private void Problem_MaximizationChanged(object sender, EventArgs e) {
179      sgaOperator.MaximizationParameter.Value = Problem.Maximization;
180      SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>().Select(x => x.MaximizationParameter.Value = Problem.Maximization);
181    }
182  }
183}
Note: See TracBrowser for help on using the repository browser.