Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16591 was 16573, checked in by gkronber, 6 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

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