[11739] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11739] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[11949] | 30 | namespace HeuristicLab.Optimization {
|
---|
[11739] | 31 | [StorableClass]
|
---|
[11814] | 32 | public abstract class BasicProblem<TEncoding, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator>, IProblemDefinition, IStorableContent
|
---|
[11739] | 33 | where TEncoding : class, IEncoding
|
---|
| 34 | where TEvaluator : class, IEvaluator {
|
---|
| 35 |
|
---|
| 36 | public string Filename { get; set; }
|
---|
| 37 |
|
---|
| 38 | protected IValueParameter<TEncoding> EncodingParameter {
|
---|
| 39 | get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[11786] | 42 | //mkommend necessary for reuse of operators if the encoding changes
|
---|
| 43 | private TEncoding oldEncoding;
|
---|
| 44 |
|
---|
[11739] | 45 | IEncoding IProblemDefinition.Encoding { get { return Encoding; } }
|
---|
| 46 | public TEncoding Encoding {
|
---|
| 47 | get { return EncodingParameter.Value; }
|
---|
| 48 | protected set {
|
---|
| 49 | if (value == null) throw new ArgumentNullException("Encoding must not be null.");
|
---|
| 50 | EncodingParameter.Value = value;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | protected override IEnumerable<IItem> GetOperators() {
|
---|
[11753] | 55 | if (Encoding == null) return base.GetOperators();
|
---|
[11739] | 56 | return base.GetOperators().Concat(Encoding.Operators);
|
---|
| 57 | }
|
---|
| 58 | public override IEnumerable<IParameterizedItem> ExecutionContextItems {
|
---|
[11753] | 59 | get {
|
---|
| 60 | if (Encoding == null) return base.ExecutionContextItems;
|
---|
| 61 | return base.ExecutionContextItems.Concat(new[] { Encoding });
|
---|
| 62 | }
|
---|
[11739] | 63 | }
|
---|
| 64 |
|
---|
[11814] | 65 | protected BasicProblem()
|
---|
[11739] | 66 | : base() {
|
---|
| 67 | Parameters.Add(new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any."));
|
---|
[11753] | 68 | oldEncoding = Encoding;
|
---|
[11998] | 69 | if(Encoding != null) Parameterize();
|
---|
[11739] | 70 | RegisterEvents();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[11814] | 73 | protected BasicProblem(BasicProblem<TEncoding, TEvaluator> original, Cloner cloner)
|
---|
[11739] | 74 | : base(original, cloner) {
|
---|
[11753] | 75 | oldEncoding = cloner.Clone(original.oldEncoding);
|
---|
[11739] | 76 | RegisterEvents();
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | [StorableConstructor]
|
---|
[11814] | 80 | protected BasicProblem(bool deserializing) : base(deserializing) { }
|
---|
[11753] | 81 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 82 | private void AfterDeserialization() {
|
---|
| 83 | oldEncoding = Encoding;
|
---|
[11739] | 84 | RegisterEvents();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | private void RegisterEvents() {
|
---|
| 88 | EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
|
---|
[11970] | 89 | var multiEncoding = Encoding as MultiEncoding;
|
---|
| 90 | if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
[11739] | 91 | }
|
---|
| 92 |
|
---|
| 93 | protected virtual void OnEncodingChanged() {
|
---|
[11996] | 94 | Parameterize();
|
---|
| 95 |
|
---|
| 96 | OnOperatorsChanged();
|
---|
| 97 | OnReset();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | private void Parameterize() {
|
---|
[11970] | 101 | if (oldEncoding != null) {
|
---|
| 102 | AdaptEncodingOperators(oldEncoding, Encoding);
|
---|
| 103 | var oldMultiEncoding = oldEncoding as MultiEncoding;
|
---|
| 104 | if (oldMultiEncoding != null)
|
---|
| 105 | oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
|
---|
| 106 | }
|
---|
[11753] | 107 | oldEncoding = Encoding;
|
---|
| 108 |
|
---|
[11786] | 109 | foreach (var op in Operators.OfType<IEncodingOperator>())
|
---|
| 110 | op.EncodingParameter.ActualName = EncodingParameter.Name;
|
---|
| 111 |
|
---|
[11739] | 112 | var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments();
|
---|
| 113 | var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType);
|
---|
| 114 | var solutionCreatorParam = (IParameter)Activator.CreateInstance(paramType, SolutionCreatorParameter.Name, SolutionCreatorParameter.Description,
|
---|
| 115 | Encoding.SolutionCreator);
|
---|
| 116 | Parameters.Remove(SolutionCreatorParameter);
|
---|
[11996] | 117 | Parameters.Add(solutionCreatorParam);
|
---|
| 118 | ((IValueParameter)solutionCreatorParam).ValueChanged += SolutionCreatorParameter_ValueChanged;
|
---|
[11753] | 119 |
|
---|
[11970] | 120 | var multiEncoding = Encoding as MultiEncoding;
|
---|
| 121 | if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
[11739] | 122 | }
|
---|
| 123 |
|
---|
| 124 | protected override void OnSolutionCreatorChanged() {
|
---|
| 125 | base.OnSolutionCreatorChanged();
|
---|
| 126 | Encoding.SolutionCreator = SolutionCreator;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[11753] | 129 | private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
|
---|
| 130 | if (oldEncoding.GetType() != newEncoding.GetType()) return;
|
---|
| 131 |
|
---|
| 132 | if (oldEncoding.GetType() == typeof(MultiEncoding)) {
|
---|
| 133 | var oldMultiEncoding = (MultiEncoding)oldEncoding;
|
---|
| 134 | var newMultiEncoding = (MultiEncoding)newEncoding;
|
---|
| 135 | if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
|
---|
| 136 |
|
---|
| 137 | var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
|
---|
| 138 | foreach (var multi in nestedEncodings)
|
---|
| 139 | AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | var comparer = new TypeEqualityComparer<IOperator>();
|
---|
[11813] | 143 | var cloner = new Cloner();
|
---|
[11753] | 144 | var oldOperators = oldEncoding.Operators;
|
---|
| 145 | var newOperators = newEncoding.Operators;
|
---|
| 146 |
|
---|
[11885] | 147 | cloner.RegisterClonedObject(oldEncoding, newEncoding);
|
---|
| 148 | var operators = oldOperators.Intersect(newOperators, comparer)
|
---|
| 149 | .Select(cloner.Clone)
|
---|
| 150 | .Union(newOperators, comparer).ToList();
|
---|
[11813] | 151 |
|
---|
[11753] | 152 | newEncoding.ConfigureOperators(operators);
|
---|
| 153 | newEncoding.Operators = operators;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[11970] | 156 | protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) {
|
---|
| 157 | OnOperatorsChanged();
|
---|
| 158 | }
|
---|
[11739] | 159 | }
|
---|
| 160 | }
|
---|