using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HEAL.Attic; namespace HeuristicLab.Problems.MetaOptimization { [Item("AlgorithmEvaluator", "")] [StorableType("955A1FA9-487C-4EB6-B6D4-4BE07BCD3DE3")] public class AlgorithmEvaluator : SingleSuccessorOperator { #region Parameter properties public ILookupParameter RandomParameter { get { return (LookupParameter)Parameters["Random"]; } } public ILookupParameter AlgorithmParameter { get { return (LookupParameter)Parameters["Algorithm"]; } } public ILookupParameter ProblemIndexParameter { get { return (LookupParameter)Parameters["ProblemIndex"]; } } public ILookupParameter RepetitionIndexParameter { get { return (LookupParameter)Parameters["RepetitionIndex"]; } } public ILookupParameter ParameterConfigurationParameter { get { return (ILookupParameter)Parameters["ParameterConfigurationTree"]; } } //public IValueParameter> RunsParameter { // get { return (IValueParameter>)Parameters["Runs"]; } //} public LookupParameter ResultsParameter { get { return (LookupParameter)Parameters["Results"]; } } #endregion [StorableConstructor] protected AlgorithmEvaluator(StorableConstructorFlag _) : base(_) { } public AlgorithmEvaluator() : base() { Parameters.Add(new LookupParameter("Random", "The pseudo random number generator which should be used to initialize the new random permutation.")); Parameters.Add(new LookupParameter("Algorithm", "")); Parameters.Add(new LookupParameter("ProblemIndex", "")); Parameters.Add(new LookupParameter("RepetitionIndex", "")); Parameters.Add(new LookupParameter("ParameterConfigurationTree", "")); Parameters.Add(new LookupParameter("Results", "")); } protected AlgorithmEvaluator(AlgorithmEvaluator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new AlgorithmEvaluator(this, cloner); } public override IOperation Apply() { bool isValidScope = RepetitionIndexParameter.ActualValue != null; // this is a workaround for OSGA, where the PMOEvaluator is executed on scopes which happen to contain additional subscopes which collide with the subscopes created for the repetitions/problems in PMOEvaluator if (!isValidScope) return base.Apply(); ItemDictionary solutionCache = ResultsParameter.ActualValue.ContainsKey("SolutionCache") ? (ItemDictionary)ResultsParameter.ActualValue["SolutionCache"].Value : null; ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue; IAlgorithm algorithm = AlgorithmParameter.ActualValue; int repetitionIndex = RepetitionIndexParameter.ActualValue.Value; if (solutionCache != null && solutionCache.Count(x => x.Key.Value == parameterConfiguration.ParameterInfoString) > 0) { algorithm.Runs.Add(solutionCache.Single(x => x.Key.Value == parameterConfiguration.ParameterInfoString).Value.ElementAt(repetitionIndex)); } else { algorithm.StartSync(CancellationToken); } return base.Apply(); } } }