using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
namespace HeuristicLab.Problems.MetaOptimization {
public static class MetaOptimizationUtil {
///
/// Removes those results from the run which are not declared in resultsToKeep
///
public static void ClearResults(IRun run, IEnumerable resultsToKeep) {
var resultsToRemove = new List();
foreach (var result in run.Results) {
if (!resultsToKeep.Contains(result.Key))
resultsToRemove.Add(result.Key);
}
foreach (var result in resultsToRemove)
run.Results.Remove(result);
}
///
/// Removes those parameters from the run which are not declared in parametersToKeep
///
public static void ClearParameters(IRun run, IEnumerable parametersToKeep) {
var parametersToRemove = new List();
foreach (var parameter in run.Parameters) {
if (!parametersToKeep.Contains(parameter.Key))
parametersToRemove.Add(parameter.Key);
}
foreach (var parameter in parametersToRemove)
run.Parameters.Remove(parameter);
}
public static double NormalizeQualities(ParameterConfigurationTree parameterConfigurationTree, double[] referenceQualities) {
double[] qualitiesNormalized = new double[referenceQualities.Length];
for (int i = 0; i < referenceQualities.Length; i++) {
qualitiesNormalized[i] = parameterConfigurationTree.AverageQualities[i] / referenceQualities[i];
}
parameterConfigurationTree.QualitiesNormalized = new DoubleArray(qualitiesNormalized);
parameterConfigurationTree.AverageQualityNormalized = new DoubleValue(qualitiesNormalized.Average());
return parameterConfigurationTree.AverageQualityNormalized.Value;
}
///
/// Creates a new instance of algorithmType, sets the given problem and parameterizes it with the given configuration
///
public static IAlgorithm CreateParameterizedAlgorithmInstance(ParameterConfigurationTree parameterConfigurationTree, Type algorithmType, IProblem problem, bool randomize = false, IRandom random = null) {
var algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
algorithm.Problem = problem;
((EngineAlgorithm)algorithm).Engine = new SequentialEngine.SequentialEngine();
if(randomize) parameterConfigurationTree.Randomize(random);
parameterConfigurationTree.Parameterize(algorithm);
return algorithm;
}
}
}