- Timestamp:
- 11/05/15 16:20:55 (8 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/LastScopeCloner.cs
r13123 r13124 22 22 using System; 23 23 using System.Linq; 24 using HeuristicLab.Analysis;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; 27 using HeuristicLab.Data;28 26 using HeuristicLab.Operators; 29 using HeuristicLab.Optimization;30 27 using HeuristicLab.Parameters; 31 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 29 33 30 namespace HeuristicLab.Algorithms.ALPS { 34 [Item("La yerCreator", "An operator which creates a new layer by cloning the current oldest one.")]31 [Item("LastScopeCloner", "An operator that creates a new scope by cloning the current last one.")] 35 32 [StorableClass] 36 public sealed class LayerCreator : SingleSuccessorOperator { 37 private ILookupParameter<IntValue> OpenLayersParameter { 38 get { return (ILookupParameter<IntValue>)Parameters["OpenLayers"]; } 39 } 40 public OperatorParameter NewLayerOperatorParameter { 41 get { return (OperatorParameter)Parameters["NewLayerOperator"]; } 33 public sealed class LastScopeCloner : SingleSuccessorOperator { 34 public OperatorParameter NewScopeOperatorParameter { 35 get { return (OperatorParameter)Parameters["NewScopeOperator"]; } 42 36 } 43 37 44 public IOperator New LayerOperator {45 get { return New LayerOperatorParameter.Value; }46 set { New LayerOperatorParameter.Value = value; }38 public IOperator NewScopeOperator { 39 get { return NewScopeOperatorParameter.Value; } 40 set { NewScopeOperatorParameter.Value = value; } 47 41 } 48 42 49 43 [StorableConstructor] 50 private La yerCreator(bool deserializing) : base(deserializing) { }44 private LastScopeCloner(bool deserializing) : base(deserializing) { } 51 45 52 private La yerCreator(LayerCreator original, Cloner cloner)46 private LastScopeCloner(LastScopeCloner original, Cloner cloner) 53 47 : base(original, cloner) { 54 48 } 55 public LayerCreator() 56 : base() { 57 Parameters.Add(new LookupParameter<IntValue>("OpenLayers")); 58 Parameters.Add(new OperatorParameter("NewLayerOperator")); 49 public override IDeepCloneable Clone(Cloner cloner) { 50 return new LastScopeCloner(this, cloner); 59 51 } 60 52 61 public override IDeepCloneable Clone(Cloner cloner) { 62 return new LayerCreator(this, cloner); 53 public LastScopeCloner() 54 : base() { 55 Parameters.Add(new OperatorParameter("NewScopeOperator", "An Operator that is performed on the new scope.")); 63 56 } 64 57 65 58 public override IOperation Apply() { 66 var layersScope= ExecutionContext.Scope.SubScopes;67 if ( layersScope.Count < 1)59 var scopes = ExecutionContext.Scope.SubScopes; 60 if (scopes.Count < 1) 68 61 throw new ArgumentException("At least one layer must exist."); 69 62 70 var new Layer = (IScope)layersScope.Last().Clone();63 var newScope = (IScope)scopes.Last().Clone(); 71 64 72 int number;73 if (int.TryParse(new Layer.Name, out number))74 new Layer.Name = (number + 1).ToString();65 int scopeNumber; 66 if (int.TryParse(newScope.Name, out scopeNumber)) 67 newScope.Name = (scopeNumber + 1).ToString(); 75 68 76 layersScope.Add(newLayer);69 scopes.Add(newScope); 77 70 78 // Set new layer number79 newLayer.Variables["Layer"].Value = new IntValue(OpenLayersParameter.ActualValue.Value);80 81 // Decrement ages, because MainOperator is goint to increment it82 foreach (var solution in newLayer.SubScopes)83 ((DoubleValue)solution.Variables["Age"].Value).Value -= 1;84 85 // Reset existing values in the results to NaN to symbolize that no layer existed during that duration86 var results = (ResultCollection)newLayer.Variables["LayerResults"].Value;87 ResetResults(results);88 71 89 72 var next = new OperationCollection(base.Apply()); 90 next.Insert(0, ExecutionContext.CreateChildOperation(New LayerOperator, newLayer));73 next.Insert(0, ExecutionContext.CreateChildOperation(NewScopeOperator, newScope)); 91 74 return next; 92 }93 94 private void ResetResults(ResultCollection results) {95 var values = results.Select(r => r.Value);96 97 // Reset all values within results in results98 foreach (var resultsCollection in values.OfType<ResultCollection>())99 ResetResults(resultsCollection);100 101 // Reset values102 foreach (var dataTable in values.OfType<DataTable>()) {103 foreach (var row in dataTable.Rows)104 for (int i = 0; i < row.Values.Count; i++)105 row.Values[i] = double.NaN;106 }107 75 } 108 76 }
Note: See TracChangeset
for help on using the changeset viewer.