using System; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.MetaOptimization { [Item("AlgorithmSubScopesCreator", "An operator which ...")] [StorableClass] public class AlgorithmSubScopesCreator : SingleSuccessorOperator { #region Parameter properties public ILookupParameter AlgorithmTypeParameter { get { return (ILookupParameter)Parameters[MetaOptimizationProblem.AlgorithmTypeParameterName]; } } public ILookupParameter> ProblemsParameter { get { return (ILookupParameter>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; } } public ILookupParameter ParameterConfigurationParameter { get { return (ILookupParameter)Parameters["ParameterConfigurationTree"]; } } public LookupParameter RepetitionsParameter { get { return (LookupParameter)Parameters[MetaOptimizationProblem.RepetitionsParameterName]; } } private ScopeParameter CurrentScopeParameter { get { return (ScopeParameter)Parameters["CurrentScope"]; } } public IScope CurrentScope { get { return CurrentScopeParameter.ActualValue; } } #endregion [StorableConstructor] protected AlgorithmSubScopesCreator(bool deserializing) : base(deserializing) { } public AlgorithmSubScopesCreator() : base() { Parameters.Add(new LookupParameter(MetaOptimizationProblem.AlgorithmTypeParameterName, "")); Parameters.Add(new LookupParameter>(MetaOptimizationProblem.ProblemsParameterName, "")); Parameters.Add(new LookupParameter("ParameterConfigurationTree", "")); Parameters.Add(new LookupParameter(MetaOptimizationProblem.RepetitionsParameterName, "Number of evaluations on one problem.")); Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes represent the parents.")); } protected AlgorithmSubScopesCreator(AlgorithmSubScopesCreator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new AlgorithmSubScopesCreator(this, cloner); } public override IOperation Apply() { ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue; IItemList problems = ProblemsParameter.ActualValue; int repetitions = RepetitionsParameter.ActualValue.Value; Type algorithmType = AlgorithmTypeParameter.ActualValue.Value; for (int i = 0; i < repetitions; i++) { for (int j = 0; j < problems.Count; j++) { IScope child = new Scope(string.Format("Problem {0}, Repetition {1}", j, i)); var algorithm = MetaOptimizationUtil.CreateParameterizedAlgorithmInstance(parameterConfiguration, algorithmType, (IProblem)problems[j].Clone()); child.Variables.Add(new Variable("Algorithm", algorithm)); child.Variables.Add(new Variable("ProblemIndex", new IntValue(j))); child.Variables.Add(new Variable("RepetitionIndex", new IntValue(i))); CurrentScope.SubScopes.Add(child); } } return base.Apply(); } } }