1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.Programmable {
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class BasicProblem<TEncoding, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator>, IProblemDefinition, IStorableContent
|
---|
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 |
|
---|
43 | //mkommend necessary for reuse of operators if the encoding changes
|
---|
44 | private TEncoding oldEncoding;
|
---|
45 |
|
---|
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() {
|
---|
56 | if (Encoding == null) return base.GetOperators();
|
---|
57 | return base.GetOperators().Concat(Encoding.Operators);
|
---|
58 | }
|
---|
59 | public override IEnumerable<IParameterizedItem> ExecutionContextItems {
|
---|
60 | get {
|
---|
61 | if (Encoding == null) return base.ExecutionContextItems;
|
---|
62 | return base.ExecutionContextItems.Concat(new[] { Encoding });
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected BasicProblem()
|
---|
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."));
|
---|
69 | oldEncoding = Encoding;
|
---|
70 | RegisterEvents();
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected BasicProblem(BasicProblem<TEncoding, TEvaluator> original, Cloner cloner)
|
---|
74 | : base(original, cloner) {
|
---|
75 | oldEncoding = cloner.Clone(original.oldEncoding);
|
---|
76 | RegisterEvents();
|
---|
77 | }
|
---|
78 |
|
---|
79 | [StorableConstructor]
|
---|
80 | protected BasicProblem(bool deserializing) : base(deserializing) { }
|
---|
81 | [StorableHook(HookType.AfterDeserialization)]
|
---|
82 | private void AfterDeserialization() {
|
---|
83 | oldEncoding = Encoding;
|
---|
84 | RegisterEvents();
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void RegisterEvents() {
|
---|
88 | EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected virtual void OnEncodingChanged() {
|
---|
92 | if (oldEncoding != null) AdaptEncodingOperators(oldEncoding, Encoding);
|
---|
93 | oldEncoding = Encoding;
|
---|
94 |
|
---|
95 | foreach (var op in Operators.OfType<IEncodingOperator>())
|
---|
96 | op.EncodingParameter.ActualName = EncodingParameter.Name;
|
---|
97 |
|
---|
98 | var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments();
|
---|
99 | var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType);
|
---|
100 | var solutionCreatorParam = (IParameter)Activator.CreateInstance(paramType, SolutionCreatorParameter.Name, SolutionCreatorParameter.Description,
|
---|
101 | Encoding.SolutionCreator);
|
---|
102 | Parameters.Remove(SolutionCreatorParameter);
|
---|
103 | Parameters.Add(solutionCreatorParam);
|
---|
104 |
|
---|
105 | //TODO register solution creator changed event, change access modifier to private
|
---|
106 | //((IValueParameter)solutionCreatorParam).ValueChanged += SolutionCreatorParameter_ValueChanged;
|
---|
107 |
|
---|
108 | OnOperatorsChanged();
|
---|
109 | OnReset();
|
---|
110 | }
|
---|
111 |
|
---|
112 | protected override void OnSolutionCreatorChanged() {
|
---|
113 | base.OnSolutionCreatorChanged();
|
---|
114 | Encoding.SolutionCreator = SolutionCreator;
|
---|
115 | }
|
---|
116 |
|
---|
117 | private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
|
---|
118 | if (oldEncoding.GetType() != newEncoding.GetType()) return;
|
---|
119 |
|
---|
120 | if (oldEncoding.GetType() == typeof(MultiEncoding)) {
|
---|
121 | var oldMultiEncoding = (MultiEncoding)oldEncoding;
|
---|
122 | var newMultiEncoding = (MultiEncoding)newEncoding;
|
---|
123 | if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
|
---|
124 |
|
---|
125 | var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
|
---|
126 | foreach (var multi in nestedEncodings)
|
---|
127 | AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
|
---|
128 | }
|
---|
129 |
|
---|
130 | var comparer = new TypeEqualityComparer<IOperator>();
|
---|
131 | var cloner = new Cloner();
|
---|
132 | var oldOperators = oldEncoding.Operators;
|
---|
133 | var newOperators = newEncoding.Operators;
|
---|
134 |
|
---|
135 | cloner.RegisterClonedObject(oldEncoding, newEncoding);
|
---|
136 | var operators = oldOperators.Intersect(newOperators, comparer)
|
---|
137 | .Select(cloner.Clone)
|
---|
138 | .Union(newOperators, comparer).ToList();
|
---|
139 |
|
---|
140 | newEncoding.ConfigureOperators(operators);
|
---|
141 | newEncoding.Operators = operators;
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|
145 | }
|
---|