Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationUtil.cs @ 10160

Last change on this file since 10160 was 6486, checked in by cneumuel, 13 years ago

#1215

  • fixed generating combinations for non EngineAlgoritms
File size: 4.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Optimization;
7
8namespace HeuristicLab.Problems.MetaOptimization {
9  public static class MetaOptimizationUtil {
10    /// <summary>
11    /// Removes those results from the run which are not declared in resultsToKeep
12    /// </summary>
13    public static void ClearResults(IRun run, IEnumerable<string> resultsToKeep) {
14      var resultsToRemove = new List<string>();
15      foreach (var result in run.Results) {
16        if (!resultsToKeep.Contains(result.Key))
17          resultsToRemove.Add(result.Key);
18      }
19      foreach (var result in resultsToRemove)
20        run.Results.Remove(result);
21    }
22
23    /// <summary>
24    /// Removes those parameters from the run which are not declared in parametersToKeep
25    /// </summary>
26    public static void ClearParameters(IRun run, IEnumerable<string> parametersToKeep) {
27      var parametersToRemove = new List<string>();
28      foreach (var parameter in run.Parameters) {
29        if (!parametersToKeep.Contains(parameter.Key))
30          parametersToRemove.Add(parameter.Key);
31      }
32      foreach (var parameter in parametersToRemove)
33        run.Parameters.Remove(parameter);
34    }
35
36    public static double Normalize(
37          ParameterConfigurationTree parameterConfigurationTree,
38          double[] referenceQualityAverages,
39          double[] referenceQualityDeviations,
40          double[] referenceEvaluatedSolutionAverages,
41          double qualityAveragesWeight,
42          double qualityDeviationsWeight,
43          double evaluatedSolutionsWeight,
44          bool maximization) {
45
46      double[] qualityAveragesNormalized = new double[referenceQualityAverages.Length];
47      double[] qualityDeviationsNormalized = new double[referenceQualityDeviations.Length];
48      double[] evaluatedSolutionAveragesNormalized = new double[referenceEvaluatedSolutionAverages.Length];
49
50      for (int i = 0; i < referenceQualityAverages.Length; i++) {
51        qualityAveragesNormalized[i] = parameterConfigurationTree.AverageQualities[i] / referenceQualityAverages[i];
52        qualityDeviationsNormalized[i] = parameterConfigurationTree.QualityStandardDeviations[i] / referenceQualityDeviations[i];
53        evaluatedSolutionAveragesNormalized[i] = parameterConfigurationTree.AverageEvaluatedSolutions[i] / referenceEvaluatedSolutionAverages[i];
54      }
55      parameterConfigurationTree.NormalizedQualityAverages = new DoubleArray(qualityAveragesNormalized);
56      parameterConfigurationTree.NormalizedQualityDeviations = new DoubleArray(qualityDeviationsNormalized);
57      parameterConfigurationTree.NormalizedEvaluatedSolutions = new DoubleArray(evaluatedSolutionAveragesNormalized);
58
59      double qualityAveragesNormalizedValue = qualityAveragesNormalized.Average();
60      double qualityDeviationsNormalizedValue = qualityDeviationsNormalized.Average();
61      double evaluatedSolutionAveragesNormalizedValue = evaluatedSolutionAveragesNormalized.Average();
62
63      // deviation and evaluatedSolutions are always minimization problems. so if maximization=true, flip the values around 1.0 (e.g. 1.15 -> 0.85)
64      if (maximization) {
65        qualityDeviationsNormalizedValue -= (qualityDeviationsNormalizedValue - 1) * 2;
66        evaluatedSolutionAveragesNormalizedValue -= (evaluatedSolutionAveragesNormalizedValue - 1) * 2;
67      }
68
69      // apply weights
70      qualityAveragesNormalizedValue *= qualityAveragesWeight;
71      qualityDeviationsNormalizedValue *= qualityDeviationsWeight;
72      evaluatedSolutionAveragesNormalizedValue *= evaluatedSolutionsWeight;
73
74      double weightSum = qualityAveragesWeight + qualityDeviationsWeight + evaluatedSolutionsWeight;
75      parameterConfigurationTree.Quality = new DoubleValue((qualityAveragesNormalizedValue + qualityDeviationsNormalizedValue + evaluatedSolutionAveragesNormalizedValue) / weightSum);
76
77      return parameterConfigurationTree.Quality.Value;
78    }
79
80    /// <summary>
81    /// Creates a new instance of algorithmType, sets the given problem and parameterizes it with the given configuration
82    /// </summary>
83    public static IAlgorithm CreateParameterizedAlgorithmInstance(ParameterConfigurationTree parameterConfigurationTree, Type algorithmType, IProblem problem, bool randomize = false, IRandom random = null) {
84      var algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
85      algorithm.Problem = problem;
86      if (algorithm is EngineAlgorithm) {
87        ((EngineAlgorithm)algorithm).Engine = new SequentialEngine.SequentialEngine();
88      }
89      if (randomize) parameterConfigurationTree.Randomize(random);
90      parameterConfigurationTree.Parameterize(algorithm);
91      return algorithm;
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.