def __mimic_setup__(fittest, n_objects): funcs = [] # functions for pmf fittest_count = len(fittest) # number of fittest individuals # cdef np.ndarray[int, ndim=2] medoid = np.zeros((fittest_count, n_objects), dtype=np.int32) medoid = np.zeros((n_objects, fittest_count), dtype=np.int32) counters = [{True: 0, False: fittest_count} for x in xrange(n_objects)] remaining = range(n_objects) sets = (x.medoids for x in fittest) # all medoid sets from individuals for j, _set in enumerate(sets): for i in xrange(n_objects): if i in _set: medoid[i, j] = 1 counters[i][True] += 1 counters[i][False] -= 1 # ---------------------------------------------------------------------------------- # -------------------- draws first object - the independent one -------------------- # ---------------------------------------------------------------------------------- entropies = map( lambda x: entropy(medoid[x], counters[x]), remaining ) last_drawn = remaining[np.argmin(entropies)] independent = copy.deepcopy(counters[last_drawn]) independent = dict(map(lambda (k, x): (k, x / float(fittest_count)), independent.items())) for child_value in [True, False]: if child_value not in independent: independent[child_value] = 1. - independent[not child_value] funcs.append( (last_drawn, declare_function(last_drawn, independent=independent, attributes=[last_drawn])) ) remaining.remove(last_drawn) return fittest_count, last_drawn, medoid, counters, remaining, funcs
def __umda__(fittest, n_objects): """ Implementation of the UMDA inference method. :type fittest: list :param fittest: list of fittest individuals in the population. :type n_individuals: int :param n_individuals: number of individuals in the population. :type n_objects: int :param n_objects: number of objects in the dataset. :rtype: dict :return: Returns a dictionary where the keys are the indexes of individuals in the dataset, and each value is a function representing the probability of that object being a medoid. """ fittest_count = len(fittest) all_medoids = reduce( operator.add, map( lambda (x, f): x.medoids, fittest ) ) counts = Counter(all_medoids) funcs = [] for _object in xrange(n_objects): counted = {x: 0 for x in [True, False]} if _object in counts.keys(): counted[True] = counts.get(_object) counted[False] = fittest_count - counts.get(_object) else: counted[True] = 0. counted[False] = fittest_count funcs.append( (_object, declare_function(_object, fittest_count=fittest_count, counted=counted, attributes=[_object])) ) return dict(funcs), range(n_objects), {x: [] for x in xrange(n_objects)}
def __mimic_link__(fittest_count, last_drawn, drawn, counters, remaining, funcs): """ Links last_drawn object with drawn object. :param fittest_count: Number of fittest individuals (i.e, above median) in the population. :param last_drawn: The father of drawn object. :param drawn: The object to be inserted in the probabilistic model. :param counters: A list of dictionaries, where each dictionary counts the number of times that the object was a medoid in the (fittest) population. :param remaining: The objects not yet added in the probabilistic model. :param funcs: A list of functions, each one defining the probability of a given object be a medoid in the next generation given the fittest individuals frequencies. :rtype: tuple :return: Returns the updated last_drawn, remaining and funcs parameters. """ v = counters[drawn] f = counters[last_drawn] dependent = {x: 0 for x in itertools.product([True, False], [True, False])} for (child_value, father_value) in itertools.product([True, False], [True, False]): if f[father_value] == 0: dependent[(child_value, father_value)] = 0.5 else: dependent[(child_value, father_value)] = float(v[child_value]) / float(f[father_value]) * (f[father_value] / float(fittest_count)) # declares function funcs.append( (drawn, declare_function(drawn, dependent=dependent, attributes=[drawn, last_drawn])) ) remaining.remove(drawn) last_drawn = drawn return last_drawn, remaining, funcs