#region License Information /* HeuristicLab * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Parameters; using HeuristicLab.Problems.DataAnalysis.Symbolic; using HeuristicLab.Random; using System.Collections.Generic; using System.Linq; namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { [Item("IslandMap", "A map of models of models of models")] [StorableType("E4AB04B9-FD5D-47EE-949D-243660754F3A")] public class EMMIslandMap : EMMMapBase { [Storable] public List ClusterNumber { get; set; } // May be only Island Map really need it public double[] AverageDistance { get; private set; } private const string ClusterNumbersParameterName = "ClusterNumbers"; private const string ClusterNumbersShowParameterName = "ClusterNumbersShow"; public IValueParameter ClusterNumbersParameter { get { return (IValueParameter)Parameters[ClusterNumbersParameterName]; } } public IValueParameter ClusterNumbersShowParameter { get { return (IValueParameter)Parameters[ClusterNumbersShowParameterName]; } } public IntValue ClusterNumbers { get { return ClusterNumbersParameter.Value; } set { ClusterNumbersParameter.Value = value; } } public IntValue ClusterNumbersShow { get { return ClusterNumbersShowParameter.Value; } set { ClusterNumbersShowParameter.Value = value; } } #region constructors [StorableConstructor] protected EMMIslandMap(StorableConstructorFlag _) : base(_) { } public EMMIslandMap() { Parameters.Add(new ValueParameter(ClusterNumbersParameterName, "The number of clusters for model Map.", new IntValue(10))); Parameters.Add(new ValueParameter(ClusterNumbersShowParameterName, "The number of clusters for model Map.", new IntValue(10))); ModelSet = new List(); ClusterNumber = new List(); } public override IDeepCloneable Clone(Cloner cloner) { return new EMMIslandMap(this, cloner); } public EMMIslandMap(EMMIslandMap original, Cloner cloner) : base(original, cloner) { if (original.ClusterNumber != null) { ClusterNumber = original.ClusterNumber.ToList(); } } #endregion #region Map Apply Functions override public void CreateMap(IRandom random) { var totalDistance = ModelSetPreparation.CalculateDistances(ModelSet); //structure distances CreateMap(random, totalDistance); } override public void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) { CreateMap(random, ModelSetPreparation.TotalDistanceMatrixCalculation(random, problem, ModelSet, DistanceParametr)); } override public void CreateMap(IRandom random, double[,] totalDistance) { if (Map != null) { Map.Clear(); } ClusterNumbersShow.Value = KMeansClusterizationAlgorithm.ApplyClusteringAlgorithm(random, totalDistance, ClusterNumber, ClusterNumbers.Value); MapSizeCheck(ClusterNumbersShow.Value); for (int i = 0; i < ModelSet.Count; i++) { Map[ClusterNumber[i]].Add(i); } AverageDistanceInClusterCalculation(totalDistance, ClusterNumbersShow.Value); } override public void MapRead(IEnumerable trees) { base.MapRead(trees); string fileName = ("Map" + DistanceParametr + ".txt"); Map = FileComuncations.IntMatrixFromFileRead(fileName); ClusterNumbers.Value = Map.Count; ClusterNumbersShow.Value = ClusterNumbers.Value; ClusterNumbersCalculate(); AverageDistanceInClusterCalculation(ModelSetPreparation.CalculateDistances(ModelSet), Map.Count); } override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) { return NewModelForInizializtion(random, out treeNumber); } private void AverageDistanceInClusterCalculation(double[,] distances, int k) { AverageDistance = new double[k]; var temp = new List(); for (int i = 0; i < k; i++) { KMeansClusterizationAlgorithm.AverageClusterDistanceCalculation(temp, distances, ClusterNumber, ClusterNumber.Count, i); var number = HelpFunctions.ChooseMinElementIndex(temp); AverageDistance[i] = temp[number] / Map[i].Count; temp.Clear(); } } public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) { if (parentTreeNumber == -10) { treeNumber = random.Next(ModelSet.Count); } else { treeNumber = Map[ClusterNumber[parentTreeNumber]].SampleRandom(random); } return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone(); } public void ClusterNumbersCalculate() { for (int i = 0; i < Map.Count; i++) { for (int j = 0; j < Map[i].Count; j++) { ClusterNumber.Add(0); } } for (int i = 0; i < Map.Count; i++) { for (int j = 0; j < Map[i].Count; j++) { ClusterNumber[Map[i][j]] = i; } } } #endregion } }