Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.FixedOperators/3.2/FixedOperatorBase.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: 6.2 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 HeuristicLab.Core;
25using HeuristicLab.Operators;
26using HeuristicLab.Data;
27using System.Threading;
28using System.Diagnostics;
29using System.Text;
30using System.Collections;
31
32namespace HeuristicLab.FixedOperators {
33  class FixedOperatorBase : CombinedOperator {
34    protected ItemList<IOperation> persistedOperations;
35    protected Stack<IOperation> executionStack;
36
37    /// <summary>
38    /// Execution pointer shows which command actually is executed
39    /// </summary>
40    protected int executionPointer;
41
42    /// <summary>
43    /// Execution pointer if execution was aborted previously
44    /// </summary>
45    protected IntData persistedExecutionPointer;
46
47    protected int[] tempExePointer;
48    protected int[] tempPersExePointer;
49
50    /// <summary>
51    /// Current operator in execution.
52    /// </summary>
53    protected IOperator currentOperator;
54
55    public FixedOperatorBase()
56      : base() {
57      executionStack = new Stack<IOperation>();
58    } // FixedOperatorBase
59
60    private bool IsExecuted() {
61      return persistedExecutionPointer.Data > executionPointer;
62    } // AlreadyExecuted
63
64    protected void ExecuteExitable(IOperator op, IScope scope) {
65
66    } // ExecuteExitable
67
68    protected void SetRegion(string region) {
69
70    } // SetRegion
71
72    protected virtual void Execute(IOperator op, IScope scope) {
73      if (!IsExecuted()) {
74        ExecuteOperation(op, scope);
75        persistedExecutionPointer.Data++;
76      } // if not executed
77      executionPointer++;
78
79      if (Canceled)
80        throw new CancelException();
81    } // Execute
82
83    protected void ExecuteOperation(IOperator op, IScope scope) {
84      IOperation operation;
85       if (persistedOperations.Count == 0) {
86        currentOperator = op;
87        operation = op.Execute(scope);
88        if (operation != null) {
89          executionStack.Push(operation);
90        }
91      } else {
92        executionStack = new Stack<IOperation>(persistedOperations); 
93      }
94
95      while (executionStack.Count > 0) {
96        operation = executionStack.Pop();
97        if (operation is AtomicOperation) {
98          AtomicOperation atomicOperation = (AtomicOperation)operation;
99          IOperation next = null;
100          try {
101            currentOperator = atomicOperation.Operator;
102            next = currentOperator.Execute(atomicOperation.Scope);
103          }
104          catch (Exception ex) {
105            throw new InvalidOperationException("Invalid Operation occured in FixedBase.Execute");
106          }
107          if (next != null)
108            executionStack.Push(next);
109        } else if (operation is CompositeOperation) {
110          CompositeOperation compositeOperation = (CompositeOperation)operation;
111          for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
112            executionStack.Push(compositeOperation.Operations[i]);
113        } // else if
114
115        if (Canceled && executionStack.Count > 0) {
116          SaveExecutionStack(executionStack);
117          throw new CancelException();
118        }
119      } // while
120    } // ExecuteOperation
121
122    private void SaveExecutionStack(Stack<IOperation> stack) {
123      persistedOperations = new ItemList<IOperation>();
124      persistedOperations.AddRange(stack.ToArray());
125    } // SaveExecutionStack
126
127    public override IOperation Apply(IScope scope) {
128      base.Apply(scope);
129      try {
130        persistedExecutionPointer = scope.GetVariableValue<IntData>("ExecutionPointer", false);
131      }
132      catch (Exception) {
133        persistedExecutionPointer = new IntData(0);
134        scope.AddVariable(new Variable("ExecutionPointer", persistedExecutionPointer));
135      }
136
137      try {
138        persistedOperations = scope.GetVariableValue<ItemList<IOperation>>("ExecutionStack", false);
139      }
140      catch (Exception) {
141        persistedOperations = new ItemList<IOperation>();
142        scope.AddVariable(new Variable("ExecutionStack", persistedOperations));
143      }
144
145      executionPointer = 0;
146
147      for (int i = 0; i < SubOperators.Count; i++) {
148        if (scope.GetVariable(SubOperators[i].Name) != null)
149          scope.RemoveVariable(SubOperators[i].Name);
150        scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
151      }
152
153      return null;
154    } // Apply
155
156    public override void Abort() {
157      base.Abort();
158      currentOperator.Abort();
159    } // Abort
160
161    /// <summary>
162    /// Saves the value of the execution pointers into temp variables
163    /// </summary>
164    protected void SaveExecutionPointer(int level) {
165      tempExePointer[level] = executionPointer;
166      tempPersExePointer[level] = persistedExecutionPointer.Data;
167    } // SaveExecutionPointer
168
169    protected void SetExecutionPointerToLastSaved(int level) {
170      if (executionPointer != persistedExecutionPointer.Data)
171        persistedExecutionPointer.Data = tempPersExePointer[level];
172      else
173        persistedExecutionPointer.Data = tempExePointer[level];
174      executionPointer = tempExePointer[level];
175    } // SetExecutionPointerToLastSaved
176
177
178    protected void ResetExecutionPointer() {
179      executionPointer = 0;
180      persistedExecutionPointer.Data = 0;
181    } // ResetExecutionPointer
182  } // class FixedOperatorBase
183
184  class CancelException : Exception {
185
186  } // class CancelException
187} // namespace HeuristicLab.FixedOperators
Note: See TracBrowser for help on using the repository browser.