Changeset 2757 for trunk/sources/HeuristicLab.Operators/3.3
- Timestamp:
- 02/08/10 03:43:36 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Operators/3.3
- Files:
-
- 3 added
- 15 deleted
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Operators/3.3/CombinedOperator.cs
r2740 r2757 68 68 } 69 69 70 public override ExecutionContextCollectionApply() {71 ExecutionContextCollection next = base.Apply();70 public override IExecutionContext Apply() { 71 ExecutionContextCollection next = new ExecutionContextCollection(base.Apply()); 72 72 if (operatorGraph.InitialOperator != null) 73 73 next.Insert(0, new ExecutionContext(ExecutionContext, operatorGraph.InitialOperator, ExecutionContext.Scope)); -
trunk/sources/HeuristicLab.Operators/3.3/ConditionalBranch.cs
r1530 r2757 23 23 using System.Collections.Generic; 24 24 using System.Text; 25 using System.Xml; 25 26 using HeuristicLab.Core; 26 27 using HeuristicLab.Data; 28 using HeuristicLab.Parameters; 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 30 28 31 namespace HeuristicLab.Operators { 29 32 /// <summary> 30 /// Branch of (one or)two operators whose executions depend on a specificcondition.33 /// A branch of two operators whose executions depend on a condition. 31 34 /// </summary> 32 public class ConditionalBranch : OperatorBase { 33 /// <inheritdoc select="summary"/> 34 public override string Description { 35 get { return @"ConditionalBranch expects to have 1 or 2 sub-operators. 36 It will return the 1st sub-operator if ""Condition"" is true and the 2nd sub-operator if ""Condition"" equals to false. 37 38 In case a 2nd sub-operator does not exist and ""Condition"" would equal to false, Conditional Branch will not return a new operation."; } 35 [Item("ConditionalBranch", "A branch of two operators whose executions depend on a boolean condition.")] 36 [Creatable("Test")] 37 [EmptyStorableClass] 38 public class ConditionalBranch : SingleSuccessorOperator { 39 public LookupParameter<BoolData> ConditionParameter { 40 get { return (LookupParameter<BoolData>)Parameters["Condition"]; } 41 } 42 protected OperatorParameter TrueBranchParameter { 43 get { return (OperatorParameter)Parameters["TrueBranch"]; } 44 } 45 protected OperatorParameter FalseBranchParameter { 46 get { return (OperatorParameter)Parameters["FalseBranch"]; } 47 } 48 public IOperator TrueBranch { 49 get { return TrueBranchParameter.Value; } 50 set { TrueBranchParameter.Value = value; } 51 } 52 public IOperator FalseBranch { 53 get { return FalseBranchParameter.Value; } 54 set { FalseBranchParameter.Value = value; } 39 55 } 40 56 41 /// <summary>42 /// Initializes a new instance of <see cref="ConditionalBranch"/> with one variable info43 /// (<c>Condition</c>).44 /// </summary>45 57 public ConditionalBranch() 46 58 : base() { 47 AddVariableInfo(new VariableInfo("Condition", "A boolean variable that decides the branch", typeof(BoolData), VariableKind.In)); 59 Parameters.Add(new LookupParameter<BoolData>("Condition", "A boolean variable which defines which branch is executed.")); 60 Parameters.Add(new OperatorParameter("TrueBranch", "The operator which is executed if the condition is true.")); 61 Parameters.Add(new OperatorParameter("FalseBranch", "The operator which is executed if the condition is false.")); 48 62 } 49 63 50 /// <summary> 51 /// Applies the operator of branch one under a specific condition on the given 52 /// <paramref name="scope"/>, or - if existent - operator of branch two if the condition could not 53 /// be fulfilled. 54 /// </summary> 55 /// <param name="scope">The scope to apply the operators on.</param> 56 /// <returns>A new <see cref="AtomicOperation"/> with either operator 1 or operator 2 applied 57 /// to the given <paramref name="scope"/> or <c>null</c>.</returns> 58 public override IOperation Apply(IScope scope) { 59 BoolData resultData = GetVariableValue<BoolData>("Condition", scope, true); 60 bool result = resultData.Data; 61 62 if ((result) && (SubOperators.Count > 0) && (SubOperators[0] != null)) 63 return new AtomicOperation(SubOperators[0], scope); 64 else if ((!result) && (SubOperators.Count > 1) && (SubOperators[1] != null)) 65 return new AtomicOperation(SubOperators[1], scope); 66 return null; 64 public override IExecutionContext Apply() { 65 ExecutionContextCollection next = new ExecutionContextCollection(base.Apply()); 66 if (ConditionParameter.ActualValue.Value) { 67 if (TrueBranch != null) next.Insert(0, new ExecutionContext(ExecutionContext.Parent, TrueBranch, ExecutionContext.Scope)); 68 } else { 69 if (FalseBranch != null) next.Insert(0, new ExecutionContext(ExecutionContext.Parent, FalseBranch, ExecutionContext.Scope)); 70 } 71 return next; 67 72 } 68 73 } -
trunk/sources/HeuristicLab.Operators/3.3/Counter.cs
r2756 r2757 39 39 get { return (LookupParameter<IntData>)Parameters["Value"]; } 40 40 } 41 public ValueLookupParameter<IntData> IncrementPara neter {41 public ValueLookupParameter<IntData> IncrementParameter { 42 42 get { return (ValueLookupParameter<IntData>)Parameters["Increment"]; } 43 43 } 44 44 public IntData Increment { 45 get { return IncrementPara neter.Value; }46 set { IncrementPara neter.Value = value; }45 get { return IncrementParameter.Value; } 46 set { IncrementParameter.Value = value; } 47 47 } 48 48 … … 53 53 } 54 54 55 public override ExecutionContextCollectionApply() {55 public override IExecutionContext Apply() { 56 56 if (ValueParameter.ActualValue == null) ValueParameter.ActualValue = new IntData(); 57 ValueParameter.ActualValue.Value += IncrementPara neter.ActualValue.Value;57 ValueParameter.ActualValue.Value += IncrementParameter.ActualValue.Value; 58 58 return base.Apply(); 59 59 } -
trunk/sources/HeuristicLab.Operators/3.3/DoubleCounter.cs
r1530 r2757 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Parameters; 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 29 28 30 namespace HeuristicLab.Operators { 29 31 /// <summary> 30 /// Class to add a specified interval to a double value.32 /// Operator which increments a double variable. 31 33 /// </summary> 32 public class DoubleCounter : OperatorBase { 33 /// <summary> 34 /// Gets the description of the current instance. 35 /// </summary> 36 public override string Description { 37 get { return @"Adds a given interval to a double value"; } 34 [Item("DoubleCounter", "An operator which increments a double variable.")] 35 [EmptyStorableClass] 36 [Creatable("Test")] 37 public sealed class DoubleCounter : SingleSuccessorOperator { 38 public LookupParameter<DoubleData> ValueParameter { 39 get { return (LookupParameter<DoubleData>)Parameters["Value"]; } 40 } 41 public ValueLookupParameter<DoubleData> IncrementParameter { 42 get { return (ValueLookupParameter<DoubleData>)Parameters["Increment"]; } 43 } 44 public DoubleData Increment { 45 get { return IncrementParameter.Value; } 46 set { IncrementParameter.Value = value; } 38 47 } 39 48 40 /// <summary>41 /// Initializes a new instance of <see cref="DoubleCounter"/>, including two variable infos42 /// (<c>Value</c> and <c>Interval</c>).43 /// </summary>44 49 public DoubleCounter() 45 50 : base() { 46 AddVariableInfo(new VariableInfo("Value", "Counter value", typeof(DoubleData), VariableKind.In | VariableKind.Out));47 AddVariableInfo(new VariableInfo("Interval", "Interval value", typeof(DoubleData), VariableKind.In));51 Parameters.Add(new LookupParameter<DoubleData>("Value", "The value which should be incremented.")); 52 Parameters.Add(new ValueLookupParameter<DoubleData>("Increment", "The increment which is added to the value.", new DoubleData(1))); 48 53 } 49 54 50 /// <summary> 51 /// Adds to a double value of the given <paramref name="scope"/> a specified interval. 52 /// </summary> 53 /// <param name="scope">The scope whose variable should be incremented.</param> 54 /// <returns><c>null</c>.</returns> 55 public override IOperation Apply(IScope scope) { 56 DoubleData value = GetVariableValue<DoubleData>("Value", scope, true); 57 double interval = GetVariableValue<DoubleData>("Interval", scope, true).Data; 58 value.Data += interval; 59 return null; 55 public override IExecutionContext Apply() { 56 if (ValueParameter.ActualValue == null) ValueParameter.ActualValue = new DoubleData(); 57 ValueParameter.ActualValue.Value += IncrementParameter.ActualValue.Value; 58 return base.Apply(); 60 59 } 61 60 } -
trunk/sources/HeuristicLab.Operators/3.3/HeuristicLab.Operators-3.3.csproj
r2754 r2757 87 87 <Compile Include="CombinedOperator.cs" /> 88 88 <None Include="HeuristicLabOperatorsPlugin.cs.frame" /> 89 <Compile Include="ConditionalBranch.cs" /> 90 <Compile Include="Comparator.cs" /> 91 <Compile Include="ScopeCleaner.cs" /> 92 <Compile Include="SubScopesRemover.cs" /> 93 <Compile Include="SubScopesSorter.cs" /> 94 <Compile Include="DoubleCounter.cs" /> 95 <Compile Include="ParallelProcessor.cs" /> 96 <Compile Include="UniformParallelSubScopesProcessor.cs" /> 97 <Compile Include="UniformSequentialSubScopesProcessor.cs" /> 98 <Compile Include="ValueCollector.cs" /> 89 99 <Compile Include="MultipleSuccessorsOperator.cs" /> 90 100 <Compile Include="Counter.cs" /> … … 97 107 <Compile Include="SequentialProcessor.cs" /> 98 108 <Compile Include="SingleSuccessorOperator.cs" /> 109 <Compile Include="SubScopesCreater.cs" /> 110 <Compile Include="VariableInjector.cs"> 111 <SubType>Code</SubType> 112 </Compile> 99 113 </ItemGroup> 100 114 <ItemGroup> -
trunk/sources/HeuristicLab.Operators/3.3/Operator.cs
r2740 r2757 61 61 } 62 62 63 [Storable] 64 private ExecutionContext executionContext; 65 protected ExecutionContext ExecutionContext { 66 get { return executionContext; } 67 private set { 68 if (value != executionContext) { 69 executionContext = value; 70 OnExecutionContextChanged(); 71 } 72 } 73 } 74 63 75 /// <summary> 64 76 /// Flag whether the current instance has been canceled. … … 68 80 protected bool Canceled { 69 81 get { return canceled; } 82 private set { 83 if (value != canceled) { 84 canceled = value; 85 OnCanceledChanged(); 86 } 87 } 70 88 } 71 89 … … 82 100 } 83 101 } 84 }85 86 [Storable]87 private ExecutionContext executionContext;88 protected ExecutionContext ExecutionContext {89 get { return executionContext; }90 102 } 91 103 … … 118 130 119 131 /// <inheritdoc/> 120 public virtual ExecutionContextCollectionExecute(ExecutionContext context) {132 public virtual IExecutionContext Execute(ExecutionContext context) { 121 133 try { 122 canceled = false;123 executionContext = context;134 Canceled = false; 135 ExecutionContext = context; 124 136 foreach (IParameter param in Parameters) 125 137 param.ExecutionContext = context; 126 ExecutionContextCollectionnext = Apply();138 IExecutionContext next = Apply(); 127 139 OnExecuted(); 128 140 return next; … … 131 143 foreach (IParameter param in Parameters) 132 144 param.ExecutionContext = null; 133 context = null;145 ExecutionContext = null; 134 146 } 135 147 } 136 148 /// <inheritdoc/> 137 149 /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks> 138 public v irtual void Abort() {139 canceled = true;150 public void Abort() { 151 Canceled = true; 140 152 } 141 153 /// <summary> … … 144 156 /// <param name="scope">The scope where to execute the operator</param> 145 157 /// <returns><c>null</c>.</returns> 146 public virtual ExecutionContextCollection Apply() { 147 return new ExecutionContextCollection(); 148 } 158 public abstract IExecutionContext Apply(); 149 159 160 protected virtual void OnExecutionContextChanged() { } 161 protected virtual void OnCanceledChanged() { } 150 162 /// <inheritdoc/> 151 163 public event EventHandler BreakpointChanged; -
trunk/sources/HeuristicLab.Operators/3.3/ParallelProcessor.cs
r1530 r2757 23 23 using System.Collections.Generic; 24 24 using System.Text; 25 using System.Xml; 26 using HeuristicLab.Collections; 25 27 using HeuristicLab.Core; 26 28 using HeuristicLab.Data; 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 30 28 31 namespace HeuristicLab.Operators { 29 32 /// <summary> 30 /// Performs <c>n</c> operators on the given scope; operations can be executedin parallel.33 /// Operator which executes multiple operators in parallel. 31 34 /// </summary> 32 public class ParallelProcessor : OperatorBase { 33 /// <inheritdoc select="summary"/> 34 public override string Description { 35 get { return @"TODO\r\nOperator description still missing ..."; } 35 [Item("ParallelProcessor", "An operator which executes multiple operators in parallel.")] 36 [Creatable("Test")] 37 [EmptyStorableClass] 38 public sealed class ParallelProcessor : MultipleSuccessorsOperator { 39 public ParallelProcessor() 40 : base() { 36 41 } 37 42 38 /// <summary> 39 /// Applies <c>n</c> operators on the given <paramref name="scope"/>. 40 /// </summary> 41 /// <param name="scope">The scope to apply the operators on.</param> 42 /// <returns>A new <see cref="CompositeOperation"/> with the <c>n</c> operators applied 43 /// to the given <paramref name="scope"/> with the <c>ExecuteInParallel</c> set to <c>true</c>.</returns> 44 public override IOperation Apply(IScope scope) { 45 CompositeOperation next = new CompositeOperation(); 46 next.ExecuteInParallel = true; 47 for (int i = 0; i < SubOperators.Count; i++) 48 next.AddOperation(new AtomicOperation(SubOperators[i], scope)); 43 public override IExecutionContext Apply() { 44 ExecutionContextCollection next = new ExecutionContextCollection(); 45 for (int i = 0; i < Successors.Count; i++) { 46 if (Successors[i] != null) 47 next.Add(new ExecutionContext(ExecutionContext.Parent, Successors[i], ExecutionContext.Scope)); 48 } 49 next.Parallel = true; 49 50 return next; 50 51 } -
trunk/sources/HeuristicLab.Operators/3.3/ScopeCleaner.cs
r1530 r2757 24 24 using System.Text; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 26 using HeuristicLab.Parameters; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 28 29 namespace HeuristicLab.Operators { 29 30 /// <summary> 30 /// Operator that removes all variables in the given scope and deletes also all subscopes.31 /// An operator which removes all variables and sub-scopes from the current scope. 31 32 /// </summary> 32 public class ScopeCleaner : OperatorBase { 33 /// <inheritdoc select="summary"/> 34 public override string Description { 35 get { return @"Removes all variables in the current scope and deletes all subscopes"; } 33 [Item("ScopeCleaner", "An operator which removes all variables and sub-scopes from the current scope.")] 34 [EmptyStorableClass] 35 [Creatable("Test")] 36 public sealed class ScopeCleaner : SingleSuccessorOperator { 37 private ScopeParameter CurrentScopeParameter { 38 get { return (ScopeParameter)Parameters["CurrentScope"]; } 39 } 40 public IScope CurrentScope { 41 get { return CurrentScopeParameter.ActualValue; } 36 42 } 37 43 38 /// <summary>39 /// Deletes all variable and sub scopes from the given <paramref name="scope"/>.40 /// </summary>41 /// <remarks>Calls <see cref="IScope.Clear"/> of interface <see cref="IScope"/>.</remarks>42 /// <param name="scope">The scope to clear.</param> 43 /// <returns><c>null</c>.</returns>44 public override IOperation Apply(IScope scope) {45 scope.Clear();46 return null;44 public ScopeCleaner() 45 : base() { 46 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose variables and sub-scopes should be removed.")); 47 } 48 49 public override IExecutionContext Apply() { 50 CurrentScope.Variables.Clear(); 51 CurrentScope.SubScopes.Clear(); 52 return base.Apply(); 47 53 } 48 54 } -
trunk/sources/HeuristicLab.Operators/3.3/SequentialProcessor.cs
r2740 r2757 41 41 } 42 42 43 public override ExecutionContextCollectionApply() {43 public override IExecutionContext Apply() { 44 44 ExecutionContextCollection next = new ExecutionContextCollection(); 45 for (int i = 0; i < Successors.Count; i++) 46 next.Add(new ExecutionContext(ExecutionContext.Parent, Successors[i], ExecutionContext.Scope)); 45 for (int i = 0; i < Successors.Count; i++) { 46 if (Successors[i] != null) 47 next.Add(new ExecutionContext(ExecutionContext.Parent, Successors[i], ExecutionContext.Scope)); 48 } 47 49 return next; 48 50 } -
trunk/sources/HeuristicLab.Operators/3.3/SingleSuccessorOperator.cs
r2740 r2757 49 49 } 50 50 51 public override ExecutionContextCollectionApply() {51 public override IExecutionContext Apply() { 52 52 if (Successor != null) 53 return new ExecutionContext Collection(new ExecutionContext(ExecutionContext.Parent, Successor, ExecutionContext.Scope));53 return new ExecutionContext(ExecutionContext.Parent, Successor, ExecutionContext.Scope); 54 54 else 55 return n ew ExecutionContextCollection();55 return null; 56 56 } 57 57 } -
trunk/sources/HeuristicLab.Operators/3.3/SubScopesCreater.cs
r1530 r2757 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Parameters; 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 29 28 30 namespace HeuristicLab.Operators { 29 31 /// <summary> 30 /// Operator to create sub scopes in a givenscope.32 /// An operator which adds new and empty sub-scopes to the current scope. 31 33 /// </summary> 32 public class SubScopesCreater : OperatorBase { 33 /// <inheritdoc select="summary"/> 34 public override string Description { 35 get { return @"TODO\r\nOperator description still missing ..."; } 34 [Item("SubScopesCreater", "An operator which adds new and empty sub-scopes to the current scope.")] 35 [EmptyStorableClass] 36 [Creatable("Test")] 37 public class SubScopesCreater : SingleSuccessorOperator { 38 public ValueLookupParameter<IntData> NumberOfSubScopesParameter { 39 get { return (ValueLookupParameter<IntData>)Parameters["NumberOfSubScopes"]; } 40 } 41 protected ScopeParameter CurrentScopeParameter { 42 get { return (ScopeParameter)Parameters["CurrentScope"]; } 43 } 44 public IScope CurrentScope { 45 get { return CurrentScopeParameter.ActualValue; } 36 46 } 37 47 38 /// <summary>39 /// Initializes a new instance of <see cref="SubScopesCreater"/> with one variable info (<c>SubScopes</c>).40 /// </summary>41 48 public SubScopesCreater() 42 49 : base() { 43 AddVariableInfo(new VariableInfo("SubScopes", "Number of sub-scopes", typeof(IntData), VariableKind.In)); 50 Parameters.Add(new ValueLookupParameter<IntData>("NumberOfSubScopes", "The number of new and empty sub-scopes which should be added to the current scope.")); 51 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new and empty sub-scopes are added.")); 44 52 } 45 53 46 /// <summary> 47 /// Creates a specified number of sub scopes in the given <paramref name="scope"/>. 48 /// </summary> 49 /// <param name="scope">The scope where to create the sub scopes.</param> 50 /// <returns><c>null</c>.</returns> 51 public override IOperation Apply(IScope scope) { 52 IntData count = GetVariableValue<IntData>("SubScopes", scope, true); 53 54 for (int i = 0; i < count.Data; i++) 55 scope.AddSubScope(new Scope(i.ToString())); 56 57 return null; 54 public override IExecutionContext Apply() { 55 int n = NumberOfSubScopesParameter.ActualValue.Value; 56 for (int i = 0; i < n; i++) 57 CurrentScope.SubScopes.Add(new Scope(i.ToString())); 58 return base.Apply(); 58 59 } 59 60 } -
trunk/sources/HeuristicLab.Operators/3.3/SubScopesRemover.cs
r1530 r2757 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Parameters; 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 29 28 30 namespace HeuristicLab.Operators { 29 31 /// <summary> 30 /// Removes one specified or - if nothing specified - all operators from the givenscope.32 /// An operator which removes one specified or (if not specified) all sub-scopes from the current scope. 31 33 /// </summary> 32 public class SubScopesRemover : OperatorBase { 33 /// <inheritdoc select="summary"/> 34 public override string Description { 35 get { 36 return @"This operator either removes the subscope specified in the variable SubScopeIndex or if this variable is absent, removes all subscopes."; 37 } 34 [Item("SubScopesRemover", "An operator which removes one specified or (if not specified) all sub-scopes from the current scope.")] 35 [EmptyStorableClass] 36 [Creatable("Test")] 37 public class SubScopesRemover : SingleSuccessorOperator { 38 public ValueLookupParameter<IntData> SubScopeIndexParameter { 39 get { return (ValueLookupParameter<IntData>)Parameters["SubScopeIndex"]; } 40 } 41 protected ScopeParameter CurrentScopeParameter { 42 get { return (ScopeParameter)Parameters["CurrentScope"]; } 43 } 44 public IScope CurrentScope { 45 get { return CurrentScopeParameter.ActualValue; } 38 46 } 39 47 40 /// <summary>41 /// Initializes a new instance of <see cref="SubScopesRemover"/> with one variable info42 /// (<c>SubScopeIndex</c>).43 /// </summary>44 48 public SubScopesRemover() 45 49 : base() { 46 AddVariableInfo(new VariableInfo("SubScopeIndex", "(Optional) the index of the subscope to remove", typeof(IntData), VariableKind.In)); 50 Parameters.Add(new ValueLookupParameter<IntData>("SubScopeIndex", "The index of the sub-scope which should be removed. If this parameter has no value, all sub-scopes of the current scope are removed.")); 51 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope from which one or all sub-scopes should be removed.")); 47 52 } 48 53 49 /// <summary> 50 /// Removes one sub scope with a specified index from the given <paramref name="scope"/>, or if no 51 /// index has been specified, all sub scopes are removed. 52 /// </summary> 53 /// <exception cref="InvalidOperationException">Thrown when no sub scope with the specified index 54 /// exists.</exception> 55 /// <param name="scope">The scope whose sub scope(s) to remove.</param> 56 /// <returns><c>null</c>.</returns> 57 public override IOperation Apply(IScope scope) { 58 IntData index = GetVariableValue<IntData>("SubScopeIndex", scope, true, false); 59 if (index == null) { // remove all scopes 60 while (scope.SubScopes.Count > 0) { 61 scope.RemoveSubScope(scope.SubScopes[0]); 62 } 63 } else { 64 if (index.Data < 0 && index.Data >= scope.SubScopes.Count) throw new InvalidOperationException("ERROR: no scope with index " + index.Data + " exists"); 65 scope.RemoveSubScope(scope.SubScopes[index.Data]); 66 } 67 return null; 54 public override IExecutionContext Apply() { 55 IntData index = SubScopeIndexParameter.ActualValue; 56 if (index != null) 57 CurrentScope.SubScopes.RemoveAt(index.Value); 58 else 59 CurrentScope.SubScopes.Clear(); 60 return base.Apply(); 68 61 } 69 62 } -
trunk/sources/HeuristicLab.Operators/3.3/UniformParallelSubScopesProcessor.cs
r1530 r2757 23 23 using System.Collections.Generic; 24 24 using System.Text; 25 using System.Xml; 25 26 using HeuristicLab.Core; 26 using HeuristicLab.Data; 27 using HeuristicLab.Parameters; 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 29 28 30 namespace HeuristicLab.Operators { 29 31 /// <summary> 30 /// Performs the same operator on all <c>n</c> existing sub scopes of a given scope; 31 /// operations can be executed in parallel. 32 /// An operator which applies a specified operator on all sub-scopes of the current scope in parallel. 32 33 /// </summary> 33 public class UniformParallelSubScopesProcessor : OperatorBase { 34 /// <inheritdoc select="summary"/> 35 public override string Description { 36 get { return @"TODO\r\nOperator description still missing ..."; } 34 [Item("UniformParallelSubScopesProcessor", "An operator which applies a specified operator on all sub-scopes of the current scope in parallel.")] 35 [Creatable("Test")] 36 [EmptyStorableClass] 37 public sealed class UniformParallelSubScopesProcessor : SingleSuccessorOperator { 38 private OperatorParameter OperatorParameter { 39 get { return (OperatorParameter)Parameters["Operator"]; } 40 } 41 public IOperator Operator { 42 get { return OperatorParameter.Value; } 43 set { OperatorParameter.Value = value; } 37 44 } 38 45 39 /// <summary> 40 /// Applies one operator on all the sub scopes of the given <paramref name="scope"/>. 41 /// </summary> 42 /// <param name="scope">The scope on whose sub scopes the operator is applied.</param> 43 /// <returns>A new <see cref="CompositeOperation"/> with one operator and all sub scopes and 44 /// the <c>ExecuteInParallel</c> flag set to <c>true</c>.</returns> 45 public override IOperation Apply(IScope scope) { 46 CompositeOperation next = new CompositeOperation(); 47 next.ExecuteInParallel = true; 48 for (int i = 0; i < scope.SubScopes.Count; i++) 49 next.AddOperation(new AtomicOperation(SubOperators[0], scope.SubScopes[i])); 46 public UniformParallelSubScopesProcessor() 47 : base() { 48 Parameters.Add(new OperatorParameter("Operator", "The operator which should be applied on all sub-scopes of the current scope in parallel.")); 49 } 50 51 public override IExecutionContext Apply() { 52 ExecutionContextCollection next = new ExecutionContextCollection(base.Apply()); 53 if (Operator != null) { 54 ExecutionContextCollection inner = new ExecutionContextCollection(); 55 inner.Parallel = true; 56 for (int i = 0; i < ExecutionContext.Scope.SubScopes.Count; i++) 57 inner.Add(new ExecutionContext(ExecutionContext.Parent, Operator, ExecutionContext.Scope.SubScopes[i])); 58 next.Insert(0, inner); 59 } 50 60 return next; 51 61 } -
trunk/sources/HeuristicLab.Operators/3.3/UniformSequentialSubScopesProcessor.cs
r1530 r2757 23 23 using System.Collections.Generic; 24 24 using System.Text; 25 using System.Xml; 25 26 using HeuristicLab.Core; 26 using HeuristicLab.Data; 27 using HeuristicLab.Parameters; 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 29 28 30 namespace HeuristicLab.Operators { 29 31 /// <summary> 30 /// Performs the same operator on all existing sub scopes of a given scope, 31 /// must be executed sequentially. 32 /// An operator which applies a specified operator sequentially on all sub-scopes of the current scope. 32 33 /// </summary> 33 public class UniformSequentialSubScopesProcessor : OperatorBase { 34 /// <inheritdoc select="summary"/> 35 public override string Description { 36 get { return @"TODO\r\nOperator description still missing ..."; } 34 [Item("UniformSequentialSubScopesProcessor", "An operator which applies a specified operator sequentially on all sub-scopes of the current scope.")] 35 [Creatable("Test")] 36 [EmptyStorableClass] 37 public sealed class UniformSequentialSubScopesProcessor : SingleSuccessorOperator { 38 private OperatorParameter OperatorParameter { 39 get { return (OperatorParameter)Parameters["Operator"]; } 40 } 41 public IOperator Operator { 42 get { return OperatorParameter.Value; } 43 set { OperatorParameter.Value = value; } 37 44 } 38 45 39 /// <summary> 40 /// Applies one operator on all the sub scopes of the given <paramref name="scope"/>. 41 /// </summary> 42 /// <param name="scope">The scope on whose sub scopes the operator is applied.</param> 43 /// <returns>A new <see cref="CompositeOperation"/> with one operator and all sub scopes.</returns> 44 public override IOperation Apply(IScope scope) { 45 CompositeOperation next = new CompositeOperation(); 46 for (int i = 0; i < scope.SubScopes.Count; i++) 47 next.AddOperation(new AtomicOperation(SubOperators[0], scope.SubScopes[i])); 46 public UniformSequentialSubScopesProcessor() 47 : base() { 48 Parameters.Add(new OperatorParameter("Operator", "The operator which should be applied sequentially on all sub-scopes of the current scope.")); 49 } 50 51 public override IExecutionContext Apply() { 52 ExecutionContextCollection next = new ExecutionContextCollection(base.Apply()); 53 if (Operator != null) { 54 ExecutionContextCollection inner = new ExecutionContextCollection(); 55 for (int i = 0; i < ExecutionContext.Scope.SubScopes.Count; i++) 56 inner.Add(new ExecutionContext(ExecutionContext.Parent, Operator, ExecutionContext.Scope.SubScopes[i])); 57 next.Insert(0, inner); 58 } 48 59 return next; 49 60 } -
trunk/sources/HeuristicLab.Operators/3.3/VariableInjector.cs
r2526 r2757 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using System.Text; 25 26 using System.Xml; 27 using HeuristicLab.Collections; 26 28 using HeuristicLab.Core; 29 using HeuristicLab.Parameters; 27 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 31 29 32 namespace HeuristicLab.Operators { 30 33 /// <summary> 31 /// Class to inject local variables into thescope.34 /// An operator which collects the actual values of parameters and clones them into the current scope. 32 35 /// </summary> 33 public class VariableInjector : OperatorBase { 34 35 private Dictionary<IVariable, IVariableInfo> variableVariableInfoTable; 36 private Dictionary<IVariableInfo, IVariable> variableInfoVariableTable; 37 38 /// <inheritdoc select="summary"/> 39 public override string Description { 40 get { return @"TODO\r\nOperator description still missing ..."; } 36 [Item("VariableInjector", "An operator which collects the actual values of parameters and clones them into the current scope.")] 37 [Creatable("Test")] 38 [EmptyStorableClass] 39 public class VariableInjector : ValueCollector { 40 protected ScopeParameter CurrentScopeParameter { 41 get { return (ScopeParameter)Parameters["CurrentScope"]; } 42 } 43 public IScope CurrentScope { 44 get { return CurrentScopeParameter.ActualValue; } 41 45 } 42 46 43 /// <summary>44 /// Initializes a new instance of <see cref="VariableInjector"/>.45 /// </summary>46 47 public VariableInjector() 47 48 : base() { 48 variableVariableInfoTable = new Dictionary<IVariable, IVariableInfo>(); 49 variableInfoVariableTable = new Dictionary<IVariableInfo, IVariable>(); 49 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope into which the parameter values are cloned.")); 50 50 } 51 51 52 /// <summary> 53 /// Clones the current instance (deep clone). 54 /// </summary> 55 /// <remarks>Deep clone performed with <see cref="cloner.Clone"/> of helper class 56 /// <see cref="Auxiliary"/>.</remarks> 57 /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param> 58 /// <returns>The cloned object as <see cref="VariableInjector"/>.</returns> 59 public override IItem Clone(ICloner cloner) { 60 VariableInjector clone = new VariableInjector(); 61 cloner.RegisterClonedObject(this, clone); 62 clone.Name = Name; 63 foreach (IVariable variable in Variables) 64 clone.AddVariable((IVariable)cloner.Clone(variable)); 65 return clone; 52 public override IExecutionContext Apply() { 53 IVariable var; 54 foreach (IParameter param in CollectedValues) { 55 CurrentScope.Variables.TryGetValue(param.Name, out var); 56 if (var != null) 57 var.Value = (IItem)param.ActualValue.Clone(); 58 else 59 CurrentScope.Variables.Add(new Variable(param.Name, (IItem)param.ActualValue.Clone())); 60 } 61 return base.Apply(); 66 62 } 67 68 /// <summary>69 /// Adds the specified <paramref name="variable"/> to the current instance to get injected.70 /// </summary>71 /// <param name="variable">The variable to add.</param>72 /// <remarks>Calls private method <see cref="CreateVariableInfo"/> and <see cref="OperatorBase.AddVariable"/>73 /// of base class <see cref="OperatorBase"/>.</remarks>74 public override void AddVariable(IVariable variable) {75 base.AddVariable(variable);76 CreateVariableInfo(variable);77 }78 79 /// <summary>80 /// Removes a variable with the specified <paramref name="name"/> from the current injector.81 /// </summary>82 /// <remarks>Calls private method <see cref="DeleteVariableInfo"/> and <see cref="OperatorBase.RemoveVariable"/>83 /// of base class <see cref="OperatorBase"/>.</remarks>84 /// <param name="name">The name of the </param>85 public override void RemoveVariable(string name) {86 DeleteVariableInfo(name);87 base.RemoveVariable(name);88 }89 90 /// <summary>91 /// Adds the specified variables to the given <paramref name="scope"/> (and removes them first,92 /// if they already exist in the current scope).93 /// </summary>94 /// <param name="scope">The scope where to inject the variables.</param>95 /// <returns><c>null</c>.</returns>96 public override IOperation Apply(IScope scope) {97 foreach (IVariable variable in Variables) {98 if (scope.GetVariable(variable.Name) != null)99 scope.RemoveVariable(variable.Name);100 scope.AddVariable((IVariable)variable.Clone());101 }102 return null;103 }104 105 [Storable]106 private KeyValuePair<Dictionary<IVariableInfo, IVariable>, Dictionary<IVariable, IVariableInfo>> VariableMappingPersistence {107 get {108 return new KeyValuePair<Dictionary<IVariableInfo, IVariable>, Dictionary<IVariable, IVariableInfo>>(109 variableInfoVariableTable, variableVariableInfoTable);110 }111 set {112 variableInfoVariableTable = value.Key;113 variableVariableInfoTable = value.Value;114 foreach (var pair in variableInfoVariableTable) {115 pair.Key.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);116 pair.Value.NameChanged += new EventHandler(Variable_NameChanged);117 }118 }119 }120 121 private void CreateVariableInfo(IVariable variable) {122 IVariableInfo info = new VariableInfo(Guid.NewGuid().ToString(), "Injected variable", variable.Value.GetType(), VariableKind.New);123 info.ActualName = variable.Name;124 AddVariableInfo(info);125 variableVariableInfoTable.Add(variable, info);126 variableInfoVariableTable.Add(info, variable);127 info.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);128 variable.NameChanged += new EventHandler(Variable_NameChanged);129 }130 private void DeleteVariableInfo(string name) {131 IVariable variable = GetVariable(name);132 if (variable != null) {133 IVariableInfo info = variableVariableInfoTable[variable];134 RemoveVariableInfo(info.FormalName);135 variableVariableInfoTable.Remove(variable);136 variableInfoVariableTable.Remove(info);137 info.ActualNameChanged -= new EventHandler(VariableInfo_ActualNameChanged);138 variable.NameChanged -= new EventHandler(Variable_NameChanged);139 }140 }141 142 #region VariableInfo and Variable Events143 private void VariableInfo_ActualNameChanged(object sender, EventArgs e) {144 IVariableInfo info = (IVariableInfo)sender;145 IVariable variable = variableInfoVariableTable[info];146 variable.Name = info.ActualName;147 }148 private void Variable_NameChanged(object sender, EventArgs e) {149 IVariable variable = (IVariable)sender;150 IVariableInfo info = variableVariableInfoTable[variable];151 info.ActualName = variable.Name;152 }153 #endregion154 63 } 155 64 }
Note: See TracChangeset
for help on using the changeset viewer.