Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Algorithms/ScopeDuplicator.cs @ 17282

Last change on this file since 17282 was 16995, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.FLA for new persistence

File size: 2.2 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Data;
4using HeuristicLab.Operators;
5using HeuristicLab.Parameters;
6using HEAL.Attic;
7
8namespace HeuristicLab.Analysis.FitnessLandscape.Algorithms {
9
10  [Item("ScopeDuplicator", "Places clones of the first sub-scope as sub-scopes into n newly created scopes.")]
11  [StorableType("99032F61-ACCD-4312-8602-71C7D8787391")]
12  public class ScopeDuplicator : SingleSuccessorOperator {
13
14    #region Parameter Properties
15    public LookupParameter<IntValue> NrOfDuplicatesParameter {
16      get { return (LookupParameter<IntValue>)Parameters["NrOfDuplicates"]; }
17    }
18    #endregion
19
20    #region Parameter Values
21    private int NrOfDuplicates {
22      get { return NrOfDuplicatesParameter.ActualValue.Value; }
23    }
24    #endregion
25
26    #region Construction & Cloning
27    [StorableConstructor]
28    protected ScopeDuplicator(StorableConstructorFlag _) : base(_) { }
29    protected ScopeDuplicator(ScopeDuplicator original, Cloner cloner) : base(original, cloner) { }
30    public ScopeDuplicator()
31      : base() {
32      Parameters.Add(new LookupParameter<IntValue>("NrOfDuplicates", "Nr of copies to be created from the first sub-scope."));
33    }
34    public override IDeepCloneable Clone(Cloner cloner) {
35      return new ScopeDuplicator(this, cloner);
36    }
37    #endregion
38
39    public override IOperation Apply() {
40      IScope parentScope = ExecutionContext.Scope;
41      if (parentScope.SubScopes.Count > 0) {
42        IScope scope = parentScope.SubScopes[0];
43        parentScope.SubScopes.Remove(scope);
44        for (int i = 0; i<NrOfDuplicates; i++) {
45          IScope subScope = new Scope(i.ToString()) { Parent = parentScope };
46          parentScope.SubScopes.Add(subScope);
47          IScope clone = (IScope)scope.Clone();
48          clone.Parent = subScope;
49          subScope.SubScopes.Add(clone);
50          foreach (Variable var in parentScope.Variables) {
51            if (!(var.Value is IRandom))
52              subScope.Variables.Add((Variable)var.Clone());
53          }
54        }
55        parentScope.Variables.AddRange(scope.Variables);
56      }
57      return base.Apply();
58    }
59  }
60}
Note: See TracBrowser for help on using the repository browser.