Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1835 was 1789, checked in by dtraxing, 15 years ago

improved support for algorithm abort. (ticket #580)

File size: 5.7 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;
[1693]29
30namespace HeuristicLab.FixedOperators {
[1789]31  class FixedOperatorBase : CombinedOperator {
[1693]32
[1789]33    private class TasksList {
34      public IOperator op;
35      public IScope scope;
36      public ManualResetEvent resetEvent;
37    }
38    /// <summary>
39    /// Execution pointer shows which command actually is executed
40    /// </summary>
41    protected int executionPointer;
[1693]42
[1789]43    /// <summary>
44    /// Execution pointer if execution was aborted previously
45    /// </summary>
46    protected IntData persistedExecutionPointer;
47
48    /// <summary>
49    /// If true, mini engine is executed in its own thread.
50    /// </summary>
51    protected bool threaded;
52
53    protected Thread engineThread;
54
55    /// <summary>
56    /// Current operator in execution.
57    /// </summary>
58    protected IOperator currentOperator;
59
60    public FixedOperatorBase() : base() {
61      //AddVariableInfo(new VariableInfo("ExecutionPointer", "Execution pointer for algorithm abortion", typeof(IntData), VariableKind.New));   
62    } // FixedOperatorBase
63
64    private bool IsExecuted() {
65      return persistedExecutionPointer.Data > executionPointer;
66    } // AlreadyExecuted
67
68    protected virtual void Execute(IOperator op, IScope scope, bool runInThread) {
69      if (!IsExecuted()) {
70        if (runInThread) {
[1693]71          try {
[1789]72            // causes fatal computing overhead 5:30 minutes for sga
73            //engineThread = new Thread(new ThreadStart(ExecuteOperation));
74            //engineThread.Start();
75            //engineThread.Join();
76
77            // causes computing overhead, 11 sec for sga
78            Stopwatch sw = new Stopwatch();
79            sw.Start();
80            TasksList tl = new TasksList() { op = op, scope = scope, resetEvent = new ManualResetEvent(false) };
81            Console.WriteLine(sw.ElapsedTicks);     
82            ThreadPool.QueueUserWorkItem(new WaitCallback(ExecuteOperationThreaded), (object)tl);
83            Console.WriteLine(sw.ElapsedTicks);
84            WaitHandle.WaitAll(new WaitHandle[] { tl.resetEvent });
85            Console.WriteLine(sw.ElapsedTicks);
[1693]86          }
[1789]87          catch (ThreadAbortException) {
88            return;
[1693]89          }
[1789]90        } else {
91          ExecuteOperation(op, scope);
92        }
93        persistedExecutionPointer.Data++;
94      } // if not executed
95      executionPointer++;
96      Console.WriteLine(executionPointer);
97
98      if (Canceled)
99        throw new CancelException();
[1693]100    } // Execute
101
[1789]102    private void ExecuteOperationThreaded(object o) {
103      TasksList tl = (TasksList)o;
104      ExecuteOperation(tl.op, tl.scope);
105      tl.resetEvent.Set();
106    } // ExecuteOperationThreaded
107
108    protected void ExecuteOperation(IOperator op, IScope scope) {
109      IOperation operation;
110      currentOperator = op;
111      operation = op.Execute(scope);
112      if (operation != null) {
113        //IOperator currentOperator;
114        Stack<IOperation> executionStack = new Stack<IOperation>();
115        executionStack.Push(op.Execute(scope));
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) {
127              throw new InvalidOperationException("Invalid Operation occured in FixedBase.Execute");
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        } // while
137      } // if (operation != null)
138    } // ExecuteOperation
139
140    public override IOperation Apply(IScope scope) {
141      try {
142        persistedExecutionPointer = scope.GetVariableValue<IntData>("ExecutionPointer", false);
143      }
144      catch (Exception) {
145        persistedExecutionPointer = new IntData(0);
146        scope.AddVariable(new Variable("ExecutionPointer", persistedExecutionPointer));
147      }
148     
149      executionPointer = 0;
150      return null;
151    } // Apply
152
153    public override void Abort() {
154      base.Abort();
155      currentOperator.Abort();
156      //engineThread.Abort();
157    }
158
159
160
[1693]161  } // class FixedBase
[1789]162
163  class CancelException : Exception {
164 
165  } // class CancelException
[1693]166} // namespace HeuristicLab.FixedOperators
Note: See TracBrowser for help on using the repository browser.