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.Data;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
29 | using HeuristicLab.Random;
|
---|
30 | using System.Collections.Generic;
|
---|
31 | using System.Linq;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
|
---|
34 | [Item("IslandMap", "A map of models of models of models")]
|
---|
35 | [StorableType("E4AB04B9-FD5D-47EE-949D-243660754F3A")]
|
---|
36 | public class EMMIslandMap : EMMMapBase<ISymbolicExpressionTree> {
|
---|
37 |
|
---|
38 | [Storable]
|
---|
39 | public List<int> ClusterNumber { get; set; } // May be only Island Map really need it
|
---|
40 | public double[] AverageDistance { get; private set; }
|
---|
41 | private const string ClusterNumbersParameterName = "ClusterNumbers";
|
---|
42 | private const string ClusterNumbersShowParameterName = "ClusterNumbersShow";
|
---|
43 | public IValueParameter<IntValue> ClusterNumbersParameter {
|
---|
44 | get { return (IValueParameter<IntValue>)Parameters[ClusterNumbersParameterName]; }
|
---|
45 | }
|
---|
46 | public IValueParameter<IntValue> ClusterNumbersShowParameter {
|
---|
47 | get { return (IValueParameter<IntValue>)Parameters[ClusterNumbersShowParameterName]; }
|
---|
48 | }
|
---|
49 | public IntValue ClusterNumbers {
|
---|
50 | get { return ClusterNumbersParameter.Value; }
|
---|
51 | set { ClusterNumbersParameter.Value = value; }
|
---|
52 | }
|
---|
53 | public IntValue ClusterNumbersShow {
|
---|
54 | get { return ClusterNumbersShowParameter.Value; }
|
---|
55 | set { ClusterNumbersShowParameter.Value = value; }
|
---|
56 | }
|
---|
57 | #region constructors
|
---|
58 | [StorableConstructor]
|
---|
59 | protected EMMIslandMap(StorableConstructorFlag _) : base(_) { }
|
---|
60 | public EMMIslandMap() {
|
---|
61 | Parameters.Add(new ValueParameter<IntValue>(ClusterNumbersParameterName, "The number of clusters for model Map.", new IntValue(10)));
|
---|
62 | Parameters.Add(new ValueParameter<IntValue>(ClusterNumbersShowParameterName, "The number of clusters for model Map.", new IntValue(10)));
|
---|
63 | ModelSet = new List<ISymbolicExpressionTree>();
|
---|
64 | ClusterNumber = new List<int>();
|
---|
65 | }
|
---|
66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
67 | return new EMMIslandMap(this, cloner);
|
---|
68 | }
|
---|
69 | public EMMIslandMap(EMMIslandMap original, Cloner cloner) : base(original, cloner) {
|
---|
70 | if (original.ClusterNumber != null) {
|
---|
71 | ClusterNumber = original.ClusterNumber.ToList();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | #endregion
|
---|
75 | #region Map Apply Functions
|
---|
76 | override public void CreateMap(IRandom random) {
|
---|
77 | var totalDistance = ModelSetPreparation.CalculateDistances(ModelSet); //structure distances
|
---|
78 | CreateMap(random, totalDistance);
|
---|
79 | }
|
---|
80 | override public void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) {
|
---|
81 | CreateMap(random, ModelSetPreparation.TotalDistanceMatrixCalculation(random, problem, ModelSet, DistanceParametr));
|
---|
82 | }
|
---|
83 | override public void CreateMap(IRandom random, double[,] totalDistance) {
|
---|
84 | if (Map != null) {
|
---|
85 | Map.Clear();
|
---|
86 | }
|
---|
87 | ClusterNumbersShow.Value = KMeansClusterizationAlgorithm.ApplyClusteringAlgorithm(random, totalDistance, ClusterNumber, ClusterNumbers.Value);
|
---|
88 | MapSizeCheck(ClusterNumbersShow.Value);
|
---|
89 | for (int i = 0; i < ModelSet.Count; i++) {
|
---|
90 | Map[ClusterNumber[i]].Add(i);
|
---|
91 | }
|
---|
92 | AverageDistanceInClusterCalculation(totalDistance, ClusterNumbersShow.Value);
|
---|
93 | }
|
---|
94 | override public void MapRead(IEnumerable<ISymbolicExpressionTree> trees) {
|
---|
95 | base.MapRead(trees);
|
---|
96 | string fileName = ("Map" + DistanceParametr + ".txt");
|
---|
97 | Map = FileComuncations.IntMatrixFromFileRead(fileName);
|
---|
98 | ClusterNumbers.Value = Map.Count;
|
---|
99 | ClusterNumbersShow.Value = ClusterNumbers.Value;
|
---|
100 | ClusterNumbersCalculate();
|
---|
101 | AverageDistanceInClusterCalculation(ModelSetPreparation.CalculateDistances(ModelSet), Map.Count);
|
---|
102 | }
|
---|
103 | override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) {
|
---|
104 | return NewModelForInizializtion(random, out treeNumber);
|
---|
105 | }
|
---|
106 | private void AverageDistanceInClusterCalculation(double[,] distances, int k) {
|
---|
107 | AverageDistance = new double[k];
|
---|
108 | var temp = new List<double>();
|
---|
109 | for (int i = 0; i < k; i++) {
|
---|
110 | KMeansClusterizationAlgorithm.AverageClusterDistanceCalculation(temp, distances, ClusterNumber, ClusterNumber.Count, i);
|
---|
111 | var number = HelpFunctions.ChooseMinElementIndex(temp);
|
---|
112 | AverageDistance[i] = temp[number] / Map[i].Count;
|
---|
113 | temp.Clear();
|
---|
114 | }
|
---|
115 | }
|
---|
116 | public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) {
|
---|
117 | if (parentTreeNumber == -10) {
|
---|
118 | treeNumber = random.Next(ModelSet.Count);
|
---|
119 | } else {
|
---|
120 | treeNumber = Map[ClusterNumber[parentTreeNumber]].SampleRandom(random);
|
---|
121 | }
|
---|
122 | return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
|
---|
123 | }
|
---|
124 | public void ClusterNumbersCalculate() {
|
---|
125 | for (int i = 0; i < Map.Count; i++) {
|
---|
126 | for (int j = 0; j < Map[i].Count; j++) {
|
---|
127 | ClusterNumber.Add(0);
|
---|
128 | }
|
---|
129 | }
|
---|
130 | for (int i = 0; i < Map.Count; i++) {
|
---|
131 | for (int j = 0; j < Map[i].Count; j++) {
|
---|
132 | ClusterNumber[Map[i][j]] = i;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | #endregion
|
---|
137 |
|
---|
138 | }
|
---|
139 | }
|
---|