/* Copyright (c) 1993 by The Johns Hopkins University */ /* PEBLS: Parallel Exemplar-Based Learning System For more information, contact: Steven Salzberg (salzberg@cs.jhu.edu) Dept. of Computer Science Johns Hopkins University Baltimore, MD 21210 Code written by: John N. Rachlin Dept. of Computer Science Johns Hopkins University Baltimore, MD 21210 */ /* PEBLS.C: Core routines for invoking various system modules */ /* Provides routines for training and testing. */ #include #include "config.h" #include "pebls.h" config_type CONFIG; instance_type data[INSTANCES_MAX]; int count[CLASSES_MAX+1][FEATURES_MAX][VALUES_MAX]; float dtable[FEATURES_MAX][VALUES_MAX][VALUES_MAX]; output_type output[CLASSES_MAX+1][TRIALS_MAX]; /* ------------------------------------------------------------ */ /* NEAREST_THRESHOLD_VOTE */ int nearest_threshold_vote(int k, int nearest[]) { int class_count[CLASSES_MAX]; int i, p, classes = CONFIG.classes; int class_nearest; for (i=0; i= CONFIG.threshold[i]) class_nearest = p; } return (class_nearest); } /* ------------------------------------------------------------ */ /* NEAREST_WEIGHTED_DISTANCE_VOTE: Give each neighbor a vote */ /* inversely proportional to its distance. */ int nearest_weighted_distance_vote(int k, int nearest[], float distances[]) { float class_distance[CLASSES_MAX]; int i, classes = CONFIG.classes; int vote_class; float vote_size; for (i=0; i vote_size) && (class_distance[i] != 0.0)) { vote_size = class_distance[i]; vote_class = i; } } return (vote_class); } /* ------------------------------------------------------------ */ /* NEAREST_MAJORITY_VOTE: Find the majority class in the set */ /* set of nearest neighbors. */ /* INPUTS: k = # neighbors */ /* nearest = set of nearest neighbors */ /* OUTPUT: The majority class of the k nearest neighbors. */ int nearest_majority_vote(int k, int nearest[]) { int class_count[CLASSES_MAX]; int i, classes = CONFIG.classes; int majority_class, majority_size; for (i=0; i majority_size) || ((class_count[i] == majority_size) && (f_random(1.0) > 0.500))) { majority_size = class_count[i]; majority_class = i; } } return (majority_class); } /* ------------------------------------------------------------ */ /* NEAREST_VOTE: Vote on the nearest neighbor. */ int nearest_vote(int k, int nearest_list[], float distances[]) { int nearest_class; switch (CONFIG.nearest_voting) { case MAJORITY: nearest_class = nearest_majority_vote(k, nearest_list); break; case WEIGHTED_DISTANCE: nearest_class = nearest_weighted_distance_vote(k, nearest_list, distances); break; case THRESHOLD: nearest_class = nearest_threshold_vote(k, nearest_list); break; } return (nearest_class); } /* ------------------------------------------------------------ */ int f_minimum(float A[], int n) { int i; float min = INFINITY; int min_idx = -1; for (i=0; i x); do i++; while (A[i] < x); if (i < j) { temp = A[i]; A[i] = A[j]; A[j] = temp; } else return (j); } } /* ------------------------------------------------------------ */ int randomized_partition(float A[], int p, int r) { int i; float temp; i = p + i_random(r - p + 1); temp = A[p]; A[p] = A[i]; A[i] = temp; return(partition(A,p,r)); } /* ------------------------------------------------------------ */ float randomized_select(float A[], int p, int r, int i) { int k, q; if (p==r) return A[p]; q = randomized_partition(A,p,r); k = q - p + 1; if (i <= k) return (randomized_select(A,p,q,i)); else return (randomized_select(A,q+1,r,i-k)); } /* ------------------------------------------------------------ */ void array_copy(float A[], float B[], int size) { int i; for (i=0; i