#region License Information /* HeuristicLab * Copyright (C) 2002-2014 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.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.Programmable { [Item("Encoding", "Base class for describing different encodings.")] [StorableClass] public abstract class Encoding : ParameterizedNamedItem, IEncoding where T : class,ISolutionCreator { public override sealed bool CanChangeName { get { return false; } } protected HashSet encodingOperators = new HashSet(new TypeEqualityComparer()); [Storable] public IEnumerable Operators { get { return encodingOperators; } private set { encodingOperators = new HashSet(value, new TypeEqualityComparer()); } } ISolutionCreator IEncoding.SolutionCreator { get { return SolutionCreator; } set { if (!(value is T)) throw new ArgumentException(string.Format("Cannot assign the solution creator {0} to the encoding {1}.", value.GetType().GetPrettyName(), GetType().GetPrettyName())); SolutionCreator = (T)value; } } private T solutionCreator; public T SolutionCreator { get { return solutionCreator; } set { if (value == null) throw new ArgumentNullException("SolutionCreator must not be null."); if (solutionCreator == value) return; encodingOperators.Remove(solutionCreator); encodingOperators.Add(value); solutionCreator = value; OnSolutionCreatorChanged(); } } [StorableConstructor] protected Encoding(bool deserializing) : base(deserializing) { } protected Encoding(Encoding original, Cloner cloner) : base(original, cloner) { encodingOperators = new HashSet(original.Operators.Select(cloner.Clone)); solutionCreator = cloner.Clone(original.solutionCreator); } protected Encoding(string name) : base(name) { } public virtual Individual GetIndividual(IScope scope) { return new SingleEncodingIndividual(this, scope); } public void ConfigureOperator(IOperator @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); } } }