Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Algorithms/ScopeDuplicator.cs @ 11194

Last change on this file since 11194 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

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