1 | using HeuristicLab.Common;
|
---|
2 | using HeuristicLab.Core;
|
---|
3 | using HeuristicLab.Data;
|
---|
4 | using HeuristicLab.Operators;
|
---|
5 | using HeuristicLab.Parameters;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 |
|
---|
9 | namespace 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 | }
|
---|