- Timestamp:
- 05/13/09 16:39:11 (15 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.FixedOperators/3.2/FixedOperatorBase.cs
r1737 r1789 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 using System.Text;26 24 using HeuristicLab.Core; 27 25 using HeuristicLab.Operators; 26 using HeuristicLab.Data; 27 using System.Threading; 28 using System.Diagnostics; 28 29 29 30 namespace HeuristicLab.FixedOperators { 30 class Fixed Base : CombinedOperator {31 class FixedOperatorBase : CombinedOperator { 31 32 32 protected virtual void Execute(IOperator op, IScope scope){ 33 IOperation operation; 34 IOperator currentOperator; 35 Stack<IOperation> executionStack = new Stack<IOperation>(); 36 executionStack.Push(op.Execute(scope)); 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; 37 42 38 while (executionStack.Count > 0) { 39 operation = executionStack.Pop(); 40 if (operation is AtomicOperation) { 41 AtomicOperation atomicOperation = (AtomicOperation)operation; 42 IOperation next = null; 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) { 43 71 try { 44 currentOperator = atomicOperation.Operator; 45 next = atomicOperation.Operator.Execute(atomicOperation.Scope); 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); 46 86 } 47 catch (Exception) { 48 //Abort(); 49 //ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); }); 50 throw new InvalidOperationException("Invalid Operation occured in FixedBase.Execute"); 87 catch (ThreadAbortException) { 88 return; 51 89 } 52 if (next != null) 53 executionStack.Push(next); 54 } else if (operation is CompositeOperation) { 55 CompositeOperation compositeOperation = (CompositeOperation)operation; 56 for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--) 57 executionStack.Push(compositeOperation.Operations[i]); 58 } // else if 59 } // while 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(); 60 100 } // Execute 61 101 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 62 161 } // class FixedBase 162 163 class CancelException : Exception { 164 165 } // class CancelException 63 166 } // namespace HeuristicLab.FixedOperators
Note: See TracChangeset
for help on using the changeset viewer.