Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.Encodings.ParameterConfigurationTreeEncoding/3.3/MetaOptimizationUtil.cs @ 14128

Last change on this file since 14128 was 7840, checked in by spimming, 13 years ago

#1853:

  • included files from metaopt branch
  • ignored mutation and crossover
  • updated plugin frame
File size: 5.6 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.Optimization;
28
29namespace HeuristicLab.Encodings.ParameterConfigurationTreeEncoding {
30  public static class MetaOptimizationUtil {
31    /// <summary>
32    /// Removes those results from the run which are not declared in resultsToKeep
33    /// </summary>
34    public static void ClearResults(IRun run, IEnumerable<string> resultsToKeep) {
35      var resultsToRemove = new List<string>();
36      foreach (var result in run.Results) {
37        if (!resultsToKeep.Contains(result.Key))
38          resultsToRemove.Add(result.Key);
39      }
40      foreach (var result in resultsToRemove)
41        run.Results.Remove(result);
42    }
43
44    /// <summary>
45    /// Removes those parameters from the run which are not declared in parametersToKeep
46    /// </summary>
47    public static void ClearParameters(IRun run, IEnumerable<string> parametersToKeep) {
48      var parametersToRemove = new List<string>();
49      foreach (var parameter in run.Parameters) {
50        if (!parametersToKeep.Contains(parameter.Key))
51          parametersToRemove.Add(parameter.Key);
52      }
53      foreach (var parameter in parametersToRemove)
54        run.Parameters.Remove(parameter);
55    }
56
57    public static double Normalize(
58          ParameterConfigurationTree parameterConfigurationTree,
59          double[] referenceQualityAverages,
60          double[] referenceQualityDeviations,
61          double[] referenceEvaluatedSolutionAverages,
62          double qualityAveragesWeight,
63          double qualityDeviationsWeight,
64          double evaluatedSolutionsWeight,
65          bool maximization) {
66
67      double[] qualityAveragesNormalized = new double[referenceQualityAverages.Length];
68      double[] qualityDeviationsNormalized = new double[referenceQualityDeviations.Length];
69      double[] evaluatedSolutionAveragesNormalized = new double[referenceEvaluatedSolutionAverages.Length];
70
71      for (int i = 0; i < referenceQualityAverages.Length; i++) {
72        qualityAveragesNormalized[i] = parameterConfigurationTree.AverageQualities[i] / referenceQualityAverages[i];
73        qualityDeviationsNormalized[i] = parameterConfigurationTree.QualityStandardDeviations[i] / referenceQualityDeviations[i];
74        evaluatedSolutionAveragesNormalized[i] = parameterConfigurationTree.AverageEvaluatedSolutions[i] / referenceEvaluatedSolutionAverages[i];
75      }
76      parameterConfigurationTree.NormalizedQualityAverages = new DoubleArray(qualityAveragesNormalized);
77      parameterConfigurationTree.NormalizedQualityDeviations = new DoubleArray(qualityDeviationsNormalized);
78      parameterConfigurationTree.NormalizedEvaluatedSolutions = new DoubleArray(evaluatedSolutionAveragesNormalized);
79
80      double qualityAveragesNormalizedValue = qualityAveragesNormalized.Average();
81      double qualityDeviationsNormalizedValue = qualityDeviationsNormalized.Average();
82      double evaluatedSolutionAveragesNormalizedValue = evaluatedSolutionAveragesNormalized.Average();
83
84      // deviation and evaluatedSolutions are always minimization problems. so if maximization=true, flip the values around 1.0 (e.g. 1.15 -> 0.85)
85      if (maximization) {
86        qualityDeviationsNormalizedValue -= (qualityDeviationsNormalizedValue - 1) * 2;
87        evaluatedSolutionAveragesNormalizedValue -= (evaluatedSolutionAveragesNormalizedValue - 1) * 2;
88      }
89
90      // apply weights
91      qualityAveragesNormalizedValue *= qualityAveragesWeight;
92      qualityDeviationsNormalizedValue *= qualityDeviationsWeight;
93      evaluatedSolutionAveragesNormalizedValue *= evaluatedSolutionsWeight;
94
95      double weightSum = qualityAveragesWeight + qualityDeviationsWeight + evaluatedSolutionsWeight;
96      parameterConfigurationTree.Quality = new DoubleValue((qualityAveragesNormalizedValue + qualityDeviationsNormalizedValue + evaluatedSolutionAveragesNormalizedValue) / weightSum);
97
98      return parameterConfigurationTree.Quality.Value;
99    }
100
101    /// <summary>
102    /// Creates a new instance of algorithmType, sets the given problem and parameterizes it with the given configuration
103    /// </summary>
104    public static IAlgorithm CreateParameterizedAlgorithmInstance(ParameterConfigurationTree parameterConfigurationTree, Type algorithmType, IProblem problem, bool randomize = false, IRandom random = null) {
105      var algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
106      algorithm.Problem = problem;
107      if (algorithm is EngineAlgorithm) {
108        ((EngineAlgorithm)algorithm).Engine = new SequentialEngine.SequentialEngine();
109      }
110      if (randomize) parameterConfigurationTree.Randomize(random);
111      parameterConfigurationTree.Parameterize(algorithm);
112      return algorithm;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.