Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3133_ProblemModifiers/HeuristicLab.Problems.Modifiers/Modifier/SingleObjectiveEndPointProblemModifier.cs @ 18094

Last change on this file since 18094 was 18029, checked in by bwerth, 3 years ago

#3133 added implementation of problem modifiers

File size: 3.2 KB
Line 
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
22using System.Collections.Generic;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29
30namespace HeuristicLab.Problems.Modifiers {
31  [StorableType("7C9DECC0-1CA1-4198-835F-CB1CF35D88E8")]
32  [Item("SingleObjectiveEndPointProblemModifier", "A dummy problem modifier leading to a single objective basic problem")]
33  public class SingleObjectiveEndPointProblemModifier<T> : ProblemModifier where T : class, IEncoding {
34    private SingleObjectiveBasicProblem<T> problem;
35    [Storable]
36    public SingleObjectiveBasicProblem<T> Problem {
37      get => problem;
38      set {
39        problem = value;
40        UpdateState();
41        //TODO attach to EncodingChanged and MaximizationChanged Events ... (not implemented in BasicProblem yet)
42      }
43    }
44
45    public override IEncoding Encoding => Problem?.Encoding;
46    public override BoolArray Maximization {
47      get {
48        if (Problem == null) return new BoolArray(new[] { false });
49        var dict = new Dictionary<string, IItem>();
50        Problem.CollectParameterValues(dict);
51        var b = dict.ContainsKey("Maximization") ? ((BoolValue)dict["Maximization"]).Value : problem.Maximization;
52        return new BoolArray(new[] { b });
53      }
54    }
55
56    [StorableConstructor]
57    protected SingleObjectiveEndPointProblemModifier(StorableConstructorFlag _) : base(_) { }
58    protected SingleObjectiveEndPointProblemModifier(SingleObjectiveEndPointProblemModifier<T> original, Cloner cloner) : base(original, cloner) {
59      Problem = cloner.Clone(original?.Problem);
60    }
61    public SingleObjectiveEndPointProblemModifier() { }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new SingleObjectiveEndPointProblemModifier<T>(this, cloner);
65    }
66
67    public override double[] ModifiedEvaluate(Individual individual, IRandom random) {
68      return new[] { Problem.Evaluate(individual, random) };
69    }
70
71    public override void ModifiedAnalyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
72      Problem.Analyze(individuals, qualities.Select(x => x.Single()).ToArray(), results, random);
73    }
74
75    public void UpdateState() {
76      InvokeEncodingChanged();
77      InvokeMaximizationChanged();
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.