[17134] | 1 | using HEAL.Attic;
|
---|
| 2 | using HeuristicLab.Common;
|
---|
| 3 | using HeuristicLab.Core;
|
---|
| 4 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 5 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 6 | using System.Collections.Generic;
|
---|
| 7 | using System.Linq;
|
---|
| 8 |
|
---|
| 9 | namespace 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 | }
|
---|