#region License Information
/* HeuristicLab
* Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Core;
using System.Collections.Generic;
using System.Linq;
namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
class HelpFunctions {
public static int OneElementFromListProportionalSelection(IRandom random, List list) {
double selectedQuality = random.NextDouble() * list.Sum();
int index = 0;
double currentQuality = list[index];
while ((currentQuality < selectedQuality) && (index < list.Count)) {
index++;
currentQuality += list[index];
}
return index;
}
public static int ChooseMinElementIndex(List distances, int currentElement, List previousNumbers) {
double minValue = 100;
int minElementNumber = 0;
int temp = 0, i = 0;
while (temp == 0) {
if ((currentElement != i) && (!CheckNumberIsInList(i, previousNumbers))) {
minValue = distances[i];
minElementNumber = i;
temp = i;
}
i++;
}
for (i = 0; i < distances.Count(); i++) {
if ((distances[i] < minValue) && (currentElement != i) && (!CheckNumberIsInList(i, previousNumbers))) {
minValue = distances[i];
minElementNumber = i;
}
}
return minElementNumber;
}
public static bool CheckNumberIsInList(int number, List priviousNumber) {
foreach (var pNum in priviousNumber) {
if (number == pNum)
return true;
}
return false;
}
public static int ChooseMinElementIndex(List averageClusterDistance) {
double minValue = averageClusterDistance[0];
int minElementNumber = 0;
for (int i = 1; i < averageClusterDistance.Count(); i++) {
if (averageClusterDistance[i] < minValue) {
minValue = averageClusterDistance[i];
minElementNumber = i;
}
}
return minElementNumber;
}
public static int ChooseMaxElementIndex(List averageClusterDistance) {
double maxValue = averageClusterDistance[0];
int maxElementNumber = 0;
for (int i = 1; i < averageClusterDistance.Count(); i++) {
if (averageClusterDistance[i] > maxValue) {
maxValue = averageClusterDistance[i];
maxElementNumber = i;
}
}
return maxElementNumber;
}
public static double CheckSocialKatre(double socialKarteValue, double value, double stepValue) {
if (value > (socialKarteValue + stepValue))
return stepValue;
else if (value > socialKarteValue)
return (value - socialKarteValue);
else return 0;
}
public static void ProbabilitiesUpDate(List> sucsessStatistics, List probabilities) {
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 = ChooseMaxElementIndex(averageQuality);
double socialKarte = 1.0 / (probabilities.Count * 20.0); // parameters of method
double stepValue = socialKarte / 5.0;
double totalChangeValue = 0, changeValue = 0;
for (int i = 0; i < probabilities.Count; i++) {
changeValue = CheckSocialKatre(socialKarte, probabilities[i], stepValue);
totalChangeValue += changeValue;
probabilities[i] -= changeValue;
}
probabilities[bestModelNumber] += totalChangeValue;
}
}
}