Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization (trunk integration)/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationUtil.cs @ 9562

Last change on this file since 9562 was 8589, checked in by jkarder, 12 years ago

#1727:

  • set the number of evaluated solutions to zero if the result is missing in the run
  • improved result lookup
File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.ParameterConfigurationEncoding;
28using HeuristicLab.Optimization;
29
30namespace HeuristicLab.Problems.MetaOptimization {
31  public static class MetaOptimizationUtil {
32    /// <summary>
33    /// Removes those results from the run which are not declared in resultsToKeep
34    /// </summary>
35    public static void ClearResults(IRun run, IEnumerable<string> resultsToKeep) {
36      var resultsToRemove = new List<string>();
37      foreach (var result in run.Results) {
38        if (!resultsToKeep.Contains(result.Key))
39          resultsToRemove.Add(result.Key);
40      }
41      foreach (var result in resultsToRemove)
42        run.Results.Remove(result);
43    }
44
45    /// <summary>
46    /// Removes those parameters from the run which are not declared in parametersToKeep
47    /// </summary>
48    public static void ClearParameters(IRun run, IEnumerable<string> parametersToKeep) {
49      var parametersToRemove = new List<string>();
50      foreach (var parameter in run.Parameters) {
51        if (!parametersToKeep.Contains(parameter.Key))
52          parametersToRemove.Add(parameter.Key);
53      }
54      foreach (var parameter in parametersToRemove)
55        run.Parameters.Remove(parameter);
56    }
57
58    public static double Normalize(
59          ParameterConfigurationTree parameterConfigurationTree,
60          double[] referenceQualityAverages,
61          double[] referenceQualityDeviations,
62          double[] referenceEvaluatedSolutionAverages,
63          double qualityAveragesWeight,
64          double qualityDeviationsWeight,
65          double evaluatedSolutionsWeight,
66          bool maximization) {
67
68      double[] qualityAveragesNormalized = new double[referenceQualityAverages.Length];
69      double[] qualityDeviationsNormalized = new double[referenceQualityDeviations.Length];
70      double[] evaluatedSolutionAveragesNormalized = new double[referenceEvaluatedSolutionAverages.Length];
71
72      for (int i = 0; i < referenceQualityAverages.Length; i++) {
73        qualityAveragesNormalized[i] = parameterConfigurationTree.AverageQualities[i] / referenceQualityAverages[i];
74        qualityDeviationsNormalized[i] = parameterConfigurationTree.QualityStandardDeviations[i] / referenceQualityDeviations[i];
75        evaluatedSolutionAveragesNormalized[i] = parameterConfigurationTree.AverageEvaluatedSolutions[i] / referenceEvaluatedSolutionAverages[i];
76        if (double.IsNaN(evaluatedSolutionAveragesNormalized[i])) evaluatedSolutionAveragesNormalized[i] = 0.0;
77      }
78      parameterConfigurationTree.NormalizedQualityAverages = new DoubleArray(qualityAveragesNormalized);
79      parameterConfigurationTree.NormalizedQualityDeviations = new DoubleArray(qualityDeviationsNormalized);
80      parameterConfigurationTree.NormalizedEvaluatedSolutions = new DoubleArray(evaluatedSolutionAveragesNormalized);
81
82      double qualityAveragesNormalizedValue = qualityAveragesNormalized.Average();
83      double qualityDeviationsNormalizedValue = qualityDeviationsNormalized.Average();
84      double evaluatedSolutionAveragesNormalizedValue = evaluatedSolutionAveragesNormalized.Average();
85
86      // deviation and evaluatedSolutions are always minimization problems. so if maximization=true, flip the values around 1.0 (e.g. 1.15 -> 0.85)
87      if (maximization) {
88        qualityDeviationsNormalizedValue -= (qualityDeviationsNormalizedValue - 1) * 2;
89        evaluatedSolutionAveragesNormalizedValue -= (evaluatedSolutionAveragesNormalizedValue - 1) * 2;
90      }
91
92      // apply weights
93      qualityAveragesNormalizedValue *= qualityAveragesWeight;
94      qualityDeviationsNormalizedValue *= qualityDeviationsWeight;
95      evaluatedSolutionAveragesNormalizedValue *= evaluatedSolutionsWeight;
96
97      double weightSum = qualityAveragesWeight + qualityDeviationsWeight + evaluatedSolutionsWeight;
98      parameterConfigurationTree.Quality = new DoubleValue((qualityAveragesNormalizedValue + qualityDeviationsNormalizedValue + evaluatedSolutionAveragesNormalizedValue) / weightSum);
99
100      return parameterConfigurationTree.Quality.Value;
101    }
102
103    /// <summary>
104    /// Creates a new instance of algorithmType, sets the given problem and parameterizes it with the given configuration
105    /// </summary>
106    public static IAlgorithm CreateParameterizedAlgorithmInstance(ParameterConfigurationTree parameterConfigurationTree, Type algorithmType, IProblem problem, bool randomize = false, IRandom random = null) {
107      var algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
108      algorithm.Problem = problem;
109      if (algorithm is EngineAlgorithm) {
110        ((EngineAlgorithm)algorithm).Engine = new SequentialEngine.SequentialEngine();
111      }
112      if (randomize) parameterConfigurationTree.Randomize(random);
113      parameterConfigurationTree.Parameterize(algorithm);
114      return algorithm;
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.