Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/AlgorithmRunsAnalyzer.cs @ 12803

Last change on this file since 12803 was 7253, checked in by gkronber, 13 years ago

#1727 set the number of evaluated solutions to 1 if the result is missing in the algorithm.

File size: 15.5 KB
RevLine 
[5653]1using System;
2using System.Collections.Generic;
3using System.Linq;
[6018]4using HeuristicLab.Common;
[5653]5using HeuristicLab.Core;
6using HeuristicLab.Data;
[6018]7using HeuristicLab.Operators;
[5653]8using HeuristicLab.Optimization;
[6018]9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[5653]11
12namespace HeuristicLab.Problems.MetaOptimization {
13  [Item("AlgorithmRunsAnalyzer", "")]
14  [StorableClass]
15  public class AlgorithmRunsAnalyzer : SingleSuccessorOperator {
16
17    #region Parameter properties
18    public ILookupParameter<DoubleValue> QualityParameter {
19      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
20    }
21    public LookupParameter<IntValue> GenerationsParameter {
22      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
23    }
24    public LookupParameter<IntValue> RepetitionsParameter {
25      get { return (LookupParameter<IntValue>)Parameters[MetaOptimizationProblem.RepetitionsParameterName]; }
26    }
27    public ILookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
28      get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
29    }
30    public ILookupParameter<IItemList<IProblem>> ProblemsParameter {
31      get { return (ILookupParameter<IItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
32    }
[6018]33    public LookupParameter<DoubleArray> ReferenceQualityAveragesParameter {
34      get { return (LookupParameter<DoubleArray>)Parameters["ReferenceQualityAverages"]; }
[5653]35    }
[6018]36    public LookupParameter<DoubleArray> ReferenceQualityDeviationsParameter {
37      get { return (LookupParameter<DoubleArray>)Parameters["ReferenceQualityDeviations"]; }
38    }
39    public LookupParameter<DoubleArray> ReferenceEvaluatedSolutionAveragesParameter {
40      get { return (LookupParameter<DoubleArray>)Parameters["ReferenceEvaluatedSolutionAverages"]; }
41    }
[5653]42    public LookupParameter<ResultCollection> ResultsParameter {
43      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
44    }
45    public ScopeTreeLookupParameter<IAlgorithm> AlgorithmParameter {
46      get { return (ScopeTreeLookupParameter<IAlgorithm>)Parameters["Algorithm"]; }
47    }
48    public ScopeTreeLookupParameter<IntValue> ProblemIndexParameter {
49      get { return (ScopeTreeLookupParameter<IntValue>)Parameters["ProblemIndex"]; }
50    }
51    public ScopeTreeLookupParameter<IntValue> RepetitionIndexParameter {
52      get { return (ScopeTreeLookupParameter<IntValue>)Parameters["RepetitionIndex"]; }
53    }
[6018]54    public LookupParameter<BoolValue> MaximizationParameter {
55      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
56    }
[6090]57    public LookupParameter<DoubleValue> QualityWeightParameter {
58      get { return (LookupParameter<DoubleValue>)Parameters[MetaOptimizationProblem.QualityWeightParameterName]; }
59    }
60    public LookupParameter<DoubleValue> StandardDeviationWeightParameter {
61      get { return (LookupParameter<DoubleValue>)Parameters[MetaOptimizationProblem.StandardDeviationWeightParameterName]; }
62    }
63    public LookupParameter<DoubleValue> EvaluatedSolutionsWeightParameter {
64      get { return (LookupParameter<DoubleValue>)Parameters[MetaOptimizationProblem.EvaluatedSolutionsWeightParameterName]; }
65    }
[6473]66    private ScopeParameter CurrentScopeParameter {
67      get { return (ScopeParameter)Parameters["CurrentScope"]; }
68    }
69    public IScope CurrentScope {
70      get { return CurrentScopeParameter.ActualValue; }
71    }
[6489]72    public LookupParameter<StringValue> QualityMeasureNameParameter {
73      get { return (LookupParameter<StringValue>)Parameters[MetaOptimizationProblem.QualityMeasureNameName]; }
74    }
[5653]75    #endregion
76
77    [StorableConstructor]
78    protected AlgorithmRunsAnalyzer(bool deserializing) : base(deserializing) { }
[6038]79    public AlgorithmRunsAnalyzer()
80      : base() {
[5653]81      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used to initialize the new random permutation."));
82      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the ParameterVector."));
83      Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
84      Parameters.Add(new LookupParameter<IntValue>(MetaOptimizationProblem.RepetitionsParameterName, "Number of evaluations on one problem."));
85      Parameters.Add(new LookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
86      Parameters.Add(new LookupParameter<IItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName, ""));
[6018]87      Parameters.Add(new LookupParameter<DoubleArray>("ReferenceQualityAverages", ""));
88      Parameters.Add(new LookupParameter<DoubleArray>("ReferenceQualityDeviations", ""));
89      Parameters.Add(new LookupParameter<DoubleArray>("ReferenceEvaluatedSolutionAverages", ""));
[5653]90      Parameters.Add(new LookupParameter<ResultCollection>("Results", ""));
91      Parameters.Add(new ScopeTreeLookupParameter<IAlgorithm>("Algorithm", "The finished algorithms containing Runs."));
92      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("ProblemIndex", "The index of the problem an algorithm was executed with."));
93      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("RepetitionIndex", "The index of the repetition"));
[6018]94      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
[6090]95      Parameters.Add(new LookupParameter<DoubleValue>(MetaOptimizationProblem.QualityWeightParameterName));
96      Parameters.Add(new LookupParameter<DoubleValue>(MetaOptimizationProblem.StandardDeviationWeightParameterName));
97      Parameters.Add(new LookupParameter<DoubleValue>(MetaOptimizationProblem.EvaluatedSolutionsWeightParameterName));
[6489]98      Parameters.Add(new LookupParameter<StringValue>(MetaOptimizationProblem.QualityMeasureNameName));
[6473]99      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes represent the parents."));
[5653]100    }
101    protected AlgorithmRunsAnalyzer(AlgorithmRunsAnalyzer original, Cloner cloner)
102      : base(original, cloner) {
103    }
104    public override IDeepCloneable Clone(Cloner cloner) {
105      return new AlgorithmRunsAnalyzer(this, cloner);
106    }
[6473]107    [StorableHook(HookType.AfterDeserialization)]
108    private void AfterDeserialization() {
109      if (!Parameters.ContainsKey("CurrentScope")) Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes represent the parents.")); // backwards compatibility
[6489]110      if (!Parameters.ContainsKey(MetaOptimizationProblem.QualityMeasureNameName)) Parameters.Add(new LookupParameter<StringValue>(MetaOptimizationProblem.QualityMeasureNameName)); // backwards compatibility
[6473]111    }
[5653]112
113    public override IOperation Apply() {
114      ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
115      ItemArray<IAlgorithm> algorithms = AlgorithmParameter.ActualValue;
116      ItemArray<IntValue> problemIndices = ProblemIndexParameter.ActualValue;
117      ItemArray<IntValue> repetitionIndices = RepetitionIndexParameter.ActualValue;
118      IEnumerable<string> parameterNames = parameterConfiguration.GetOptimizedParameterNames();
119      IItemList<IProblem> problems = ProblemsParameter.ActualValue;
[6018]120      bool maximization = MaximizationParameter.ActualValue.Value;
[5653]121      int repetitions = RepetitionsParameter.ActualValue.Value;
[6090]122      double qualityWeight = QualityWeightParameter.ActualValue.Value;
123      double standardDeviationWeight = StandardDeviationWeightParameter.ActualValue.Value;
124      double evaluatedSolutionsWeight = EvaluatedSolutionsWeightParameter.ActualValue.Value;
[6489]125      string qualityMeasureName = QualityMeasureNameParameter.ActualValue.Value;
126      var resultNames = new List<string> { qualityMeasureName, "Execution Time", "EvaluatedSolutions" };
[5653]127      int currentGeneration = GenerationsParameter.ActualValue != null ? GenerationsParameter.ActualValue.Value : 0;
[6018]128      double[] referenceQualityAverages;
129      double[] referenceQualityDeviations;
130      double[] referenceEvaluatedSolutionAverages;
131      GetReferenceValues(problems.Count, out referenceQualityAverages, out referenceQualityDeviations, out referenceEvaluatedSolutionAverages);
132
[5653]133      ResultCollection results = ResultsParameter.ActualValue;
134
135      if (algorithms.All(x => x.Runs.Count == 1)) {
136        var runs = new RunCollection();
137        var qualities = new double[problems.Count][];
138        var executionTimes = new TimeSpan[problems.Count][];
[6018]139        var evaluatedSolutions = new int[problems.Count][];
[5653]140
141        for (int i = 0; i < problems.Count; i++) {
142          qualities[i] = new double[repetitions];
[6018]143          evaluatedSolutions[i] = new int[repetitions];
[5653]144          executionTimes[i] = new TimeSpan[repetitions];
145        }
146
147        for (int i = 0; i < algorithms.Length; i++) {
148          int problemIndex = problemIndices[i].Value;
149          int repetitionIndex = repetitionIndices[i].Value;
150
[6197]151          IRun run = (IRun)algorithms[i].Runs.Single().Clone();
[5653]152          MetaOptimizationUtil.ClearResults(run, resultNames);
153          MetaOptimizationUtil.ClearParameters(run, parameterNames);
[6489]154          run.Results.Add("Meta-FromCache", new BoolValue(false));
155          run.Results.Add("Meta-Generation", new IntValue(currentGeneration));
156          run.Results.Add("Meta-ProblemIndex", new IntValue(problemIndex));
[5653]157          run.Name = string.Format("{0} Problem {1} Run {2}", parameterConfiguration.ParameterInfoString, problemIndex, repetitionIndex);
[7253]158
159          qualities[problemIndex][repetitionIndex] = GetResultValue<DoubleValue>(run.Results, qualityMeasureName).Value;
[5653]160          executionTimes[problemIndex][repetitionIndex] = (((TimeSpanValue)run.Results["Execution Time"]).Value);
[7253]161          if (run.Results.ContainsKey("EvaluatedSolutions")) {
162            evaluatedSolutions[problemIndex][repetitionIndex] = (((IntValue)run.Results["EvaluatedSolutions"]).Value);
163          } else {
164            evaluatedSolutions[problemIndex][repetitionIndex] = 1;
165          }
[6197]166          runs.Add(run);
[5653]167        }
168
169        parameterConfiguration.AverageExecutionTimes = new ItemList<TimeSpanValue>(executionTimes.Select(t => new TimeSpanValue(TimeSpan.FromMilliseconds(t.Average(ts => ts.TotalMilliseconds)))));
[6018]170        parameterConfiguration.AverageEvaluatedSolutions = new DoubleArray(evaluatedSolutions.Select(x => x.Average()).ToArray());
[5653]171        parameterConfiguration.Repetitions = new IntValue(repetitions);
172        parameterConfiguration.AverageQualities = new DoubleArray(qualities.Select(q => q.Average()).ToArray());
[6018]173
[6038]174        if (maximization)
[6018]175          parameterConfiguration.BestQualities = new DoubleArray(qualities.Select(q => q.Max()).ToArray());
176        else
177          parameterConfiguration.BestQualities = new DoubleArray(qualities.Select(q => q.Min()).ToArray());
[6038]178
[6018]179        if (maximization)
180          parameterConfiguration.WorstQualities = new DoubleArray(qualities.Select(q => q.Min()).ToArray());
181        else
182          parameterConfiguration.WorstQualities = new DoubleArray(qualities.Select(q => q.Max()).ToArray());
183
[5653]184        parameterConfiguration.QualityVariances = new DoubleArray(qualities.Select(q => q.Variance()).ToArray());
185        parameterConfiguration.QualityStandardDeviations = new DoubleArray(qualities.Select(q => q.StandardDeviation()).ToArray());
186        parameterConfiguration.Runs = runs;
187
[6090]188        this.QualityParameter.ActualValue = new DoubleValue(MetaOptimizationUtil.Normalize(parameterConfiguration, referenceQualityAverages, referenceQualityDeviations, referenceEvaluatedSolutionAverages, qualityWeight, standardDeviationWeight, evaluatedSolutionsWeight, maximization));
[5653]189      } else {
190        // something terrible happened -> most probably due to invalid parameters.
191        // penalty with worst quality from latest generation!
[6038]192        double penaltyValue;
193        if (maximization)
[6090]194          penaltyValue = results.ContainsKey("CurrentWorstQuality") ? ((DoubleValue)results["CurrentWorstQuality"].Value).Value : referenceQualityAverages.Min();
[6038]195        else
[6090]196          penaltyValue = results.ContainsKey("CurrentWorstQuality") ? ((DoubleValue)results["CurrentWorstQuality"].Value).Value : referenceQualityAverages.Max();
[6473]197
[5653]198        this.QualityParameter.ActualValue = new DoubleValue(penaltyValue);
[6090]199        parameterConfiguration.Quality = new DoubleValue(penaltyValue);
200
201        parameterConfiguration.AverageExecutionTimes = new ItemList<TimeSpanValue>(Enumerable.Repeat(new TimeSpanValue(TimeSpan.Zero), problems.Count));
202        parameterConfiguration.AverageEvaluatedSolutions = new DoubleArray(Enumerable.Repeat(0.0, problems.Count).ToArray());
203        parameterConfiguration.Repetitions = new IntValue(repetitions);
204        parameterConfiguration.AverageQualities = new DoubleArray(Enumerable.Repeat(0.0, problems.Count).ToArray());
205        parameterConfiguration.BestQualities = new DoubleArray(Enumerable.Repeat(0.0, problems.Count).ToArray());
206        parameterConfiguration.WorstQualities = new DoubleArray(Enumerable.Repeat(0.0, problems.Count).ToArray());
207        parameterConfiguration.QualityVariances = new DoubleArray(Enumerable.Repeat(0.0, problems.Count).ToArray());
208        parameterConfiguration.QualityStandardDeviations = new DoubleArray(Enumerable.Repeat(0.0, problems.Count).ToArray());
209        parameterConfiguration.Runs = null;
[5653]210      }
211
[6473]212      // in OSGA there are more subscopes, so be careful which to delete
213      CurrentScope.SubScopes.RemoveAll(x => x.Variables.Count((v) => (v.Name == "RepetitionCount")) == 1);
[7253]214
[5653]215      return base.Apply();
216    }
217
[6489]218    private T1 GetResultValue<T1>(IDictionary<string, IItem> results, string resultName) {
219      return (T1)results[resultName];
220
221      //string separator = ".";
222      //string[] tokens = resultName.Split(separator.ToCharArray());
223
224      //IDictionary<string, IItem> currentResults = results;
225      //IItem currentResult = null;
226      //for (int i = 0; i < tokens.Length; i++) {
227      //  if(currentResults == null)
228      //    throw new KeyNotFoundException("Result value " + resultName + " was not found");
229      //  if (currentResults.ContainsKey(tokens[i])) {
230      //    currentResult = currentResults[tokens[i]];
231      //    currentResults = currentResult as IDictionary<string, IItem>;
232      //  } else {
233      //    throw new KeyNotFoundException("Result value " + resultName + " was not found");
234      //  }
235      //}
236      //return (T1)currentResult;
237    }
238
[6018]239    private void GetReferenceValues(int problemsCount, out double[] referenceQualityAverages, out double[] referenceQualityDeviations, out double[] referenceEvaluatedSolutionAverages) {
240      if (ReferenceQualityAveragesParameter.ActualValue == null) {
[5653]241        // this is generation zero. no reference qualities for normalization have been calculated yet. in this special case the ReferenceQualityAnalyzer will do the normalization
[6018]242        referenceQualityAverages = new double[problemsCount];
243        referenceQualityDeviations = new double[problemsCount];
244        referenceEvaluatedSolutionAverages = new double[problemsCount];
245        for (int i = 0; i < referenceQualityAverages.Length; i++) {
246          referenceQualityAverages[i] = 1;
247          referenceQualityDeviations[i] = 1;
248          referenceEvaluatedSolutionAverages[i] = 1;
[5653]249        }
250      } else {
[6018]251        referenceQualityAverages = ReferenceQualityAveragesParameter.ActualValue.ToArray();
252        referenceQualityDeviations = ReferenceQualityDeviationsParameter.ActualValue.ToArray();
253        referenceEvaluatedSolutionAverages = ReferenceEvaluatedSolutionAveragesParameter.ActualValue.ToArray();
[5653]254      }
255    }
256  }
257}
Note: See TracBrowser for help on using the repository browser.