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