Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.FixedOperators/3.2/FixedOSGAMain.cs @ 2123

Last change on this file since 2123 was 2120, checked in by dtraxing, 15 years ago

Extracted base class of SGAMain and OSGAMain (ticket #580)

File size: 4.8 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
22#region Using directives
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Evolutionary;
26using HeuristicLab.Logging;
27using HeuristicLab.Operators;
28using HeuristicLab.Selection;
29using System;
30using System.Collections.Generic;
31using System.Linq;
32using System.Text;
33using HeuristicLab.Selection.OffspringSelection;
34#endregion
35
36namespace HeuristicLab.FixedOperators {
37  class FixedOSGAMain : FixedGAMainBase {
38    WeightedOffspringFitnessComparer wofc;
39    OffspringSelector os;
40
41    public FixedOSGAMain()
42      : base() {
43      Name = "FixeOSGAMain";
44      wofc = new WeightedOffspringFitnessComparer();
45      os = new OffspringSelector();
46      os.AddSubOperator(new EmptyOperator());
47    }
48
49    /// <summary>
50    /// Apply
51    /// </summary>
52    public override IOperation Apply(IScope scope) {
53      base.Apply(scope);
54     
55      #region Initialization
56      DataCollector selectionPressureDC = new DataCollector();
57      selectionPressureDC.GetVariableInfo("Values").ActualName = "SelPressValues";
58      ItemList<StringData> names = selectionPressureDC.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
59      names.Add(new StringData("SelectionPressure"));
60
61      LinechartInjector selectionPressureLCI = new LinechartInjector();
62      selectionPressureLCI.GetVariableInfo("Linechart").ActualName = "SelectionPressureLinechart";
63      selectionPressureLCI.GetVariable("NumberOfLines").GetValue<IntData>().Data = 1;
64      selectionPressureLCI.GetVariableInfo("Values").ActualName = "SelPressValues";
65
66      IScope s;
67      IScope s2;
68
69      DoubleData selectionPressure = null;
70      DoubleData selectionPressureLimit = new DoubleData();
71      try {
72        selectionPressureLimit = scope.GetVariableValue<DoubleData>("SelectionPressureLimit", false);
73      }
74      catch (Exception) {
75      }
76
77      #endregion
78
79      tempExePointer = new int[10];
80      tempPersExePointer = new int[10];
81      try {
82        for (; nrOfGenerations.Data < maxGenerations.Data; nrOfGenerations.Data++) {
83          SaveExecutionPointer(0);
84          do {
85            SetExecutionPointerToLastSaved(0);
86            Execute(selector, scope);
87
88            ////// Create Children //////
89            // ChildrenInitializer
90            s = scope.SubScopes[1];
91
92            Execute(ci, s);
93
94            SaveExecutionPointer(1);
95            // UniformSequentialSubScopesProcessor
96            for (; subscopeNr.Data < s.SubScopes.Count; subscopeNr.Data++) {
97              SetExecutionPointerToLastSaved(1);
98
99              s2 = s.SubScopes[subscopeNr.Data];
100              Execute(crossover, s2);
101              // Stochastic Branch
102              Execute(sb, s2);
103              Execute(evaluator, s2);
104              Execute(counter, s2);
105              Execute(wofc, s2);
106              Execute(sr, s2);
107
108            } // foreach
109            subscopeNr.Data = 0;
110
111            Execute(sorter, s);
112            ////// END Create Children //////
113
114
115          } while (os.Execute(scope) != null);
116
117          DoReplacement(scope);
118          Execute(ql, scope);
119          Execute(bawqc, scope);
120          Execute(dc, scope);
121          Execute(lci, scope);
122          Execute(selectionPressureDC, scope);
123          Execute(selectionPressureLCI, scope);
124          Console.WriteLine(nrOfGenerations);
125
126          if (selectionPressure == null)
127            selectionPressure = scope.GetVariableValue<DoubleData>("SelectionPressure", false);
128          if (selectionPressure.Data > selectionPressureLimit.Data)
129            break;
130
131          ResetExecutionPointer();
132        } // for i generations
133      } // try
134      catch (CancelException) {
135        //Console.WriteLine("Micro engine aborted by cancel flag.");
136        return new AtomicOperation(this, scope);
137      }
138      catch (Exception ex) {
139        ex.ToString();
140      }
141
142      return null;
143    } // Apply
144  } // FixedOSGAMain
145} // namespace HeuristicLab.FixedOperators
Note: See TracBrowser for help on using the repository browser.