[1875] | 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 |
|
---|
| 22 | //using System;
|
---|
| 23 | //using System.Collections.Generic;
|
---|
| 24 | //using HeuristicLab.Core;
|
---|
| 25 | //using HeuristicLab.Operators;
|
---|
| 26 | //using HeuristicLab.Data;
|
---|
| 27 | //using System.Threading;
|
---|
| 28 | //using System.Diagnostics;
|
---|
| 29 |
|
---|
| 30 | //namespace HeuristicLab.FixedOperators {
|
---|
| 31 | // class FixedOperatorBase : CombinedOperator {
|
---|
| 32 | // protected class SubScopesProcessor : OperatorBase {
|
---|
| 33 | // public SubScopesProcessor(VirtualScope scope) {
|
---|
| 34 | // this.scope = scope;
|
---|
| 35 | // }
|
---|
| 36 | // private VirtualScope scope;
|
---|
| 37 | // public IList<IOperator> operators = new List<IOperator>();
|
---|
| 38 | // public void AddOperator(IOperator op) {
|
---|
| 39 | // operators.Add(op);
|
---|
| 40 | // }
|
---|
| 41 |
|
---|
| 42 | // public void Unwind(Queue<KeyValuePair<IOperator, VirtualScope>> list, int currentOpIdx) {
|
---|
| 43 | // int i = 0;
|
---|
| 44 | // //for(int idx = 0; idx < scope.Scope.SubScopes.Count; idx++){
|
---|
| 45 | // // foreach (IOperator op in operators) {
|
---|
| 46 | // // list.Insert(currentOpIdx+i, new KeyValuePair<IOperator, VirtualScope>(op, new VirtualScope(scope.Scope.SubScopes[idx])));
|
---|
| 47 | // // i++;
|
---|
| 48 | // // } // foreach
|
---|
| 49 | // //} // for idx
|
---|
| 50 |
|
---|
| 51 | // //for (int idx = scope.Scope.SubScopes.Count - 1; idx >= 0 ; idx--) {
|
---|
| 52 | // // for (int y = operators.Count - 1; y >= 0; y-- ) {
|
---|
| 53 | // // list.Insert(currentOpIdx, new KeyValuePair<IOperator, VirtualScope>(operators[y], new VirtualScope(scope.Scope.SubScopes[idx])));
|
---|
| 54 | // // } // foreach
|
---|
| 55 | // //} // for idx
|
---|
| 56 |
|
---|
| 57 | // for (int idx = 0; idx < scope.Scope.SubScopes.Count; idx++) {
|
---|
| 58 | // foreach (IOperator op in operators) {
|
---|
| 59 | // list.Enqueue(new KeyValuePair<IOperator, VirtualScope>(op, new VirtualScope(scope.Scope.SubScopes[idx])));
|
---|
| 60 | // i++;
|
---|
| 61 | // } // foreach
|
---|
| 62 | // } // for idx
|
---|
| 63 | // }
|
---|
| 64 | // } // ForLoop
|
---|
| 65 |
|
---|
| 66 | // protected class VirtualScope {
|
---|
| 67 | // public VirtualScope(VirtualScope parent, int subscopeIndex) {
|
---|
| 68 | // this.parent = parent;
|
---|
| 69 | // this.subscope = true;
|
---|
| 70 | // this.subscopeIndex = subscopeIndex;
|
---|
| 71 | // }
|
---|
| 72 |
|
---|
| 73 | // public VirtualScope(IScope scope, int subscopeIndex) {
|
---|
| 74 | // this.scope = scope;
|
---|
| 75 | // this.subscope = true;
|
---|
| 76 | // this.subscopeIndex = subscopeIndex;
|
---|
| 77 | // }
|
---|
| 78 |
|
---|
| 79 | // public VirtualScope(IScope scope) {
|
---|
| 80 | // this.scope = scope;
|
---|
| 81 | // }
|
---|
| 82 | // private IScope scope;
|
---|
| 83 | // private bool subscope;
|
---|
| 84 | // private int subscopeIndex;
|
---|
| 85 | // public IScope Scope {
|
---|
| 86 | // get { return getScope(); }
|
---|
| 87 | // }
|
---|
| 88 | // private IScope getScope() {
|
---|
| 89 | // IScope s;
|
---|
| 90 | // if (parent != null)
|
---|
| 91 | // s = parent.getScope();
|
---|
| 92 | // else
|
---|
| 93 | // s = scope;
|
---|
| 94 |
|
---|
| 95 | // if (subscope)
|
---|
| 96 | // return s.SubScopes[subscopeIndex];
|
---|
| 97 | // else
|
---|
| 98 | // return scope;
|
---|
| 99 | // }
|
---|
| 100 |
|
---|
| 101 | // private VirtualScope parent;
|
---|
| 102 |
|
---|
| 103 | // //public static implicit operator Scope(ScopeDescriptor s) {
|
---|
| 104 | // // return (Scope)s.getScope();
|
---|
| 105 | // //}
|
---|
| 106 | // }
|
---|
| 107 | // /// <summary>
|
---|
| 108 | // /// true if algorithm is in execution
|
---|
| 109 | // /// </summary>
|
---|
| 110 | // protected BoolData inExecution;
|
---|
| 111 |
|
---|
| 112 | // /// <summary>
|
---|
| 113 | // /// Current operator in execution.
|
---|
| 114 | // /// </summary>
|
---|
| 115 | // protected IOperator currentOperator;
|
---|
| 116 |
|
---|
| 117 | // protected IList<KeyValuePair<IOperator, VirtualScope>> operators;
|
---|
| 118 | // protected Queue<KeyValuePair<IOperator, VirtualScope>> operatorsQueue;
|
---|
| 119 |
|
---|
| 120 | // public FixedOperatorBase() : base() {
|
---|
| 121 | // operators = new List<KeyValuePair<IOperator, VirtualScope>>();
|
---|
| 122 | // operatorsQueue = new Queue<KeyValuePair<IOperator, VirtualScope>>();
|
---|
| 123 | // //AddVariableInfo(new VariableInfo("ExecutionPointer", "Execution pointer for algorithm abortion", typeof(IntData), VariableKind.New));
|
---|
| 124 | // } // FixedOperatorBase
|
---|
| 125 |
|
---|
| 126 | // protected void Execute() {
|
---|
| 127 | // inExecution.Data = true;
|
---|
| 128 | // KeyValuePair<IOperator, VirtualScope> ao;
|
---|
| 129 | // int currentOpIdx = 0;
|
---|
| 130 | // while (currentOpIdx < operators.Count && !Canceled) {
|
---|
| 131 | // //ao = operators.Dequeue();
|
---|
| 132 | // if (operatorsQueue.Count > 0) {
|
---|
| 133 | // ao = operatorsQueue.Dequeue();
|
---|
| 134 | // } else {
|
---|
| 135 | // ao = operators[currentOpIdx];
|
---|
| 136 | // currentOpIdx++;
|
---|
| 137 | // }
|
---|
| 138 | // if (ao.Key is SubScopesProcessor) {
|
---|
| 139 | // SubScopesProcessor sp = (SubScopesProcessor)ao.Key;
|
---|
| 140 | // sp.Unwind(operatorsQueue, currentOpIdx);
|
---|
| 141 | // } else
|
---|
| 142 | // ExecuteOperation(ao.Key, ao.Value.Scope);
|
---|
| 143 | // } // while
|
---|
| 144 |
|
---|
| 145 | // if (operators.Count == 0)
|
---|
| 146 | // inExecution.Data = false;
|
---|
| 147 | // } // Execute()
|
---|
| 148 |
|
---|
| 149 | // protected virtual void Execute(IOperator op, VirtualScope scope) {
|
---|
| 150 | // operators.Add(new KeyValuePair<IOperator, VirtualScope>(op, scope)) ;
|
---|
| 151 | // } // Execute
|
---|
| 152 |
|
---|
| 153 | // protected void ExecuteOperation(IOperator op, IScope scope) {
|
---|
| 154 | // IOperation operation;
|
---|
| 155 | // currentOperator = op;
|
---|
| 156 | // operation = op.Execute(scope);
|
---|
| 157 | // if (operation != null) {
|
---|
| 158 | // //IOperator currentOperator;
|
---|
| 159 | // Stack<IOperation> executionStack = new Stack<IOperation>();
|
---|
| 160 | // executionStack.Push(op.Execute(scope));
|
---|
| 161 |
|
---|
| 162 | // while (executionStack.Count > 0) {
|
---|
| 163 | // operation = executionStack.Pop();
|
---|
| 164 | // if (operation is AtomicOperation) {
|
---|
| 165 | // AtomicOperation atomicOperation = (AtomicOperation)operation;
|
---|
| 166 | // IOperation next = null;
|
---|
| 167 | // try {
|
---|
| 168 | // currentOperator = atomicOperation.Operator;
|
---|
| 169 | // next = currentOperator.Execute(atomicOperation.Scope);
|
---|
| 170 | // }
|
---|
| 171 | // catch (Exception) {
|
---|
| 172 | // throw new InvalidOperationException("Invalid Operation occured in FixedBase.Execute");
|
---|
| 173 | // }
|
---|
| 174 | // if (next != null)
|
---|
| 175 | // executionStack.Push(next);
|
---|
| 176 | // } else if (operation is CompositeOperation) {
|
---|
| 177 | // CompositeOperation compositeOperation = (CompositeOperation)operation;
|
---|
| 178 | // for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
|
---|
| 179 | // executionStack.Push(compositeOperation.Operations[i]);
|
---|
| 180 | // } // else if
|
---|
| 181 | // } // while
|
---|
| 182 | // } // if (operation != null)
|
---|
| 183 | // } // ExecuteOperation
|
---|
| 184 |
|
---|
| 185 | // public override IOperation Apply(IScope scope) {
|
---|
| 186 | // try {
|
---|
| 187 | // inExecution = scope.GetVariableValue<BoolData>("InExecution", false);
|
---|
| 188 | // }
|
---|
| 189 | // catch (Exception) {
|
---|
| 190 | // inExecution = new BoolData(false);
|
---|
| 191 | // scope.AddVariable(new Variable("InExecution", inExecution));
|
---|
| 192 | // operators.Clear();
|
---|
| 193 | // } // catch
|
---|
| 194 | // return null;
|
---|
| 195 | // } // Apply
|
---|
| 196 |
|
---|
| 197 | // public override void Abort() {
|
---|
| 198 | // base.Abort();
|
---|
| 199 | // currentOperator.Abort();
|
---|
| 200 | // //engineThread.Abort();
|
---|
| 201 | // }
|
---|
| 202 |
|
---|
| 203 | // public bool InExecution {
|
---|
| 204 | // get { return inExecution.Data; }
|
---|
| 205 | // }
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | // } // class FixedBase
|
---|
| 209 |
|
---|
| 210 | // class CancelException : Exception {
|
---|
| 211 |
|
---|
| 212 | // } // class CancelException
|
---|
| 213 | //} // namespace HeuristicLab.FixedOperators
|
---|