Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on linkage between algorithms and problems (#898)

  • finished TSP and started to work on SGA
File size: 13.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    //#region Private Members
41    //// store operator references in order to be able to access them easily after deserialization
42    //[Storable]
43    //private PopulationCreator populationCreator;
44    //[Storable]
45    //private SGAOperator sgaOperator;
46    //#endregion
47
48    //#region Problem Properties
49    //public override Type ProblemType {
50    //  get { return typeof(ISingleObjectiveProblem); }
51    //}
52    //public new ISingleObjectiveProblem Problem {
53    //  get { return (ISingleObjectiveProblem)base.Problem; }
54    //  set { base.Problem = value; }
55    //}
56    //#endregion
57
58    //#region Parameter Properties
59    //private ValueParameter<IntData> SeedParameter {
60    //  get { return (ValueParameter<IntData>)Parameters["Seed"]; }
61    //}
62    //private ValueParameter<BoolData> SetSeedRandomlyParameter {
63    //  get { return (ValueParameter<BoolData>)Parameters["SetSeedRandomly"]; }
64    //}
65    //private ValueParameter<IntData> PopulationSizeParameter {
66    //  get { return (ValueParameter<IntData>)Parameters["PopulationSize"]; }
67    //}
68    //private ConstrainedValueParameter<ISelector> SelectorParameter {
69    //  get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
70    //}
71    //private ConstrainedValueParameter<ICrossover> CrossoverParameter {
72    //  get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
73    //}
74    //private ValueParameter<DoubleData> MutationProbabilityParameter {
75    //  get { return (ValueParameter<DoubleData>)Parameters["MutationProbability"]; }
76    //}
77    //private OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
78    //  get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
79    //}
80    //private ValueParameter<IntData> ElitesParameter {
81    //  get { return (ValueParameter<IntData>)Parameters["Elites"]; }
82    //}
83    //private ValueParameter<IntData> MaximumGenerationsParameter {
84    //  get { return (ValueParameter<IntData>)Parameters["MaximumGenerations"]; }
85    //}
86    //#endregion
87
88    //#region Properties
89    //public IntData Seed {
90    //  get { return SeedParameter.Value; }
91    //  set { SeedParameter.Value = value; }
92    //}
93    //public BoolData SetSeedRandomly {
94    //  get { return SetSeedRandomlyParameter.Value; }
95    //  set { SetSeedRandomlyParameter.Value = value; }
96    //}
97    //public IntData PopulationSize {
98    //  get { return PopulationSizeParameter.Value; }
99    //  set { PopulationSizeParameter.Value = value; }
100    //}
101    //public ISelector Selector {
102    //  get { return SelectorParameter.Value; }
103    //  set { SelectorParameter.Value = value; }
104    //}
105    //public ICrossover Crossover {
106    //  get { return CrossoverParameter.Value; }
107    //  set { CrossoverParameter.Value = value; }
108    //}
109    //public DoubleData MutationProbability {
110    //  get { return MutationProbabilityParameter.Value; }
111    //  set { MutationProbabilityParameter.Value = value; }
112    //}
113    //public IManipulator Mutator {
114    //  get { return MutatorParameter.Value; }
115    //  set { MutatorParameter.Value = value; }
116    //}
117    //public IntData Elites {
118    //  get { return ElitesParameter.Value; }
119    //  set { ElitesParameter.Value = value; }
120    //}
121    //public IntData MaximumGenerations {
122    //  get { return MaximumGenerationsParameter.Value; }
123    //  set { MaximumGenerationsParameter.Value = value; }
124    //}
125    //#endregion
126
127    //#region Persistence Properties
128    //[Storable]
129    //private object RestoreEvents {
130    //  get { return null; }
131    //  set { RegisterEvents(); }
132    //}
133    //#endregion
134
135    //public SGA()
136    //  : base() {
137    //  Parameters.Add(new ValueParameter<IntData>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntData(0)));
138    //  Parameters.Add(new ValueParameter<BoolData>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolData(true)));
139    //  Parameters.Add(new ValueParameter<IntData>("PopulationSize", "The size of the population of solutions.", new IntData(100)));
140    //  Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
141    //  Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
142    //  Parameters.Add(new ValueParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new DoubleData(0.05)));
143    //  Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
144    //  Parameters.Add(new ValueParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation.", new IntData(1)));
145    //  Parameters.Add(new ValueParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntData(1000)));
146
147    //  RandomCreator randomCreator = new RandomCreator();
148    //  populationCreator = new PopulationCreator();
149    //  sgaOperator = new SGAOperator();
150
151    //  randomCreator.RandomParameter.ActualName = "Random";
152    //  randomCreator.SeedParameter.ActualName = "Seed";
153    //  randomCreator.SeedParameter.Value = null;
154    //  randomCreator.SetSeedRandomlyParameter.ActualName = "SetSeedRandomly";
155    //  randomCreator.SetSeedRandomlyParameter.Value = null;
156    //  randomCreator.Successor = populationCreator;
157
158    //  populationCreator.PopulationSizeParameter.ActualName = "PopulationSize";
159    //  populationCreator.PopulationSizeParameter.Value = null;
160    //  populationCreator.Successor = sgaOperator;
161
162    //  sgaOperator.SelectorParameter.ActualName = "Selector";
163    //  sgaOperator.CrossoverParameter.ActualName = "Crossover";
164    //  sgaOperator.ElitesParameter.ActualName = "Elites";
165    //  sgaOperator.MaximumGenerationsParameter.ActualName = "MaximumGenerations";
166    //  sgaOperator.MutatorParameter.ActualName = "Mutator";
167    //  sgaOperator.MutationProbabilityParameter.ActualName = "MutationProbability";
168    //  sgaOperator.RandomParameter.ActualName = "Random";
169    //  sgaOperator.ResultsParameter.ActualName = "Results";
170
171    //  OperatorGraph.InitialOperator = randomCreator;
172
173    //  if (ApplicationManager.Manager != null) {
174    //    var selectors = ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name);
175    //    foreach (ISelector selector in selectors)
176    //      SelectorParameter.ValidValues.Add(selector);
177    //    ParameterizeSelectors();
178    //  }
179
180    //  RegisterEvents();
181    //}
182
183    //public override IDeepCloneable Clone(Cloner cloner) {
184    //  SGA clone = (SGA)base.Clone(cloner);
185    //  clone.populationCreator = (PopulationCreator)cloner.Clone(populationCreator);
186    //  clone.sgaOperator = (SGAOperator)cloner.Clone(sgaOperator);
187    //  clone.RegisterEvents();
188    //  return clone;
189    //}
190
191    //#region Events
192    //protected override void OnProblemChanged() {
193    //  if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
194    //  if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random";
195    //  foreach (IStochasticOperator op in Problem.Operators.OfType<IStochasticOperator>())
196    //    op.RandomParameter.ActualName = "Random";
197
198    //  populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
199    //  populationCreator.EvaluatorParameter.Value = Problem.Evaluator;
200    //  sgaOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
201    //  sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
202    //  sgaOperator.EvaluatorParameter.Value = Problem.Evaluator;
203
204    //  foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
205    //    op.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
206    //    op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
207    //  }
208
209    //  ICrossover oldCrossover = CrossoverParameter.Value;
210    //  CrossoverParameter.ValidValues.Clear();
211    //  foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
212    //    CrossoverParameter.ValidValues.Add(crossover);
213    //  if (oldCrossover != null) {
214    //    CrossoverParameter.Value = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
215    //  }
216
217    //  IManipulator oldMutator = MutatorParameter.Value;
218    //  MutatorParameter.ValidValues.Clear();
219    //  foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
220    //    MutatorParameter.ValidValues.Add(mutator);
221    //  if (oldMutator != null) {
222    //    MutatorParameter.Value = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
223    //  }
224
225    //  base.OnProblemChanged();
226    //}
227    //protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
228    //  if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
229    //  populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
230    //  base.Problem_SolutionCreatorChanged(sender, e);
231    //}
232    //protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
233    //  if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random";
234
235    //  foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
236    //    op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
237    //  }
238
239    //  populationCreator.EvaluatorParameter.Value = Problem.Evaluator;
240    //  sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
241    //  sgaOperator.EvaluatorParameter.Value = Problem.Evaluator;
242    //  base.Problem_EvaluatorChanged(sender, e);
243    //}
244    //private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
245    //  foreach (ISelector selector in SelectorParameter.ValidValues)
246    //    selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
247    //}
248    //private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
249    //  foreach (ISelector selector in SelectorParameter.ValidValues)
250    //    selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
251    //}
252    //#endregion
253
254    //#region Helpers
255    //private void RegisterEvents() {
256    //  PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
257    //  ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
258    //}
259    //private void ParameterizeSelectors() {
260    //  foreach (ISelector selector in SelectorParameter.ValidValues) {
261    //    selector.CopySelected = new BoolData(true);
262    //    selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
263    //    if (selector is IStochasticOperator) ((IStochasticOperator)selector).RandomParameter.ActualName = "Random";
264    //  }
265    //}
266    //private void ParameterizePopulationCreator() {
267    //  populationCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
268    //  populationCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
269    //}
270    //private void ParameterizeSGAOperator() {
271    //  sgaOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
272    //  sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
273    //  sgaOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
274    //}
275    //private void ParameterizeSolutionCreator() {
276    //}
277    //#endregion
278  }
279}
Note: See TracBrowser for help on using the repository browser.