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 |
|
---|
22 | using HEAL.Attic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
27 | using System.Collections.Generic;
|
---|
28 | using System.Linq;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
|
---|
31 | [StorableType("A9AE93F0-E589-44D0-AD34-0E3AA358D669")]
|
---|
32 | [Item("TreeModelMap", "A map of models of models of models")]
|
---|
33 | public class EMMMapTreeModel : EMMMapBase<ISymbolicExpressionTree> {
|
---|
34 | #region conctructors
|
---|
35 | [StorableConstructor]
|
---|
36 | protected EMMMapTreeModel(StorableConstructorFlag _) : base(_) { }
|
---|
37 | public EMMMapTreeModel() : this(1) { }
|
---|
38 | public EMMMapTreeModel(int k) {
|
---|
39 | K = k;
|
---|
40 | ModelSet = new List<ISymbolicExpressionTree>();
|
---|
41 | ClusterNumber = new List<int>();
|
---|
42 | Map = new List<List<int>>();
|
---|
43 | }
|
---|
44 | public EMMMapTreeModel(EMMMapTreeModel original, Cloner cloner) {
|
---|
45 | //original.ModelSet.ForEach(x => ModelSet.Add((ISymbolicExpressionTree)x.Clone(cloner)));
|
---|
46 | //original.ClusterNumber.ForEach(x => ClusterNumber.Add(x));
|
---|
47 | //original.Map.ForEach(x => Map.Add(x));
|
---|
48 | if (original.ModelSet != null) {
|
---|
49 | ModelSet = original.ModelSet.Select(cloner.Clone).ToList();
|
---|
50 | }
|
---|
51 | if (original.ClusterNumber != null) {
|
---|
52 | ClusterNumber = original.ClusterNumber.ToList();
|
---|
53 | }
|
---|
54 | if (original.Map != null) {
|
---|
55 | Map = original.Map.Select(x => x.ToList()).ToList();
|
---|
56 | }
|
---|
57 | K = original.K;
|
---|
58 | }
|
---|
59 | public EMMMapTreeModel(IRandom random, IEnumerable<ISymbolicExpressionTree> trees, int k) : this(k) {
|
---|
60 | ModelSet = trees.ToList();
|
---|
61 | CalculateDistances();
|
---|
62 | CreateMap(random, k);
|
---|
63 | }
|
---|
64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
65 | return new EMMMapTreeModel(this, cloner);
|
---|
66 | }
|
---|
67 | #endregion
|
---|
68 | #region MapTransformation
|
---|
69 | override protected void CalculateDistances() {
|
---|
70 | Distances = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(ModelSet, simplify: false, strict: true);
|
---|
71 | for (int i = 0; i < ModelSet.Count - 1; i++) {
|
---|
72 | for (int j = i + 1; j < ModelSet.Count; j++) {
|
---|
73 | Distances[j, i] = Distances[i, j] = 1 - Distances[i, j];
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | override public void CreateMap(IRandom random, int k) {
|
---|
78 | K = k;
|
---|
79 | //Clusterization
|
---|
80 | EMModelsClusterizationAlgorithm clusteringAlgorithm = new EMModelsClusterizationAlgorithm(K);
|
---|
81 | K = clusteringAlgorithm.Apply(random, Distances, ClusterNumber);
|
---|
82 | // Cheking a Map size
|
---|
83 | if (Map != null) Map.Clear();
|
---|
84 | else Map = new List<List<int>>();
|
---|
85 | if (Map.Count != K) {
|
---|
86 | if (Map.Count != 0) {
|
---|
87 | Map.Clear();
|
---|
88 | }
|
---|
89 | for (int i = 0; i < K; i++) {
|
---|
90 | Map.Add(new List<int>());
|
---|
91 | }
|
---|
92 | }
|
---|
93 | // Map fulfilment
|
---|
94 | for (int i = 0; i < ModelSet.Count; i++) {
|
---|
95 | Map[ClusterNumber[i]].Add(i);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | #endregion
|
---|
99 | #region Dialog with surroudings
|
---|
100 | override public ISymbolicExpressionTree NewModelForInizializtion(IRandom random, out int cluster, out int treeNumber) {
|
---|
101 | treeNumber = random.Next(ModelSet.Count);
|
---|
102 | cluster = ClusterNumber[treeNumber];
|
---|
103 | return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
|
---|
104 | }
|
---|
105 |
|
---|
106 | #endregion
|
---|
107 | }
|
---|
108 | }
|
---|