#region License Information
/* HeuristicLab
* Copyright (C) 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.Drawing;
using HEAL.Attic;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Parameters;
namespace HeuristicLab.Optimization {
[StorableType("C213CE21-A970-4886-BC4C-9790B9897738")]
public abstract class Problem : ParameterizedNamedItem, IProblem {
public string Filename { get; set; }
public static new Image StaticItemImage {
get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
}
[Storable]
public ResultCollection Results { get; private set; } = new ResultCollection();
[StorableConstructor]
protected Problem(StorableConstructorFlag _) : base(_) { }
protected Problem(Problem original, Cloner cloner) : base(original, cloner) {
Results = cloner.Clone(original.Results);
}
public Problem() : base() { }
protected override IEnumerable> GetCollectedValues(IValueParameter param) {
var children = base.GetCollectedValues(param);
foreach (var child in children) {
if (child.Value is IOperator)
yield return new KeyValuePair(child.Key, new StringValue(((IOperator)child.Value).Name));
else yield return child;
}
}
public event EventHandler Reset;
protected virtual void OnReset() {
EventHandler handler = Reset;
if (handler != null)
handler(this, EventArgs.Empty);
}
}
[Item("Problem", "Represents the base class for a problem.")]
[StorableType("6DC97432-9BD1-4304-802A-1FC48A0E0468")]
public abstract class EncodedProblem : Problem, IEncodedProblem {
private const string OperatorsParameterName = "Operators";
public IFixedValueParameter> OperatorsParameter {
get { return (IFixedValueParameter>)Parameters[OperatorsParameterName]; }
}
[StorableConstructor]
protected EncodedProblem(StorableConstructorFlag _) : base(_) { }
protected EncodedProblem(EncodedProblem original, Cloner cloner)
: base(original, cloner) {
RegisterEventHandlers();
}
protected EncodedProblem()
: base() {
Parameters.Add(new FixedValueParameter>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection()) { GetsCollected = false });
OperatorsParameter.Hidden = true;
RegisterEventHandlers();
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
// BackwardsCompatibility3.3
#region Backwards compatible code, remove with 3.4
IParameter operatorsParam;
if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
var operators = operatorsParam.ActualValue as OperatorCollection;
if (operators != null) {
Parameters.Remove(OperatorsParameterName);
Parameters.Add(new FixedValueParameter>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection(operators)) { GetsCollected = false });
OperatorsParameter.Hidden = true;
}
}
#endregion
RegisterEventHandlers();
}
private void RegisterEventHandlers() {
Operators.ItemsAdded += new CollectionItemsChangedEventHandler(Operators_Changed);
Operators.ItemsRemoved += new CollectionItemsChangedEventHandler(Operators_Changed);
Operators.CollectionReset += new CollectionItemsChangedEventHandler(Operators_Changed);
}
#region properties
// BackwardsCompatibility3.3
#region Backwards compatible code, remove with 3.4
[Storable(OldName = "Operators")]
private IEnumerable StorableOperators {
set {
IParameter operatorsParam;
if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
var items = operatorsParam.ActualValue as ItemCollection;
if (items == null) Parameters.Remove(operatorsParam);
}
//necessary to convert old experiments files where no parameter was used for saving the operators
if (!Parameters.ContainsKey(OperatorsParameterName)) {
Parameters.Add(new FixedValueParameter>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection()) { GetsCollected = false });
OperatorsParameter.Hidden = true;
}
if (value != null) Operators.AddRange(value);
}
}
#endregion
protected ItemCollection Operators {
get {
// BackwardsCompatibility3.3
#region Backwards compatible code, remove with 3.4
if (!Parameters.ContainsKey(OperatorsParameterName)) {
Parameters.Add(new FixedValueParameter>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection()) { GetsCollected = false });
OperatorsParameter.Hidden = true;
}
#endregion
return OperatorsParameter.Value;
}
}
IEnumerable IEncodedProblem.Operators { get { return GetOperators(); } }
protected virtual IEnumerable GetOperators() {
return Operators;
}
public virtual IEnumerable ExecutionContextItems {
get { yield return this; }
}
#endregion
protected virtual void ParameterizeOperators() { }
#region events
private void Operators_Changed(object sender, EventArgs e) {
OnOperatorsChanged();
ParameterizeOperators();
}
public event EventHandler OperatorsChanged;
protected virtual void OnOperatorsChanged() {
EventHandler handler = OperatorsChanged;
if (handler != null)
handler(this, EventArgs.Empty);
}
#endregion
}
}