1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.Modifiers {
|
---|
32 | [StorableType("D5B5D05B-FBF5-434F-AB25-E382D83FAA0B")]
|
---|
33 | [Item("MultiObjectiveEndPointProblemModifier", "A dummy problem modifier leading to a multi-objective basic problem")]
|
---|
34 | public class MultiObjectiveEndPointProblemModifier<T> : ProblemModifier where T : class, IEncoding {
|
---|
35 |
|
---|
36 | private MultiObjectiveBasicProblem<T> problem;
|
---|
37 | [Storable]
|
---|
38 | public MultiObjectiveBasicProblem<T> Problem {
|
---|
39 | get => problem;
|
---|
40 | set {
|
---|
41 | if (problem == value) return;
|
---|
42 | if (problem != null) {
|
---|
43 | problem.SolutionCreatorChanged -= OnUpdateRequired;
|
---|
44 | if (problem.Encoding?.Parameters != null) {
|
---|
45 | foreach (var encodingParameter in problem.Encoding.Parameters.OfType<IValueParameter>())
|
---|
46 | encodingParameter.ValueChanged -= OnParameterValueChanged;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | problem = value;
|
---|
50 | UpdateState();
|
---|
51 | try { //this is to catch inconsistent states during deserialization
|
---|
52 | var enc = problem.Encoding.Parameters;
|
---|
53 | } catch (NullReferenceException) {
|
---|
54 | return;
|
---|
55 | }
|
---|
56 | problem.SolutionCreatorChanged += OnUpdateRequired;
|
---|
57 | if (problem.Encoding?.Parameters == null) return;
|
---|
58 | foreach (var encodingParameter in problem.Encoding.Parameters.OfType<IValueParameter>())
|
---|
59 | encodingParameter.ValueChanged += OnParameterValueChanged;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void OnUpdateRequired(object o, EventArgs e) {
|
---|
64 | UpdateState();
|
---|
65 | }
|
---|
66 | private void OnParameterValueChanged(object o, EventArgs e) {
|
---|
67 | UpdateState();
|
---|
68 | ((IValueParameter)o).Value.ToStringChanged += OnUpdateRequired;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override IEncoding Encoding => Problem?.Encoding;
|
---|
72 | public override BoolArray Maximization {
|
---|
73 | get {
|
---|
74 | if (Problem == null) return new BoolArray(new[] { false, false });
|
---|
75 | var dict = new Dictionary<string, IItem>();
|
---|
76 | Problem.CollectParameterValues(dict);
|
---|
77 | return dict.ContainsKey("Maximization") ? (BoolArray)dict["Maximization"] : new BoolArray(problem.Maximization);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | #region constructors and cloning
|
---|
82 | public override ISolutionCreator SolutionCreator => Problem?.SolutionCreator;
|
---|
83 |
|
---|
84 | [StorableConstructor]
|
---|
85 | protected MultiObjectiveEndPointProblemModifier(StorableConstructorFlag _) : base(_) { }
|
---|
86 | protected MultiObjectiveEndPointProblemModifier(MultiObjectiveEndPointProblemModifier<T> original, Cloner cloner) : base(original, cloner) {
|
---|
87 | Problem = cloner.Clone(original?.Problem);
|
---|
88 | }
|
---|
89 | public MultiObjectiveEndPointProblemModifier() { }
|
---|
90 |
|
---|
91 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
92 | return new MultiObjectiveEndPointProblemModifier<T>(this, cloner);
|
---|
93 | }
|
---|
94 |
|
---|
95 | [StorableHook(HookType.AfterDeserialization)]
|
---|
96 | private void AfterDeserialization() {
|
---|
97 | if (problem == null) return;
|
---|
98 | problem.SolutionCreatorChanged += OnUpdateRequired;
|
---|
99 | if (problem.Encoding?.Parameters == null) return;
|
---|
100 | foreach (var encodingParameter in problem.Encoding.Parameters.OfType<IValueParameter>())
|
---|
101 | encodingParameter.ValueChanged += OnParameterValueChanged;
|
---|
102 | }
|
---|
103 | #endregion
|
---|
104 |
|
---|
105 | public override double[] ModifiedEvaluate(Individual individual, IRandom random) {
|
---|
106 | return Problem.Evaluate(individual, random);
|
---|
107 | }
|
---|
108 |
|
---|
109 | public override void ModifiedAnalyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
|
---|
110 | Problem.Analyze(individuals, qualities, results, random);
|
---|
111 | }
|
---|
112 |
|
---|
113 | public void UpdateState() {
|
---|
114 | InvokeEncodingChanged();
|
---|
115 | InvokeMaximizationChanged();
|
---|
116 | InvokeSolutionCreatorChanged();
|
---|
117 | }
|
---|
118 |
|
---|
119 | }
|
---|
120 | }
|
---|