#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.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Problems.DataAnalysis.Symbolic; using System.Collections.Generic; using System.Linq; namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { [StorableType("A9AE93F0-E589-44D0-AD34-0E3AA358D669")] [Item("TreeModelMap", "A map of models of models of models")] public class EMMMapTreeModel : EMMMapBase { #region conctructors [StorableConstructor] protected EMMMapTreeModel(StorableConstructorFlag _) : base(_) { } public EMMMapTreeModel() : this(1) { } public EMMMapTreeModel(int k) { K = k; ModelSet = new List(); ClusterNumber = new List(); Map = new List>(); } public EMMMapTreeModel(EMMMapTreeModel original, Cloner cloner) { //original.ModelSet.ForEach(x => ModelSet.Add((ISymbolicExpressionTree)x.Clone(cloner))); //original.ClusterNumber.ForEach(x => ClusterNumber.Add(x)); //original.Map.ForEach(x => Map.Add(x)); if (original.ModelSet != null) { ModelSet = original.ModelSet.Select(cloner.Clone).ToList(); } if (original.ClusterNumber != null) { ClusterNumber = original.ClusterNumber.ToList(); } if (original.Map != null) { Map = original.Map.Select(x => x.ToList()).ToList(); } K = original.K; } public EMMMapTreeModel(IRandom random, IEnumerable trees, int k) : this(k) { ModelSet = trees.ToList(); CalculateDistances(); CreateMap(random, k); } public override IDeepCloneable Clone(Cloner cloner) { return new EMMMapTreeModel(this, cloner); } #endregion #region MapTransformation override protected void CalculateDistances() { Distances = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(ModelSet, simplify: false, strict: true); for (int i = 0; i < ModelSet.Count - 1; i++) { for (int j = i + 1; j < ModelSet.Count; j++) { Distances[j, i] = Distances[i, j] = 1 - Distances[i, j]; } } } override public void CreateMap(IRandom random, int k) { K = k; //Clusterization EMModelsClusterizationAlgorithm clusteringAlgorithm = new EMModelsClusterizationAlgorithm(K); K = clusteringAlgorithm.Apply(random, Distances, ClusterNumber); // Cheking a Map size if (Map != null) Map.Clear(); else Map = new List>(); if (Map.Count != K) { if (Map.Count != 0) { Map.Clear(); } for (int i = 0; i < K; i++) { Map.Add(new List()); } } // Map fulfilment for (int i = 0; i < ModelSet.Count; i++) { Map[ClusterNumber[i]].Add(i); } } #endregion #region Dialog with surroudings override public ISymbolicExpressionTree NewModelForInizializtion(IRandom random, out int cluster, out int treeNumber) { treeNumber = random.Next(ModelSet.Count); cluster = ClusterNumber[treeNumber]; return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone(); } #endregion } }