#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("RankMap", "A map of models of models of models")]
[StorableType("1D4DD90E-553A-46DB-B0CD-6A899AA0B6D0")]
public class EMMRankMap : EMMMapBase { // it do not work absolutely
[Storable]
public List> Probabilities { get; set; }
#region constructors
[StorableConstructor]
protected EMMRankMap(StorableConstructorFlag _) : base(_) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new EMMRankMap(this, cloner);
}
public EMMRankMap() : base() {
ModelSet = new List();
Probabilities = new List>();
}
public EMMRankMap(EMMRankMap original, Cloner cloner) : base(original, cloner) {
if (original.Probabilities != null) {
Probabilities = original.Probabilities.Select(x => x.ToList()).ToList();
}
}
#endregion
#region MapCreation
override public void CreateMap(IRandom random) {
MapSizeCheck(ModelSet.Count);
ApplyRankMapCreationAlgorithm(ModelSetPreparation.CalculateDistances(ModelSet), Map, Probabilities);
}
override public void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) {
MapSizeCheck(ModelSet.Count);
ApplyRankMapCreationAlgorithm(ModelSetPreparation.DistanceMatrixCalculation(ModelSet, DistanceParametr, problem), Map, Probabilities);
}
override public void MapRead(IEnumerable trees) {
base.MapRead(trees);
MapFullment(trees.Count());
string fileName = ("Map" + DistanceParametr + ".txt");
Probabilities = FileComuncations.DoubleMatrixFromFileRead(fileName);
}
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);
}
}
}
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;
}
public static void ApplyRankMapCreationAlgorithm(double[,] distances, List> map, List> probabilities) {
int mapSize = distances.GetLength(0);
double tempSum = 0;
for (int i = 0; i < mapSize; i++) {
tempSum += i;
}
List> currentList = new List>();
for (int t = 0; t < mapSize; t++) {
for (int i = 0; i < mapSize; i++) {
if (distances[i, t].IsAlmost(0))
continue;
currentList.Add(new List());
currentList[i].Add(i);
currentList[i].Add(distances[i, t]);
}
currentList.Sort((a, b) => a[1].CompareTo(b[1])); ///workable sorting
for (int i = 0; i < currentList.Count; i++) {
currentList[i].Add(currentList.Count - i);
}
probabilities.Add(new List());
for (int i = 0; i < mapSize; i++) {
if (distances[i, t].IsAlmost(0))
continue;
map[t].Add(Convert.ToInt32(currentList[i][0]));
probabilities[t].Add(currentList[i][2] / tempSum);
}
currentList.Clear();
}
}
#endregion
#region Map Apply Functions
public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) {
treeNumber = Map[parentTreeNumber][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
}
}