Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/Maps/EMMIslandMap.cs @ 18183

Last change on this file since 18183 was 17134, checked in by msemenki, 5 years ago

#2988:

  1. The file system was changed, folders was added and part of files was transferred in these folders.
  2. HelpFunctions class was divided on 2 parts: HelpFuctions for common purposes static functions and SelfConfiguration that include functions for self-configuration mechanism realization (is used in EMMSucsessMap).
  3. Parts of self-configuration mechanism was transferred from EMMSucsessMap.cs to SelfConfiguration.cs. Now EMMSucsessMap used SelfConfiguration like one of data member. Other parts of project was adopted for this changing.
  4. FileComunication class was added. It include the majority of functions for printing to files or reading from files. Here were realized possibility to write and read to hl files.
  5. ModelTreeNode.cs has additional possibility - to write sub-model in string (then it is possible to write it in file).
  6. InfixExpressionFormatter.cs can work with TreeModelNode.
  7. Possibility for different map types to be readable from files was extended and cheeked.
  8. Such parameters like - ClusterNumbers, ClusterNumbersShow, NegbourNumber, NegbourType (that is used only in several maps) was transferred from EMMAlgorithm to Map Parameters. Now EMMBaseMap class inherited from ParameterizedNamedItem (not from Item). And EMMIslandMap and EMMNetworkMap contains their parameters (constructors was modified). CreationMap calls functions were simplified.
  9. Functions for different distance metric calculation was added. Now, it is possible to calculate different types of distances between models (with different random values of constants).
  10. DistanceParametr was added. Now maps can be created according different types of distance calculations.
  11. The class EMMClustering has new name KMeansClusterizationAlgorithm. On KMeansClusterizationAlgorithm bug with bloating of centroids list was fixed. Algorithm was adopted for working with different type of distance metric and get maximum number of iterations.
  12. Possibilities for constants optimization in sub-models an whole tree was added. EMMAlgorithm get new function for evaluation of individuals (and some additional technical stuff for that). Function for trees with model in usual tree transformation and back was added.
  13. EMMAlgorithm was divided on 2 parts:
  • EMMAlgorithm, that contain evolutionary algorithm working with sub-models, and use ready to use maps;
  • ModelSetPreparation, that contain distance calculation, model set simplification and map creation.
File size: 6.1 KB
RevLine 
[16722]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HEAL.Attic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
[17134]25using HeuristicLab.Data;
[16722]26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[17134]27using HeuristicLab.Parameters;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
[17002]29using HeuristicLab.Random;
[16722]30using System.Collections.Generic;
[17002]31using System.Linq;
[16722]32
33namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
[16899]34  [Item("IslandMap", "A map of models of models of models")]
[16734]35  [StorableType("E4AB04B9-FD5D-47EE-949D-243660754F3A")]
[16899]36  public class EMMIslandMap : EMMMapBase<ISymbolicExpressionTree> {
[17134]37
[17002]38    [Storable]
39    public List<int> ClusterNumber { get; set; }  // May be only Island Map really need it
[17134]40    public double[] AverageDistance { get; private set; }
41    private const string ClusterNumbersParameterName = "ClusterNumbers";
42    private const string ClusterNumbersShowParameterName = "ClusterNumbersShow";
43    public IValueParameter<IntValue> ClusterNumbersParameter {
44      get { return (IValueParameter<IntValue>)Parameters[ClusterNumbersParameterName]; }
45    }
46    public IValueParameter<IntValue> ClusterNumbersShowParameter {
47      get { return (IValueParameter<IntValue>)Parameters[ClusterNumbersShowParameterName]; }
48    }
49    public IntValue ClusterNumbers {
50      get { return ClusterNumbersParameter.Value; }
51      set { ClusterNumbersParameter.Value = value; }
52    }
53    public IntValue ClusterNumbersShow {
54      get { return ClusterNumbersShowParameter.Value; }
55      set { ClusterNumbersShowParameter.Value = value; }
56    }
57    #region constructors
[16722]58    [StorableConstructor]
[16899]59    protected EMMIslandMap(StorableConstructorFlag _) : base(_) { }
[17002]60    public EMMIslandMap() {
[17134]61      Parameters.Add(new ValueParameter<IntValue>(ClusterNumbersParameterName, "The number of clusters for model Map.", new IntValue(10)));
62      Parameters.Add(new ValueParameter<IntValue>(ClusterNumbersShowParameterName, "The number of clusters for model Map.", new IntValue(10)));
[17002]63      ModelSet = new List<ISymbolicExpressionTree>();
64      ClusterNumber = new List<int>();
65    }
[16722]66    public override IDeepCloneable Clone(Cloner cloner) {
[16899]67      return new EMMIslandMap(this, cloner);
[16722]68    }
[17002]69    public EMMIslandMap(EMMIslandMap original, Cloner cloner) : base(original, cloner) {
70      if (original.ClusterNumber != null) {
71        ClusterNumber = original.ClusterNumber.ToList();
72      }
73    }
[16722]74    #endregion
[17134]75    #region Map Apply Functions
76    override public void CreateMap(IRandom random) {
77      var totalDistance = ModelSetPreparation.CalculateDistances(ModelSet); //structure distances
78      CreateMap(random, totalDistance);
79    }
80    override public void CreateMap(IRandom random, ISymbolicDataAnalysisSingleObjectiveProblem problem) {
81      CreateMap(random, ModelSetPreparation.TotalDistanceMatrixCalculation(random, problem, ModelSet, DistanceParametr));
82    }
83    override public void CreateMap(IRandom random, double[,] totalDistance) {
84      if (Map != null) {
85        Map.Clear();
86      }
87      ClusterNumbersShow.Value = KMeansClusterizationAlgorithm.ApplyClusteringAlgorithm(random, totalDistance, ClusterNumber, ClusterNumbers.Value);
88      MapSizeCheck(ClusterNumbersShow.Value);
[16722]89      for (int i = 0; i < ModelSet.Count; i++) {
90        Map[ClusterNumber[i]].Add(i);
91      }
[17134]92      AverageDistanceInClusterCalculation(totalDistance, ClusterNumbersShow.Value);
[16722]93    }
[17134]94    override public void MapRead(IEnumerable<ISymbolicExpressionTree> trees) {
95      base.MapRead(trees);
96      string fileName = ("Map" + DistanceParametr + ".txt");
97      Map = FileComuncations.IntMatrixFromFileRead(fileName);
98      ClusterNumbers.Value = Map.Count;
99      ClusterNumbersShow.Value = ClusterNumbers.Value;
100      ClusterNumbersCalculate();
101      AverageDistanceInClusterCalculation(ModelSetPreparation.CalculateDistances(ModelSet), Map.Count);
102    }
[17002]103    override public ISymbolicExpressionTree NewModelForInizializtionNotTree(IRandom random, out int treeNumber) {
104      return NewModelForInizializtion(random, out treeNumber);
[16734]105    }
[17134]106    private void AverageDistanceInClusterCalculation(double[,] distances, int k) {
107      AverageDistance = new double[k];
108      var temp = new List<double>();
109      for (int i = 0; i < k; i++) {
110        KMeansClusterizationAlgorithm.AverageClusterDistanceCalculation(temp, distances, ClusterNumber, ClusterNumber.Count, i);
111        var number = HelpFunctions.ChooseMinElementIndex(temp);
112        AverageDistance[i] = temp[number] / Map[i].Count;
113        temp.Clear();
114      }
115    }
[17002]116    public override ISymbolicExpressionTree NewModelForMutation(IRandom random, out int treeNumber, int parentTreeNumber) {
117      if (parentTreeNumber == -10) {
118        treeNumber = random.Next(ModelSet.Count);
119      } else {
120        treeNumber = Map[ClusterNumber[parentTreeNumber]].SampleRandom(random);
121      }
122      return (ISymbolicExpressionTree)ModelSet[treeNumber].Clone();
123    }
[17134]124    public void ClusterNumbersCalculate() {
[17002]125      for (int i = 0; i < Map.Count; i++) {
126        for (int j = 0; j < Map[i].Count; j++) {
127          ClusterNumber.Add(0);
128        }
129      }
130      for (int i = 0; i < Map.Count; i++) {
131        for (int j = 0; j < Map[i].Count; j++) {
132          ClusterNumber[Map[i][j]] = i;
133        }
134      }
135    }
[16722]136    #endregion
137
138  }
139}
Note: See TracBrowser for help on using the repository browser.