1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 |
|
---|
8 | namespace 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 NormalizeQualities(ParameterConfigurationTree parameterConfigurationTree, double[] referenceQualities) {
|
---|
37 | double[] qualitiesNormalized = new double[referenceQualities.Length];
|
---|
38 | for (int i = 0; i < referenceQualities.Length; i++) {
|
---|
39 | qualitiesNormalized[i] = parameterConfigurationTree.AverageQualities[i] / referenceQualities[i];
|
---|
40 | }
|
---|
41 | parameterConfigurationTree.QualitiesNormalized = new DoubleArray(qualitiesNormalized);
|
---|
42 | parameterConfigurationTree.AverageQualityNormalized = new DoubleValue(qualitiesNormalized.Average());
|
---|
43 | return parameterConfigurationTree.AverageQualityNormalized.Value;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /// <summary>
|
---|
47 | /// Creates a new instance of algorithmType, sets the given problem and parameterizes it with the given configuration
|
---|
48 | /// </summary>
|
---|
49 | public static IAlgorithm CreateParameterizedAlgorithmInstance(ParameterConfigurationTree parameterConfigurationTree, Type algorithmType, IProblem problem, bool randomize = false, IRandom random = null) {
|
---|
50 | var algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
|
---|
51 | algorithm.Problem = problem;
|
---|
52 | ((EngineAlgorithm)algorithm).Engine = new SequentialEngine.SequentialEngine();
|
---|
53 | if(randomize) parameterConfigurationTree.Randomize(random);
|
---|
54 | parameterConfigurationTree.Parameterize(algorithm);
|
---|
55 | return algorithm;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|