#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 HeuristicLab.Random; using System.Collections.Generic; using System.IO; using System.Linq; namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { [StorableType("83CF9650-98FF-454B-9072-82EA4D39C752")] public abstract class EMMMapBase : Item where T : class { #region data members [Storable] public List ModelSet { get; set; } [Storable] public List> Map { get; set; } #endregion #region constructors [StorableConstructor] protected EMMMapBase(StorableConstructorFlag _) : base(_) { } public EMMMapBase() { Map = new List>(); } public EMMMapBase(EMMMapBase original, Cloner cloner) { if (original.ModelSet != null) { if (original.ModelSet is List originalSet && ModelSet is List set) set = originalSet.Select(cloner.Clone).ToList(); else ModelSet = original.ModelSet.ToList(); /// check this if you want to use it } if (original.Map != null) { Map = original.Map.Select(x => x.ToList()).ToList(); } } #endregion #region map creation functions protected double[,] CalculateDistances() { double[,] distances; if (ModelSet is List set) { distances = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(set, simplify: false, strict: true); } else { /// for future work distances = new double[ModelSet.Count, ModelSet.Count]; for (int i = 0; i < ModelSet.Count - 1; i++) { for (int j = 0; j <= i; j++) { distances[i, j] = 0; } } } 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]; } } return distances; } public abstract void CreateMap(IRandom random, int k); public void MapCreationPrepare(IEnumerable trees) { ModelSet = trees.ToList(); } protected void MapSizeCheck(int k) { 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()); } } } #endregion #region map and files public void MapRead(IRandom random, IEnumerable trees, string fileName = "Map.txt") { ModelSet = trees.ToList(); MapFromFileRead(fileName); if (this is EMMIslandMap island) { island.ClusterNumbersCalculate(); } if (this is EMMNetworkMap one) { one.NeghboorNumber = Map[0].Count; } } public void WriteMapToTxtFile(IRandom random) { string s = random.ToString(); string fileName = "Map"; fileName += s; fileName += ".txt"; File.WriteAllLines(fileName, MapToString()); string fileName2 = "MapToSee"; fileName2 += s; fileName2 += ".txt"; File.WriteAllLines(fileName2, MapToSee()); } public string[] MapToString() { // Function that preapre 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[i].Count; j++) { s[i] += Map[i][j].ToString(); s[i] += " "; } } return s; } public string[] MapToSee() { // Function that prepare Map to printing in .txt File: create a set of strings in human readable view var fmt = new InfixExpressionFormatter(); string[] s; s = new string[(ModelSet.Count) + 1]; s[0] = "ClusterNumber" + "," + "ModelNumber" + "," + "Model"; for (int i = 1; i < ((ModelSet.Count) + 1); i++) { s[i] = ""; if (this is EMMIslandMap island) { s[i] += island.ClusterNumber[i - 1].ToString() + ","; } s[i] += (i - 1).ToString() + ","; if (ModelSet[i - 1] is ISymbolicExpressionTree model) { s[i] += fmt.Format(model); } else { s[i] += ModelSet[i - 1].ToString(); } } return s; } public void MapFromFileRead(string fileName) { string input = File.ReadAllText(fileName); int i = 0; foreach (var row in input.Split('\n')) { Map.Add(new List()); foreach (var col in row.Trim().Split(' ')) { Map[i].Add(int.Parse(col.Trim())); } i++; } } #endregion #region map use functions public abstract T NewModelForInizializtionNotTree(IRandom random, out int treeNumber); public ISymbolicExpressionTree NewModelForInizializtion(IRandom random, out int treeNumber) { treeNumber = random.Next(ModelSet.Count); if (ModelSet[treeNumber] is ISymbolicExpressionTree model) return (ISymbolicExpressionTree)(model.Clone()); return new SymbolicExpressionTree(); } public void NodeManipulationForInizializtion(IRandom random, TreeModelTreeNode node) { node.Tree = NewModelForInizializtion(random, out int treeNumber); node.SetLocalParameters(random, 0.5); node.TreeNumber = treeNumber; } public abstract ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber); public virtual void NodeForMutationChange(IRandom random, TreeModelTreeNode treeNode) { int treeNumber = treeNode.TreeNumber; int treeNumber2 = treeNode.TreeNumber; treeNode.Tree = new SymbolicExpressionTree(NewModelForMutation(random, out treeNumber, treeNumber2).Root); treeNode.TreeNumber = treeNumber; SetLocalParametersForTree(random, 0.5, treeNode.Tree); } public void SetLocalParametersForTree(IRandom random, double shakingFactor, ISymbolicExpressionTree tree) { foreach (var node in tree.IterateNodesPrefix().Where(x => x.HasLocalParameters)) { if (node is VariableTreeNode variableTreeNode) { var symbol = variableTreeNode.Symbol; variableTreeNode.Weight = NormalDistributedRandom.NextDouble(random, symbol.WeightManipulatorMu, symbol.WeightManipulatorSigma); } else { node.ResetLocalParameters(random); } } } public virtual void MapUpDate(Dictionary population) { } #endregion } }