编辑:哇,很多很棒的回复.是的,我使用它作为一个适应度函数来判断遗传算法执行的排序质量.因此,评估成本很重要(即,必须快速,最好O(n).)
作为我正在使用的AI应用程序的一部分,我希望能够根据其单调性(也就是其"排序性")对整数的候选数组进行评级.目前,我正在使用一种计算最长排序运行的启发式算法,然后将其除以数组的长度:
public double monotonicity(int[] array) {
if (array.length == 0) return 1d;
int longestRun = longestSortedRun(array);
return (double) longestRun / (double) array.length;
}
public int longestSortedRun(int[] array) {
if (array.length == 0) return 0;
int longestRun = 1;
int currentRun = 1;
for (int i = 1; i < array.length; i++) {
if (array[i] >= array[i - 1]) {
currentRun++;
} else {
currentRun = 1;
}
if (currentRun > longestRun) longestRun = currentRun;
}
return …Run Code Online (Sandbox Code Playgroud) math artificial-intelligence information-theory genetic-algorithm