[11739] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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]
|
---|
[13339] | 32 | public abstract class Problem<TEncoding, TSolution, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator<TSolution>>, IProblemDefinition<TEncoding, TSolution>, IStorableContent
|
---|
| 33 | where TEncoding : class, IEncoding<TSolution>
|
---|
[13336] | 34 | where TSolution : class, ISolution
|
---|
[11739] | 35 | where TEvaluator : class, IEvaluator {
|
---|
| 36 |
|
---|
| 37 | public string Filename { get; set; }
|
---|
| 38 |
|
---|
[13339] | 39 | protected IValueParameter<TEncoding> EncodingParameter {
|
---|
| 40 | get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
|
---|
[11739] | 41 | }
|
---|
| 42 |
|
---|
[13365] | 43 | //mkommend necessary for reuse of operators if the encoding changes
|
---|
| 44 | private TEncoding oldEncoding;
|
---|
| 45 |
|
---|
[13339] | 46 | public TEncoding Encoding {
|
---|
[11739] | 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 |
|
---|
[13336] | 65 | protected Problem()
|
---|
[11739] | 66 | : base() {
|
---|
[13339] | 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."));
|
---|
| 68 | if (Encoding != null) {
|
---|
[13365] | 69 | oldEncoding = Encoding;
|
---|
[13339] | 70 | SolutionCreator = Encoding.SolutionCreator;
|
---|
| 71 | Parameterize();
|
---|
| 72 | }
|
---|
[11739] | 73 | RegisterEvents();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[13339] | 76 | protected Problem(Problem<TEncoding, TSolution, TEvaluator> original, Cloner cloner)
|
---|
[11739] | 77 | : base(original, cloner) {
|
---|
[13365] | 78 | oldEncoding = cloner.Clone(original.oldEncoding);
|
---|
[11739] | 79 | RegisterEvents();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | [StorableConstructor]
|
---|
[13336] | 83 | protected Problem(bool deserializing) : base(deserializing) { }
|
---|
[11753] | 84 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 85 | private void AfterDeserialization() {
|
---|
[13365] | 86 | oldEncoding = Encoding;
|
---|
[11739] | 87 | RegisterEvents();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | private void RegisterEvents() {
|
---|
| 91 | EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
|
---|
[13336] | 92 | //var multiEncoding = Encoding as MultiEncoding;
|
---|
| 93 | //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
[11739] | 94 | }
|
---|
| 95 |
|
---|
| 96 | protected virtual void OnEncodingChanged() {
|
---|
[11996] | 97 | Parameterize();
|
---|
| 98 |
|
---|
| 99 | OnOperatorsChanged();
|
---|
| 100 | OnReset();
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | private void Parameterize() {
|
---|
[13365] | 104 | if (oldEncoding != null) {
|
---|
| 105 | AdaptEncodingOperators(oldEncoding, Encoding);
|
---|
| 106 | //var oldMultiEncoding = oldEncoding as MultiEncoding;
|
---|
| 107 | //if (oldMultiEncoding != null)
|
---|
| 108 | // oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
|
---|
| 109 | }
|
---|
| 110 | oldEncoding = Encoding;
|
---|
| 111 |
|
---|
[13336] | 112 | foreach (var op in Operators.OfType<IEncodingOperator<TSolution>>())
|
---|
[11786] | 113 | op.EncodingParameter.ActualName = EncodingParameter.Name;
|
---|
| 114 |
|
---|
[13365] | 115 | SolutionCreator = Encoding.SolutionCreator;
|
---|
| 116 |
|
---|
[13336] | 117 | //var multiEncoding = Encoding as MultiEncoding;
|
---|
| 118 | //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
[11739] | 119 | }
|
---|
| 120 |
|
---|
| 121 | protected override void OnSolutionCreatorChanged() {
|
---|
| 122 | base.OnSolutionCreatorChanged();
|
---|
| 123 | Encoding.SolutionCreator = SolutionCreator;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[13365] | 126 | private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
|
---|
| 127 | if (oldEncoding.GetType() != newEncoding.GetType()) return;
|
---|
| 128 |
|
---|
| 129 | if (oldEncoding.GetType() == typeof(MultiEncoding)) {
|
---|
| 130 | var oldMultiEncoding = (MultiEncoding)oldEncoding;
|
---|
| 131 | var newMultiEncoding = (MultiEncoding)newEncoding;
|
---|
| 132 | if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
|
---|
| 133 |
|
---|
| 134 | var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
|
---|
| 135 | foreach (var multi in nestedEncodings)
|
---|
| 136 | AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | var comparer = new TypeEqualityComparer<IOperator>();
|
---|
| 140 | var cloner = new Cloner();
|
---|
| 141 | var oldOperators = oldEncoding.Operators;
|
---|
| 142 | var newOperators = newEncoding.Operators;
|
---|
| 143 |
|
---|
| 144 | cloner.RegisterClonedObject(oldEncoding, newEncoding);
|
---|
| 145 | var operators = oldOperators.Intersect(newOperators, comparer)
|
---|
| 146 | .Select(cloner.Clone)
|
---|
| 147 | .Union(newOperators, comparer).ToList();
|
---|
| 148 |
|
---|
| 149 | newEncoding.ConfigureOperators(operators);
|
---|
| 150 | newEncoding.Operators = operators;
|
---|
[11970] | 151 | }
|
---|
[11739] | 152 | }
|
---|
| 153 | }
|
---|