#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.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.Programmable.Interfaces; namespace HeuristicLab.Problems.Programmable { [StorableClass] public abstract class ProgrammableProblem : HeuristicOptimizationProblem, IProblemDefinition, IStorableContent where TEncoding : class, IEncoding where TEvaluator : class, IEvaluator { public string Filename { get; set; } protected IValueParameter EncodingParameter { get { return (IValueParameter)Parameters["Encoding"]; } } IEncoding IProblemDefinition.Encoding { get { return Encoding; } } public TEncoding Encoding { get { return EncodingParameter.Value; } protected set { if (value == null) throw new ArgumentNullException("Encoding must not be null."); EncodingParameter.Value = value; } } //mkommend necessary for reuse of operators if the encoding changes private TEncoding oldEncoding; public virtual IEnumerable GetNeighbors(Individual individual, IRandom random) { return Enumerable.Empty(); } protected override IEnumerable GetOperators() { if (Encoding == null) return base.GetOperators(); return base.GetOperators().Concat(Encoding.Operators); } public override IEnumerable ExecutionContextItems { get { if (Encoding == null) return base.ExecutionContextItems; return base.ExecutionContextItems.Concat(new[] { Encoding }); } } protected ProgrammableProblem() : base() { Parameters.Add(new ValueParameter("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.")); oldEncoding = Encoding; RegisterEvents(); } protected ProgrammableProblem(ProgrammableProblem original, Cloner cloner) : base(original, cloner) { oldEncoding = cloner.Clone(original.oldEncoding); RegisterEvents(); } [StorableConstructor] protected ProgrammableProblem(bool deserializing) : base(deserializing) { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { oldEncoding = Encoding; RegisterEvents(); } private void RegisterEvents() { EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged(); } protected virtual void OnEncodingChanged() { if (oldEncoding != null) AdaptEncodingOperators(oldEncoding, Encoding); oldEncoding = Encoding; var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments(); var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType); var solutionCreatorParam = (IParameter)Activator.CreateInstance(paramType, SolutionCreatorParameter.Name, SolutionCreatorParameter.Description, Encoding.SolutionCreator); Parameters.Remove(SolutionCreatorParameter); Parameters.Add(solutionCreatorParam); OnOperatorsChanged(); OnReset(); } protected override void OnSolutionCreatorChanged() { base.OnSolutionCreatorChanged(); Encoding.SolutionCreator = SolutionCreator; } protected virtual void ParameterizeOperators() { foreach (var op in Operators.OfType()) op.EncodingParameter.ActualName = EncodingParameter.Name; foreach (var op in Operators.OfType()) op.GetNeighborsFunc = GetNeighbors; } private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) { if (oldEncoding.GetType() != newEncoding.GetType()) return; if (oldEncoding.GetType() == typeof(MultiEncoding)) { var oldMultiEncoding = (MultiEncoding)oldEncoding; var newMultiEncoding = (MultiEncoding)newEncoding; if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer())) return; var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n }); foreach (var multi in nestedEncodings) AdaptEncodingOperators(multi.oldEnc, multi.newEnc); foreach (var op in oldMultiEncoding.Operators.OfType()) foreach (var multi in nestedEncodings) op.ReplaceEncoding(multi.oldEnc, multi.newEnc); } var comparer = new TypeEqualityComparer(); var oldOperators = oldEncoding.Operators; var newOperators = newEncoding.Operators; var operators = oldOperators.Intersect(newOperators, comparer); operators = operators.Union(newOperators, comparer); newEncoding.ConfigureOperators(operators); newEncoding.Operators = operators; } } }