Changeset 1789
- Timestamp:
- 05/13/09 16:39:11 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.FixedOperators/3.2
- Files:
-
- 2 edited
- 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 -
trunk/sources/HeuristicLab.FixedOperators/3.2/FixedSGAMain.cs
r1731 r1789 33 33 using System.Diagnostics; 34 34 using HeuristicLab.Selection; 35 using System.Threading; 35 36 36 37 namespace HeuristicLab.FixedOperators { 37 class FixedSGAMain : Fixed Base {38 class FixedSGAMain : FixedOperatorBase { 38 39 public override string Description { 39 40 get { return @"Implements the functionality of SGAMain with fixed control structures. Operators like selection, crossover, mutation and evaluation are delegated."; } … … 60 61 MergingReducer mr; 61 62 63 Thread executionThread; 64 Thread cancelThread; 65 62 66 63 67 //long[] timesExecuteCreateChildren; 64 65 68 public FixedSGAMain() 66 69 : base() { … … 68 71 AddVariableInfo(new VariableInfo("MaximumGenerations", "Maximum number of generations to create", typeof(IntData), VariableKind.In)); 69 72 AddVariableInfo(new VariableInfo("Generations", "Number of processed generations", typeof(IntData), VariableKind.In | VariableKind.Out)); 70 AddVariableInfo(new VariableInfo("ExecutionPointer", "Execution pointer for algorithm abortion", typeof(IntData), VariableKind.New));71 73 72 74 Name = "FixedSGAMain"; … … 111 113 112 114 public override IOperation Apply(IScope scope) { 113 Stopwatch swApply = new Stopwatch(); 114 swApply.Start(); 115 for (int i = 0; i < SubOperators.Count; i++) { 116 if (scope.GetVariable(SubOperators[i].Name) != null) 117 scope.RemoveVariable(SubOperators[i].Name); 118 scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i])); 115 base.Apply(scope); 116 object o = (object)scope; 117 WorkerMethod(o); 118 //try { 119 // executionThread = new Thread(new ParameterizedThreadStart(WorkerMethod)); 120 // executionThread.Name = "ExecutionThread"; 121 // executionThread.Start(o); 122 123 // //cancelThread = new Thread(new ThreadStart(CheckCancelFlag)); 124 // //cancelThread.Name = "CancelFlagCheckerThread"; 125 // //cancelThread.Start(); 126 127 // executionThread.Join(); 128 //} 129 //catch (ThreadAbortException) { 130 // return new AtomicOperation(this, scope); 131 //} 132 133 if (Canceled) { 134 return new AtomicOperation(this, scope); 119 135 } 120 136 121 OperatorBase selector = (OperatorBase)GetVariableValue("Selector", scope, true); 122 QualityLogger ql = new QualityLogger(); 123 124 BestAverageWorstQualityCalculator bawqc = new BestAverageWorstQualityCalculator(); 125 DataCollector dc = new DataCollector(); 126 ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>(); 127 names.Add(new StringData("BestQuality")); 128 names.Add(new StringData("AverageQuality")); 129 names.Add(new StringData("WorstQuality")); 130 131 LinechartInjector lci = new LinechartInjector(); 132 lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart"; 133 lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3; 134 135 LessThanComparator ltc = new LessThanComparator(); 136 ltc.GetVariableInfo("LeftSide").ActualName = "Generations"; 137 ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations"; 138 ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition"; 139 140 IntData maxGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true); 141 IntData nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true); 142 143 144 // executionpointer saves the reentry point after engine abort occurs. 145 IntData executionPointer; 137 return null; 138 } // Apply 139 140 private void CheckCancelFlag() { 141 while (executionThread.IsAlive) { 142 if (Canceled) { 143 executionThread.Abort(); 144 return; 145 } 146 Thread.Sleep(500); 147 } // while 148 } // CheckCancelFlag 149 150 private void WorkerMethod(object o) { 146 151 try { 147 executionPointer = GetVariableValue<IntData>("ExecutionPointer", scope, true); 152 IScope scope = o as IScope; 153 Stopwatch swApply = new Stopwatch(); 154 swApply.Start(); 155 for (int i = 0; i < SubOperators.Count; i++) { 156 if (scope.GetVariable(SubOperators[i].Name) != null) 157 scope.RemoveVariable(SubOperators[i].Name); 158 scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i])); 159 } 160 161 OperatorBase selector = (OperatorBase)GetVariableValue("Selector", scope, true); 162 QualityLogger ql = new QualityLogger(); 163 164 BestAverageWorstQualityCalculator bawqc = new BestAverageWorstQualityCalculator(); 165 DataCollector dc = new DataCollector(); 166 ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>(); 167 names.Add(new StringData("BestQuality")); 168 names.Add(new StringData("AverageQuality")); 169 names.Add(new StringData("WorstQuality")); 170 171 LinechartInjector lci = new LinechartInjector(); 172 lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart"; 173 lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3; 174 175 LessThanComparator ltc = new LessThanComparator(); 176 ltc.GetVariableInfo("LeftSide").ActualName = "Generations"; 177 ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations"; 178 ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition"; 179 180 IntData maxGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true); 181 IntData nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true); 182 nrOfGenerations.Data = 0; 183 184 threaded = false; 185 // fetch variables from scope for create children 186 InitializeExecuteCreateChildren(scope); 187 try { 188 //for (int i = nrOfGenerations.Data; i < maxGenerations.Data; i++) { 189 // Execute(selector, scope, false); 190 // ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]); 191 // ExecuteCreateReplacementWithFixedConstrolStructures(scope); 192 // ql.Execute(scope); 193 // bawqc.Execute(scope); 194 // dc.Execute(scope); 195 // lci.Execute(scope); 196 // nrOfGenerations.Data++; 197 //} // for i 198 for (int i = 0; i < maxGenerations.Data; i++) { 199 Execute(selector, scope, false); 200 ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]); 201 ExecuteCreateReplacementWithFixedConstrolStructures(scope); 202 Execute(ql, scope, false); 203 Execute(bawqc, scope, false); 204 Execute(dc, scope, false); 205 Execute(lci, scope, false); 206 nrOfGenerations.Data++; 207 } // for i 208 } // try 209 catch (CancelException) { 210 Console.WriteLine("Micro engine aborted by cancel flag."); 211 } 212 213 //for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) { 214 // if (Canceled) { 215 // executionPointer.Data = -1; 216 // continue; 217 // } 218 // if (executionPointer.Data < 0) 219 // Execute(selector, scope); 220 221 // if (Canceled) { 222 // executionPointer.Data = 0; 223 // continue; 224 // } 225 // if (executionPointer.Data < 1) 226 // ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]); 227 228 // if (Canceled) { 229 // executionPointer.Data = 1; 230 // continue; 231 // } 232 // if (executionPointer.Data < 2) 233 // ExecuteCreateReplacementWithFixedConstrolStructures(scope); 234 235 // if (Canceled) { 236 // executionPointer.Data = 2; 237 // continue; 238 // } 239 // if (executionPointer.Data < 3) { 240 // ql.Execute(scope); 241 // bawqc.Execute(scope); 242 // dc.Execute(scope); 243 // lci.Execute(scope); 244 // nrOfGenerations.Data++; 245 // } 246 // executionPointer.Data = -1; 247 //} // for i 248 249 250 //Stopwatch watch = new Stopwatch(); 251 //long[] times = new long[10]; 252 //timesExecuteCreateChildren = new long[10]; 253 //for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) { 254 // watch.Start(); 255 // selector.Execute(scope); 256 // watch.Stop(); 257 // times[0] += watch.ElapsedTicks; 258 // watch.Reset(); 259 // watch.Start(); 260 // ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]); 261 // watch.Stop(); 262 // times[1] += watch.ElapsedTicks; 263 // watch.Reset(); 264 // watch.Start(); 265 // ExecuteCreateReplacementWithFixedConstrolStructures(scope); 266 // watch.Stop(); 267 // times[2] += watch.ElapsedTicks; 268 // watch.Reset(); 269 // watch.Start(); 270 // ql.Execute(scope); 271 // watch.Stop(); 272 // times[3] += watch.ElapsedTicks; 273 // watch.Reset(); 274 // watch.Start(); 275 // bawqc.Execute(scope); 276 // watch.Stop(); 277 // times[4] += watch.ElapsedTicks; 278 // watch.Reset(); 279 // watch.Start(); 280 // dc.Execute(scope); 281 // watch.Stop(); 282 // times[5] += watch.ElapsedTicks; 283 // watch.Reset(); 284 // watch.Start(); 285 // lci.Execute(scope); 286 // watch.Stop(); 287 // times[6] += watch.ElapsedTicks; 288 // watch.Reset(); 289 // watch.Start(); 290 // nrOfGenerations.Data++; 291 //} 292 293 swApply.Stop(); 294 Console.WriteLine("SGAMain.Apply(): {0}", swApply.Elapsed); 295 296 //if (Canceled) { 297 // return new AtomicOperation(this, scope); 298 //} 299 300 //return null; 301 148 302 } 149 catch ( Exception) {150 scope.AddVariable(new Variable("ExecutionPointer", new IntData(-1))); 151 executionPointer = GetVariableValue<IntData>("ExecutionPointer", scope, true);303 catch (ThreadAbortException) { 304 305 throw; 152 306 } 153 154 // fetch variables from scope for create children155 InitializeExecuteCreateChildren(scope);156 for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) {157 if (Canceled) {158 executionPointer.Data = -1;159 continue;160 }161 if (executionPointer.Data < 0)162 Execute(selector, scope);163 164 if (Canceled) {165 executionPointer.Data = 0;166 continue;167 }168 if (executionPointer.Data < 1)169 ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);170 171 if (Canceled) {172 executionPointer.Data = 1;173 continue;174 }175 if (executionPointer.Data < 2)176 ExecuteCreateReplacementWithFixedConstrolStructures(scope);177 178 if (Canceled) {179 executionPointer.Data = 2;180 continue;181 }182 if (executionPointer.Data < 3) {183 ql.Execute(scope);184 bawqc.Execute(scope);185 dc.Execute(scope);186 lci.Execute(scope);187 nrOfGenerations.Data++;188 }189 executionPointer.Data = -1;190 } // for i191 192 //Stopwatch watch = new Stopwatch();193 //long[] times = new long[10];194 //timesExecuteCreateChildren = new long[10];195 //for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) {196 // watch.Start();197 // selector.Execute(scope);198 // watch.Stop();199 // times[0] += watch.ElapsedTicks;200 // watch.Reset();201 // watch.Start();202 // ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);203 // watch.Stop();204 // times[1] += watch.ElapsedTicks;205 // watch.Reset();206 // watch.Start();207 // ExecuteCreateReplacementWithFixedConstrolStructures(scope);208 // watch.Stop();209 // times[2] += watch.ElapsedTicks;210 // watch.Reset();211 // watch.Start();212 // ql.Execute(scope);213 // watch.Stop();214 // times[3] += watch.ElapsedTicks;215 // watch.Reset();216 // watch.Start();217 // bawqc.Execute(scope);218 // watch.Stop();219 // times[4] += watch.ElapsedTicks;220 // watch.Reset();221 // watch.Start();222 // dc.Execute(scope);223 // watch.Stop();224 // times[5] += watch.ElapsedTicks;225 // watch.Reset();226 // watch.Start();227 // lci.Execute(scope);228 // watch.Stop();229 // times[6] += watch.ElapsedTicks;230 // watch.Reset();231 // watch.Start();232 // nrOfGenerations.Data++;233 //}234 235 swApply.Stop();236 Console.WriteLine("SGAMain.Apply(): {0}", swApply.Elapsed);237 238 if (Canceled)239 return new AtomicOperation(this, scope);240 241 return null;242 307 } // Apply 243 308 … … 264 329 /// <param name="scope"></param> 265 330 protected void ExecuteCreateChildrenWithFixedControlStructures(IScope scope) { 331 //// ChildrenInitializer 332 //ci.Apply(scope); 333 //// UniformSequentialSubScopesProcessor 334 //foreach (IScope s in scope.SubScopes) { 335 // Execute(crossover, s, false); 336 // // Stochastic Branch 337 // if (random.NextDouble() < probability.Data) 338 // Execute(mutator, s, false); 339 // Execute(evaluator, s, false); 340 // sr.Execute(s); 341 // counter.Execute(s); 342 //} // foreach 343 344 //sorter.Execute(scope); 345 346 EmptyOperator empty = new EmptyOperator(); 347 266 348 // ChildrenInitializer 267 ci.Apply(scope);349 Execute(ci, scope, false); 268 350 // UniformSequentialSubScopesProcessor 269 351 foreach (IScope s in scope.SubScopes) { 270 Execute(crossover, s );352 Execute(crossover, s, false); 271 353 // Stochastic Branch 272 354 if (random.NextDouble() < probability.Data) 273 Execute(mutator, s); 274 Execute(evaluator, s); 275 sr.Execute(s); 276 counter.Execute(s); 355 Execute(mutator, s, false); 356 else 357 Execute(empty, s, false); 358 Execute(evaluator, s, false); 359 Execute(sr, s, false); 360 Execute(counter, s, false); 277 361 } // foreach 278 362 279 sorter.Execute(scope);363 Execute(sorter, scope, false); 280 364 } // ExecuteCreateChildrenHWCS 281 365 282 366 private void ExecuteCreateReplacementWithFixedConstrolStructures(IScope scope) { 283 // SequentialSubScopesProcessor 284 ls.Execute(scope.SubScopes[0]); 285 rr.Execute(scope.SubScopes[0]); 286 287 rs.Execute(scope.SubScopes[1]); 288 lr.Execute(scope.SubScopes[1]); 289 290 mr.Execute(scope); 291 sorter.Execute(scope); 367 //// SequentialSubScopesProcessor 368 //ls.Execute(scope.SubScopes[0]); 369 //rr.Execute(scope.SubScopes[0]); 370 371 //rs.Execute(scope.SubScopes[1]); 372 //lr.Execute(scope.SubScopes[1]); 373 374 //mr.Execute(scope); 375 //sorter.Execute(scope); 376 377 Execute(ls, scope.SubScopes[0], false); 378 Execute(rr, scope.SubScopes[0], false); 379 380 Execute(rs, scope.SubScopes[1], false); 381 Execute(lr, scope.SubScopes[1], false); 382 383 Execute(mr, scope, false); 384 Execute(sorter, scope, false); 385 292 386 } // ExecuteCreateReplacementWithFixedConstrolStructures 387 388 //public override void Abort() { 389 // base.Abort(); 390 // executionThread.Abort(); 391 //} // Abort 392 293 393 } // class FixedSGAMain 294 394 } // namespace HeuristicLab.FixedOperators -
trunk/sources/HeuristicLab.FixedOperators/3.2/HeuristicLab.FixedOperators-3.2.csproj
r1739 r1789 100 100 </ItemGroup> 101 101 <ItemGroup> 102 <Compile Include="FixedAllSGAMain.cs" /> 103 <Compile Include="CreateReplacement.cs" /> 104 <Compile Include="CreateChildrenHardWired.cs" /> 105 <Compile Include="FixedBase.cs" /> 102 <Compile Include="FixedOperatorBase.cs" /> 106 103 <Compile Include="FixedSGAMain.cs" /> 107 104 <Compile Include="HeuristicLabFixedOperatorsPlugin.cs" /> 108 105 <Compile Include="Properties\AssemblyInfo.cs" /> 109 <Compile Include="CreateChildren.cs" />110 106 </ItemGroup> 111 107 <ItemGroup>
Note: See TracChangeset
for help on using the changeset viewer.