#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.IO;
using System.Linq;
namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
[Item("TreeModelMap", "A map of models of models of models")]
[StorableType("E4AB04B9-FD5D-47EE-949D-243660754F3A")]
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, int neghboorNumber) : this(k) {
ModelSet = trees.ToList();
CalculateDistances();
if (k < ModelSet.Count)
CreateIslandMap(random, k);
else if (k == ModelSet.Count) {
CreateFullConnectedMap(random, k, neghboorNumber);
} else {
k -= ModelSet.Count;
CreateIslandMap(random, k);
}
}
public EMMMapTreeModel(IRandom random, IEnumerable trees) : this(1) {
ModelSet = trees.ToList();
string input = File.ReadAllText("Map.txt");
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++;
}
K = Map.Count;
MapPreparation();
}
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];
}
}
}
protected void CreateFullConnectedMap(IRandom random, int k, int neghboorNumber) {
EMModelsClusterizationAlgorithm.ApplyFullConectedMapCreationAlgorithm(random, Distances, Map, k, neghboorNumber);
K = k;
for (int i = 0; i < Map.Count; i++) {
ClusterNumber.Add(i);
}
}
override protected void CreateIslandMap(IRandom random, int k) {
//Clusterization
K = EMModelsClusterizationAlgorithm.ApplyClusteringAlgorithm(random, Distances, ClusterNumber, k);
// 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);
}
}
protected void MapPreparation() {
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
#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();
}
public string[] MapToString() {
string[] s;
s = new string[K];
for (int i = 0; i < K; 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() {
var fmt = new InfixExpressionFormatter();
string[] s;
s = new string[(ModelSet.Count) + 1];
s[0] = "ClusterNumber" + "," + "Modfelnumber" + "," + "Model";
for (int i = 1; i < ((ModelSet.Count) + 1); i++) {
s[i] = ClusterNumber[i - 1].ToString() + "," + (i - 1).ToString() + "," + fmt.Format(ModelSet[i - 1]);
}
return s;
}
#endregion
}
}