Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.FixedOperators/3.2/FixedGAMainBase.cs @ 4539

Last change on this file since 4539 was 2158, checked in by dtraxing, 15 years ago

FixedGAMainBase: fixed type in operator name; added virtual modifier to GetOperatorsFromScope Method
FixedOperatorBase: removed general exception catching
added fixed operator for evolution strategies
(ticket #580)

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.Text;
25using System.Linq;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Permutation;
29using HeuristicLab.Evolutionary;
30using HeuristicLab.Operators;
31using HeuristicLab.Routing.TSP;
32using HeuristicLab.Logging;
33using System.Diagnostics;
34using HeuristicLab.Selection;
35using System.Threading;
36using System.IO;
37using HeuristicLab.Random;
38
39namespace HeuristicLab.FixedOperators {
40  /// <summary>
41  /// Fixed GA base
42  /// </summary>
43  class FixedGAMainBase : FixedOperatorBase {
44
45    protected OperatorBase selector;
46    protected Sorter sorter;
47
48    // operators for CreateChildren
49    protected Counter counter;
50    protected ChildrenInitializer ci;
51    protected OperatorBase crossover;
52    protected OperatorBase mutator;
53    protected OperatorBase evaluator;
54    protected SubScopesRemover sr;
55    protected StochasticBranch sb;
56
57    // operators for CreateReplacement
58    protected LeftSelector ls;
59    protected RightReducer rr;
60    protected RightSelector rs;
61    protected LeftReducer lr;
62    protected MergingReducer mr;
63   
64    protected QualityLogger ql;
65    protected BestAverageWorstQualityCalculator bawqc;
66    protected DataCollector dc;
67    protected LinechartInjector lci;
68    protected IntData maxGenerations;
69    protected IntData nrOfGenerations;
70    protected IntData subscopeNr;
71
72    public FixedGAMainBase() : base() {
73      AddVariableInfo(new VariableInfo("Selector", "Selection strategy for SGA", typeof(OperatorBase), VariableKind.In));
74      AddVariableInfo(new VariableInfo("MaximumGenerations", "Maximum number of generations to create", typeof(IntData), VariableKind.In));
75      AddVariableInfo(new VariableInfo("Generations", "Number of processed generations", typeof(IntData), VariableKind.In | VariableKind.Out));                                               
76
77      sorter = new Sorter();
78      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
79      sorter.GetVariableInfo("Value").ActualName = "Quality";
80
81      InitCreateChildren();
82      InitReplacement();
83
84      sb = new StochasticBranch();
85      sb.GetVariableInfo("Probability").ActualName = "MutationRate";
86      Name = "FixedGAMain";
87    } // FixedGABase
88
89    protected virtual void InitReplacement() {
90      ls = new LeftSelector();
91      rr = new RightReducer();
92      rs = new RightSelector();
93      lr = new LeftReducer();
94      mr = new MergingReducer();
95
96      ls.GetVariableInfo("Selected").ActualName = "Elites";
97      rs.GetVariableInfo("Selected").ActualName = "Elites";
98    }
99
100    private void InitCreateChildren() {
101      // variables for create children
102      ci = new ChildrenInitializer();
103
104      // variables infos
105      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
106      AddVariableInfo(new VariableInfo("MutationRate", "Probability to choose first branch", typeof(DoubleData), VariableKind.In));
107      AddVariableInfo(new VariableInfo("Crossover", "Crossover strategy for SGA", typeof(OperatorBase), VariableKind.In));
108      AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In));
109      AddVariableInfo(new VariableInfo("Evaluator", "Evaluation strategy for SGA", typeof(OperatorBase), VariableKind.In));
110
111      sr = new SubScopesRemover();
112      sr.GetVariableInfo("SubScopeIndex").Local = true;
113
114      counter = new Counter();
115      counter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
116    }
117
118    public override IOperation Apply(IScope scope) {
119      base.Apply(scope);
120
121      #region Initialization
122      ql = new QualityLogger();
123
124      bawqc = new BestAverageWorstQualityCalculator();
125      dc = new DataCollector();
126      ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
127      names.Add(new StringData("BestQuality"));
128      names.Add(new StringData("AverageQuality"));
129      names.Add(new StringData("WorstQuality"));
130
131      lci = new LinechartInjector();
132      lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
133      lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
134
135      maxGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true);
136      nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true);
137
138     
139      try {
140        subscopeNr = scope.GetVariableValue<IntData>("SubScopeNr", false);
141      }
142      catch (Exception) {
143        subscopeNr = new IntData(0);
144        scope.AddVariable(new Variable("SubScopeNr", subscopeNr));
145      }
146
147      GetOperatorsFromScope(scope);
148
149      try {
150        sb.RemoveSubOperator(0);
151      }
152      catch (Exception) {
153      }
154      sb.AddSubOperator(mutator);
155      #endregion
156     
157      return null;
158    }
159
160    /// <summary>
161    /// Fetch main operators like selector, crossover, mutator, ... from scope
162    /// and store them in instance variables.
163    /// </summary>
164    /// <param name="scope"></param>
165    protected virtual void GetOperatorsFromScope(IScope scope) {
166      selector = (OperatorBase)GetVariableValue("Selector", scope, true);
167      crossover = (OperatorBase)GetVariableValue("Crossover", scope, true);
168      mutator = (OperatorBase)GetVariableValue("Mutator", scope, true);
169      evaluator = GetVariableValue<OperatorBase>("Evaluator", scope, true);
170    } // GetOperatorsFromScope
171
172    protected virtual void DoReplacement(IScope scope) {
173      //// SequentialSubScopesProcessor
174      Execute(ls, scope.SubScopes[0]);
175      Execute(rr, scope.SubScopes[0]);
176
177      Execute(rs, scope.SubScopes[1]);
178      Execute(lr, scope.SubScopes[1]);
179
180      Execute(mr, scope);
181      Execute(sorter, scope);
182
183    } // DoReplacement
184 
185  } // class FixedGABase
186} // namespace HeuristicLab.FixedOperators 
Note: See TracBrowser for help on using the repository browser.