1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 | using HEAL.Attic;
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
25 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
26 | using System.Collections.Generic;
|
---|
27 | using System.Linq;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
|
---|
30 | [Item("SucsessMap", "A map of models of models of models")]
|
---|
31 | [StorableType("3880BA82-4CB0-4838-A17A-823E91BC046C")]
|
---|
32 | public class EMMSucsessMap : EMMMapBase<ISymbolicExpressionTree> {
|
---|
33 | [Storable]
|
---|
34 | public List<double> Probabilities { get; private set; }
|
---|
35 | [Storable]
|
---|
36 | public List<List<double>> SucsessStatistics { get; private set; }
|
---|
37 | #region conctructors
|
---|
38 | [StorableConstructor]
|
---|
39 | protected EMMSucsessMap(StorableConstructorFlag _) : base(_) { }
|
---|
40 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
41 | return new EMMSucsessMap(this, cloner);
|
---|
42 | }
|
---|
43 | public EMMSucsessMap() : base() {
|
---|
44 | ModelSet = new List<ISymbolicExpressionTree>();
|
---|
45 | SucsessStatistics = new List<List<double>>();
|
---|
46 | }
|
---|
47 | public EMMSucsessMap(EMMSucsessMap original, Cloner cloner) : base(original, cloner) {
|
---|
48 | SucsessStatistics = original.SucsessStatistics.Select(x => x.ToList()).ToList();
|
---|
49 | }
|
---|
50 | #endregion
|
---|
51 | #region MapCreation
|
---|
52 | override public void CreateMap(IRandom random, int k) {
|
---|
53 |
|
---|
54 | Probabilities = new List<double>();
|
---|
55 | Map.Clear();
|
---|
56 | Map.Add(new List<int>());
|
---|
57 | MapSizeCheck(ModelSet.Count);
|
---|
58 | ApplySucsessMapCreationAlgorithm(random, CalculateDistances(), Map, Probabilities, SucsessStatistics);
|
---|
59 | }
|
---|
60 | public static void ApplySucsessMapCreationAlgorithm(IRandom random, double[,] distances, List<List<int>> map, List<double> probabilities, List<List<double>> sucsessStatistics) {
|
---|
61 | int mapSize = distances.GetLength(0);
|
---|
62 | for (int t = 0; t < mapSize; t++) {
|
---|
63 | map[t].Add(t);
|
---|
64 | probabilities.Add(1.0 / ((double)(mapSize))); // uniform distribution as start point
|
---|
65 | }
|
---|
66 | }
|
---|
67 | public override void MapUpDate(Dictionary<ISymbolicExpressionTree, double> population) {
|
---|
68 | SucsessStatisticCollection(population);
|
---|
69 | HelpFunctions.ProbabilitiesUpDate(SucsessStatistics, Probabilities);
|
---|
70 | }
|
---|
71 | private void SucsessStatisticCollection(Dictionary<ISymbolicExpressionTree, double> population) {
|
---|
72 | if (SucsessStatistics.Count != 0)
|
---|
73 | SucsessStatistics.Clear();
|
---|
74 | for (int t = 0; t < Probabilities.Count; t++) {
|
---|
75 | SucsessStatistics.Add(new List<double>());
|
---|
76 | SucsessStatistics[t].Add(0);
|
---|
77 | SucsessStatistics[t].Add(0);
|
---|
78 | }
|
---|
79 | foreach (var solution in population) {
|
---|
80 | TreeCheck(solution.Key, solution.Value);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | private void TreeCheck(ISymbolicExpressionTree tree, double treeQuality) {
|
---|
84 | foreach (var treeNode in tree.IterateNodesPrefix().OfType<TreeModelTreeNode>()) {
|
---|
85 | SucsessStatistics[treeNode.TreeNumber][0] += 1;
|
---|
86 | SucsessStatistics[treeNode.TreeNumber][1] += treeQuality;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | #endregion
|
---|
90 | #region MapApplayFunctions
|
---|
91 | public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) {
|
---|
92 | treeNumber = Map[HelpFunctions.OneElementFromListProportionalSelection(random, Probabilities)][0];
|
---|
93 | return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
|
---|
94 | }
|
---|
95 | override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) {
|
---|
96 | return NewModelForInizializtion(random, out treeNumber);
|
---|
97 | }
|
---|
98 | #endregion
|
---|
99 | }
|
---|
100 | }
|
---|