Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SGA/3.3/SGA.cs @ 2864

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

Operator architecture refactoring (#95)

  • worked on algorithms
File size: 6.2 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 HeuristicLab.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Evolutionary;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using System;
30
31namespace HeuristicLab.SGA {
32  /// <summary>
33  /// A standard genetic algorithm.
34  /// </summary>
35  [Item("SGA", "A standard genetic algorithm.")]
36  [Creatable("Algorithms")]
37  public sealed class SGA : EngineAlgorithm {
38    [Storable]
39    private PopulationCreator populationCreator;
40    [Storable]
41    private SGAOperator sgaOperator;
42
43    public override Type ProblemType {
44      get { return typeof(ISingleObjectiveProblem); }
45    }
46    public new ISingleObjectiveProblem Problem {
47      get { return (ISingleObjectiveProblem)base.Problem; }
48      set { base.Problem = value; }
49    }
50
51    public SGA()
52      : base() {
53      Parameters.Add(new ValueParameter<IntData>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntData(0)));
54      Parameters.Add(new ValueParameter<BoolData>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolData(true)));
55      Parameters.Add(new ValueParameter<IntData>("PopulationSize", "The size of the population of solutions.", new IntData(100)));
56      Parameters.Add(new OperatorParameter("CrossoverOperator", "The operator used to cross solutions."));
57      Parameters.Add(new ValueParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new DoubleData(0.05)));
58      Parameters.Add(new OperatorParameter("MutationOperator", "The operator used to mutate solutions."));
59      Parameters.Add(new ValueParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation.", new IntData(1)));
60      Parameters.Add(new ValueParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntData(1000)));
61
62      RandomCreator randomCreator = new RandomCreator();
63      populationCreator = new PopulationCreator();
64      sgaOperator = new SGAOperator();
65
66      randomCreator.RandomParameter.ActualName = "Random";
67      randomCreator.SeedParameter.ActualName = "Seed";
68      randomCreator.SeedParameter.Value = null;
69      randomCreator.SetSeedRandomlyParameter.ActualName = "SetSeedRandomly";
70      randomCreator.SetSeedRandomlyParameter.Value = null;
71      randomCreator.Successor = populationCreator;
72
73      populationCreator.PopulationSizeParameter.ActualName = "PopulationSize";
74      populationCreator.PopulationSizeParameter.Value = null;
75      populationCreator.Successor = sgaOperator;
76
77      sgaOperator.CrossoverOperatorParameter.ActualName = "CrossoverOperator";
78      sgaOperator.ElitesParameter.ActualName = "Elites";
79      sgaOperator.MaximumGenerationsParameter.ActualName = "MaximumGenerations";
80      sgaOperator.MutationOperatorParameter.ActualName = "MutationOperator";
81      sgaOperator.MutationProbabilityParameter.ActualName = "MutationProbability";
82      sgaOperator.RandomParameter.ActualName = "Random";
83
84      OperatorGraph.InitialOperator = randomCreator;
85    }
86
87    public override IDeepCloneable Clone(Cloner cloner) {
88      SGA clone = (SGA)base.Clone(cloner);
89      clone.populationCreator = (PopulationCreator)cloner.Clone(populationCreator);
90      clone.sgaOperator = (SGAOperator)cloner.Clone(sgaOperator);
91      return clone;
92    }
93
94    protected override void DeregisterProblemEvents() {
95      Problem.MaximizationChanged -= new EventHandler(Problem_MaximizationChanged);
96      base.DeregisterProblemEvents();
97    }
98    protected override void RegisterProblemEvents() {
99      base.RegisterProblemEvents();
100      Problem.MaximizationChanged += new EventHandler(Problem_MaximizationChanged);
101    }
102
103    protected override void OnProblemChanged() {
104      if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
105      populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
106      populationCreator.SolutionEvaluatorParameter.Value = Problem.Evaluator;
107      sgaOperator.MaximizationParameter.Value = Problem.Maximization;
108      sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
109      sgaOperator.SolutionEvaluatorParameter.Value = Problem.Evaluator;
110      base.OnProblemChanged();
111    }
112    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
113      if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
114      populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
115      base.Problem_SolutionCreatorChanged(sender, e);
116    }
117    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
118      populationCreator.SolutionEvaluatorParameter.Value = Problem.Evaluator;
119      sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
120      sgaOperator.SolutionEvaluatorParameter.Value = Problem.Evaluator;
121      base.Problem_EvaluatorChanged(sender, e);
122    }
123    private void Problem_MaximizationChanged(object sender, EventArgs e) {
124      sgaOperator.MaximizationParameter.Value = Problem.Maximization;
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.