Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/AlgorithmSubScopesCreator.cs

Last change on this file was 16996, checked in by gkronber, 5 years ago

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

File size: 3.5 KB
Line 
1using System;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Operators;
6using HeuristicLab.Optimization;
7using HeuristicLab.Parameters;
8using HEAL.Attic;
9
10namespace HeuristicLab.Problems.MetaOptimization {
11  [Item("AlgorithmSubScopesCreator", "An operator which ...")]
12  [StorableType("4569682A-267C-4471-93F4-5CB202E36B0B")]
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(StorableConstructorFlag _) : base(_) { }
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}
Note: See TracBrowser for help on using the repository browser.