Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.FixedOperators/3.2/FixedOperatorBase.cs @ 2166

Last change on this file since 2166 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: 7.0 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    /// <summary>
73    /// Executes only the operator op, suboperator are wont be executed.
74    /// </summary>
75    /// <param name="op">Operator to execute.</param>
76    /// <param name="scope">Scope on which operator is executed.</param>
77    /// <returns>True, if operator has suboperators after execution. </returns>
78    protected virtual bool ExecuteFirstOperator(IOperator op, IScope scope){
79      bool suboperatorsExist = false;
80     
81      if (!IsExecuted()) {
82        suboperatorsExist = op.Execute(scope) != null;
83        persistedExecutionPointer.Data++;
84      } // if not executed
85      executionPointer++;
86
87      if (Canceled)
88        throw new CancelException();
89
90      return suboperatorsExist;
91    } // ExecuteFirstOperator
92
93    protected virtual void Execute(IOperator op, IScope scope) {
94      if (!IsExecuted()) {
95        ExecuteOperation(op, scope);
96        persistedExecutionPointer.Data++;
97      } // if not executed
98      executionPointer++;
99
100      if (Canceled)
101        throw new CancelException();
102    } // Execute
103
104    protected void ExecuteOperation(IOperator op, IScope scope) {
105      IOperation operation;
106       if (persistedOperations.Count == 0) {
107        currentOperator = op;
108        operation = op.Execute(scope);
109        if (operation != null) {
110          executionStack.Push(operation);
111        }
112      } else {
113        executionStack = new Stack<IOperation>(persistedOperations);
114        persistedOperations.Clear();
115      }
116
117      while (executionStack.Count > 0) {
118        operation = executionStack.Pop();
119        if (operation is AtomicOperation) {
120          AtomicOperation atomicOperation = (AtomicOperation)operation;
121          IOperation next = null;
122          //try {
123            currentOperator = atomicOperation.Operator;
124            next = currentOperator.Execute(atomicOperation.Scope);
125          //}
126          //catch (Exception ex) {
127          //  throw new InvalidOperationException("Invalid Operation occured in FixedBase.Execute " + ex.InnerException);
128          //}
129          if (next != null)
130            executionStack.Push(next);
131        } else if (operation is CompositeOperation) {
132          CompositeOperation compositeOperation = (CompositeOperation)operation;
133          for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
134            executionStack.Push(compositeOperation.Operations[i]);
135        } // else if
136
137        if (Canceled && executionStack.Count > 0) {
138          SaveExecutionStack(executionStack);
139          throw new CancelException();
140        }
141      } // while
142    } // ExecuteOperation
143
144    private void SaveExecutionStack(Stack<IOperation> stack) {
145      persistedOperations.Clear();
146      persistedOperations.AddRange(stack.ToArray());
147    } // SaveExecutionStack
148
149    public override IOperation Apply(IScope scope) {
150      base.Apply(scope);
151      try {
152        persistedExecutionPointer = scope.GetVariableValue<IntData>("ExecutionPointer", false);
153      }
154      catch (Exception) {
155        persistedExecutionPointer = new IntData(0);
156        scope.AddVariable(new Variable("ExecutionPointer", persistedExecutionPointer));
157      }
158
159      try {
160        persistedOperations = scope.GetVariableValue<ItemList<IOperation>>("ExecutionStack", false);
161      }
162      catch (Exception) {
163        persistedOperations = new ItemList<IOperation>();
164        scope.AddVariable(new Variable("ExecutionStack", persistedOperations));
165      }
166
167      executionPointer = 0;
168
169      for (int i = 0; i < SubOperators.Count; i++) {
170        if (scope.GetVariable(SubOperators[i].Name) != null)
171          scope.RemoveVariable(SubOperators[i].Name);
172        scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
173      }
174
175      return null;
176    } // Apply
177
178    public override void Abort() {
179      base.Abort();
180      currentOperator.Abort();
181    } // Abort
182
183    /// <summary>
184    /// Saves the value of the execution pointers into temp variables
185    /// </summary>
186    protected void SaveExecutionPointer(int level) {
187      tempExePointer[level] = executionPointer;
188      tempPersExePointer[level] = persistedExecutionPointer.Data;
189    } // SaveExecutionPointer
190
191    protected void SetExecutionPointerToLastSaved(int level) {
192      if (executionPointer != persistedExecutionPointer.Data)
193        persistedExecutionPointer.Data = tempPersExePointer[level];
194      else
195        persistedExecutionPointer.Data = tempExePointer[level];
196      executionPointer = tempExePointer[level];
197    } // SetExecutionPointerToLastSaved
198
199
200    protected void ResetExecutionPointer() {
201      executionPointer = 0;
202      persistedExecutionPointer.Data = 0;
203    } // ResetExecutionPointer
204  } // class FixedOperatorBase
205
206  class CancelException : Exception {
207
208  } // class CancelException
209} // namespace HeuristicLab.FixedOperators
Note: See TracBrowser for help on using the repository browser.