Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/SelfConfiguration.cs @ 17625

Last change on this file since 17625 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: 5.3 KB
Line 
1using HEAL.Attic;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
5using HeuristicLab.Problems.DataAnalysis.Symbolic;
6using System.Collections.Generic;
7using System.Linq;
8
9namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
10  [Item("SelfConfiguration", "A fluent realization of self-configuration mechanism")]
11  [StorableType("7888A6B9-CDC8-42A0-BDA6-F9D89A56067A")]
12  public class SelfConfiguration : Item {
13    #region data members
14    [Storable]
15    public List<double> Probabilities { get; private set; }
16    [Storable]
17    protected List<List<double>> SucsessStatistics { get; private set; }
18    [Storable]
19    public double SocialKarte { get; private set; } // parameters of method
20    [Storable]
21    public double StepValue { get; private set; }
22    [Storable]
23    protected List<int> ChoiseForPopulation { get; private set; }
24    #endregion
25    #region constructors
26    public SelfConfiguration() : base() {
27      SucsessStatistics = new List<List<double>>();
28      Probabilities = new List<double>();
29      ChoiseForPopulation = new List<int>();
30    }
31    public SelfConfiguration(SelfConfiguration original, Cloner cloner) : base(original, cloner) {
32      SucsessStatistics = original.SucsessStatistics.Select(x => x.ToList()).ToList();
33      Probabilities = original.Probabilities.ToList();
34      SocialKarte = original.SocialKarte;
35      StepValue = original.StepValue;
36    }
37    [StorableConstructor]
38    protected SelfConfiguration(StorableConstructorFlag _) : base(_) {
39    }
40
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new SelfConfiguration(this, cloner);
43    }
44    #endregion
45    #region dialog functions
46    public void Initialization(int size) {
47      for (int t = 0; t < size; t++) {
48        Probabilities.Add(1.0 / ((double)(size))); // uniform distribution as start point
49      }
50      SocialKarte = 1.0 / (size * 20.0); // parameters of method
51      StepValue = SocialKarte / 5.0;
52    }
53    public void ReadFromFile(int size, string fileName) {
54      SocialKarte = 1.0 / (size * 20.0); // parameters of method
55      StepValue = SocialKarte / 5.0;
56      var temp = FileComuncations.DoubleMatrixFromFileRead(fileName);
57      Probabilities = temp[0];
58    }
59    public int Aplay(IRandom random) {
60      return HelpFunctions.OneElementFromListProportionalSelection(random, Probabilities);
61    }
62    public int Aplay(int individNumber) {
63      return ChoiseForPopulation[individNumber];
64    }
65    public void DecisionForPopulation(int size, IRandom random) {
66      if (ChoiseForPopulation != null) {
67        ChoiseForPopulation.Clear();
68      }
69      for (int i = 0; i < size; i++) {
70        ChoiseForPopulation.Add(HelpFunctions.OneElementFromListProportionalSelection(random, Probabilities));
71      }
72    }
73    #region probabilities updating
74    public void UpDate(Dictionary<ISymbolicExpressionTree, double> population) {
75      SucsessStatisticCollection(population);
76      ProbabilitiesUpDate();
77    }
78    public void UpDate(double[] population) {
79      SucsessStatisticCollection(population);
80      ProbabilitiesUpDate();
81    }
82    private void SucsessStatisticCollection(double[] qualities) {
83      if (SucsessStatistics.Count != 0)
84        SucsessStatistics.Clear();
85      for (int t = 0; t < Probabilities.Count; t++) {
86        SucsessStatistics.Add(new List<double>());
87        SucsessStatistics[t].Add(t);
88        SucsessStatistics[t].Add(0);
89      }
90      for (int i = 0; i < ChoiseForPopulation.Count; i++) {
91        SucsessStatistics[ChoiseForPopulation[i]][1] = qualities[i];
92      }
93    }
94    private void SucsessStatisticCollection(Dictionary<ISymbolicExpressionTree, double> population) {
95      if (SucsessStatistics.Count != 0)
96        SucsessStatistics.Clear();
97      for (int t = 0; t < Probabilities.Count; t++) {
98        SucsessStatistics.Add(new List<double>());
99        SucsessStatistics[t].Add(0);
100        SucsessStatistics[t].Add(0);
101      }
102      foreach (var solution in population) {
103        TreeCheck(solution.Key, solution.Value);
104      }
105    }
106    private void TreeCheck(ISymbolicExpressionTree tree, double treeQuality) {
107      foreach (var treeNode in tree.IterateNodesPrefix().OfType<TreeModelTreeNode>()) {
108        SucsessStatistics[treeNode.TreeNumber][0] += 1;
109        SucsessStatistics[treeNode.TreeNumber][1] += treeQuality;
110      }
111    }
112    private void ProbabilitiesUpDate() {
113      var averageQuality = new List<double>();
114      foreach (var variant in SucsessStatistics) {
115        if (variant[0] > 0.005) {
116          averageQuality.Add(variant[1] / variant[0]);
117        } else { averageQuality.Add(0); }
118      }
119      int bestModelNumber = HelpFunctions.ChooseMaxElementIndex(averageQuality);
120      double totalChangeValue = 0, changeValue = 0;
121      for (int i = 0; i < Probabilities.Count; i++) {
122        changeValue = CheckSocialKatre(Probabilities[i]);
123        totalChangeValue += changeValue;
124        Probabilities[i] -= changeValue;
125      }
126      Probabilities[bestModelNumber] += totalChangeValue;
127    }
128    private double CheckSocialKatre(double value) {
129      if (value > (SocialKarte + StepValue))
130        return StepValue;
131      else if (value > SocialKarte)
132        return (value - SocialKarte);
133      else return 0;
134    }
135    #endregion
136    #endregion
137  }
138}
Note: See TracBrowser for help on using the repository browser.