#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Core;
using HeuristicLab.Optimization;
using HeuristicLab.Common;
using HeuristicLab.Parameters;
using HeuristicLab.Data;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Problems.MetaOptimization {
[Item("Meta Optimization Problem", "Represents a Meta Optimization Problem.")]
[Creatable("Problems")]
[StorableClass]
public sealed class MetaOptimizationProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
public string Filename { get; set; }
public override Image ItemImage {
get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
}
#region Parameter Properties
public ValueParameter RepetitionsParameter {
get { return (ValueParameter)Parameters["Repetitions"]; }
}
public ValueParameter SolutionCreatorParameter {
get { return (ValueParameter)Parameters["SolutionCreator"]; }
}
IParameter IProblem.SolutionCreatorParameter {
get { return SolutionCreatorParameter; }
}
public ValueParameter EvaluatorParameter {
get { return (ValueParameter)Parameters["Evaluator"]; }
}
IParameter IProblem.EvaluatorParameter {
get { return EvaluatorParameter; }
}
public OptionalValueParameter BestKnownSolutionParameter {
get { return (OptionalValueParameter)Parameters["BestKnownSolution"]; }
}
public ValueParameter MaximizationParameter {
get { return (ValueParameter)Parameters["Maximization"]; }
}
IParameter ISingleObjectiveProblem.MaximizationParameter {
get { return MaximizationParameter; }
}
public OptionalValueParameter BestKnownQualityParameter {
get { return (OptionalValueParameter)Parameters["BestKnownQuality"]; }
}
IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
get { return BestKnownQualityParameter; }
}
public ValueParameter AlgorithmParameter {
get { return (ValueParameter)Parameters["Algorithm"]; }
}
public ValueParameter ParametersToOptimizeParameter {
get { return (ValueParameter)Parameters["ParametersToOptimize"]; }
}
#endregion
#region Properties
public IntValue Repetitions {
get { return RepetitionsParameter.Value; }
set { RepetitionsParameter.Value = value; }
}
public IEnumerable Operators {
get { return operators; }
}
IEvaluator IProblem.Evaluator {
get { return EvaluatorParameter.Value; }
}
public IMetaOptimizationEvaluator Evaluator {
get { return EvaluatorParameter.Value; }
set { EvaluatorParameter.Value = value; }
}
ISolutionCreator IProblem.SolutionCreator {
get { return SolutionCreatorParameter.Value; }
}
public IParameterSetCreator SolutionCreator {
get { return SolutionCreatorParameter.Value; }
}
ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
get { return this.Evaluator; }
}
public DoubleValue BestKnownQuality {
get { return BestKnownQualityParameter.Value; }
set { BestKnownQualityParameter.Value = value; }
}
public ParameterSet BestKnownSolution {
get { return BestKnownSolutionParameter.Value; }
set { BestKnownSolutionParameter.Value = value; }
}
public IAlgorithm Algorithm {
get { return AlgorithmParameter.Value; }
set { AlgorithmParameter.Value = value; }
}
public ParameterConfigurationList ParametersToOptimize {
get { return ParametersToOptimizeParameter.Value; }
set { ParametersToOptimizeParameter.Value = value; }
}
private BestQualityAnalyzer BestQualityAnalyzer {
get { return operators.OfType().FirstOrDefault(); }
}
#endregion
[Storable]
private List operators;
[StorableConstructor]
private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
public MetaOptimizationProblem()
: base() {
IParameterSetCreator creator = new RandomParameterSetCreator();
MetaOptimizationEvaluator evaluator = new MetaOptimizationEvaluator();
ParameterConfigurationList parametersToOptimize = new ParameterConfigurationList();
Parameters.Add(new ValueParameter("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false)));
Parameters.Add(new ValueParameter("Repetitions", "Number of evaluations for one individual.", new IntValue(3)));
Parameters.Add(new ValueParameter("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
Parameters.Add(new ValueParameter("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
Parameters.Add(new OptionalValueParameter("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
Parameters.Add(new OptionalValueParameter("BestKnownSolution", "The best known solution of this TSP instance."));
Parameters.Add(new ValueParameter("Algorithm", "The algorithm and problem which's parameters should be optimized."));
Parameters.Add(new ValueParameter("ParametersToOptimize", "List of parameters that should be optimized.", parametersToOptimize));
ParameterizeSolutionCreator();
ParameterizeEvaluator();
InitializeOperators();
AttachEventHandlers();
}
#region Cloning
public override IDeepCloneable Clone(Cloner cloner) {
MetaOptimizationProblem clone = (MetaOptimizationProblem)base.Clone(cloner);
clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
// todo
clone.AttachEventHandlers();
return clone;
}
#endregion
#region Helpers
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserializationHook() {
AttachEventHandlers();
}
private void AttachEventHandlers() {
SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
AlgorithmParameter.ValueChanged += new EventHandler(BaseLevelAlgorithmParameter_ValueChanged);
}
private void InitializeOperators() {
operators = new List();
operators.Add(new BestQualityAnalyzer());
ParameterizeAnalyzer();
operators.AddRange(ApplicationManager.Manager.GetInstances().Cast());
ParameterizeOperators();
//UpdateMoveEvaluators();
//InitializeMoveGenerators();
}
private void ParameterizeSolutionCreator() {
SolutionCreator.ParametersToOptimize = this.ParametersToOptimize;
}
private void ParameterizeEvaluator() {
}
private void ParameterizeAnalyzer() {
BestQualityAnalyzer.ResultsParameter.ActualName = "Results";
}
private void ParameterizeOperators() {
}
private void AddAlgorithmParameters() {
foreach (IParameter parameter in Algorithm.Parameters) {
this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Algorithm"), false);
}
}
private void RemoveAlgorithmParameters() {
foreach (IParameter parameter in Algorithm.Parameters) {
IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);
if (parameterConfiguration != null) {
this.ParametersToOptimize.Remove(parameterConfiguration);
}
}
}
private void ClearAlgorithmParameters() {
//this.ParametersToOptimize.Clear();
}
private void AddProblemParameters() {
foreach (IParameter parameter in Algorithm.Problem.Parameters) {
this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Problem"), false);
}
}
private void RemoveProblemParameters() {
foreach (IParameter parameter in Algorithm.Problem.Parameters) {
IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);
if (parameterConfiguration != null) {
this.ParametersToOptimize.Remove(parameterConfiguration);
}
}
}
private void ClearProblemParameters() {
//this.ParametersToOptimize.Clear();
}
#endregion
#region Events
public event EventHandler SolutionCreatorChanged;
private void OnSolutionCreatorChanged() {
EventHandler handler = SolutionCreatorChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler EvaluatorChanged;
private void OnEvaluatorChanged() {
EventHandler handler = EvaluatorChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler OperatorsChanged;
private void OnOperatorsChanged() {
EventHandler handler = OperatorsChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Reset;
private void OnReset() {
EventHandler handler = Reset;
if (handler != null) handler(this, EventArgs.Empty);
}
private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
ParameterizeSolutionCreator();
ParameterizeEvaluator();
ParameterizeAnalyzer();
ParameterizeOperators();
OnSolutionCreatorChanged();
}
private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
ParameterizeEvaluator();
ParameterizeAnalyzer();
OnEvaluatorChanged();
}
private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
ParameterizeAnalyzer();
}
void BaseLevelAlgorithmParameter_ValueChanged(object sender, EventArgs e) {
ClearAlgorithmParameters();
if (Algorithm != null) {
Algorithm.ProblemChanged += new EventHandler(BaseLevelAlgorithm_ProblemChanged);
AddAlgorithmParameters(); // TODO: When to Detach?
}
BaseLevelAlgorithm_ProblemChanged(sender, e);
}
void BaseLevelAlgorithm_ProblemChanged(object sender, EventArgs e) {
ClearProblemParameters();
if (Algorithm.Problem != null) {
AddProblemParameters();
}
}
#endregion
}
}