[11739] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16956] | 3 | * Copyright (C) 2002-2019 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;
|
---|
[16956] | 25 | using HEAL.Attic;
|
---|
[11739] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[16956] | 28 | using HeuristicLab.Data;
|
---|
[11739] | 29 | using HeuristicLab.Parameters;
|
---|
| 30 |
|
---|
[11949] | 31 | namespace HeuristicLab.Optimization {
|
---|
[16956] | 32 | [StorableType("D877082E-9E77-4CB1-ABDB-35F63878E116")]
|
---|
[11814] | 33 | public abstract class BasicProblem<TEncoding, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator>, IProblemDefinition, IStorableContent
|
---|
[11739] | 34 | where TEncoding : class, IEncoding
|
---|
| 35 | where TEvaluator : class, IEvaluator {
|
---|
| 36 |
|
---|
| 37 | public string Filename { get; set; }
|
---|
| 38 |
|
---|
| 39 | protected IValueParameter<TEncoding> EncodingParameter {
|
---|
| 40 | get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[11786] | 43 | //mkommend necessary for reuse of operators if the encoding changes
|
---|
| 44 | private TEncoding oldEncoding;
|
---|
| 45 |
|
---|
[11739] | 46 | IEncoding IProblemDefinition.Encoding { get { return Encoding; } }
|
---|
| 47 | public TEncoding Encoding {
|
---|
| 48 | get { return EncodingParameter.Value; }
|
---|
| 49 | protected set {
|
---|
| 50 | if (value == null) throw new ArgumentNullException("Encoding must not be null.");
|
---|
| 51 | EncodingParameter.Value = value;
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | protected override IEnumerable<IItem> GetOperators() {
|
---|
[11753] | 56 | if (Encoding == null) return base.GetOperators();
|
---|
[11739] | 57 | return base.GetOperators().Concat(Encoding.Operators);
|
---|
| 58 | }
|
---|
| 59 | public override IEnumerable<IParameterizedItem> ExecutionContextItems {
|
---|
[11753] | 60 | get {
|
---|
| 61 | if (Encoding == null) return base.ExecutionContextItems;
|
---|
| 62 | return base.ExecutionContextItems.Concat(new[] { Encoding });
|
---|
| 63 | }
|
---|
[11739] | 64 | }
|
---|
| 65 |
|
---|
[11814] | 66 | protected BasicProblem()
|
---|
[11739] | 67 | : base() {
|
---|
| 68 | 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] | 69 | oldEncoding = Encoding;
|
---|
[11998] | 70 | if(Encoding != null) Parameterize();
|
---|
[11739] | 71 | RegisterEvents();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[11814] | 74 | protected BasicProblem(BasicProblem<TEncoding, TEvaluator> original, Cloner cloner)
|
---|
[11739] | 75 | : base(original, cloner) {
|
---|
[11753] | 76 | oldEncoding = cloner.Clone(original.oldEncoding);
|
---|
[11739] | 77 | RegisterEvents();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | [StorableConstructor]
|
---|
[16956] | 81 | protected BasicProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[11753] | 82 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 83 | private void AfterDeserialization() {
|
---|
| 84 | oldEncoding = Encoding;
|
---|
[11739] | 85 | RegisterEvents();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | private void RegisterEvents() {
|
---|
| 89 | EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
|
---|
[11970] | 90 | var multiEncoding = Encoding as MultiEncoding;
|
---|
| 91 | if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
[11739] | 92 | }
|
---|
| 93 |
|
---|
| 94 | protected virtual void OnEncodingChanged() {
|
---|
[11996] | 95 | Parameterize();
|
---|
| 96 |
|
---|
| 97 | OnOperatorsChanged();
|
---|
| 98 | OnReset();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | private void Parameterize() {
|
---|
[11970] | 102 | if (oldEncoding != null) {
|
---|
| 103 | AdaptEncodingOperators(oldEncoding, Encoding);
|
---|
| 104 | var oldMultiEncoding = oldEncoding as MultiEncoding;
|
---|
| 105 | if (oldMultiEncoding != null)
|
---|
| 106 | oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
|
---|
| 107 | }
|
---|
[11753] | 108 | oldEncoding = Encoding;
|
---|
| 109 |
|
---|
[11786] | 110 | foreach (var op in Operators.OfType<IEncodingOperator>())
|
---|
| 111 | op.EncodingParameter.ActualName = EncodingParameter.Name;
|
---|
| 112 |
|
---|
[11739] | 113 | var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments();
|
---|
| 114 | var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType);
|
---|
| 115 | var solutionCreatorParam = (IParameter)Activator.CreateInstance(paramType, SolutionCreatorParameter.Name, SolutionCreatorParameter.Description,
|
---|
| 116 | Encoding.SolutionCreator);
|
---|
| 117 | Parameters.Remove(SolutionCreatorParameter);
|
---|
[11996] | 118 | Parameters.Add(solutionCreatorParam);
|
---|
| 119 | ((IValueParameter)solutionCreatorParam).ValueChanged += SolutionCreatorParameter_ValueChanged;
|
---|
[11753] | 120 |
|
---|
[11970] | 121 | var multiEncoding = Encoding as MultiEncoding;
|
---|
| 122 | if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
[11739] | 123 | }
|
---|
| 124 |
|
---|
| 125 | protected override void OnSolutionCreatorChanged() {
|
---|
| 126 | base.OnSolutionCreatorChanged();
|
---|
| 127 | Encoding.SolutionCreator = SolutionCreator;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[11753] | 130 | private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
|
---|
| 131 | if (oldEncoding.GetType() != newEncoding.GetType()) return;
|
---|
| 132 |
|
---|
| 133 | if (oldEncoding.GetType() == typeof(MultiEncoding)) {
|
---|
| 134 | var oldMultiEncoding = (MultiEncoding)oldEncoding;
|
---|
| 135 | var newMultiEncoding = (MultiEncoding)newEncoding;
|
---|
| 136 | if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
|
---|
| 137 |
|
---|
| 138 | var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
|
---|
| 139 | foreach (var multi in nestedEncodings)
|
---|
| 140 | AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | var comparer = new TypeEqualityComparer<IOperator>();
|
---|
[11813] | 144 | var cloner = new Cloner();
|
---|
[11753] | 145 | var oldOperators = oldEncoding.Operators;
|
---|
| 146 | var newOperators = newEncoding.Operators;
|
---|
| 147 |
|
---|
[11885] | 148 | cloner.RegisterClonedObject(oldEncoding, newEncoding);
|
---|
| 149 | var operators = oldOperators.Intersect(newOperators, comparer)
|
---|
| 150 | .Select(cloner.Clone)
|
---|
| 151 | .Union(newOperators, comparer).ToList();
|
---|
[11813] | 152 |
|
---|
[11753] | 153 | newEncoding.ConfigureOperators(operators);
|
---|
| 154 | newEncoding.Operators = operators;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[11970] | 157 | protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) {
|
---|
| 158 | OnOperatorsChanged();
|
---|
| 159 | }
|
---|
[16956] | 160 |
|
---|
| 161 | protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
|
---|
| 162 | if (param.Value == null) yield break;
|
---|
| 163 | if (param.GetsCollected) {
|
---|
| 164 | if (param == EncodingParameter) // store only the name of the encoding
|
---|
| 165 | yield return new KeyValuePair<string, IItem>(String.Empty, new StringValue(EncodingParameter.Value.Name));
|
---|
| 166 | else yield return new KeyValuePair<string, IItem>(String.Empty, param.Value);
|
---|
| 167 | }
|
---|
| 168 | var parameterizedItem = param.Value as IParameterizedItem;
|
---|
| 169 | if (parameterizedItem != null) {
|
---|
| 170 | var children = new Dictionary<string, IItem>();
|
---|
| 171 | parameterizedItem.CollectParameterValues(children);
|
---|
| 172 | foreach (var child in children) yield return child;
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
[11739] | 175 | }
|
---|
| 176 | }
|
---|