Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/06/19 14:20:06 (5 years ago)
Author:
msemenki
Message:

#2988: New version of class structure.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMBaseMap.cs

    r16898 r16899  
    2121
    2222using HEAL.Attic;
     23using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     26using HeuristicLab.Problems.DataAnalysis.Symbolic;
    2427using System.Collections.Generic;
     28using System.IO;
     29using System.Linq;
    2530
    2631namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
    2732  [StorableType("83CF9650-98FF-454B-9072-82EA4D39C752")]
    2833  public abstract class EMMMapBase<T> : Item where T : class {
     34    [Storable]
    2935    public List<T> ModelSet { get; set; }
     36    [Storable]
    3037    public List<int> ClusterNumber { get; set; }
     38    [Storable]
    3139    public List<List<int>> Map { get; set; }
     40    [Storable]
    3241    public double[,] Distances { get; set; }
    33     public int K { get; set; }
     42    [Storable]
     43    public int K { get; protected set; }
    3444
    35     protected abstract void CalculateDistances();
    36     protected abstract void CreateIslandMap(IRandom random, int k);
    37     public abstract T NewModelForInizializtion(IRandom random, out int cluster, out int treeNumber);
     45    protected void CalculateDistances() {
     46      if (ModelSet is List<ISymbolicExpressionTree> set) {
     47        Distances = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(set, simplify: false, strict: true);
     48      } else { /// for future work
     49        for (int i = 0; i < ModelSet.Count - 1; i++) {
     50          for (int j = 0; j <= i; j++) {
     51            Distances[i, j] = 0;
     52          }
     53        }
     54      }
     55      for (int i = 0; i < ModelSet.Count - 1; i++) {
     56        for (int j = i + 1; j < ModelSet.Count; j++) {
     57          Distances[j, i] = Distances[i, j] = 1 - Distances[i, j];
     58        }
     59      }
     60
     61    }
     62    public abstract void CreateMap(IRandom random, int k);
     63    public abstract T NewModelForInizializtionNotTree(IRandom random, out int cluster, out int treeNumber);
     64    public ISymbolicExpressionTree NewModelForInizializtion(IRandom random, out int cluster, out int treeNumber) {
     65      treeNumber = random.Next(ModelSet.Count);
     66      cluster = ClusterNumber[treeNumber];
     67      if (ModelSet[treeNumber] is ISymbolicExpressionTree model)
     68        return (ISymbolicExpressionTree)(model.Clone());
     69      return new SymbolicExpressionTree();
     70    }
    3871
    3972    [StorableConstructor]
    4073    protected EMMMapBase(StorableConstructorFlag _) : base(_) { }
    41     protected EMMMapBase() { }
    42 
     74    protected EMMMapBase() : this(1) { }
     75    public EMMMapBase(int k) {
     76      K = k;
     77      ClusterNumber = new List<int>();
     78      Map = new List<List<int>>();
     79    }
     80    public EMMMapBase(EMMMapBase<T> original, Cloner cloner) {
     81      if (original.ModelSet != null) {
     82        if (original.ModelSet is List<ISymbolicExpressionTree> originalSet && ModelSet is List<ISymbolicExpressionTree> set)
     83          set = originalSet.Select(cloner.Clone).ToList();
     84        else ModelSet = original.ModelSet.ToList(); /// check this if you want to use it
     85      }
     86      if (original.ClusterNumber != null) {
     87        ClusterNumber = original.ClusterNumber.ToList();
     88      }
     89      if (original.Map != null) {
     90        Map = original.Map.Select(x => x.ToList()).ToList();
     91      }
     92      K = original.K;
     93    }
     94    //public EMMMapBase(IRandom random, IEnumerable<T> trees, int k) : this(k) {
     95    //  // constructor that should be used in case of creation of a new map from the start point
     96    //  ModelSet = trees.ToList();
     97    //  CalculateDistances();
     98    //  CreateMap(random, k);
     99    //}
     100    //public EMMMapBase(IRandom random, IEnumerable<T> trees, string fileName = "Map.txt") : this(1) {
     101    //  // constructor that shoud be used in case of using of "old" map, that was previously created and now shoud be readed from file
     102    //  ModelSet = trees.ToList();
     103    //  MapFromFileRead(fileName);
     104    //  K = Map.Count;
     105    //  MapPreparation();
     106    //}
     107    public void MapCreationPrepare(IRandom random, IEnumerable<T> trees, int k) {
     108      ModelSet = trees.ToList();
     109      CalculateDistances();
     110      CreateMap(random, k);
     111    }
     112    public void MapRead(IRandom random, IEnumerable<T> trees, string fileName = "Map.txt") {
     113      ModelSet = trees.ToList();
     114      MapFromFileRead(fileName);
     115      K = Map.Count;
     116      MapPreparation();
     117      if (this is EMMNetworkMap one) { one.NeghboorNumber = Map[0].Count; }
     118    }
     119    public void WriteMapToTxtFile(IRandom random) {
     120      string s = random.ToString();
     121      string fileName = "Map";
     122      fileName += s;
     123      fileName += ".txt";
     124      File.WriteAllLines(fileName, MapToString());
     125      string fileName2 = "MapToSee";
     126      fileName2 += s;
     127      fileName2 += ".txt";
     128      File.WriteAllLines(fileName, MapToSee());
     129    }
     130    public string[] MapToString() { // Function that preapre Map to printing in .txt File: create a set of strings for future reading by computer
     131      string[] s;
     132      s = new string[K];
     133      for (int i = 0; i < K; i++) {
     134        s[i] = "";
     135        for (int j = 0; j < Map[i].Count; j++) {
     136          s[i] += Map[i][j].ToString();
     137          s[i] += " ";
     138        }
     139      }
     140      return s;
     141    }
     142    public string[] MapToSee() { // Function that prepare Map to printing in .txt File: create a set of strings in human readable view
     143      var fmt = new InfixExpressionFormatter();
     144      string[] s;
     145      s = new string[(ModelSet.Count) + 1];
     146      s[0] = "ClusterNumber" + "," + "ModelNumber" + "," + "Model";
     147      for (int i = 1; i < ((ModelSet.Count) + 1); i++) {
     148        s[i] = ClusterNumber[i - 1].ToString() + "," + (i - 1).ToString() + ",";
     149        if (ModelSet[i - 1] is ISymbolicExpressionTree model) {
     150          s[i] += fmt.Format(model);
     151        } else { s[i] += ModelSet[i - 1].ToString(); }
     152      }
     153      return s;
     154    }
     155    public void MapFromFileRead(string fileName) {
     156      string input = File.ReadAllText(fileName);
     157      int i = 0;
     158      foreach (var row in input.Split('\n')) {
     159        Map.Add(new List<int>());
     160        foreach (var col in row.Trim().Split(' ')) {
     161          Map[i].Add(int.Parse(col.Trim()));
     162        }
     163        i++;
     164      }
     165    }
     166    protected void MapSizeCheck() {
     167      if (Map != null) Map.Clear();
     168      else Map = new List<List<int>>();
     169      if (Map.Count != K) {
     170        if (Map.Count != 0) {
     171          Map.Clear();
     172        }
     173        for (int i = 0; i < K; i++) {
     174          Map.Add(new List<int>());
     175        }
     176      }
     177    }
     178    protected void MapPreparation() {
     179      for (int i = 0; i < Map.Count; i++) {
     180        for (int j = 0; j < Map[i].Count; j++) {
     181          ClusterNumber.Add(0);
     182        }
     183      }
     184      for (int i = 0; i < Map.Count; i++) {
     185        for (int j = 0; j < Map[i].Count; j++) {
     186          ClusterNumber[Map[i][j]] = i;
     187        }
     188      }
     189    }
    43190  }
    44191}
Note: See TracChangeset for help on using the changeset viewer.