#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 {
[StorableType("83CF9650-98FF-454B-9072-82EA4D39C752")]
public abstract class EMMMapBase : ParameterizedNamedItem where T : class {
#region data members
[Storable]
public List ModelSet { get; protected set; }
[Storable]
public List> Map { get; set; }
public string DistanceParametr { get; set; }
#endregion
#region constructors
[StorableConstructor]
protected EMMMapBase(StorableConstructorFlag _) : base(_) { }
public EMMMapBase() {
Map = new List>();
DistanceParametr = "Symbolic";
}
public EMMMapBase(EMMMapBase original, Cloner cloner) : base(original, 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();
}
DistanceParametr = original.DistanceParametr;
}
#endregion
#region map creation functions
public abstract void CreateMap(IRandom random);
public void MapCreationPrepare(IEnumerable trees) {
ModelSet = trees.ToList();
}
public virtual void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) {
if (Map != null) {
Map.Clear();
}
CreateMap(random);
}
public virtual void CreateMap(IRandom random, double[,] totalDistance) {
if (Map != null) {
Map.Clear();
}
CreateMap(random);
}
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 virtual void MapRead(IEnumerable trees) {
ModelSet = trees.ToList();
}
public void WriteMapToTxtFile(IRandom random) {
string s = random.NextDouble().ToString();
string fileName = "MapToAnalize";
fileName += s;
fileName += DistanceParametr;
fileName += ".txt";
File.WriteAllLines(fileName, MapToString());
string fileName2 = "MapToSee";
fileName2 += s;
fileName2 += DistanceParametr;
fileName2 += ".txt";
File.WriteAllLines(fileName2, MapToSee());
string fileName3 = "Map";
fileName3 += DistanceParametr;
fileName3 += ".txt";
File.WriteAllLines(fileName3, MapToStoreInFile());
}
public string[] MapToString() { // Function that prepare Map to printing in .txt File: create a set of strings for future analyzing
string[] s;
s = new string[Map.Count];
for (int i = 0; i < Map.Count; i++) {
s[i] = i.ToString() + ": ";
for (int j = 0; j < Map[i].Count; j++) {
s[i] += Map[i][j].ToString();
s[i] += " ";
}
if (this is EMMIslandMap island) {
s[i] += " Average distance:" + island.AverageDistance[i].ToString();
}
}
return s;
}
public virtual 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[i].Count; j++) {
s[i] += Map[i][j].ToString();
if (j != (Map[i].Count - 1)) { 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;
}
#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;
HelpFunctions.SetLocalParametersForTree(random, 0.5, treeNode.Tree);
}
public virtual void MapUpDate(Dictionary population) { }
#endregion
}
}