Exemple #1
0
void KNN_prune_sort
(
    PatternList p,      // source
    Categories c,   // source
    long k,         // k(!)
    long * indices, // indices of instances to be sorted
    long nindices   // the number of instances to be sorted
)
{
	long n = nindices;
	autoNUMvector <long> h (0L, nindices - 1);
	for (long cc = 0; cc < nindices; ++ cc)
		h [cc] = KNN_friendsAmongkNeighbours (p, p, c, indices [cc], k);
	while (-- n) {   // insertion-sort, is heap-sort worth the effort?
		for (long m = n; m < nindices - 1; m ++) {
			if (h [m - 1] > h[m]) 
				break;
			if (h [m - 1] < h[m]) {
				OlaSWAP (long, indices [m - 1], indices [m]);
			} else {
				if (KNN_nearestEnemy (p, p, c, indices [m - 1]) < KNN_nearestEnemy (p, p, c, indices [m])) {
					OlaSWAP (long, indices [m - 1], indices [m]);
				} else {
					if (NUMrandomUniform (0, 1) > 0.5) {
						OlaSWAP (long, indices [m - 1], indices [m]);
					}
				}
			}
		}
	}
}
Exemple #2
0
void KNN_prune_sort
(
    ///////////////////////////////
    // Parameters                //
    ///////////////////////////////

    Pattern p,      // source
    //
    Categories c,   // source
    //
    long k,         // k(!)
    //
    long * indices, // indices of instances to be sorted
    //
    long nindices   // the number of instances to be sorted
    //
)

{
    long n = nindices;
    long *h = NUMlvector (0, nindices - 1);

    for (long cc = 0; cc < nindices; ++cc)
        h[cc] = KNN_friendsAmongkNeighbours(p, p, c, indices[cc], k);

    while (--n) // insertion-sort, is heap-sort worth the effort?
    {
        for (long m = n; m < nindices - 1; m++)
        {
            if (h[m - 1] > h[m])
                break;

            if (h[m - 1] < h[m])
            {
                OlaSWAP(long, indices[m - 1], indices[m]);
            }
            else
            {
                if (KNN_nearestEnemy(p, p, c, indices[m - 1]) < KNN_nearestEnemy(p, p, c, indices[m]))
                {
                    OlaSWAP(long, indices[m - 1], indices[m]);
                }
                else
                {
                    if (NUMrandomUniform(0, 1) > 0.5)
                        OlaSWAP(long, indices[m - 1], indices[m]);
                }
            }
        }
    }
    NUMlvector_free (h, 0);
}