[16722] | 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;
|
---|
[17134] | 25 | using HeuristicLab.Data;
|
---|
[16722] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[17134] | 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
[17002] | 29 | using HeuristicLab.Random;
|
---|
[16722] | 30 | using System.Collections.Generic;
|
---|
[17002] | 31 | using System.Linq;
|
---|
[16722] | 32 |
|
---|
| 33 | namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
|
---|
[16899] | 34 | [Item("IslandMap", "A map of models of models of models")]
|
---|
[16734] | 35 | [StorableType("E4AB04B9-FD5D-47EE-949D-243660754F3A")]
|
---|
[16899] | 36 | public class EMMIslandMap : EMMMapBase<ISymbolicExpressionTree> {
|
---|
[17134] | 37 |
|
---|
[17002] | 38 | [Storable]
|
---|
| 39 | public List<int> ClusterNumber { get; set; } // May be only Island Map really need it
|
---|
[17134] | 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
|
---|
[16722] | 58 | [StorableConstructor]
|
---|
[16899] | 59 | protected EMMIslandMap(StorableConstructorFlag _) : base(_) { }
|
---|
[17002] | 60 | public EMMIslandMap() {
|
---|
[17134] | 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)));
|
---|
[17002] | 63 | ModelSet = new List<ISymbolicExpressionTree>();
|
---|
| 64 | ClusterNumber = new List<int>();
|
---|
| 65 | }
|
---|
[16722] | 66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[16899] | 67 | return new EMMIslandMap(this, cloner);
|
---|
[16722] | 68 | }
|
---|
[17002] | 69 | public EMMIslandMap(EMMIslandMap original, Cloner cloner) : base(original, cloner) {
|
---|
| 70 | if (original.ClusterNumber != null) {
|
---|
| 71 | ClusterNumber = original.ClusterNumber.ToList();
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
[16722] | 74 | #endregion
|
---|
[17134] | 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);
|
---|
[16722] | 89 | for (int i = 0; i < ModelSet.Count; i++) {
|
---|
| 90 | Map[ClusterNumber[i]].Add(i);
|
---|
| 91 | }
|
---|
[17134] | 92 | AverageDistanceInClusterCalculation(totalDistance, ClusterNumbersShow.Value);
|
---|
[16722] | 93 | }
|
---|
[17134] | 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 | }
|
---|
[17002] | 103 | override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) {
|
---|
| 104 | return NewModelForInizializtion(random, out treeNumber);
|
---|
[16734] | 105 | }
|
---|
[17134] | 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 | }
|
---|
[17002] | 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 | }
|
---|
[17134] | 124 | public void ClusterNumbersCalculate() {
|
---|
[17002] | 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 | }
|
---|
[16722] | 136 | #endregion
|
---|
| 137 |
|
---|
| 138 | }
|
---|
| 139 | }
|
---|