Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/HelpFunctions.cs @ 17002

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

#2988:
Class HelpFuction get new static functions that are used in different Map’s classes and possible in other classes.
Branch was adapted to Hive.
New version of class structure for Maps:

  1. 3 new variants of maps (RankMap, SuccessMap and ZeroMap) are added.
  2. BaseMap class was simplified, some class members were deleted and other were transported to child class, because some of them are not used in all kinds of maps.
  3. Functions between base class and child class were divided in other way.
  4. Mutation operators were adapted to work with new class structure. Now mutation make less work for ModelNodes than previously.
  5. ModelNode and Model symbols were simplified. They should not take into account a map type.
  6. Models frequency analyzers were adapted for new variants of maps.
  7. EMMAlgorithm class was adapted to new maps
File size: 4.3 KB
Line 
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
21using HeuristicLab.Core;
22using System.Collections.Generic;
23using System.Linq;
24
25namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
26  class HelpFunctions {
27    public static int OneElementFromListProportionalSelection(IRandom random, List<double> list) {
28      double selectedQuality = random.NextDouble() * list.Sum();
29      int index = 0;
30      double currentQuality = list[index];
31      while ((currentQuality < selectedQuality) && (index < list.Count)) {
32        index++;
33        currentQuality += list[index];
34      }
35      return index;
36    }
37    public static int ChooseMinElementIndex(List<double> distances, int currentElement, List<int> previousNumbers) {
38      double minValue = 100;
39      int minElementNumber = 0;
40      int temp = 0, i = 0;
41      while (temp == 0) {
42        if ((currentElement != i) && (!CheckNumberIsInList(i, previousNumbers))) {
43          minValue = distances[i];
44          minElementNumber = i;
45          temp = i;
46        }
47        i++;
48      }
49      for (i = 0; i < distances.Count(); i++) {
50        if ((distances[i] < minValue) && (currentElement != i) && (!CheckNumberIsInList(i, previousNumbers))) {
51          minValue = distances[i];
52          minElementNumber = i;
53        }
54      }
55      return minElementNumber;
56    }
57
58    public static bool CheckNumberIsInList(int number, List<int> priviousNumber) {
59      foreach (var pNum in priviousNumber) {
60        if (number == pNum)
61          return true;
62      }
63      return false;
64    }
65    public static int ChooseMinElementIndex(List<double> averageClusterDistance) {
66      double minValue = averageClusterDistance[0];
67      int minElementNumber = 0;
68      for (int i = 1; i < averageClusterDistance.Count(); i++) {
69        if (averageClusterDistance[i] < minValue) {
70          minValue = averageClusterDistance[i];
71          minElementNumber = i;
72        }
73      }
74      return minElementNumber;
75    }
76    public static int ChooseMaxElementIndex(List<double> averageClusterDistance) {
77      double maxValue = averageClusterDistance[0];
78      int maxElementNumber = 0;
79      for (int i = 1; i < averageClusterDistance.Count(); i++) {
80        if (averageClusterDistance[i] > maxValue) {
81          maxValue = averageClusterDistance[i];
82          maxElementNumber = i;
83        }
84      }
85      return maxElementNumber;
86    }
87    public static double CheckSocialKatre(double socialKarteValue, double value, double stepValue) {
88      if (value > (socialKarteValue + stepValue))
89        return stepValue;
90      else if (value > socialKarteValue)
91        return (value - socialKarteValue);
92      else return 0;
93    }
94    public static void ProbabilitiesUpDate(List<List<double>> sucsessStatistics, List<double> probabilities) {
95
96      var averageQuality = new List<double>();
97      foreach (var variant in sucsessStatistics) {
98        if (variant[0] > 0.005) {
99          averageQuality.Add(variant[1] / variant[0]);
100        } else { averageQuality.Add(0); }
101      }
102      int bestModelNumber = ChooseMaxElementIndex(averageQuality);
103      double socialKarte = 1.0 / (probabilities.Count * 20.0); // parameters of method
104      double stepValue = socialKarte / 5.0;
105      double totalChangeValue = 0, changeValue = 0;
106      for (int i = 0; i < probabilities.Count; i++) {
107        changeValue = CheckSocialKatre(socialKarte, probabilities[i], stepValue);
108        totalChangeValue += changeValue;
109        probabilities[i] -= changeValue;
110      }
111      probabilities[bestModelNumber] += totalChangeValue;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.