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 ProgrammableProblem<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 ProgrammableProblem()
|
---|
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 ProgrammableProblem(ProgrammableProblem<TEncoding, TEvaluator> original, Cloner cloner)
|
---|
74 | : base(original, cloner) {
|
---|
75 | oldEncoding = cloner.Clone(original.oldEncoding);
|
---|
76 | RegisterEvents();
|
---|
77 | }
|
---|
78 |
|
---|
79 | [StorableConstructor]
|
---|
80 | protected ProgrammableProblem(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
|
---|
106 |
|
---|
107 | OnOperatorsChanged();
|
---|
108 | OnReset();
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected override void OnSolutionCreatorChanged() {
|
---|
112 | base.OnSolutionCreatorChanged();
|
---|
113 | Encoding.SolutionCreator = SolutionCreator;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
|
---|
117 | if (oldEncoding.GetType() != newEncoding.GetType()) return;
|
---|
118 |
|
---|
119 | if (oldEncoding.GetType() == typeof(MultiEncoding)) {
|
---|
120 | var oldMultiEncoding = (MultiEncoding)oldEncoding;
|
---|
121 | var newMultiEncoding = (MultiEncoding)newEncoding;
|
---|
122 | if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
|
---|
123 |
|
---|
124 | var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
|
---|
125 | foreach (var multi in nestedEncodings)
|
---|
126 | AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
|
---|
127 |
|
---|
128 | foreach (var op in oldMultiEncoding.Operators.OfType<IMultiEncodingOperator>())
|
---|
129 | foreach (var multi in nestedEncodings)
|
---|
130 | op.ReplaceEncoding(multi.oldEnc, multi.newEnc);
|
---|
131 | }
|
---|
132 |
|
---|
133 | var comparer = new TypeEqualityComparer<IOperator>();
|
---|
134 | var oldOperators = oldEncoding.Operators;
|
---|
135 | var newOperators = newEncoding.Operators;
|
---|
136 |
|
---|
137 | var operators = oldOperators.Intersect(newOperators, comparer);
|
---|
138 | //TODO clone overlapping operators
|
---|
139 | operators = operators.Union(newOperators, comparer);
|
---|
140 |
|
---|
141 | newEncoding.ConfigureOperators(operators);
|
---|
142 | newEncoding.Operators = operators;
|
---|
143 | }
|
---|
144 |
|
---|
145 | }
|
---|
146 | }
|
---|