Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2498 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
RevLine 
[1693]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;
[1789]26using HeuristicLab.Data;
27using System.Threading;
28using System.Diagnostics;
[1995]29using System.Text;
[2120]30using System.Collections;
[1693]31
32namespace HeuristicLab.FixedOperators {
[1789]33  class FixedOperatorBase : CombinedOperator {
[1995]34    protected ItemList<IOperation> persistedOperations;
35    protected Stack<IOperation> executionStack;
36
[1789]37    /// <summary>
38    /// Execution pointer shows which command actually is executed
39    /// </summary>
40    protected int executionPointer;
[1693]41
[1789]42    /// <summary>
43    /// Execution pointer if execution was aborted previously
44    /// </summary>
45    protected IntData persistedExecutionPointer;
46
[2120]47    protected int[] tempExePointer;
48    protected int[] tempPersExePointer;
[1995]49
[1789]50    /// <summary>
51    /// Current operator in execution.
52    /// </summary>
53    protected IOperator currentOperator;
54
[1995]55    public FixedOperatorBase()
56      : base() {
57      executionStack = new Stack<IOperation>();
[1789]58    } // FixedOperatorBase
59
60    private bool IsExecuted() {
61      return persistedExecutionPointer.Data > executionPointer;
62    } // AlreadyExecuted
63
[1995]64    protected void ExecuteExitable(IOperator op, IScope scope) {
65
[1875]66    } // ExecuteExitable
67
[1995]68    protected void SetRegion(string region) {
69
[1875]70    } // SetRegion
71
[2129]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
[1875]93    protected virtual void Execute(IOperator op, IScope scope) {
[1789]94      if (!IsExecuted()) {
[1875]95        ExecuteOperation(op, scope);
[1789]96        persistedExecutionPointer.Data++;
97      } // if not executed
98      executionPointer++;
99
100      if (Canceled)
101        throw new CancelException();
[1693]102    } // Execute
103
[1789]104    protected void ExecuteOperation(IOperator op, IScope scope) {
105      IOperation operation;
[1995]106       if (persistedOperations.Count == 0) {
107        currentOperator = op;
108        operation = op.Execute(scope);
109        if (operation != null) {
110          executionStack.Push(operation);
111        }
112      } else {
[2129]113        executionStack = new Stack<IOperation>(persistedOperations);
114        persistedOperations.Clear();
[1995]115      }
[1789]116
[1995]117      while (executionStack.Count > 0) {
118        operation = executionStack.Pop();
119        if (operation is AtomicOperation) {
120          AtomicOperation atomicOperation = (AtomicOperation)operation;
121          IOperation next = null;
[2158]122          //try {
[1995]123            currentOperator = atomicOperation.Operator;
124            next = currentOperator.Execute(atomicOperation.Scope);
[2158]125          //}
126          //catch (Exception ex) {
127          //  throw new InvalidOperationException("Invalid Operation occured in FixedBase.Execute " + ex.InnerException);
128          //}
[1995]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
[1789]142    } // ExecuteOperation
143
[1995]144    private void SaveExecutionStack(Stack<IOperation> stack) {
[2129]145      persistedOperations.Clear();
[1995]146      persistedOperations.AddRange(stack.ToArray());
147    } // SaveExecutionStack
148
[1789]149    public override IOperation Apply(IScope scope) {
[1995]150      base.Apply(scope);
[1789]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      }
[1995]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
[1789]167      executionPointer = 0;
[1995]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
[1789]175      return null;
176    } // Apply
177
178    public override void Abort() {
179      base.Abort();
180      currentOperator.Abort();
[1995]181    } // Abort
[1789]182
[1995]183    /// <summary>
184    /// Saves the value of the execution pointers into temp variables
185    /// </summary>
[2120]186    protected void SaveExecutionPointer(int level) {
187      tempExePointer[level] = executionPointer;
188      tempPersExePointer[level] = persistedExecutionPointer.Data;
[1995]189    } // SaveExecutionPointer
[1789]190
[2120]191    protected void SetExecutionPointerToLastSaved(int level) {
[1995]192      if (executionPointer != persistedExecutionPointer.Data)
[2120]193        persistedExecutionPointer.Data = tempPersExePointer[level];
[1995]194      else
[2120]195        persistedExecutionPointer.Data = tempExePointer[level];
196      executionPointer = tempExePointer[level];
[1995]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
[1789]208  } // class CancelException
[1693]209} // namespace HeuristicLab.FixedOperators
Note: See TracBrowser for help on using the repository browser.