using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Problems.DataAnalysis.Symbolic; using System.Collections.Generic; using System.Linq; namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels { [Item("SelfConfiguration", "A fluent realization of self-configuration mechanism")] [StorableType("7888A6B9-CDC8-42A0-BDA6-F9D89A56067A")] public class SelfConfiguration : Item { #region data members [Storable] public List Probabilities { get; private set; } [Storable] protected List> SucsessStatistics { get; private set; } [Storable] public double SocialKarte { get; private set; } // parameters of method [Storable] public double StepValue { get; private set; } [Storable] protected List ChoiseForPopulation { get; private set; } #endregion #region constructors public SelfConfiguration() : base() { SucsessStatistics = new List>(); Probabilities = new List(); ChoiseForPopulation = new List(); } public SelfConfiguration(SelfConfiguration original, Cloner cloner) : base(original, cloner) { SucsessStatistics = original.SucsessStatistics.Select(x => x.ToList()).ToList(); Probabilities = original.Probabilities.ToList(); SocialKarte = original.SocialKarte; StepValue = original.StepValue; } [StorableConstructor] protected SelfConfiguration(StorableConstructorFlag _) : base(_) { } public override IDeepCloneable Clone(Cloner cloner) { return new SelfConfiguration(this, cloner); } #endregion #region dialog functions public void Initialization(int size) { for (int t = 0; t < size; t++) { Probabilities.Add(1.0 / ((double)(size))); // uniform distribution as start point } SocialKarte = 1.0 / (size * 20.0); // parameters of method StepValue = SocialKarte / 5.0; } public void ReadFromFile(int size, string fileName) { SocialKarte = 1.0 / (size * 20.0); // parameters of method StepValue = SocialKarte / 5.0; var temp = FileComuncations.DoubleMatrixFromFileRead(fileName); Probabilities = temp[0]; } public int Aplay(IRandom random) { return HelpFunctions.OneElementFromListProportionalSelection(random, Probabilities); } public int Aplay(int individNumber) { return ChoiseForPopulation[individNumber]; } public void DecisionForPopulation(int size, IRandom random) { if (ChoiseForPopulation != null) { ChoiseForPopulation.Clear(); } for (int i = 0; i < size; i++) { ChoiseForPopulation.Add(HelpFunctions.OneElementFromListProportionalSelection(random, Probabilities)); } } #region probabilities updating public void UpDate(Dictionary population) { SucsessStatisticCollection(population); ProbabilitiesUpDate(); } public void UpDate(double[] population) { SucsessStatisticCollection(population); ProbabilitiesUpDate(); } private void SucsessStatisticCollection(double[] qualities) { if (SucsessStatistics.Count != 0) SucsessStatistics.Clear(); for (int t = 0; t < Probabilities.Count; t++) { SucsessStatistics.Add(new List()); SucsessStatistics[t].Add(t); SucsessStatistics[t].Add(0); } for (int i = 0; i < ChoiseForPopulation.Count; i++) { SucsessStatistics[ChoiseForPopulation[i]][1] = qualities[i]; } } private void SucsessStatisticCollection(Dictionary population) { if (SucsessStatistics.Count != 0) SucsessStatistics.Clear(); for (int t = 0; t < Probabilities.Count; t++) { SucsessStatistics.Add(new List()); SucsessStatistics[t].Add(0); SucsessStatistics[t].Add(0); } foreach (var solution in population) { TreeCheck(solution.Key, solution.Value); } } private void TreeCheck(ISymbolicExpressionTree tree, double treeQuality) { foreach (var treeNode in tree.IterateNodesPrefix().OfType()) { SucsessStatistics[treeNode.TreeNumber][0] += 1; SucsessStatistics[treeNode.TreeNumber][1] += treeQuality; } } private void ProbabilitiesUpDate() { var averageQuality = new List(); foreach (var variant in SucsessStatistics) { if (variant[0] > 0.005) { averageQuality.Add(variant[1] / variant[0]); } else { averageQuality.Add(0); } } int bestModelNumber = HelpFunctions.ChooseMaxElementIndex(averageQuality); double totalChangeValue = 0, changeValue = 0; for (int i = 0; i < Probabilities.Count; i++) { changeValue = CheckSocialKatre(Probabilities[i]); totalChangeValue += changeValue; Probabilities[i] -= changeValue; } Probabilities[bestModelNumber] += totalChangeValue; } private double CheckSocialKatre(double value) { if (value > (SocialKarte + StepValue)) return StepValue; else if (value > SocialKarte) return (value - SocialKarte); else return 0; } #endregion #endregion } }