def CalculatePi(self, predict_y_all, flexibility): """PI value""" njobs = self.n_jobs if self.up: front_y = self.scrap(self.front_point) else: front_y = self.front_point front_y -= flexibility def tile_func(i): tile = 0 for front_y_i in front_y.T: big = i - front_y_i big_bool = np.max(big, axis=1) < 0 tile |= big_bool return tile tile_all = batch_parallelize(n_jobs=njobs, func=tile_func, iterable=predict_y_all, batch_size=100) pi = np.sum(1 - np.array(tile_all), axis=1) / predict_y_all.shape[1] self.Pi = pi return pi
def _transform(self, structures: List[Structure], **kwargs): """ Args: structures:(list) Preprocessing of samples need to transform to Graph. **kwargs: Returns: list of graphs: List of dict """ assert isinstance(structures, Iterable) if hasattr(structures, "__len__"): assert len(structures) > 0, "Empty input data!" le = len(structures) for i in kwargs.keys(): if kwargs[i] is None: kwargs[i] = [kwargs[i]] * len(structures) elif not isinstance(kwargs[i], Iterable): kwargs[i] = [kwargs[i]] * len(structures) kw = [{k: v[i] for k, v in kwargs.items()} for i in range(le)] iterables = zip(structures, kw) if not self.batch_calculate: rets = parallelize(self.n_jobs, self._wrapper, iterables, tq=True, respective=True, respective_kwargs=True) ret, self.support_ = zip(*rets) else: rets = batch_parallelize(self.n_jobs, self._wrapper, iterables, respective=True, respective_kwargs=True, tq=True, mode="j", batch_size=self.batch_size) ret, self.support_ = zip(*rets) if self.add_label: [ i.update({"label": torch.tensor([n, n])}) for n, i in enumerate(ret) ] # double for after. return ret
def _transform(self, structures: List[Structure], state_attributes: List = None): """ Parameters ---------- structures:list preprocessing of samples need to transform to Graph. state_attributes:List preprocessing of samples need to add to Graph. Returns ------- list of graphs: List of dict """ if state_attributes is None: state_attributes = [None] * len(structures) assert isinstance(structures, Iterable) if hasattr(structures, "__len__"): assert len(structures) > 0, "Empty input data!" iterables = zip(structures, state_attributes) if not self.batch_calculate: rets = parallelize(self.n_jobs, self._wrapper, iterables, tq=True, respective=True) ret, self.support_ = zip(*rets) else: rets = batch_parallelize(self.n_jobs, self._wrapper, iterables, respective=True, tq=True, batch_size=self.batch_size) ret, self.support_ = zip(*rets) return ret
def eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None, n_jobs=2, halloffame=None, verbose=__debug__): """This algorithm reproduce the simplest evolutionary algorithm. :param population: A list of individuals. :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution operators. :param cxpb: The probability of mating two individuals. :param mutpb: The probability of mutating an individual. :param ngen: The number of generation. :param stats: A :class:`~deap.tools.Statistics` object that is updated inplace, optional. :param halloffame: A :class:`~deap.tools.HallOfFame` object that will contain the best individuals, optional. :param verbose: Whether or not to log the statistics. :returns: The final population :returns: A class:`~deap.tools.Logbook` with the statistics of the evolution """ logbook = tools.Logbook() logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in population if not ind.fitness.valid] # n_job=n invalid_ind2 = [tuple(i) for i in invalid_ind] fitnesses = batch_parallelize(n_jobs, toolbox.evaluate, invalid_ind2, batch_size=30, tq=True) # n_job=1 # fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit if halloffame is not None: halloffame.update(population) best = deque([halloffame.items[0].fitness.values[0]], maxlen=15) # Begin the generational process for gen in range(1, ngen + 1): # Append the current generation statistics to the logbook record = stats.compile(population + halloffame.items) if stats else {} logbook.record(gen=gen, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) # Select the next generation individuals offspring = toolbox.select(population, len(population) - halloffame.maxsize) offspring.extend(halloffame.items) # Vary the pool of individuals offspring = varAnd(offspring, toolbox, cxpb, mutpb) offspring = toolbox.map(toolbox.filt, offspring) offspring = list(offspring) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] # n_job=n invalid_ind2 = [tuple(i) for i in invalid_ind] fitnesses = batch_parallelize(n_jobs, toolbox.evaluate, invalid_ind2, batch_size=30, tq=True) # fitnesses = parallelize(n_jobs, toolbox.evaluate, invalid_ind2) # n_job=1 # fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(offspring) best.append(halloffame.items[0].fitness.values[0]) if sum(best) / best.maxlen == best[-1]: break # Replace the current population by the offspring population[:] = offspring return population, logbook
from mgetool.tool import parallelize, tt, parallelize_imap, batch_parallelize import numpy as np def func(n, _=None): # time.sleep(0.0001) s = np.random.random((10, 50)) return s if __name__ == "__main__": iterable = np.arange(50) s0 = batch_parallelize(4, func, iterable, respective=False, tq=True, mode="m") #无tq s1 = parallelize(4, func, iterable, respective=False, tq=True, mode="j") s2 = parallelize_imap(4, func, iterable, tq=True) s0 = parallelize(4, func, iterable, respective=False, tq=False, mode="m") #无tq s1 = parallelize(4, func, iterable, respective=False, tq=False, mode="j") s2 = parallelize_imap(4, func, iterable, tq=False) def func(n, _=None): # time.sleep(0.0001) s = np.random.random((100, 50)) return s iterable = np.arange(10000)