#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Optimization {
[Item("Encoding", "Base class for describing different encodings.")]
[StorableClass]
public abstract class Encoding : ParameterizedNamedItem, IEncoding
where TSolution : class,ISolution {
public override sealed bool CanChangeName {
get { return false; }
}
private ItemSet encodingOperators = new ItemSet(new TypeEqualityComparer());
[Storable(Name = "Operators")]
private IEnumerable StorableOperators {
get { return encodingOperators; }
set { encodingOperators = new ItemSet(value, new TypeEqualityComparer()); ; }
}
public IEnumerable Operators {
get { return encodingOperators; }
set {
if (!value.OfType>().Any())
throw new ArgumentException("The provided operators contain no suitable solution creator");
encodingOperators.Clear();
foreach (var op in value) encodingOperators.Add(op);
ISolutionCreator newSolutionCreator = (ISolutionCreator)encodingOperators.FirstOrDefault(o => o.GetType() == SolutionCreator.GetType()) ??
encodingOperators.OfType>().First();
SolutionCreator = newSolutionCreator;
OnOperatorsChanged();
}
}
public IValueParameter SolutionCreatorParameter {
get { return (IValueParameter)Parameters[Name + ".SolutionCreator"]; }
}
ISolutionCreator IEncoding.SolutionCreator {
get { return SolutionCreator; }
}
public ISolutionCreator SolutionCreator {
get { return (ISolutionCreator)SolutionCreatorParameter.Value; }
set {
if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
encodingOperators.Remove(SolutionCreator);
encodingOperators.Add(value);
SolutionCreatorParameter.Value = value;
OnSolutionCreatorChanged();
}
}
[StorableConstructor]
protected Encoding(bool deserializing) : base(deserializing) { }
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEventHandlers();
}
protected Encoding(Encoding original, Cloner cloner)
: base(original, cloner) {
encodingOperators = cloner.Clone(original.encodingOperators);
RegisterEventHandlers();
}
protected Encoding(string name)
: base(name) {
Parameters.Add(new ValueParameter>(name + ".SolutionCreator", "The operator to create a solution."));
Parameters.Add(new FixedValueParameter>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly()));
RegisterEventHandlers();
}
private void RegisterEventHandlers() {
SolutionCreatorParameter.ValueChanged += (o, e) => OnSolutionCreatorChanged();
}
protected bool AddOperator(IOperator @operator) {
return encodingOperators.Add(@operator);
}
protected bool RemoveOperator(IOperator @operator) {
return encodingOperators.Remove(@operator);
}
public void ConfigureOperator(IItem @operator) { ConfigureOperators(new[] { @operator }); }
public abstract void ConfigureOperators(IEnumerable operators);
public event EventHandler SolutionCreatorChanged;
protected virtual void OnSolutionCreatorChanged() {
ConfigureOperator(SolutionCreator);
var handler = SolutionCreatorChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler OperatorsChanged;
protected virtual void OnOperatorsChanged() {
ConfigureOperators(Operators);
var handler = OperatorsChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
}
}