Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3133_ProblemModifiers/HeuristicLab.Problems.Modifiers/Modifier/TranscodingProblemModifier.cs

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

#3133 added implementation of problem modifiers

File size: 4.1 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;
23using System.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29
30namespace HeuristicLab.Problems.Modifiers {
31  [StorableType("AD8A9A90-109A-4496-A057-A94728F2C7E2")]
32  [Item("TranscodingProblemModifier", "A problem modifier that converts the solution candidate for evaluation and Analysis between a new (alg sided) and an original (problem sided) encoding")]
33  public abstract class TranscodingProblemModifier : ProblemModifier {
34    [Storable] protected IEncoding encoding;
35    [Storable] protected ISolutionCreator solutionCreator;
36
37    public override IEncoding Encoding => encoding ?? NextModifier?.Encoding;
38    public override ISolutionCreator SolutionCreator => solutionCreator ?? NextModifier?.SolutionCreator;
39
40    #region constructors
41    [StorableConstructor]
42    protected TranscodingProblemModifier(StorableConstructorFlag _) : base(_) { }
43
44    protected TranscodingProblemModifier(TranscodingProblemModifier original, Cloner cloner) : base(original, cloner) {
45      encoding = cloner.Clone(original.encoding);
46    }
47    public TranscodingProblemModifier() { }
48    #endregion
49
50    #region ProblemModifier
51    public override double[] ModifiedEvaluate(Individual individual, IRandom random) {
52      var oldIndividual = ChangeIndividual(individual);
53      var res = base.ModifiedEvaluate(oldIndividual, random);
54      CopyIntoIndividual(oldIndividual, individual);
55      return res;
56    }
57    public override void ModifiedAnalyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
58      var oldIndividuals = individuals.Select(ChangeIndividual).ToArray();
59      base.ModifiedAnalyze(oldIndividuals, qualities, results, random);
60      for (var i = 0; i < oldIndividuals.Length; i++)
61        CopyIntoIndividual(oldIndividuals[i], individuals[i]);
62    }
63
64    protected override void OnNextEncodingChanged(object sender, EventArgs e) {
65      encoding = ChangeEncoding(NextModifier.Encoding);
66      solutionCreator = ChangeSolutionCreator(NextModifier.SolutionCreator);
67      InvokeEncodingChanged();
68      InvokeSolutionCreatorChanged();
69    }
70
71    protected override void OnNextSolutionCreatorChanged(object sender, EventArgs e) {
72      solutionCreator = ChangeSolutionCreator(NextModifier.SolutionCreator);
73      InvokeSolutionCreatorChanged();
74    }
75
76    #endregion
77
78    #region helpers
79    //ChangeSolutionCreator
80    protected abstract ISolutionCreator ChangeSolutionCreator(ISolutionCreator oldCreator);
81    protected abstract IEncoding ChangeEncoding(IEncoding oldEncoding); //changes upwards (original enc to new enc)
82    protected abstract Individual ChangeIndividual(Individual newIndividual); //changes downwards (individual with new enc to individual with original enc)
83    private static void CopyIntoIndividual(Individual oldIndividual, Individual newIndividual) { //moves additional information after evaluation or analysis to the new (alg-sided) individual
84      var set = new HashSet<string>();
85      foreach (var v in newIndividual.Values.Select(x => x.Key)) set.Add(v);
86      foreach (var v in oldIndividual.Values.Where(v1 => !set.Contains(v1.Key))) newIndividual[v.Key] = v.Value;
87    }
88    #endregion
89  }
90}
Note: See TracBrowser for help on using the repository browser.