1 | using System;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
11 | [Item("AlgorithmSubScopesCreator", "An operator which ...")]
|
---|
12 | [StorableClass]
|
---|
13 | public class AlgorithmSubScopesCreator : SingleSuccessorOperator {
|
---|
14 | #region Parameter properties
|
---|
15 | public ILookupParameter<TypeValue> AlgorithmTypeParameter {
|
---|
16 | get { return (ILookupParameter<TypeValue>)Parameters[MetaOptimizationProblem.AlgorithmTypeParameterName]; }
|
---|
17 | }
|
---|
18 | public ILookupParameter<IItemList<IProblem>> ProblemsParameter {
|
---|
19 | get { return (ILookupParameter<IItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
|
---|
20 | }
|
---|
21 | public ILookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
|
---|
22 | get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
|
---|
23 | }
|
---|
24 | public LookupParameter<IntValue> RepetitionsParameter {
|
---|
25 | get { return (LookupParameter<IntValue>)Parameters[MetaOptimizationProblem.RepetitionsParameterName]; }
|
---|
26 | }
|
---|
27 | private ScopeParameter CurrentScopeParameter {
|
---|
28 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
29 | }
|
---|
30 | public IScope CurrentScope {
|
---|
31 | get { return CurrentScopeParameter.ActualValue; }
|
---|
32 | }
|
---|
33 | #endregion
|
---|
34 |
|
---|
35 | [StorableConstructor]
|
---|
36 | protected AlgorithmSubScopesCreator(bool deserializing) : base(deserializing) { }
|
---|
37 | public AlgorithmSubScopesCreator()
|
---|
38 | : base() {
|
---|
39 | Parameters.Add(new LookupParameter<TypeValue>(MetaOptimizationProblem.AlgorithmTypeParameterName, ""));
|
---|
40 | Parameters.Add(new LookupParameter<IItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName, ""));
|
---|
41 | Parameters.Add(new LookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
|
---|
42 | Parameters.Add(new LookupParameter<IntValue>(MetaOptimizationProblem.RepetitionsParameterName, "Number of evaluations on one problem."));
|
---|
43 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes represent the parents."));
|
---|
44 | }
|
---|
45 | protected AlgorithmSubScopesCreator(AlgorithmSubScopesCreator original, Cloner cloner)
|
---|
46 | : base(original, cloner) {
|
---|
47 | }
|
---|
48 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
49 | return new AlgorithmSubScopesCreator(this, cloner);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override IOperation Apply() {
|
---|
53 | ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
|
---|
54 | IItemList<IProblem> problems = ProblemsParameter.ActualValue;
|
---|
55 | int repetitions = RepetitionsParameter.ActualValue.Value;
|
---|
56 | Type algorithmType = AlgorithmTypeParameter.ActualValue.Value;
|
---|
57 |
|
---|
58 | for (int i = 0; i < repetitions; i++) {
|
---|
59 | for (int j = 0; j < problems.Count; j++) {
|
---|
60 | IScope child = new Scope(string.Format("Problem {0}, Repetition {1}", j, i));
|
---|
61 | var algorithm = MetaOptimizationUtil.CreateParameterizedAlgorithmInstance(parameterConfiguration, algorithmType, (IProblem)problems[j].Clone());
|
---|
62 | child.Variables.Add(new Variable("Algorithm", algorithm));
|
---|
63 | child.Variables.Add(new Variable("ProblemIndex", new IntValue(j)));
|
---|
64 | child.Variables.Add(new Variable("RepetitionIndex", new IntValue(i)));
|
---|
65 | CurrentScope.SubScopes.Add(child);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return base.Apply();
|
---|
69 | }
|
---|
70 | }
|
---|
71 | } |
---|