Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2325 was 2150, checked in by dtraxing, 15 years ago

added fixed operator for SteadyStateOSGA
FixedOSGAMain: fixed a type in the name; removed catch block to in Apply method
FixedGAMainBase: add virtual and protected modifier to InitReplacement and DoReplacement
(ticket #580)

File size: 5.1 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    protected IOperator os;
40
41    public FixedOSGAMain()
42      : base() {
43      Name = "FixedOSGAMain";
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      bool loopSkipped = true;
69      bool running = false;
70
71      DoubleData selectionPressure = null;
72      DoubleData selectionPressureLimit = new DoubleData();
73      try {
74        selectionPressureLimit = scope.GetVariableValue<DoubleData>("SelectionPressureLimit", false);
75      }
76      catch (Exception) {
77      }
78
79      #endregion
80
81      tempExePointer = new int[10];
82      tempPersExePointer = new int[10];
83      try {
84        for (; nrOfGenerations.Data < maxGenerations.Data; nrOfGenerations.Data++) {
85          SaveExecutionPointer(0);
86          do {
87            SetExecutionPointerToLastSaved(0);
88            Execute(selector, scope);
89
90            ////// Create Children //////
91            // ChildrenInitializer
92            s = scope.SubScopes[1];
93
94            Execute(ci, s);
95
96            SaveExecutionPointer(1);
97            // UniformSequentialSubScopesProcessor
98            for (; subscopeNr.Data < s.SubScopes.Count; subscopeNr.Data++) {
99              SetExecutionPointerToLastSaved(1);
100
101              s2 = s.SubScopes[subscopeNr.Data];
102              Execute(crossover, s2);
103              // Stochastic Branch
104              Execute(sb, s2);
105              Execute(evaluator, s2);
106              Execute(counter, s2);
107              Execute(wofc, s2);
108              Execute(sr, s2);
109              loopSkipped = false;
110            } // foreach
111
112            // if for loop skipped, we had to add skiped operations
113            // to execution pointer.
114            if (loopSkipped)
115              executionPointer += 5;
116
117            Execute(sorter, s);
118            ////// END Create Children //////
119           
120            running = ExecuteFirstOperator(os, scope);
121            if (running) subscopeNr.Data = 0;
122          } while (running);
123
124          DoReplacement(scope);
125          Execute(ql, scope);
126          Execute(bawqc, scope);
127          Execute(dc, scope);
128          Execute(lci, scope);
129          Execute(selectionPressureDC, scope);
130          Execute(selectionPressureLCI, scope);
131          Console.WriteLine(nrOfGenerations);
132
133          if (selectionPressure == null)
134            selectionPressure = scope.GetVariableValue<DoubleData>("SelectionPressure", false);
135          if (selectionPressure.Data > selectionPressureLimit.Data)
136            break;
137
138          subscopeNr.Data = 0;
139          ResetExecutionPointer();
140        } // for i generations
141      } // try
142      catch (CancelException) {
143        //Console.WriteLine("Micro engine aborted by cancel flag.");
144        return new AtomicOperation(this, scope);
145      }
146
147      return null;
148    } // Apply
149  } // FixedOSGAMain
150} // namespace HeuristicLab.FixedOperators
Note: See TracBrowser for help on using the repository browser.