Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/17/11 01:04:30 (14 years ago)
Author:
cneumuel
Message:

#1215

  • manipulators for one and all parameters
  • SolutionCache to avoid multiple evaluations of equal solutions
  • RunsAnalyzer which stores all base level runs
  • ItemDictionaryView for runs
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/ParameterConfigurationEvaluator.cs

    r5293 r5303  
    4343      get { return (LookupParameter<DoubleArray>)Parameters["ProblemQualityReferences"]; }
    4444    }
    45 
     45    public LookupParameter<IntValue> GenerationsParameter {
     46      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
     47    }
     48    public LookupParameter<ResultCollection> ResultsParameter {
     49      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
     50    }
     51   
    4652    public IntValue Repetitions {
    4753      get { return RepetitionsParameter.ActualValue; }
     
    5662      Parameters.Add(new LookupParameter<IntValue>(MetaOptimizationProblem.RepetitionsParameterName, "Number of evaluations on one problem."));
    5763      Parameters.Add(new LookupParameter<DoubleArray>("ProblemQualityReferences", ""));
     64      Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
     65      Parameters.Add(new LookupParameter<ResultCollection>("Results", ""));
    5866    }
    5967
     
    6876
    6977    public override IOperation Apply() {
    70       EngineAlgorithm algorithm = (EngineAlgorithm)AlgorithmParameter.ActualValue.Clone();
     78      ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
     79      EngineAlgorithm algorithm = (EngineAlgorithm)AlgorithmParameter.ActualValue;
    7180      IItemList<ISingleObjectiveProblem> problems = ProblemsParameter.ActualValue;
    72       ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
     81      ItemDictionary<StringValue, RunCollection> runsCache = ResultsParameter.ActualValue.ContainsKey("Runs") ? (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["Runs"].Value : null;
     82      double[] referenceQualities = GetReferenceQualities(problems);
    7383
    74       double[] referenceQualities;
    75       if (ProblemQualityReferencesParameter.ActualValue == null) {
    76         // this is generation zero. no reference qualities for normalization have been calculated yet.
    77         referenceQualities = new double[problems.Count];
    78         for (int i = 0; i < referenceQualities.Length; i++) {
    79           referenceQualities[i] = 1;
    80         }
     84      RunCollection runs;
     85      if (runsCache != null && runsCache.Count(x => x.Key.Value == parameterConfiguration.ParameterInfoString) > 0) {
     86        runs = runsCache.Single(x => x.Key.Value == parameterConfiguration.ParameterInfoString).Value;
     87        Console.WriteLine("Used Cache for {0}", parameterConfiguration.ParameterInfoString);
    8188      } else {
    82         referenceQualities = ProblemQualityReferencesParameter.ActualValue.ToArray();
     89        runs = ExecuteAlgorithm(parameterConfiguration, algorithm, problems);
    8390      }
    84 
    85       // set parameters
    86       parameterConfiguration.Parameterize(algorithm);
    87       algorithm.StoreAlgorithmInEachRun = false;
    8891
    8992      List<List<double>> qualities = new List<List<double>>();
    9093      List<List<TimeSpan>> executionTimes = new List<List<TimeSpan>>();
    91       algorithm.Engine = new SequentialEngine.SequentialEngine();
    92       algorithm.Prepare(true);
    9394
    94       foreach (ISingleObjectiveProblem problem in problems) {
    95         algorithm.Problem = (IProblem)problem.Clone();
    96         var problemQualities = new List<double>();
    97         var problemExecutionTimes = new List<TimeSpan>();
    98 
    99         for (int i = 0; i < Repetitions.Value; i++) {
    100           algorithm.Prepare();
    101 
    102           AlgorithmExecutor executor = new AlgorithmExecutor(algorithm);
    103           executor.StartSync();
    104 
    105           if (algorithm.ExecutionState == ExecutionState.Paused) {
    106             // this parametercombination was bad. set penalty for this solution
    107             problemQualities.Add(PenaltyQuality); // todo: respect Maximization; problem: this messes up average value; solution: use currently worst quality*2
    108             problemExecutionTimes.Add(algorithm.ExecutionTime);
    109           } else {
    110             problemQualities.Add(((DoubleValue)algorithm.Results["BestQuality"].Value).Value);
    111             problemExecutionTimes.Add(algorithm.ExecutionTime);
    112 
    113             // parameters will be stored in ParameterConfigurationTree anyway. they would be redundant in runs
    114             algorithm.Runs.Last().Parameters.Clear();
    115             // but keep the problem, since this differs in runs
    116             algorithm.Runs.Last().Parameters.Add("Problem", problem);
    117           }
    118         }
    119         qualities.Add(problemQualities);
    120         executionTimes.Add(problemExecutionTimes);
     95      for (int i = 0; i < problems.Count; i++) {
     96        var problemRuns = runs.Where(x => ((IntValue)x.Results["Meta.ProblemIndex"]).Value == i + 1);
     97        qualities.Add(problemRuns.Select(x => ((DoubleValue)x.Results["BestQuality"]).Value).ToList());
     98        executionTimes.Add(problemRuns.Select(x => ((TimeSpanValue)x.Results["Execution Time"]).Value).ToList());
    12199      }
    122       algorithm.Prepare();
    123100
    124101      parameterConfiguration.AverageExecutionTimes = new ItemList<TimeSpanValue>(executionTimes.Select(t => new TimeSpanValue(TimeSpan.FromMilliseconds(t.Average(ts => ts.TotalMilliseconds)))));
     
    129106      parameterConfiguration.QualityVariances = new DoubleArray(qualities.Select(q => q.Variance()).ToArray());
    130107      parameterConfiguration.QualityStandardDeviations = new DoubleArray(qualities.Select(q => q.StandardDeviation()).ToArray());
    131       parameterConfiguration.Runs = (RunCollection)algorithm.Runs.Clone();
     108      parameterConfiguration.Runs = (RunCollection)runs.Clone();
    132109
    133110      this.QualityParameter.ActualValue = new DoubleValue(NormalizeQualities(parameterConfiguration, referenceQualities));
    134111
    135112      return base.Apply();
     113    }
     114
     115    private double[] GetReferenceQualities(IItemList<ISingleObjectiveProblem> problems) {
     116      double[] referenceQualities;
     117      if (ProblemQualityReferencesParameter.ActualValue == null) {
     118        // this is generation zero. no reference qualities for normalization have been calculated yet. in this special case the ReferenceQualityAnalyzer will do the normalization
     119        referenceQualities = new double[problems.Count];
     120        for (int i = 0; i < referenceQualities.Length; i++) {
     121          referenceQualities[i] = 1;
     122        }
     123      } else {
     124        referenceQualities = ProblemQualityReferencesParameter.ActualValue.ToArray();
     125      }
     126      return referenceQualities;
     127    }
     128
     129    private RunCollection ExecuteAlgorithm(ParameterConfigurationTree parameterConfiguration, EngineAlgorithm algorithm, IItemList<ISingleObjectiveProblem> problems) {
     130      EngineAlgorithm algorithmClone = (EngineAlgorithm)algorithm.Clone();
     131
     132      // set parameters
     133      parameterConfiguration.Parameterize(algorithmClone);
     134      algorithmClone.StoreAlgorithmInEachRun = false;
     135
     136      algorithmClone.Engine = new SequentialEngine.SequentialEngine();
     137      algorithmClone.Prepare(true);
     138
     139      foreach (ISingleObjectiveProblem problem in problems) {
     140        algorithmClone.Problem = (IProblem)problem.Clone();
     141
     142        for (int i = 0; i < Repetitions.Value; i++) {
     143          algorithmClone.Prepare();
     144
     145          AlgorithmExecutor executor = new AlgorithmExecutor(algorithmClone);
     146          executor.StartSync();
     147
     148          if (algorithmClone.ExecutionState == ExecutionState.Paused) {
     149            // this parametercombination was bad. set penalty for this solution
     150            // TODO!
     151          }
     152          int problemIndex = problems.IndexOf(problem) + 1;
     153          IRun run = algorithmClone.Runs.Last();
     154          run.Results.Add("Meta.FromCache", new BoolValue(false));
     155          run.Results.Add("Meta.Generation", new IntValue(GenerationsParameter.ActualValue != null ? GenerationsParameter.ActualValue.Value : 0));
     156          run.Results.Add("Meta.ProblemIndex", new IntValue(problemIndex));
     157          int runCountOfThisProblem = algorithmClone.Runs.Where(x => ((IntValue)x.Results["Meta.ProblemIndex"]).Value == problemIndex).Count();
     158          run.Name = string.Format("{0} Problem {1} Run {2}", parameterConfiguration.ParameterInfoString, problemIndex, runCountOfThisProblem);
     159        }
     160      }
     161      algorithmClone.Prepare();
     162      return algorithmClone.Runs;
    136163    }
    137164
Note: See TracChangeset for help on using the changeset viewer.