[11739] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16723] | 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;
|
---|
[16751] | 25 | using HEAL.Attic;
|
---|
[11739] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[16946] | 28 | using HeuristicLab.Data;
|
---|
[11739] | 29 | using HeuristicLab.Parameters;
|
---|
| 30 |
|
---|
[11949] | 31 | namespace HeuristicLab.Optimization {
|
---|
[16723] | 32 | [StorableType("D877082E-9E77-4CB1-ABDB-35F63878E116")]
|
---|
[16751] | 33 | public abstract class Problem<TEncoding, TEncodedSolution, TEvaluator> : Problem,
|
---|
| 34 | IHeuristicOptimizationProblem, IProblemDefinition<TEncoding, TEncodedSolution>, IStorableContent
|
---|
| 35 | where TEncoding : class, IEncoding<TEncodedSolution>
|
---|
| 36 | where TEncodedSolution : class, IEncodedSolution
|
---|
[11739] | 37 | where TEvaluator : class, IEvaluator {
|
---|
| 38 |
|
---|
[13469] | 39 | public string Filename { get; set; } // TODO: Really okay here? should be in Problem (non-generic)
|
---|
[11739] | 40 |
|
---|
[16751] | 41 | //TODO remove parameter for encoding?
|
---|
[13339] | 42 | protected IValueParameter<TEncoding> EncodingParameter {
|
---|
| 43 | get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
|
---|
[11739] | 44 | }
|
---|
[13365] | 45 | //mkommend necessary for reuse of operators if the encoding changes
|
---|
| 46 | private TEncoding oldEncoding;
|
---|
[13339] | 47 | public TEncoding Encoding {
|
---|
[11739] | 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 |
|
---|
[13469] | 55 | ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
|
---|
| 56 | get { return Encoding.SolutionCreator; }
|
---|
| 57 | }
|
---|
| 58 | IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
|
---|
| 59 | get { return Encoding.SolutionCreatorParameter; }
|
---|
| 60 | }
|
---|
| 61 | event EventHandler IHeuristicOptimizationProblem.SolutionCreatorChanged {
|
---|
| 62 | add { Encoding.SolutionCreatorChanged += value; }
|
---|
| 63 | remove { Encoding.SolutionCreatorChanged -= value; }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | //TODO is a parameter for the evaluator really necessary, only single-objective or multi-objective evulators calling the func are possible
|
---|
| 67 | public ValueParameter<TEvaluator> EvaluatorParameter {
|
---|
| 68 | get { return (ValueParameter<TEvaluator>)Parameters["Evaluator"]; }
|
---|
| 69 | }
|
---|
| 70 | public TEvaluator Evaluator {
|
---|
| 71 | get { return EvaluatorParameter.Value; }
|
---|
| 72 | protected set { EvaluatorParameter.Value = value; }
|
---|
| 73 | }
|
---|
| 74 | IEvaluator IHeuristicOptimizationProblem.Evaluator { get { return Evaluator; } }
|
---|
| 75 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter { get { return EvaluatorParameter; } }
|
---|
| 76 |
|
---|
| 77 | public event EventHandler EvaluatorChanged;
|
---|
| 78 | protected virtual void OnEvaluatorChanged() {
|
---|
| 79 | EventHandler handler = EvaluatorChanged;
|
---|
| 80 | if (handler != null)
|
---|
| 81 | handler(this, EventArgs.Empty);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[16532] | 84 |
|
---|
[11739] | 85 | protected override IEnumerable<IItem> GetOperators() {
|
---|
[11753] | 86 | if (Encoding == null) return base.GetOperators();
|
---|
[11739] | 87 | return base.GetOperators().Concat(Encoding.Operators);
|
---|
| 88 | }
|
---|
| 89 | public override IEnumerable<IParameterizedItem> ExecutionContextItems {
|
---|
[11753] | 90 | get {
|
---|
| 91 | if (Encoding == null) return base.ExecutionContextItems;
|
---|
| 92 | return base.ExecutionContextItems.Concat(new[] { Encoding });
|
---|
| 93 | }
|
---|
[11739] | 94 | }
|
---|
| 95 |
|
---|
[13336] | 96 | protected Problem()
|
---|
[11739] | 97 | : base() {
|
---|
[16948] | 98 | 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.") { Hidden = true });
|
---|
| 99 | Parameters.Add(new ValueParameter<TEvaluator>("Evaluator", "The operator used to evaluate a solution.") { Hidden = true });
|
---|
| 100 |
|
---|
[13339] | 101 | if (Encoding != null) {
|
---|
[13365] | 102 | oldEncoding = Encoding;
|
---|
[13339] | 103 | Parameterize();
|
---|
| 104 | }
|
---|
[11739] | 105 | RegisterEvents();
|
---|
| 106 | }
|
---|
[13396] | 107 | protected Problem(TEncoding encoding) {
|
---|
| 108 | if (encoding == null) throw new ArgumentNullException("encoding");
|
---|
[16948] | 109 | 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.", encoding) { Hidden = true });
|
---|
| 110 | Parameters.Add(new ValueParameter<TEvaluator>("Evaluator", "The operator used to evaluate a solution.") { Hidden = true });
|
---|
[13469] | 111 |
|
---|
[13396] | 112 | oldEncoding = Encoding;
|
---|
| 113 | Parameterize();
|
---|
[11739] | 114 |
|
---|
[13396] | 115 | RegisterEvents();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[16751] | 118 | protected Problem(Problem<TEncoding, TEncodedSolution, TEvaluator> original, Cloner cloner)
|
---|
[11739] | 119 | : base(original, cloner) {
|
---|
[13365] | 120 | oldEncoding = cloner.Clone(original.oldEncoding);
|
---|
[11739] | 121 | RegisterEvents();
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | [StorableConstructor]
|
---|
[16723] | 125 | protected Problem(StorableConstructorFlag _) : base(_) { }
|
---|
[11753] | 126 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 127 | private void AfterDeserialization() {
|
---|
[13365] | 128 | oldEncoding = Encoding;
|
---|
[11739] | 129 | RegisterEvents();
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | private void RegisterEvents() {
|
---|
| 133 | EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
|
---|
[13469] | 134 | EvaluatorParameter.ValueChanged += (o, e) => OnEvaluatorChanged();
|
---|
[13336] | 135 | //var multiEncoding = Encoding as MultiEncoding;
|
---|
| 136 | //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
[11739] | 137 | }
|
---|
| 138 |
|
---|
| 139 | protected virtual void OnEncodingChanged() {
|
---|
[11996] | 140 | Parameterize();
|
---|
| 141 |
|
---|
| 142 | OnOperatorsChanged();
|
---|
| 143 | OnReset();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | private void Parameterize() {
|
---|
[13365] | 147 | if (oldEncoding != null) {
|
---|
| 148 | AdaptEncodingOperators(oldEncoding, Encoding);
|
---|
| 149 | //var oldMultiEncoding = oldEncoding as MultiEncoding;
|
---|
| 150 | //if (oldMultiEncoding != null)
|
---|
| 151 | // oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
|
---|
| 152 | }
|
---|
| 153 | oldEncoding = Encoding;
|
---|
| 154 |
|
---|
[16751] | 155 | foreach (var op in Operators.OfType<IEncodingOperator<TEncodedSolution>>())
|
---|
[11786] | 156 | op.EncodingParameter.ActualName = EncodingParameter.Name;
|
---|
| 157 |
|
---|
[13336] | 158 | //var multiEncoding = Encoding as MultiEncoding;
|
---|
| 159 | //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
[11739] | 160 | }
|
---|
| 161 |
|
---|
[13469] | 162 | //protected override void OnSolutionCreatorChanged() {
|
---|
| 163 | // base.OnSolutionCreatorChanged();
|
---|
| 164 | // Encoding.SolutionCreator = SolutionCreator;
|
---|
| 165 | //}
|
---|
[11739] | 166 |
|
---|
[13365] | 167 | private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
|
---|
| 168 | if (oldEncoding.GetType() != newEncoding.GetType()) return;
|
---|
| 169 |
|
---|
[13469] | 170 | if (oldEncoding is CombinedEncoding) {
|
---|
[13376] | 171 | var oldMultiEncoding = (CombinedEncoding)oldEncoding;
|
---|
| 172 | var newMultiEncoding = (CombinedEncoding)newEncoding;
|
---|
[13365] | 173 | if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
|
---|
| 174 |
|
---|
| 175 | var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
|
---|
| 176 | foreach (var multi in nestedEncodings)
|
---|
| 177 | AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | var comparer = new TypeEqualityComparer<IOperator>();
|
---|
| 181 | var cloner = new Cloner();
|
---|
| 182 | var oldOperators = oldEncoding.Operators;
|
---|
| 183 | var newOperators = newEncoding.Operators;
|
---|
| 184 |
|
---|
| 185 | cloner.RegisterClonedObject(oldEncoding, newEncoding);
|
---|
| 186 | var operators = oldOperators.Intersect(newOperators, comparer)
|
---|
| 187 | .Select(cloner.Clone)
|
---|
| 188 | .Union(newOperators, comparer).ToList();
|
---|
| 189 |
|
---|
| 190 | newEncoding.ConfigureOperators(operators);
|
---|
| 191 | newEncoding.Operators = operators;
|
---|
[11970] | 192 | }
|
---|
[16946] | 193 |
|
---|
| 194 | protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
|
---|
| 195 | if (param.Value == null) yield break;
|
---|
| 196 | if (param.GetsCollected) {
|
---|
| 197 | if (param == EncodingParameter) // store only the name of the encoding
|
---|
| 198 | yield return new KeyValuePair<string, IItem>(String.Empty, new StringValue(EncodingParameter.Value.Name));
|
---|
| 199 | else yield return new KeyValuePair<string, IItem>(String.Empty, param.Value);
|
---|
| 200 | }
|
---|
| 201 | var parameterizedItem = param.Value as IParameterizedItem;
|
---|
| 202 | if (parameterizedItem != null) {
|
---|
| 203 | var children = new Dictionary<string, IItem>();
|
---|
| 204 | parameterizedItem.CollectParameterValues(children);
|
---|
| 205 | foreach (var child in children) yield return child;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
[11739] | 208 | }
|
---|
| 209 | }
|
---|