#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; using System.Collections.Generic; using System.Linq; namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { [Item("DistanceMap", "A map of models of models of models")] [StorableType("456692FB-2149-4359-8106-45D59D2D7FA0")] public class EMMDisatanceMap : EMMMapBase { [Storable] public List> Probabilities { get; set; } #region constructors [StorableConstructor] protected EMMDisatanceMap(StorableConstructorFlag _) : base(_) { } public override IDeepCloneable Clone(Cloner cloner) { return new EMMDisatanceMap(this, cloner); } public EMMDisatanceMap() : base() { ModelSet = new List(); Probabilities = new List>(); } public EMMDisatanceMap(EMMDisatanceMap original, Cloner cloner) : base(original, cloner) { if (original.Probabilities != null) { Probabilities = original.Probabilities.Select(x => x.ToList()).ToList(); } } #endregion #region Map Creation override public void CreateMap(IRandom random) { MapSizeCheck(ModelSet.Count); ApplyDistanceMapCreationAlgorithm(random, ModelSetPreparation.CalculateDistances(ModelSet), Map, Probabilities); } override public string[] MapToStoreInFile() { // Function that prepare Map to printing in .txt File: create a set of strings for future reading by computer string[] s; s = new string[Map.Count]; for (int i = 0; i < Map.Count; i++) { s[i] = ""; for (int j = 0; j < (Map.Count - 1); j++) { s[i] += Probabilities[i][j].ToString(); if (j != (Map.Count - 2)) { s[i] += " "; } } } return s; } override public void MapRead(IEnumerable trees) { base.MapRead(trees); MapFullment(trees.Count()); string fileName = ("Map" + DistanceParametr + ".txt"); Probabilities = FileComuncations.DoubleMatrixFromFileRead(fileName); } override public void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) { MapSizeCheck(ModelSet.Count); if (Map != null) { Map.Clear(); } ApplyDistanceMapCreationAlgorithm(random, ModelSetPreparation.DistanceMatrixCalculation(ModelSet, DistanceParametr, problem), Map, Probabilities); } protected void ApplyDistanceMapCreationAlgorithm(IRandom random, double[,] distances, List> map, List> probabilities) { int mapSize = distances.GetLength(0); for (int t = 0; t < mapSize; t++) { probabilities.Add(new List()); double tempSum = 0; for (int i = 0; i < mapSize; i++) { tempSum += Math.Log(distances[i, t]); } for (int i = 0; i < mapSize; i++) { if (distances[i, t].IsAlmost(0)) continue; map[t].Add(i); probabilities[t].Add(Math.Log(distances[i, t]) / tempSum); } } } protected void MapFullment(int mapSize) { if (Map != null) { Map.Clear(); } for (int t = 0; t < mapSize; t++) { for (int i = 0; i < mapSize; i++) { if (i == t) continue; Map[t].Add(i); } } } #endregion #region Map Apply Functions public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) { treeNumber = HelpFunctions.OneElementFromListProportionalSelection(random, Probabilities[parentTreeNumber]); return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone(); } override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) { return NewModelForInizializtion(random, out treeNumber); } #endregion } }