repository_name
stringlengths
5
67
func_path_in_repository
stringlengths
4
234
func_name
stringlengths
0
314
whole_func_string
stringlengths
52
3.87M
language
stringclasses
6 values
func_code_string
stringlengths
39
1.84M
func_code_tokens
listlengths
15
672k
func_documentation_string
stringlengths
1
47.2k
func_documentation_tokens
listlengths
1
3.92k
split_name
stringclasses
1 value
func_code_url
stringlengths
85
339
aarongarrett/inspyred
inspyred/ec/observers.py
file_observer
def file_observer(population, num_generations, num_evaluations, args): """Print the output of the evolutionary computation to a file. This function saves the results of the evolutionary computation to two files. The first file, which by default is named 'inspyred-statistics-file-<timestamp>.csv', contains the basic generational statistics of the population throughout the run (worst, best, median, and average fitness and standard deviation of the fitness values). The second file, which by default is named 'inspyred-individuals-file-<timestamp>.csv', contains every individual during each generation of the run. Both files may be passed to the function as keyword arguments (see below). The format of each line of the statistics file is as follows:: generation number, population size, worst, best, median, average, standard deviation The format of each line of the individuals file is as follows:: generation number, individual number, fitness, string representation of candidate .. note:: This function makes use of the ``inspyred.ec.analysis.fitness_statistics`` function, so it is subject to the same requirements. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *statistics_file* -- a file object (default: see text) - *individuals_file* -- a file object (default: see text) """ try: statistics_file = args['statistics_file'] except KeyError: statistics_file = open('inspyred-statistics-file-{0}.csv'.format(time.strftime('%m%d%Y-%H%M%S')), 'w') args['statistics_file'] = statistics_file try: individuals_file = args['individuals_file'] except KeyError: individuals_file = open('inspyred-individuals-file-{0}.csv'.format(time.strftime('%m%d%Y-%H%M%S')), 'w') args['individuals_file'] = individuals_file stats = inspyred.ec.analysis.fitness_statistics(population) worst_fit = stats['worst'] best_fit = stats['best'] avg_fit = stats['mean'] med_fit = stats['median'] std_fit = stats['std'] statistics_file.write('{0}, {1}, {2}, {3}, {4}, {5}, {6}\n'.format(num_generations, len(population), worst_fit, best_fit, med_fit, avg_fit, std_fit)) for i, p in enumerate(population): individuals_file.write('{0}, {1}, {2}, {3}\n'.format(num_generations, i, p.fitness, str(p.candidate))) statistics_file.flush() individuals_file.flush()
python
def file_observer(population, num_generations, num_evaluations, args): try: statistics_file = args['statistics_file'] except KeyError: statistics_file = open('inspyred-statistics-file-{0}.csv'.format(time.strftime('%m%d%Y-%H%M%S')), 'w') args['statistics_file'] = statistics_file try: individuals_file = args['individuals_file'] except KeyError: individuals_file = open('inspyred-individuals-file-{0}.csv'.format(time.strftime('%m%d%Y-%H%M%S')), 'w') args['individuals_file'] = individuals_file stats = inspyred.ec.analysis.fitness_statistics(population) worst_fit = stats['worst'] best_fit = stats['best'] avg_fit = stats['mean'] med_fit = stats['median'] std_fit = stats['std'] statistics_file.write('{0}, {1}, {2}, {3}, {4}, {5}, {6}\n'.format(num_generations, len(population), worst_fit, best_fit, med_fit, avg_fit, std_fit)) for i, p in enumerate(population): individuals_file.write('{0}, {1}, {2}, {3}\n'.format(num_generations, i, p.fitness, str(p.candidate))) statistics_file.flush() individuals_file.flush()
[ "def", "file_observer", "(", "population", ",", "num_generations", ",", "num_evaluations", ",", "args", ")", ":", "try", ":", "statistics_file", "=", "args", "[", "'statistics_file'", "]", "except", "KeyError", ":", "statistics_file", "=", "open", "(", "'inspyred-statistics-file-{0}.csv'", ".", "format", "(", "time", ".", "strftime", "(", "'%m%d%Y-%H%M%S'", ")", ")", ",", "'w'", ")", "args", "[", "'statistics_file'", "]", "=", "statistics_file", "try", ":", "individuals_file", "=", "args", "[", "'individuals_file'", "]", "except", "KeyError", ":", "individuals_file", "=", "open", "(", "'inspyred-individuals-file-{0}.csv'", ".", "format", "(", "time", ".", "strftime", "(", "'%m%d%Y-%H%M%S'", ")", ")", ",", "'w'", ")", "args", "[", "'individuals_file'", "]", "=", "individuals_file", "stats", "=", "inspyred", ".", "ec", ".", "analysis", ".", "fitness_statistics", "(", "population", ")", "worst_fit", "=", "stats", "[", "'worst'", "]", "best_fit", "=", "stats", "[", "'best'", "]", "avg_fit", "=", "stats", "[", "'mean'", "]", "med_fit", "=", "stats", "[", "'median'", "]", "std_fit", "=", "stats", "[", "'std'", "]", "statistics_file", ".", "write", "(", "'{0}, {1}, {2}, {3}, {4}, {5}, {6}\\n'", ".", "format", "(", "num_generations", ",", "len", "(", "population", ")", ",", "worst_fit", ",", "best_fit", ",", "med_fit", ",", "avg_fit", ",", "std_fit", ")", ")", "for", "i", ",", "p", "in", "enumerate", "(", "population", ")", ":", "individuals_file", ".", "write", "(", "'{0}, {1}, {2}, {3}\\n'", ".", "format", "(", "num_generations", ",", "i", ",", "p", ".", "fitness", ",", "str", "(", "p", ".", "candidate", ")", ")", ")", "statistics_file", ".", "flush", "(", ")", "individuals_file", ".", "flush", "(", ")" ]
Print the output of the evolutionary computation to a file. This function saves the results of the evolutionary computation to two files. The first file, which by default is named 'inspyred-statistics-file-<timestamp>.csv', contains the basic generational statistics of the population throughout the run (worst, best, median, and average fitness and standard deviation of the fitness values). The second file, which by default is named 'inspyred-individuals-file-<timestamp>.csv', contains every individual during each generation of the run. Both files may be passed to the function as keyword arguments (see below). The format of each line of the statistics file is as follows:: generation number, population size, worst, best, median, average, standard deviation The format of each line of the individuals file is as follows:: generation number, individual number, fitness, string representation of candidate .. note:: This function makes use of the ``inspyred.ec.analysis.fitness_statistics`` function, so it is subject to the same requirements. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *statistics_file* -- a file object (default: see text) - *individuals_file* -- a file object (default: see text)
[ "Print", "the", "output", "of", "the", "evolutionary", "computation", "to", "a", "file", ".", "This", "function", "saves", "the", "results", "of", "the", "evolutionary", "computation", "to", "two", "files", ".", "The", "first", "file", "which", "by", "default", "is", "named", "inspyred", "-", "statistics", "-", "file", "-", "<timestamp", ">", ".", "csv", "contains", "the", "basic", "generational", "statistics", "of", "the", "population", "throughout", "the", "run", "(", "worst", "best", "median", "and", "average", "fitness", "and", "standard", "deviation", "of", "the", "fitness", "values", ")", ".", "The", "second", "file", "which", "by", "default", "is", "named", "inspyred", "-", "individuals", "-", "file", "-", "<timestamp", ">", ".", "csv", "contains", "every", "individual", "during", "each", "generation", "of", "the", "run", ".", "Both", "files", "may", "be", "passed", "to", "the", "function", "as", "keyword", "arguments", "(", "see", "below", ")", ".", "The", "format", "of", "each", "line", "of", "the", "statistics", "file", "is", "as", "follows", "::", "generation", "number", "population", "size", "worst", "best", "median", "average", "standard", "deviation" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/observers.py#L135-L195
aarongarrett/inspyred
inspyred/ec/observers.py
archive_observer
def archive_observer(population, num_generations, num_evaluations, args): """Print the current archive to the screen. This function displays the current archive of the evolutionary computation to the screen. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments """ archive = args['_ec'].archive print('----------------------------------------------------------------------------') print(' Archive ({0:5} individuals)'.format(len(archive))) print('----------------------------------------------------------------------------') for a in archive: print(a) print('----------------------------------------------------------------------------')
python
def archive_observer(population, num_generations, num_evaluations, args): archive = args['_ec'].archive print('----------------------------------------------------------------------------') print(' Archive ({0:5} individuals)'.format(len(archive))) print('----------------------------------------------------------------------------') for a in archive: print(a) print('----------------------------------------------------------------------------')
[ "def", "archive_observer", "(", "population", ",", "num_generations", ",", "num_evaluations", ",", "args", ")", ":", "archive", "=", "args", "[", "'_ec'", "]", ".", "archive", "print", "(", "'----------------------------------------------------------------------------'", ")", "print", "(", "' Archive ({0:5} individuals)'", ".", "format", "(", "len", "(", "archive", ")", ")", ")", "print", "(", "'----------------------------------------------------------------------------'", ")", "for", "a", "in", "archive", ":", "print", "(", "a", ")", "print", "(", "'----------------------------------------------------------------------------'", ")" ]
Print the current archive to the screen. This function displays the current archive of the evolutionary computation to the screen. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments
[ "Print", "the", "current", "archive", "to", "the", "screen", ".", "This", "function", "displays", "the", "current", "archive", "of", "the", "evolutionary", "computation", "to", "the", "screen", ".", "..", "Arguments", ":", "population", "--", "the", "population", "of", "Individuals", "num_generations", "--", "the", "number", "of", "elapsed", "generations", "num_evaluations", "--", "the", "number", "of", "candidate", "solution", "evaluations", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/observers.py#L198-L217
aarongarrett/inspyred
inspyred/ec/observers.py
plot_observer
def plot_observer(population, num_generations, num_evaluations, args): """Plot the output of the evolutionary computation as a graph. This function plots the performance of the EC as a line graph using matplotlib and numpy. The graph consists of a blue line representing the best fitness, a green line representing the average fitness, and a red line representing the median fitness. It modifies the keyword arguments variable 'args' by including an entry called 'plot_data'. If this observer is used, the calling script should also import the matplotlib library and should end the script with:: matplotlib.pyplot.show() Otherwise, the program may generate a runtime error. .. note:: This function makes use of the matplotlib and numpy libraries. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments """ import matplotlib.pyplot as plt import numpy stats = inspyred.ec.analysis.fitness_statistics(population) best_fitness = stats['best'] worst_fitness = stats['worst'] median_fitness = stats['median'] average_fitness = stats['mean'] colors = ['black', 'blue', 'green', 'red'] labels = ['average', 'median', 'best', 'worst'] data = [] if num_generations == 0: plt.ion() data = [[num_evaluations], [average_fitness], [median_fitness], [best_fitness], [worst_fitness]] lines = [] for i in range(4): line, = plt.plot(data[0], data[i+1], color=colors[i], label=labels[i]) lines.append(line) # Add the legend when the first data is added. plt.legend(loc='lower right') args['plot_data'] = data args['plot_lines'] = lines plt.xlabel('Evaluations') plt.ylabel('Fitness') else: data = args['plot_data'] data[0].append(num_evaluations) data[1].append(average_fitness) data[2].append(median_fitness) data[3].append(best_fitness) data[4].append(worst_fitness) lines = args['plot_lines'] for i, line in enumerate(lines): line.set_xdata(numpy.array(data[0])) line.set_ydata(numpy.array(data[i+1])) args['plot_data'] = data args['plot_lines'] = lines ymin = min([min(d) for d in data[1:]]) ymax = max([max(d) for d in data[1:]]) yrange = ymax - ymin plt.xlim((0, num_evaluations)) plt.ylim((ymin - 0.1*yrange, ymax + 0.1*yrange)) plt.draw()
python
def plot_observer(population, num_generations, num_evaluations, args): import matplotlib.pyplot as plt import numpy stats = inspyred.ec.analysis.fitness_statistics(population) best_fitness = stats['best'] worst_fitness = stats['worst'] median_fitness = stats['median'] average_fitness = stats['mean'] colors = ['black', 'blue', 'green', 'red'] labels = ['average', 'median', 'best', 'worst'] data = [] if num_generations == 0: plt.ion() data = [[num_evaluations], [average_fitness], [median_fitness], [best_fitness], [worst_fitness]] lines = [] for i in range(4): line, = plt.plot(data[0], data[i+1], color=colors[i], label=labels[i]) lines.append(line) plt.legend(loc='lower right') args['plot_data'] = data args['plot_lines'] = lines plt.xlabel('Evaluations') plt.ylabel('Fitness') else: data = args['plot_data'] data[0].append(num_evaluations) data[1].append(average_fitness) data[2].append(median_fitness) data[3].append(best_fitness) data[4].append(worst_fitness) lines = args['plot_lines'] for i, line in enumerate(lines): line.set_xdata(numpy.array(data[0])) line.set_ydata(numpy.array(data[i+1])) args['plot_data'] = data args['plot_lines'] = lines ymin = min([min(d) for d in data[1:]]) ymax = max([max(d) for d in data[1:]]) yrange = ymax - ymin plt.xlim((0, num_evaluations)) plt.ylim((ymin - 0.1*yrange, ymax + 0.1*yrange)) plt.draw()
[ "def", "plot_observer", "(", "population", ",", "num_generations", ",", "num_evaluations", ",", "args", ")", ":", "import", "matplotlib", ".", "pyplot", "as", "plt", "import", "numpy", "stats", "=", "inspyred", ".", "ec", ".", "analysis", ".", "fitness_statistics", "(", "population", ")", "best_fitness", "=", "stats", "[", "'best'", "]", "worst_fitness", "=", "stats", "[", "'worst'", "]", "median_fitness", "=", "stats", "[", "'median'", "]", "average_fitness", "=", "stats", "[", "'mean'", "]", "colors", "=", "[", "'black'", ",", "'blue'", ",", "'green'", ",", "'red'", "]", "labels", "=", "[", "'average'", ",", "'median'", ",", "'best'", ",", "'worst'", "]", "data", "=", "[", "]", "if", "num_generations", "==", "0", ":", "plt", ".", "ion", "(", ")", "data", "=", "[", "[", "num_evaluations", "]", ",", "[", "average_fitness", "]", ",", "[", "median_fitness", "]", ",", "[", "best_fitness", "]", ",", "[", "worst_fitness", "]", "]", "lines", "=", "[", "]", "for", "i", "in", "range", "(", "4", ")", ":", "line", ",", "=", "plt", ".", "plot", "(", "data", "[", "0", "]", ",", "data", "[", "i", "+", "1", "]", ",", "color", "=", "colors", "[", "i", "]", ",", "label", "=", "labels", "[", "i", "]", ")", "lines", ".", "append", "(", "line", ")", "# Add the legend when the first data is added.", "plt", ".", "legend", "(", "loc", "=", "'lower right'", ")", "args", "[", "'plot_data'", "]", "=", "data", "args", "[", "'plot_lines'", "]", "=", "lines", "plt", ".", "xlabel", "(", "'Evaluations'", ")", "plt", ".", "ylabel", "(", "'Fitness'", ")", "else", ":", "data", "=", "args", "[", "'plot_data'", "]", "data", "[", "0", "]", ".", "append", "(", "num_evaluations", ")", "data", "[", "1", "]", ".", "append", "(", "average_fitness", ")", "data", "[", "2", "]", ".", "append", "(", "median_fitness", ")", "data", "[", "3", "]", ".", "append", "(", "best_fitness", ")", "data", "[", "4", "]", ".", "append", "(", "worst_fitness", ")", "lines", "=", "args", "[", "'plot_lines'", "]", "for", "i", ",", "line", "in", "enumerate", "(", "lines", ")", ":", "line", ".", "set_xdata", "(", "numpy", ".", "array", "(", "data", "[", "0", "]", ")", ")", "line", ".", "set_ydata", "(", "numpy", ".", "array", "(", "data", "[", "i", "+", "1", "]", ")", ")", "args", "[", "'plot_data'", "]", "=", "data", "args", "[", "'plot_lines'", "]", "=", "lines", "ymin", "=", "min", "(", "[", "min", "(", "d", ")", "for", "d", "in", "data", "[", "1", ":", "]", "]", ")", "ymax", "=", "max", "(", "[", "max", "(", "d", ")", "for", "d", "in", "data", "[", "1", ":", "]", "]", ")", "yrange", "=", "ymax", "-", "ymin", "plt", ".", "xlim", "(", "(", "0", ",", "num_evaluations", ")", ")", "plt", ".", "ylim", "(", "(", "ymin", "-", "0.1", "*", "yrange", ",", "ymax", "+", "0.1", "*", "yrange", ")", ")", "plt", ".", "draw", "(", ")" ]
Plot the output of the evolutionary computation as a graph. This function plots the performance of the EC as a line graph using matplotlib and numpy. The graph consists of a blue line representing the best fitness, a green line representing the average fitness, and a red line representing the median fitness. It modifies the keyword arguments variable 'args' by including an entry called 'plot_data'. If this observer is used, the calling script should also import the matplotlib library and should end the script with:: matplotlib.pyplot.show() Otherwise, the program may generate a runtime error. .. note:: This function makes use of the matplotlib and numpy libraries. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments
[ "Plot", "the", "output", "of", "the", "evolutionary", "computation", "as", "a", "graph", ".", "This", "function", "plots", "the", "performance", "of", "the", "EC", "as", "a", "line", "graph", "using", "matplotlib", "and", "numpy", ".", "The", "graph", "consists", "of", "a", "blue", "line", "representing", "the", "best", "fitness", "a", "green", "line", "representing", "the", "average", "fitness", "and", "a", "red", "line", "representing", "the", "median", "fitness", ".", "It", "modifies", "the", "keyword", "arguments", "variable", "args", "by", "including", "an", "entry", "called", "plot_data", ".", "If", "this", "observer", "is", "used", "the", "calling", "script", "should", "also", "import", "the", "matplotlib", "library", "and", "should", "end", "the", "script", "with", "::", "matplotlib", ".", "pyplot", ".", "show", "()", "Otherwise", "the", "program", "may", "generate", "a", "runtime", "error", ".", "..", "note", "::", "This", "function", "makes", "use", "of", "the", "matplotlib", "and", "numpy", "libraries", ".", "..", "Arguments", ":", "population", "--", "the", "population", "of", "Individuals", "num_generations", "--", "the", "number", "of", "elapsed", "generations", "num_evaluations", "--", "the", "number", "of", "candidate", "solution", "evaluations", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/observers.py#L337-L407
aarongarrett/inspyred
inspyred/ec/terminators.py
diversity_termination
def diversity_termination(population, num_generations, num_evaluations, args): """Return True if population diversity is less than a minimum diversity. This function calculates the Euclidean distance between every pair of individuals in the population. It then compares the maximum of those distances with a specified minimum required diversity. This terminator is really only well-defined for candidate solutions which are list types of numeric values. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *min_diversity* -- the minimum population diversity allowed (default 0.001) """ min_diversity = args.setdefault('min_diversity', 0.001) cart_prod = itertools.product(population, population) distance = [] for (p, q) in cart_prod: d = 0 for x, y in zip(p.candidate, q.candidate): d += (x - y)**2 distance.append(math.sqrt(d)) return max(distance) < min_diversity
python
def diversity_termination(population, num_generations, num_evaluations, args): min_diversity = args.setdefault('min_diversity', 0.001) cart_prod = itertools.product(population, population) distance = [] for (p, q) in cart_prod: d = 0 for x, y in zip(p.candidate, q.candidate): d += (x - y)**2 distance.append(math.sqrt(d)) return max(distance) < min_diversity
[ "def", "diversity_termination", "(", "population", ",", "num_generations", ",", "num_evaluations", ",", "args", ")", ":", "min_diversity", "=", "args", ".", "setdefault", "(", "'min_diversity'", ",", "0.001", ")", "cart_prod", "=", "itertools", ".", "product", "(", "population", ",", "population", ")", "distance", "=", "[", "]", "for", "(", "p", ",", "q", ")", "in", "cart_prod", ":", "d", "=", "0", "for", "x", ",", "y", "in", "zip", "(", "p", ".", "candidate", ",", "q", ".", "candidate", ")", ":", "d", "+=", "(", "x", "-", "y", ")", "**", "2", "distance", ".", "append", "(", "math", ".", "sqrt", "(", "d", ")", ")", "return", "max", "(", "distance", ")", "<", "min_diversity" ]
Return True if population diversity is less than a minimum diversity. This function calculates the Euclidean distance between every pair of individuals in the population. It then compares the maximum of those distances with a specified minimum required diversity. This terminator is really only well-defined for candidate solutions which are list types of numeric values. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *min_diversity* -- the minimum population diversity allowed (default 0.001)
[ "Return", "True", "if", "population", "diversity", "is", "less", "than", "a", "minimum", "diversity", ".", "This", "function", "calculates", "the", "Euclidean", "distance", "between", "every", "pair", "of", "individuals", "in", "the", "population", ".", "It", "then", "compares", "the", "maximum", "of", "those", "distances", "with", "a", "specified", "minimum", "required", "diversity", ".", "This", "terminator", "is", "really", "only", "well", "-", "defined", "for", "candidate", "solutions", "which", "are", "list", "types", "of", "numeric", "values", ".", "..", "Arguments", ":", "population", "--", "the", "population", "of", "Individuals", "num_generations", "--", "the", "number", "of", "elapsed", "generations", "num_evaluations", "--", "the", "number", "of", "candidate", "solution", "evaluations", "args", "--", "a", "dictionary", "of", "keyword", "arguments", "Optional", "keyword", "arguments", "in", "args", ":", "-", "*", "min_diversity", "*", "--", "the", "minimum", "population", "diversity", "allowed", "(", "default", "0", ".", "001", ")" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/terminators.py#L69-L97
aarongarrett/inspyred
inspyred/ec/terminators.py
average_fitness_termination
def average_fitness_termination(population, num_generations, num_evaluations, args): """Return True if the population's average fitness is near its best fitness. This function calculates the average fitness of the population, as well as the best fitness. If the difference between those values is less than a specified tolerance, the function returns True. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *tolerance* -- the minimum allowable difference between average and best fitness (default 0.001) """ tolerance = args.setdefault('tolerance', 0.001) avg_fit = sum([x.fitness for x in population]) / float(len(population)) best_fit = max([x.fitness for x in population]) return (best_fit - avg_fit) < tolerance
python
def average_fitness_termination(population, num_generations, num_evaluations, args): tolerance = args.setdefault('tolerance', 0.001) avg_fit = sum([x.fitness for x in population]) / float(len(population)) best_fit = max([x.fitness for x in population]) return (best_fit - avg_fit) < tolerance
[ "def", "average_fitness_termination", "(", "population", ",", "num_generations", ",", "num_evaluations", ",", "args", ")", ":", "tolerance", "=", "args", ".", "setdefault", "(", "'tolerance'", ",", "0.001", ")", "avg_fit", "=", "sum", "(", "[", "x", ".", "fitness", "for", "x", "in", "population", "]", ")", "/", "float", "(", "len", "(", "population", ")", ")", "best_fit", "=", "max", "(", "[", "x", ".", "fitness", "for", "x", "in", "population", "]", ")", "return", "(", "best_fit", "-", "avg_fit", ")", "<", "tolerance" ]
Return True if the population's average fitness is near its best fitness. This function calculates the average fitness of the population, as well as the best fitness. If the difference between those values is less than a specified tolerance, the function returns True. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *tolerance* -- the minimum allowable difference between average and best fitness (default 0.001)
[ "Return", "True", "if", "the", "population", "s", "average", "fitness", "is", "near", "its", "best", "fitness", ".", "This", "function", "calculates", "the", "average", "fitness", "of", "the", "population", "as", "well", "as", "the", "best", "fitness", ".", "If", "the", "difference", "between", "those", "values", "is", "less", "than", "a", "specified", "tolerance", "the", "function", "returns", "True", ".", "..", "Arguments", ":", "population", "--", "the", "population", "of", "Individuals", "num_generations", "--", "the", "number", "of", "elapsed", "generations", "num_evaluations", "--", "the", "number", "of", "candidate", "solution", "evaluations", "args", "--", "a", "dictionary", "of", "keyword", "arguments", "Optional", "keyword", "arguments", "in", "args", ":", "-", "*", "tolerance", "*", "--", "the", "minimum", "allowable", "difference", "between", "average", "and", "best", "fitness", "(", "default", "0", ".", "001", ")" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/terminators.py#L100-L122
aarongarrett/inspyred
inspyred/ec/terminators.py
evaluation_termination
def evaluation_termination(population, num_generations, num_evaluations, args): """Return True if the number of function evaluations meets or exceeds a maximum. This function compares the number of function evaluations that have been generated with a specified maximum. It returns True if the maximum is met or exceeded. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *max_evaluations* -- the maximum candidate solution evaluations (default len(population)) """ max_evaluations = args.setdefault('max_evaluations', len(population)) return num_evaluations >= max_evaluations
python
def evaluation_termination(population, num_generations, num_evaluations, args): max_evaluations = args.setdefault('max_evaluations', len(population)) return num_evaluations >= max_evaluations
[ "def", "evaluation_termination", "(", "population", ",", "num_generations", ",", "num_evaluations", ",", "args", ")", ":", "max_evaluations", "=", "args", ".", "setdefault", "(", "'max_evaluations'", ",", "len", "(", "population", ")", ")", "return", "num_evaluations", ">=", "max_evaluations" ]
Return True if the number of function evaluations meets or exceeds a maximum. This function compares the number of function evaluations that have been generated with a specified maximum. It returns True if the maximum is met or exceeded. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *max_evaluations* -- the maximum candidate solution evaluations (default len(population))
[ "Return", "True", "if", "the", "number", "of", "function", "evaluations", "meets", "or", "exceeds", "a", "maximum", ".", "This", "function", "compares", "the", "number", "of", "function", "evaluations", "that", "have", "been", "generated", "with", "a", "specified", "maximum", ".", "It", "returns", "True", "if", "the", "maximum", "is", "met", "or", "exceeded", ".", "..", "Arguments", ":", "population", "--", "the", "population", "of", "Individuals", "num_generations", "--", "the", "number", "of", "elapsed", "generations", "num_evaluations", "--", "the", "number", "of", "candidate", "solution", "evaluations", "args", "--", "a", "dictionary", "of", "keyword", "arguments", "Optional", "keyword", "arguments", "in", "args", ":", "-", "*", "max_evaluations", "*", "--", "the", "maximum", "candidate", "solution", "evaluations", "(", "default", "len", "(", "population", "))" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/terminators.py#L125-L145
aarongarrett/inspyred
inspyred/ec/terminators.py
generation_termination
def generation_termination(population, num_generations, num_evaluations, args): """Return True if the number of generations meets or exceeds a maximum. This function compares the number of generations with a specified maximum. It returns True if the maximum is met or exceeded. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *max_generations* -- the maximum generations (default 1) """ max_generations = args.setdefault('max_generations', 1) return num_generations >= max_generations
python
def generation_termination(population, num_generations, num_evaluations, args): max_generations = args.setdefault('max_generations', 1) return num_generations >= max_generations
[ "def", "generation_termination", "(", "population", ",", "num_generations", ",", "num_evaluations", ",", "args", ")", ":", "max_generations", "=", "args", ".", "setdefault", "(", "'max_generations'", ",", "1", ")", "return", "num_generations", ">=", "max_generations" ]
Return True if the number of generations meets or exceeds a maximum. This function compares the number of generations with a specified maximum. It returns True if the maximum is met or exceeded. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *max_generations* -- the maximum generations (default 1)
[ "Return", "True", "if", "the", "number", "of", "generations", "meets", "or", "exceeds", "a", "maximum", ".", "This", "function", "compares", "the", "number", "of", "generations", "with", "a", "specified", "maximum", ".", "It", "returns", "True", "if", "the", "maximum", "is", "met", "or", "exceeded", ".", "..", "Arguments", ":", "population", "--", "the", "population", "of", "Individuals", "num_generations", "--", "the", "number", "of", "elapsed", "generations", "num_evaluations", "--", "the", "number", "of", "candidate", "solution", "evaluations", "args", "--", "a", "dictionary", "of", "keyword", "arguments", "Optional", "keyword", "arguments", "in", "args", ":", "-", "*", "max_generations", "*", "--", "the", "maximum", "generations", "(", "default", "1", ")" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/terminators.py#L148-L166
aarongarrett/inspyred
inspyred/ec/terminators.py
time_termination
def time_termination(population, num_generations, num_evaluations, args): """Return True if the elapsed time meets or exceeds a duration of time. This function compares the elapsed time with a specified maximum. It returns True if the maximum is met or exceeded. If the `start_time` keyword argument is omitted, it defaults to `None` and will be set to the current system time (in seconds). If the `max_time` keyword argument is omitted, it will default to `None` and will immediately terminate. The `max_time` argument can be specified in seconds as a floating-point number, as minutes/seconds as a two-element tuple of floating-point numbers, or as hours/minutes/seconds as a three-element tuple of floating-point numbers. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *start_time* -- the time from which to start measuring (default None) - *max_time* -- the maximum time that should elapse (default None) """ start_time = args.setdefault('start_time', None) max_time = args.setdefault('max_time', None) logging = args.get('_ec').logger if start_time is None: start_time = time.time() args['start_time'] = start_time logging.debug('time_termination terminator added without setting the start_time argument; setting start_time to current time') if max_time is None: logging.debug('time_termination terminator added without setting the max_time argument; terminator will immediately terminate') else: try: max_time = max_time[0] * 3600.0 + max_time[1] * 60.00 + max_time[2] args['max_time'] = max_time except TypeError: pass except IndexError: max_time = max_time[0] * 60 + max_time[1] args['max_time'] = max_time time_elapsed = time.time() - start_time return max_time is None or time_elapsed >= max_time
python
def time_termination(population, num_generations, num_evaluations, args): start_time = args.setdefault('start_time', None) max_time = args.setdefault('max_time', None) logging = args.get('_ec').logger if start_time is None: start_time = time.time() args['start_time'] = start_time logging.debug('time_termination terminator added without setting the start_time argument; setting start_time to current time') if max_time is None: logging.debug('time_termination terminator added without setting the max_time argument; terminator will immediately terminate') else: try: max_time = max_time[0] * 3600.0 + max_time[1] * 60.00 + max_time[2] args['max_time'] = max_time except TypeError: pass except IndexError: max_time = max_time[0] * 60 + max_time[1] args['max_time'] = max_time time_elapsed = time.time() - start_time return max_time is None or time_elapsed >= max_time
[ "def", "time_termination", "(", "population", ",", "num_generations", ",", "num_evaluations", ",", "args", ")", ":", "start_time", "=", "args", ".", "setdefault", "(", "'start_time'", ",", "None", ")", "max_time", "=", "args", ".", "setdefault", "(", "'max_time'", ",", "None", ")", "logging", "=", "args", ".", "get", "(", "'_ec'", ")", ".", "logger", "if", "start_time", "is", "None", ":", "start_time", "=", "time", ".", "time", "(", ")", "args", "[", "'start_time'", "]", "=", "start_time", "logging", ".", "debug", "(", "'time_termination terminator added without setting the start_time argument; setting start_time to current time'", ")", "if", "max_time", "is", "None", ":", "logging", ".", "debug", "(", "'time_termination terminator added without setting the max_time argument; terminator will immediately terminate'", ")", "else", ":", "try", ":", "max_time", "=", "max_time", "[", "0", "]", "*", "3600.0", "+", "max_time", "[", "1", "]", "*", "60.00", "+", "max_time", "[", "2", "]", "args", "[", "'max_time'", "]", "=", "max_time", "except", "TypeError", ":", "pass", "except", "IndexError", ":", "max_time", "=", "max_time", "[", "0", "]", "*", "60", "+", "max_time", "[", "1", "]", "args", "[", "'max_time'", "]", "=", "max_time", "time_elapsed", "=", "time", ".", "time", "(", ")", "-", "start_time", "return", "max_time", "is", "None", "or", "time_elapsed", ">=", "max_time" ]
Return True if the elapsed time meets or exceeds a duration of time. This function compares the elapsed time with a specified maximum. It returns True if the maximum is met or exceeded. If the `start_time` keyword argument is omitted, it defaults to `None` and will be set to the current system time (in seconds). If the `max_time` keyword argument is omitted, it will default to `None` and will immediately terminate. The `max_time` argument can be specified in seconds as a floating-point number, as minutes/seconds as a two-element tuple of floating-point numbers, or as hours/minutes/seconds as a three-element tuple of floating-point numbers. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *start_time* -- the time from which to start measuring (default None) - *max_time* -- the maximum time that should elapse (default None)
[ "Return", "True", "if", "the", "elapsed", "time", "meets", "or", "exceeds", "a", "duration", "of", "time", ".", "This", "function", "compares", "the", "elapsed", "time", "with", "a", "specified", "maximum", ".", "It", "returns", "True", "if", "the", "maximum", "is", "met", "or", "exceeded", ".", "If", "the", "start_time", "keyword", "argument", "is", "omitted", "it", "defaults", "to", "None", "and", "will", "be", "set", "to", "the", "current", "system", "time", "(", "in", "seconds", ")", ".", "If", "the", "max_time", "keyword", "argument", "is", "omitted", "it", "will", "default", "to", "None", "and", "will", "immediately", "terminate", ".", "The", "max_time", "argument", "can", "be", "specified", "in", "seconds", "as", "a", "floating", "-", "point", "number", "as", "minutes", "/", "seconds", "as", "a", "two", "-", "element", "tuple", "of", "floating", "-", "point", "numbers", "or", "as", "hours", "/", "minutes", "/", "seconds", "as", "a", "three", "-", "element", "tuple", "of", "floating", "-", "point", "numbers", ".", "..", "Arguments", ":", "population", "--", "the", "population", "of", "Individuals", "num_generations", "--", "the", "number", "of", "elapsed", "generations", "num_evaluations", "--", "the", "number", "of", "candidate", "solution", "evaluations", "args", "--", "a", "dictionary", "of", "keyword", "arguments", "Optional", "keyword", "arguments", "in", "args", ":", "-", "*", "start_time", "*", "--", "the", "time", "from", "which", "to", "start", "measuring", "(", "default", "None", ")", "-", "*", "max_time", "*", "--", "the", "maximum", "time", "that", "should", "elapse", "(", "default", "None", ")" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/terminators.py#L169-L214
aarongarrett/inspyred
inspyred/ec/terminators.py
user_termination
def user_termination(population, num_generations, num_evaluations, args): """Return True if user presses the ESC key when prompted. This function prompts the user to press the ESC key to terminate the evolution. The prompt persists for a specified number of seconds before evolution continues. Additionally, the function can be customized to allow any press of the ESC key to be stored until the next time this function is called. .. note:: This function makes use of the ``msvcrt`` (Windows) and ``curses`` (Unix) libraries. Other systems may not be supported. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *termination_response_timeout* -- the number of seconds to wait for the user to press the ESC key (default 5) - *clear_termination_buffer* -- whether the keyboard buffer should be cleared before allowing the user to press a key (default True) """ def getch(): unix = ('darwin', 'linux2') if sys.platform not in unix: try: import msvcrt except ImportError: return -1 if msvcrt.kbhit(): return msvcrt.getch() else: return -1 elif sys.platform in unix: def _getch(stdscr): stdscr.nodelay(1) ch = stdscr.getch() stdscr.nodelay(0) return ch import curses return curses.wrapper(_getch) num_secs = args.get('termination_response_timeout', 5) clear_buffer = args.get('clear_termination_buffer', True) if clear_buffer: while getch() > -1: pass sys.stdout.write('Press ESC to terminate (%d secs):' % num_secs) count = 1 start = time.time() while time.time() - start < num_secs: ch = getch() if ch > -1 and ord(ch) == 27: sys.stdout.write('\n\n') return True elif time.time() - start == count: sys.stdout.write('.') count += 1 sys.stdout.write('\n') return False
python
def user_termination(population, num_generations, num_evaluations, args): def getch(): unix = ('darwin', 'linux2') if sys.platform not in unix: try: import msvcrt except ImportError: return -1 if msvcrt.kbhit(): return msvcrt.getch() else: return -1 elif sys.platform in unix: def _getch(stdscr): stdscr.nodelay(1) ch = stdscr.getch() stdscr.nodelay(0) return ch import curses return curses.wrapper(_getch) num_secs = args.get('termination_response_timeout', 5) clear_buffer = args.get('clear_termination_buffer', True) if clear_buffer: while getch() > -1: pass sys.stdout.write('Press ESC to terminate (%d secs):' % num_secs) count = 1 start = time.time() while time.time() - start < num_secs: ch = getch() if ch > -1 and ord(ch) == 27: sys.stdout.write('\n\n') return True elif time.time() - start == count: sys.stdout.write('.') count += 1 sys.stdout.write('\n') return False
[ "def", "user_termination", "(", "population", ",", "num_generations", ",", "num_evaluations", ",", "args", ")", ":", "def", "getch", "(", ")", ":", "unix", "=", "(", "'darwin'", ",", "'linux2'", ")", "if", "sys", ".", "platform", "not", "in", "unix", ":", "try", ":", "import", "msvcrt", "except", "ImportError", ":", "return", "-", "1", "if", "msvcrt", ".", "kbhit", "(", ")", ":", "return", "msvcrt", ".", "getch", "(", ")", "else", ":", "return", "-", "1", "elif", "sys", ".", "platform", "in", "unix", ":", "def", "_getch", "(", "stdscr", ")", ":", "stdscr", ".", "nodelay", "(", "1", ")", "ch", "=", "stdscr", ".", "getch", "(", ")", "stdscr", ".", "nodelay", "(", "0", ")", "return", "ch", "import", "curses", "return", "curses", ".", "wrapper", "(", "_getch", ")", "num_secs", "=", "args", ".", "get", "(", "'termination_response_timeout'", ",", "5", ")", "clear_buffer", "=", "args", ".", "get", "(", "'clear_termination_buffer'", ",", "True", ")", "if", "clear_buffer", ":", "while", "getch", "(", ")", ">", "-", "1", ":", "pass", "sys", ".", "stdout", ".", "write", "(", "'Press ESC to terminate (%d secs):'", "%", "num_secs", ")", "count", "=", "1", "start", "=", "time", ".", "time", "(", ")", "while", "time", ".", "time", "(", ")", "-", "start", "<", "num_secs", ":", "ch", "=", "getch", "(", ")", "if", "ch", ">", "-", "1", "and", "ord", "(", "ch", ")", "==", "27", ":", "sys", ".", "stdout", ".", "write", "(", "'\\n\\n'", ")", "return", "True", "elif", "time", ".", "time", "(", ")", "-", "start", "==", "count", ":", "sys", ".", "stdout", ".", "write", "(", "'.'", ")", "count", "+=", "1", "sys", ".", "stdout", ".", "write", "(", "'\\n'", ")", "return", "False" ]
Return True if user presses the ESC key when prompted. This function prompts the user to press the ESC key to terminate the evolution. The prompt persists for a specified number of seconds before evolution continues. Additionally, the function can be customized to allow any press of the ESC key to be stored until the next time this function is called. .. note:: This function makes use of the ``msvcrt`` (Windows) and ``curses`` (Unix) libraries. Other systems may not be supported. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *termination_response_timeout* -- the number of seconds to wait for the user to press the ESC key (default 5) - *clear_termination_buffer* -- whether the keyboard buffer should be cleared before allowing the user to press a key (default True)
[ "Return", "True", "if", "user", "presses", "the", "ESC", "key", "when", "prompted", ".", "This", "function", "prompts", "the", "user", "to", "press", "the", "ESC", "key", "to", "terminate", "the", "evolution", ".", "The", "prompt", "persists", "for", "a", "specified", "number", "of", "seconds", "before", "evolution", "continues", ".", "Additionally", "the", "function", "can", "be", "customized", "to", "allow", "any", "press", "of", "the", "ESC", "key", "to", "be", "stored", "until", "the", "next", "time", "this", "function", "is", "called", ".", "..", "note", "::", "This", "function", "makes", "use", "of", "the", "msvcrt", "(", "Windows", ")", "and", "curses", "(", "Unix", ")", "libraries", ".", "Other", "systems", "may", "not", "be", "supported", ".", "..", "Arguments", ":", "population", "--", "the", "population", "of", "Individuals", "num_generations", "--", "the", "number", "of", "elapsed", "generations", "num_evaluations", "--", "the", "number", "of", "candidate", "solution", "evaluations", "args", "--", "a", "dictionary", "of", "keyword", "arguments", "Optional", "keyword", "arguments", "in", "args", ":", "-", "*", "termination_response_timeout", "*", "--", "the", "number", "of", "seconds", "to", "wait", "for", "the", "user", "to", "press", "the", "ESC", "key", "(", "default", "5", ")", "-", "*", "clear_termination_buffer", "*", "--", "whether", "the", "keyboard", "buffer", "should", "be", "cleared", "before", "allowing", "the", "user", "to", "press", "a", "key", "(", "default", "True", ")" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/terminators.py#L217-L282
aarongarrett/inspyred
inspyred/ec/terminators.py
no_improvement_termination
def no_improvement_termination(population, num_generations, num_evaluations, args): """Return True if the best fitness does not change for a number of generations. This function keeps track of the current best fitness and compares it to the best fitness in previous generations. Whenever those values are the same, it begins a generation count. If that count exceeds a specified number, the terminator returns True. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *max_generations* -- the number of generations allowed for no change in fitness (default 10) """ max_generations = args.setdefault('max_generations', 10) previous_best = args.setdefault('previous_best', None) current_best = max(population).fitness if previous_best is None or previous_best != current_best: args['previous_best'] = current_best args['generation_count'] = 0 return False else: if args['generation_count'] >= max_generations: return True else: args['generation_count'] += 1 return False
python
def no_improvement_termination(population, num_generations, num_evaluations, args): max_generations = args.setdefault('max_generations', 10) previous_best = args.setdefault('previous_best', None) current_best = max(population).fitness if previous_best is None or previous_best != current_best: args['previous_best'] = current_best args['generation_count'] = 0 return False else: if args['generation_count'] >= max_generations: return True else: args['generation_count'] += 1 return False
[ "def", "no_improvement_termination", "(", "population", ",", "num_generations", ",", "num_evaluations", ",", "args", ")", ":", "max_generations", "=", "args", ".", "setdefault", "(", "'max_generations'", ",", "10", ")", "previous_best", "=", "args", ".", "setdefault", "(", "'previous_best'", ",", "None", ")", "current_best", "=", "max", "(", "population", ")", ".", "fitness", "if", "previous_best", "is", "None", "or", "previous_best", "!=", "current_best", ":", "args", "[", "'previous_best'", "]", "=", "current_best", "args", "[", "'generation_count'", "]", "=", "0", "return", "False", "else", ":", "if", "args", "[", "'generation_count'", "]", ">=", "max_generations", ":", "return", "True", "else", ":", "args", "[", "'generation_count'", "]", "+=", "1", "return", "False" ]
Return True if the best fitness does not change for a number of generations. This function keeps track of the current best fitness and compares it to the best fitness in previous generations. Whenever those values are the same, it begins a generation count. If that count exceeds a specified number, the terminator returns True. .. Arguments: population -- the population of Individuals num_generations -- the number of elapsed generations num_evaluations -- the number of candidate solution evaluations args -- a dictionary of keyword arguments Optional keyword arguments in args: - *max_generations* -- the number of generations allowed for no change in fitness (default 10)
[ "Return", "True", "if", "the", "best", "fitness", "does", "not", "change", "for", "a", "number", "of", "generations", ".", "This", "function", "keeps", "track", "of", "the", "current", "best", "fitness", "and", "compares", "it", "to", "the", "best", "fitness", "in", "previous", "generations", ".", "Whenever", "those", "values", "are", "the", "same", "it", "begins", "a", "generation", "count", ".", "If", "that", "count", "exceeds", "a", "specified", "number", "the", "terminator", "returns", "True", ".", "..", "Arguments", ":", "population", "--", "the", "population", "of", "Individuals", "num_generations", "--", "the", "number", "of", "elapsed", "generations", "num_evaluations", "--", "the", "number", "of", "candidate", "solution", "evaluations", "args", "--", "a", "dictionary", "of", "keyword", "arguments", "Optional", "keyword", "arguments", "in", "args", ":", "-", "*", "max_generations", "*", "--", "the", "number", "of", "generations", "allowed", "for", "no", "change", "in", "fitness", "(", "default", "10", ")" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/terminators.py#L285-L316
aarongarrett/inspyred
inspyred/benchmarks.py
DTLZ2.global_optimum
def global_optimum(self): """Return a globally optimal solution to this problem. This function returns a globally optimal solution (i.e., a solution that lives on the Pareto front). Since there are many solutions that are Pareto-optimal, this function randomly chooses one to return. """ x = [random.uniform(0, 1) for _ in range(self.objectives - 1)] x.extend([0.5 for _ in range(self.dimensions - self.objectives + 1)]) return x
python
def global_optimum(self): x = [random.uniform(0, 1) for _ in range(self.objectives - 1)] x.extend([0.5 for _ in range(self.dimensions - self.objectives + 1)]) return x
[ "def", "global_optimum", "(", "self", ")", ":", "x", "=", "[", "random", ".", "uniform", "(", "0", ",", "1", ")", "for", "_", "in", "range", "(", "self", ".", "objectives", "-", "1", ")", "]", "x", ".", "extend", "(", "[", "0.5", "for", "_", "in", "range", "(", "self", ".", "dimensions", "-", "self", ".", "objectives", "+", "1", ")", "]", ")", "return", "x" ]
Return a globally optimal solution to this problem. This function returns a globally optimal solution (i.e., a solution that lives on the Pareto front). Since there are many solutions that are Pareto-optimal, this function randomly chooses one to return.
[ "Return", "a", "globally", "optimal", "solution", "to", "this", "problem", ".", "This", "function", "returns", "a", "globally", "optimal", "solution", "(", "i", ".", "e", ".", "a", "solution", "that", "lives", "on", "the", "Pareto", "front", ")", ".", "Since", "there", "are", "many", "solutions", "that", "are", "Pareto", "-", "optimal", "this", "function", "randomly", "chooses", "one", "to", "return", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/benchmarks.py#L552-L563
aarongarrett/inspyred
inspyred/benchmarks.py
TSP.generator
def generator(self, random, args): """Return a candidate solution for an evolutionary computation.""" locations = [i for i in range(len(self.weights))] random.shuffle(locations) return locations
python
def generator(self, random, args): locations = [i for i in range(len(self.weights))] random.shuffle(locations) return locations
[ "def", "generator", "(", "self", ",", "random", ",", "args", ")", ":", "locations", "=", "[", "i", "for", "i", "in", "range", "(", "len", "(", "self", ".", "weights", ")", ")", "]", "random", ".", "shuffle", "(", "locations", ")", "return", "locations" ]
Return a candidate solution for an evolutionary computation.
[ "Return", "a", "candidate", "solution", "for", "an", "evolutionary", "computation", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/benchmarks.py#L980-L984
aarongarrett/inspyred
inspyred/benchmarks.py
TSP.constructor
def constructor(self, random, args): """Return a candidate solution for an ant colony optimization.""" self._use_ants = True candidate = [] while len(candidate) < len(self.weights) - 1: # Find feasible components feasible_components = [] if len(candidate) == 0: feasible_components = self.components elif len(candidate) == len(self.weights) - 1: first = candidate[0] last = candidate[-1] feasible_components = [c for c in self.components if c.element[0] == last.element[1] and c.element[1] == first.element[0]] else: last = candidate[-1] already_visited = [c.element[0] for c in candidate] already_visited.extend([c.element[1] for c in candidate]) already_visited = set(already_visited) feasible_components = [c for c in self.components if c.element[0] == last.element[1] and c.element[1] not in already_visited] if len(feasible_components) == 0: candidate = [] else: # Choose a feasible component if random.random() <= self.bias: next_component = max(feasible_components) else: next_component = selectors.fitness_proportionate_selection(random, feasible_components, {'num_selected': 1})[0] candidate.append(next_component) return candidate
python
def constructor(self, random, args): self._use_ants = True candidate = [] while len(candidate) < len(self.weights) - 1: feasible_components = [] if len(candidate) == 0: feasible_components = self.components elif len(candidate) == len(self.weights) - 1: first = candidate[0] last = candidate[-1] feasible_components = [c for c in self.components if c.element[0] == last.element[1] and c.element[1] == first.element[0]] else: last = candidate[-1] already_visited = [c.element[0] for c in candidate] already_visited.extend([c.element[1] for c in candidate]) already_visited = set(already_visited) feasible_components = [c for c in self.components if c.element[0] == last.element[1] and c.element[1] not in already_visited] if len(feasible_components) == 0: candidate = [] else: if random.random() <= self.bias: next_component = max(feasible_components) else: next_component = selectors.fitness_proportionate_selection(random, feasible_components, {'num_selected': 1})[0] candidate.append(next_component) return candidate
[ "def", "constructor", "(", "self", ",", "random", ",", "args", ")", ":", "self", ".", "_use_ants", "=", "True", "candidate", "=", "[", "]", "while", "len", "(", "candidate", ")", "<", "len", "(", "self", ".", "weights", ")", "-", "1", ":", "# Find feasible components", "feasible_components", "=", "[", "]", "if", "len", "(", "candidate", ")", "==", "0", ":", "feasible_components", "=", "self", ".", "components", "elif", "len", "(", "candidate", ")", "==", "len", "(", "self", ".", "weights", ")", "-", "1", ":", "first", "=", "candidate", "[", "0", "]", "last", "=", "candidate", "[", "-", "1", "]", "feasible_components", "=", "[", "c", "for", "c", "in", "self", ".", "components", "if", "c", ".", "element", "[", "0", "]", "==", "last", ".", "element", "[", "1", "]", "and", "c", ".", "element", "[", "1", "]", "==", "first", ".", "element", "[", "0", "]", "]", "else", ":", "last", "=", "candidate", "[", "-", "1", "]", "already_visited", "=", "[", "c", ".", "element", "[", "0", "]", "for", "c", "in", "candidate", "]", "already_visited", ".", "extend", "(", "[", "c", ".", "element", "[", "1", "]", "for", "c", "in", "candidate", "]", ")", "already_visited", "=", "set", "(", "already_visited", ")", "feasible_components", "=", "[", "c", "for", "c", "in", "self", ".", "components", "if", "c", ".", "element", "[", "0", "]", "==", "last", ".", "element", "[", "1", "]", "and", "c", ".", "element", "[", "1", "]", "not", "in", "already_visited", "]", "if", "len", "(", "feasible_components", ")", "==", "0", ":", "candidate", "=", "[", "]", "else", ":", "# Choose a feasible component", "if", "random", ".", "random", "(", ")", "<=", "self", ".", "bias", ":", "next_component", "=", "max", "(", "feasible_components", ")", "else", ":", "next_component", "=", "selectors", ".", "fitness_proportionate_selection", "(", "random", ",", "feasible_components", ",", "{", "'num_selected'", ":", "1", "}", ")", "[", "0", "]", "candidate", ".", "append", "(", "next_component", ")", "return", "candidate" ]
Return a candidate solution for an ant colony optimization.
[ "Return", "a", "candidate", "solution", "for", "an", "ant", "colony", "optimization", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/benchmarks.py#L986-L1014
aarongarrett/inspyred
inspyred/benchmarks.py
TSP.evaluator
def evaluator(self, candidates, args): """Return the fitness values for the given candidates.""" fitness = [] if self._use_ants: for candidate in candidates: total = 0 for c in candidate: total += self.weights[c.element[0]][c.element[1]] last = (candidate[-1].element[1], candidate[0].element[0]) total += self.weights[last[0]][last[1]] fitness.append(1 / total) else: for candidate in candidates: total = 0 for src, dst in zip(candidate, candidate[1:] + [candidate[0]]): total += self.weights[src][dst] fitness.append(1 / total) return fitness
python
def evaluator(self, candidates, args): fitness = [] if self._use_ants: for candidate in candidates: total = 0 for c in candidate: total += self.weights[c.element[0]][c.element[1]] last = (candidate[-1].element[1], candidate[0].element[0]) total += self.weights[last[0]][last[1]] fitness.append(1 / total) else: for candidate in candidates: total = 0 for src, dst in zip(candidate, candidate[1:] + [candidate[0]]): total += self.weights[src][dst] fitness.append(1 / total) return fitness
[ "def", "evaluator", "(", "self", ",", "candidates", ",", "args", ")", ":", "fitness", "=", "[", "]", "if", "self", ".", "_use_ants", ":", "for", "candidate", "in", "candidates", ":", "total", "=", "0", "for", "c", "in", "candidate", ":", "total", "+=", "self", ".", "weights", "[", "c", ".", "element", "[", "0", "]", "]", "[", "c", ".", "element", "[", "1", "]", "]", "last", "=", "(", "candidate", "[", "-", "1", "]", ".", "element", "[", "1", "]", ",", "candidate", "[", "0", "]", ".", "element", "[", "0", "]", ")", "total", "+=", "self", ".", "weights", "[", "last", "[", "0", "]", "]", "[", "last", "[", "1", "]", "]", "fitness", ".", "append", "(", "1", "/", "total", ")", "else", ":", "for", "candidate", "in", "candidates", ":", "total", "=", "0", "for", "src", ",", "dst", "in", "zip", "(", "candidate", ",", "candidate", "[", "1", ":", "]", "+", "[", "candidate", "[", "0", "]", "]", ")", ":", "total", "+=", "self", ".", "weights", "[", "src", "]", "[", "dst", "]", "fitness", ".", "append", "(", "1", "/", "total", ")", "return", "fitness" ]
Return the fitness values for the given candidates.
[ "Return", "the", "fitness", "values", "for", "the", "given", "candidates", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/benchmarks.py#L1016-L1033
aarongarrett/inspyred
inspyred/benchmarks.py
Knapsack.generator
def generator(self, random, args): """Return a candidate solution for an evolutionary computation.""" if self.duplicates: max_count = [self.capacity // item[0] for item in self.items] return [random.randint(0, m) for m in max_count] else: return [random.choice([0, 1]) for _ in range(len(self.items))]
python
def generator(self, random, args): if self.duplicates: max_count = [self.capacity // item[0] for item in self.items] return [random.randint(0, m) for m in max_count] else: return [random.choice([0, 1]) for _ in range(len(self.items))]
[ "def", "generator", "(", "self", ",", "random", ",", "args", ")", ":", "if", "self", ".", "duplicates", ":", "max_count", "=", "[", "self", ".", "capacity", "//", "item", "[", "0", "]", "for", "item", "in", "self", ".", "items", "]", "return", "[", "random", ".", "randint", "(", "0", ",", "m", ")", "for", "m", "in", "max_count", "]", "else", ":", "return", "[", "random", ".", "choice", "(", "[", "0", ",", "1", "]", ")", "for", "_", "in", "range", "(", "len", "(", "self", ".", "items", ")", ")", "]" ]
Return a candidate solution for an evolutionary computation.
[ "Return", "a", "candidate", "solution", "for", "an", "evolutionary", "computation", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/benchmarks.py#L1085-L1091
aarongarrett/inspyred
inspyred/benchmarks.py
Knapsack.constructor
def constructor(self, random, args): """Return a candidate solution for an ant colony optimization.""" self._use_ants = True candidate = [] while len(candidate) < len(self.components): # Find feasible components feasible_components = [] if len(candidate) == 0: feasible_components = self.components else: remaining_capacity = self.capacity - sum([c.element for c in candidate]) if self.duplicates: feasible_components = [c for c in self.components if c.element <= remaining_capacity] else: feasible_components = [c for c in self.components if c not in candidate and c.element <= remaining_capacity] if len(feasible_components) == 0: break else: # Choose a feasible component if random.random() <= self.bias: next_component = max(feasible_components) else: next_component = selectors.fitness_proportionate_selection(random, feasible_components, {'num_selected': 1})[0] candidate.append(next_component) return candidate
python
def constructor(self, random, args): self._use_ants = True candidate = [] while len(candidate) < len(self.components): feasible_components = [] if len(candidate) == 0: feasible_components = self.components else: remaining_capacity = self.capacity - sum([c.element for c in candidate]) if self.duplicates: feasible_components = [c for c in self.components if c.element <= remaining_capacity] else: feasible_components = [c for c in self.components if c not in candidate and c.element <= remaining_capacity] if len(feasible_components) == 0: break else: if random.random() <= self.bias: next_component = max(feasible_components) else: next_component = selectors.fitness_proportionate_selection(random, feasible_components, {'num_selected': 1})[0] candidate.append(next_component) return candidate
[ "def", "constructor", "(", "self", ",", "random", ",", "args", ")", ":", "self", ".", "_use_ants", "=", "True", "candidate", "=", "[", "]", "while", "len", "(", "candidate", ")", "<", "len", "(", "self", ".", "components", ")", ":", "# Find feasible components", "feasible_components", "=", "[", "]", "if", "len", "(", "candidate", ")", "==", "0", ":", "feasible_components", "=", "self", ".", "components", "else", ":", "remaining_capacity", "=", "self", ".", "capacity", "-", "sum", "(", "[", "c", ".", "element", "for", "c", "in", "candidate", "]", ")", "if", "self", ".", "duplicates", ":", "feasible_components", "=", "[", "c", "for", "c", "in", "self", ".", "components", "if", "c", ".", "element", "<=", "remaining_capacity", "]", "else", ":", "feasible_components", "=", "[", "c", "for", "c", "in", "self", ".", "components", "if", "c", "not", "in", "candidate", "and", "c", ".", "element", "<=", "remaining_capacity", "]", "if", "len", "(", "feasible_components", ")", "==", "0", ":", "break", "else", ":", "# Choose a feasible component", "if", "random", ".", "random", "(", ")", "<=", "self", ".", "bias", ":", "next_component", "=", "max", "(", "feasible_components", ")", "else", ":", "next_component", "=", "selectors", ".", "fitness_proportionate_selection", "(", "random", ",", "feasible_components", ",", "{", "'num_selected'", ":", "1", "}", ")", "[", "0", "]", "candidate", ".", "append", "(", "next_component", ")", "return", "candidate" ]
Return a candidate solution for an ant colony optimization.
[ "Return", "a", "candidate", "solution", "for", "an", "ant", "colony", "optimization", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/benchmarks.py#L1093-L1117
aarongarrett/inspyred
inspyred/benchmarks.py
Knapsack.evaluator
def evaluator(self, candidates, args): """Return the fitness values for the given candidates.""" fitness = [] if self._use_ants: for candidate in candidates: total = 0 for c in candidate: total += c.value fitness.append(total) else: for candidate in candidates: total_value = 0 total_weight = 0 for c, i in zip(candidate, self.items): total_weight += c * i[0] total_value += c * i[1] if total_weight > self.capacity: fitness.append(self.capacity - total_weight) else: fitness.append(total_value) return fitness
python
def evaluator(self, candidates, args): fitness = [] if self._use_ants: for candidate in candidates: total = 0 for c in candidate: total += c.value fitness.append(total) else: for candidate in candidates: total_value = 0 total_weight = 0 for c, i in zip(candidate, self.items): total_weight += c * i[0] total_value += c * i[1] if total_weight > self.capacity: fitness.append(self.capacity - total_weight) else: fitness.append(total_value) return fitness
[ "def", "evaluator", "(", "self", ",", "candidates", ",", "args", ")", ":", "fitness", "=", "[", "]", "if", "self", ".", "_use_ants", ":", "for", "candidate", "in", "candidates", ":", "total", "=", "0", "for", "c", "in", "candidate", ":", "total", "+=", "c", ".", "value", "fitness", ".", "append", "(", "total", ")", "else", ":", "for", "candidate", "in", "candidates", ":", "total_value", "=", "0", "total_weight", "=", "0", "for", "c", ",", "i", "in", "zip", "(", "candidate", ",", "self", ".", "items", ")", ":", "total_weight", "+=", "c", "*", "i", "[", "0", "]", "total_value", "+=", "c", "*", "i", "[", "1", "]", "if", "total_weight", ">", "self", ".", "capacity", ":", "fitness", ".", "append", "(", "self", ".", "capacity", "-", "total_weight", ")", "else", ":", "fitness", ".", "append", "(", "total_value", ")", "return", "fitness" ]
Return the fitness values for the given candidates.
[ "Return", "the", "fitness", "values", "for", "the", "given", "candidates", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/benchmarks.py#L1119-L1139
aarongarrett/inspyred
inspyred/ec/variators/crossovers.py
crossover
def crossover(cross): """Return an inspyred crossover function based on the given function. This function generator takes a function that operates on only two parent candidates to produce an iterable sequence of offspring (typically two). The generator handles the pairing of selected parents and collecting of all offspring. The generated function chooses every odd candidate as a 'mom' and every even as a 'dad' (discounting the last candidate if there is an odd number). For each mom-dad pair, offspring are produced via the `cross` function. The given function ``cross`` must have the following signature:: offspring = cross(random, mom, dad, args) This function is most commonly used as a function decorator with the following usage:: @crossover def cross(random, mom, dad, args): # Implementation of paired crossing pass The generated function also contains an attribute named ``single_crossover`` which holds the original crossover function. In this way, the original single-set-of-parents function can be retrieved if necessary. """ @functools.wraps(cross) def inspyred_crossover(random, candidates, args): if len(candidates) % 2 == 1: candidates = candidates[:-1] moms = candidates[::2] dads = candidates[1::2] children = [] for i, (mom, dad) in enumerate(zip(moms, dads)): cross.index = i offspring = cross(random, mom, dad, args) for o in offspring: children.append(o) return children inspyred_crossover.single_crossover = cross return inspyred_crossover
python
def crossover(cross): @functools.wraps(cross) def inspyred_crossover(random, candidates, args): if len(candidates) % 2 == 1: candidates = candidates[:-1] moms = candidates[::2] dads = candidates[1::2] children = [] for i, (mom, dad) in enumerate(zip(moms, dads)): cross.index = i offspring = cross(random, mom, dad, args) for o in offspring: children.append(o) return children inspyred_crossover.single_crossover = cross return inspyred_crossover
[ "def", "crossover", "(", "cross", ")", ":", "@", "functools", ".", "wraps", "(", "cross", ")", "def", "inspyred_crossover", "(", "random", ",", "candidates", ",", "args", ")", ":", "if", "len", "(", "candidates", ")", "%", "2", "==", "1", ":", "candidates", "=", "candidates", "[", ":", "-", "1", "]", "moms", "=", "candidates", "[", ":", ":", "2", "]", "dads", "=", "candidates", "[", "1", ":", ":", "2", "]", "children", "=", "[", "]", "for", "i", ",", "(", "mom", ",", "dad", ")", "in", "enumerate", "(", "zip", "(", "moms", ",", "dads", ")", ")", ":", "cross", ".", "index", "=", "i", "offspring", "=", "cross", "(", "random", ",", "mom", ",", "dad", ",", "args", ")", "for", "o", "in", "offspring", ":", "children", ".", "append", "(", "o", ")", "return", "children", "inspyred_crossover", ".", "single_crossover", "=", "cross", "return", "inspyred_crossover" ]
Return an inspyred crossover function based on the given function. This function generator takes a function that operates on only two parent candidates to produce an iterable sequence of offspring (typically two). The generator handles the pairing of selected parents and collecting of all offspring. The generated function chooses every odd candidate as a 'mom' and every even as a 'dad' (discounting the last candidate if there is an odd number). For each mom-dad pair, offspring are produced via the `cross` function. The given function ``cross`` must have the following signature:: offspring = cross(random, mom, dad, args) This function is most commonly used as a function decorator with the following usage:: @crossover def cross(random, mom, dad, args): # Implementation of paired crossing pass The generated function also contains an attribute named ``single_crossover`` which holds the original crossover function. In this way, the original single-set-of-parents function can be retrieved if necessary.
[ "Return", "an", "inspyred", "crossover", "function", "based", "on", "the", "given", "function", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/variators/crossovers.py#L38-L83
aarongarrett/inspyred
inspyred/ec/variators/crossovers.py
n_point_crossover
def n_point_crossover(random, mom, dad, args): """Return the offspring of n-point crossover on the candidates. This function performs n-point crossover (NPX). It selects *n* random points without replacement at which to 'cut' the candidate solutions and recombine them. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *num_crossover_points* -- the number of crossover points used (default 1) """ crossover_rate = args.setdefault('crossover_rate', 1.0) num_crossover_points = args.setdefault('num_crossover_points', 1) children = [] if random.random() < crossover_rate: num_cuts = min(len(mom)-1, num_crossover_points) cut_points = random.sample(range(1, len(mom)), num_cuts) cut_points.sort() bro = copy.copy(dad) sis = copy.copy(mom) normal = True for i, (m, d) in enumerate(zip(mom, dad)): if i in cut_points: normal = not normal if not normal: bro[i] = m sis[i] = d normal = not normal children.append(bro) children.append(sis) else: children.append(mom) children.append(dad) return children
python
def n_point_crossover(random, mom, dad, args): crossover_rate = args.setdefault('crossover_rate', 1.0) num_crossover_points = args.setdefault('num_crossover_points', 1) children = [] if random.random() < crossover_rate: num_cuts = min(len(mom)-1, num_crossover_points) cut_points = random.sample(range(1, len(mom)), num_cuts) cut_points.sort() bro = copy.copy(dad) sis = copy.copy(mom) normal = True for i, (m, d) in enumerate(zip(mom, dad)): if i in cut_points: normal = not normal if not normal: bro[i] = m sis[i] = d normal = not normal children.append(bro) children.append(sis) else: children.append(mom) children.append(dad) return children
[ "def", "n_point_crossover", "(", "random", ",", "mom", ",", "dad", ",", "args", ")", ":", "crossover_rate", "=", "args", ".", "setdefault", "(", "'crossover_rate'", ",", "1.0", ")", "num_crossover_points", "=", "args", ".", "setdefault", "(", "'num_crossover_points'", ",", "1", ")", "children", "=", "[", "]", "if", "random", ".", "random", "(", ")", "<", "crossover_rate", ":", "num_cuts", "=", "min", "(", "len", "(", "mom", ")", "-", "1", ",", "num_crossover_points", ")", "cut_points", "=", "random", ".", "sample", "(", "range", "(", "1", ",", "len", "(", "mom", ")", ")", ",", "num_cuts", ")", "cut_points", ".", "sort", "(", ")", "bro", "=", "copy", ".", "copy", "(", "dad", ")", "sis", "=", "copy", ".", "copy", "(", "mom", ")", "normal", "=", "True", "for", "i", ",", "(", "m", ",", "d", ")", "in", "enumerate", "(", "zip", "(", "mom", ",", "dad", ")", ")", ":", "if", "i", "in", "cut_points", ":", "normal", "=", "not", "normal", "if", "not", "normal", ":", "bro", "[", "i", "]", "=", "m", "sis", "[", "i", "]", "=", "d", "normal", "=", "not", "normal", "children", ".", "append", "(", "bro", ")", "children", ".", "append", "(", "sis", ")", "else", ":", "children", ".", "append", "(", "mom", ")", "children", ".", "append", "(", "dad", ")", "return", "children" ]
Return the offspring of n-point crossover on the candidates. This function performs n-point crossover (NPX). It selects *n* random points without replacement at which to 'cut' the candidate solutions and recombine them. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *num_crossover_points* -- the number of crossover points used (default 1)
[ "Return", "the", "offspring", "of", "n", "-", "point", "crossover", "on", "the", "candidates", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/variators/crossovers.py#L87-L129
aarongarrett/inspyred
inspyred/ec/variators/crossovers.py
uniform_crossover
def uniform_crossover(random, mom, dad, args): """Return the offspring of uniform crossover on the candidates. This function performs uniform crossover (UX). For each element of the parents, a biased coin is flipped to determine whether the first offspring gets the 'mom' or the 'dad' element. An optional keyword argument in args, ``ux_bias``, determines the bias. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *ux_bias* -- the bias toward the first candidate in the crossover (default 0.5) """ ux_bias = args.setdefault('ux_bias', 0.5) crossover_rate = args.setdefault('crossover_rate', 1.0) children = [] if random.random() < crossover_rate: bro = copy.copy(dad) sis = copy.copy(mom) for i, (m, d) in enumerate(zip(mom, dad)): if random.random() < ux_bias: bro[i] = m sis[i] = d children.append(bro) children.append(sis) else: children.append(mom) children.append(dad) return children
python
def uniform_crossover(random, mom, dad, args): ux_bias = args.setdefault('ux_bias', 0.5) crossover_rate = args.setdefault('crossover_rate', 1.0) children = [] if random.random() < crossover_rate: bro = copy.copy(dad) sis = copy.copy(mom) for i, (m, d) in enumerate(zip(mom, dad)): if random.random() < ux_bias: bro[i] = m sis[i] = d children.append(bro) children.append(sis) else: children.append(mom) children.append(dad) return children
[ "def", "uniform_crossover", "(", "random", ",", "mom", ",", "dad", ",", "args", ")", ":", "ux_bias", "=", "args", ".", "setdefault", "(", "'ux_bias'", ",", "0.5", ")", "crossover_rate", "=", "args", ".", "setdefault", "(", "'crossover_rate'", ",", "1.0", ")", "children", "=", "[", "]", "if", "random", ".", "random", "(", ")", "<", "crossover_rate", ":", "bro", "=", "copy", ".", "copy", "(", "dad", ")", "sis", "=", "copy", ".", "copy", "(", "mom", ")", "for", "i", ",", "(", "m", ",", "d", ")", "in", "enumerate", "(", "zip", "(", "mom", ",", "dad", ")", ")", ":", "if", "random", ".", "random", "(", ")", "<", "ux_bias", ":", "bro", "[", "i", "]", "=", "m", "sis", "[", "i", "]", "=", "d", "children", ".", "append", "(", "bro", ")", "children", ".", "append", "(", "sis", ")", "else", ":", "children", ".", "append", "(", "mom", ")", "children", ".", "append", "(", "dad", ")", "return", "children" ]
Return the offspring of uniform crossover on the candidates. This function performs uniform crossover (UX). For each element of the parents, a biased coin is flipped to determine whether the first offspring gets the 'mom' or the 'dad' element. An optional keyword argument in args, ``ux_bias``, determines the bias. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *ux_bias* -- the bias toward the first candidate in the crossover (default 0.5)
[ "Return", "the", "offspring", "of", "uniform", "crossover", "on", "the", "candidates", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/variators/crossovers.py#L133-L170
aarongarrett/inspyred
inspyred/ec/variators/crossovers.py
partially_matched_crossover
def partially_matched_crossover(random, mom, dad, args): """Return the offspring of partially matched crossover on the candidates. This function performs partially matched crossover (PMX). This type of crossover assumes that candidates are composed of discrete values that are permutations of a given set (typically integers). It produces offspring that are themselves permutations of the set. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) """ crossover_rate = args.setdefault('crossover_rate', 1.0) if random.random() < crossover_rate: size = len(mom) points = random.sample(range(size), 2) x, y = min(points), max(points) bro = copy.copy(dad) bro[x:y+1] = mom[x:y+1] sis = copy.copy(mom) sis[x:y+1] = dad[x:y+1] for parent, child in zip([dad, mom], [bro, sis]): for i in range(x, y+1): if parent[i] not in child[x:y+1]: spot = i while x <= spot <= y: spot = parent.index(child[spot]) child[spot] = parent[i] return [bro, sis] else: return [mom, dad]
python
def partially_matched_crossover(random, mom, dad, args): crossover_rate = args.setdefault('crossover_rate', 1.0) if random.random() < crossover_rate: size = len(mom) points = random.sample(range(size), 2) x, y = min(points), max(points) bro = copy.copy(dad) bro[x:y+1] = mom[x:y+1] sis = copy.copy(mom) sis[x:y+1] = dad[x:y+1] for parent, child in zip([dad, mom], [bro, sis]): for i in range(x, y+1): if parent[i] not in child[x:y+1]: spot = i while x <= spot <= y: spot = parent.index(child[spot]) child[spot] = parent[i] return [bro, sis] else: return [mom, dad]
[ "def", "partially_matched_crossover", "(", "random", ",", "mom", ",", "dad", ",", "args", ")", ":", "crossover_rate", "=", "args", ".", "setdefault", "(", "'crossover_rate'", ",", "1.0", ")", "if", "random", ".", "random", "(", ")", "<", "crossover_rate", ":", "size", "=", "len", "(", "mom", ")", "points", "=", "random", ".", "sample", "(", "range", "(", "size", ")", ",", "2", ")", "x", ",", "y", "=", "min", "(", "points", ")", ",", "max", "(", "points", ")", "bro", "=", "copy", ".", "copy", "(", "dad", ")", "bro", "[", "x", ":", "y", "+", "1", "]", "=", "mom", "[", "x", ":", "y", "+", "1", "]", "sis", "=", "copy", ".", "copy", "(", "mom", ")", "sis", "[", "x", ":", "y", "+", "1", "]", "=", "dad", "[", "x", ":", "y", "+", "1", "]", "for", "parent", ",", "child", "in", "zip", "(", "[", "dad", ",", "mom", "]", ",", "[", "bro", ",", "sis", "]", ")", ":", "for", "i", "in", "range", "(", "x", ",", "y", "+", "1", ")", ":", "if", "parent", "[", "i", "]", "not", "in", "child", "[", "x", ":", "y", "+", "1", "]", ":", "spot", "=", "i", "while", "x", "<=", "spot", "<=", "y", ":", "spot", "=", "parent", ".", "index", "(", "child", "[", "spot", "]", ")", "child", "[", "spot", "]", "=", "parent", "[", "i", "]", "return", "[", "bro", ",", "sis", "]", "else", ":", "return", "[", "mom", ",", "dad", "]" ]
Return the offspring of partially matched crossover on the candidates. This function performs partially matched crossover (PMX). This type of crossover assumes that candidates are composed of discrete values that are permutations of a given set (typically integers). It produces offspring that are themselves permutations of the set. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0)
[ "Return", "the", "offspring", "of", "partially", "matched", "crossover", "on", "the", "candidates", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/variators/crossovers.py#L174-L212
aarongarrett/inspyred
inspyred/ec/variators/crossovers.py
arithmetic_crossover
def arithmetic_crossover(random, mom, dad, args): """Return the offspring of arithmetic crossover on the candidates. This function performs arithmetic crossover (AX), which is similar to a generalized weighted averaging of the candidate elements. The allele of each parent is weighted by the *ax_alpha* keyword argument, and the allele of the complement parent is weighted by 1 - *ax_alpha*. This averaging is only done on the alleles listed in the *ax_points* keyword argument. If this argument is ``None``, then all alleles are used. This means that if this function is used with all default values, then offspring are simple averages of their parents. This function also makes use of the bounder function as specified in the EC's ``evolve`` method. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *ax_alpha* -- the weight for the averaging (default 0.5) - *ax_points* -- a list of points specifying the alleles to recombine (default None) """ ax_alpha = args.setdefault('ax_alpha', 0.5) ax_points = args.setdefault('ax_points', None) crossover_rate = args.setdefault('crossover_rate', 1.0) bounder = args['_ec'].bounder children = [] if random.random() < crossover_rate: bro = copy.copy(dad) sis = copy.copy(mom) if ax_points is None: ax_points = list(range(min(len(bro), len(sis)))) for i in ax_points: bro[i] = ax_alpha * mom[i] + (1 - ax_alpha) * dad[i] sis[i] = ax_alpha * dad[i] + (1 - ax_alpha) * mom[i] bro = bounder(bro, args) sis = bounder(sis, args) children.append(bro) children.append(sis) else: children.append(mom) children.append(dad) return children
python
def arithmetic_crossover(random, mom, dad, args): ax_alpha = args.setdefault('ax_alpha', 0.5) ax_points = args.setdefault('ax_points', None) crossover_rate = args.setdefault('crossover_rate', 1.0) bounder = args['_ec'].bounder children = [] if random.random() < crossover_rate: bro = copy.copy(dad) sis = copy.copy(mom) if ax_points is None: ax_points = list(range(min(len(bro), len(sis)))) for i in ax_points: bro[i] = ax_alpha * mom[i] + (1 - ax_alpha) * dad[i] sis[i] = ax_alpha * dad[i] + (1 - ax_alpha) * mom[i] bro = bounder(bro, args) sis = bounder(sis, args) children.append(bro) children.append(sis) else: children.append(mom) children.append(dad) return children
[ "def", "arithmetic_crossover", "(", "random", ",", "mom", ",", "dad", ",", "args", ")", ":", "ax_alpha", "=", "args", ".", "setdefault", "(", "'ax_alpha'", ",", "0.5", ")", "ax_points", "=", "args", ".", "setdefault", "(", "'ax_points'", ",", "None", ")", "crossover_rate", "=", "args", ".", "setdefault", "(", "'crossover_rate'", ",", "1.0", ")", "bounder", "=", "args", "[", "'_ec'", "]", ".", "bounder", "children", "=", "[", "]", "if", "random", ".", "random", "(", ")", "<", "crossover_rate", ":", "bro", "=", "copy", ".", "copy", "(", "dad", ")", "sis", "=", "copy", ".", "copy", "(", "mom", ")", "if", "ax_points", "is", "None", ":", "ax_points", "=", "list", "(", "range", "(", "min", "(", "len", "(", "bro", ")", ",", "len", "(", "sis", ")", ")", ")", ")", "for", "i", "in", "ax_points", ":", "bro", "[", "i", "]", "=", "ax_alpha", "*", "mom", "[", "i", "]", "+", "(", "1", "-", "ax_alpha", ")", "*", "dad", "[", "i", "]", "sis", "[", "i", "]", "=", "ax_alpha", "*", "dad", "[", "i", "]", "+", "(", "1", "-", "ax_alpha", ")", "*", "mom", "[", "i", "]", "bro", "=", "bounder", "(", "bro", ",", "args", ")", "sis", "=", "bounder", "(", "sis", ",", "args", ")", "children", ".", "append", "(", "bro", ")", "children", ".", "append", "(", "sis", ")", "else", ":", "children", ".", "append", "(", "mom", ")", "children", ".", "append", "(", "dad", ")", "return", "children" ]
Return the offspring of arithmetic crossover on the candidates. This function performs arithmetic crossover (AX), which is similar to a generalized weighted averaging of the candidate elements. The allele of each parent is weighted by the *ax_alpha* keyword argument, and the allele of the complement parent is weighted by 1 - *ax_alpha*. This averaging is only done on the alleles listed in the *ax_points* keyword argument. If this argument is ``None``, then all alleles are used. This means that if this function is used with all default values, then offspring are simple averages of their parents. This function also makes use of the bounder function as specified in the EC's ``evolve`` method. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *ax_alpha* -- the weight for the averaging (default 0.5) - *ax_points* -- a list of points specifying the alleles to recombine (default None)
[ "Return", "the", "offspring", "of", "arithmetic", "crossover", "on", "the", "candidates", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/variators/crossovers.py#L216-L265
aarongarrett/inspyred
inspyred/ec/variators/crossovers.py
blend_crossover
def blend_crossover(random, mom, dad, args): """Return the offspring of blend crossover on the candidates. This function performs blend crossover (BLX), which is similar to arithmetic crossover with a bit of mutation. It creates offspring whose values are chosen randomly from a range bounded by the parent alleles but that is also extended by some amount proportional to the *blx_alpha* keyword argument. It is this extension of the range that provides the additional exploration. This averaging is only done on the alleles listed in the *blx_points* keyword argument. If this argument is ``None``, then all alleles are used. This function also makes use of the bounder function as specified in the EC's ``evolve`` method. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *blx_alpha* -- the blending rate (default 0.1) - *blx_points* -- a list of points specifying the alleles to recombine (default None) """ blx_alpha = args.setdefault('blx_alpha', 0.1) blx_points = args.setdefault('blx_points', None) crossover_rate = args.setdefault('crossover_rate', 1.0) bounder = args['_ec'].bounder children = [] if random.random() < crossover_rate: bro = copy.copy(dad) sis = copy.copy(mom) if blx_points is None: blx_points = list(range(min(len(bro), len(sis)))) for i in blx_points: smallest, largest = min(mom[i], dad[i]), max(mom[i], dad[i]) delta = blx_alpha * (largest - smallest) bro[i] = smallest - delta + random.random() * (largest - smallest + 2 * delta) sis[i] = smallest - delta + random.random() * (largest - smallest + 2 * delta) bro = bounder(bro, args) sis = bounder(sis, args) children.append(bro) children.append(sis) else: children.append(mom) children.append(dad) return children
python
def blend_crossover(random, mom, dad, args): blx_alpha = args.setdefault('blx_alpha', 0.1) blx_points = args.setdefault('blx_points', None) crossover_rate = args.setdefault('crossover_rate', 1.0) bounder = args['_ec'].bounder children = [] if random.random() < crossover_rate: bro = copy.copy(dad) sis = copy.copy(mom) if blx_points is None: blx_points = list(range(min(len(bro), len(sis)))) for i in blx_points: smallest, largest = min(mom[i], dad[i]), max(mom[i], dad[i]) delta = blx_alpha * (largest - smallest) bro[i] = smallest - delta + random.random() * (largest - smallest + 2 * delta) sis[i] = smallest - delta + random.random() * (largest - smallest + 2 * delta) bro = bounder(bro, args) sis = bounder(sis, args) children.append(bro) children.append(sis) else: children.append(mom) children.append(dad) return children
[ "def", "blend_crossover", "(", "random", ",", "mom", ",", "dad", ",", "args", ")", ":", "blx_alpha", "=", "args", ".", "setdefault", "(", "'blx_alpha'", ",", "0.1", ")", "blx_points", "=", "args", ".", "setdefault", "(", "'blx_points'", ",", "None", ")", "crossover_rate", "=", "args", ".", "setdefault", "(", "'crossover_rate'", ",", "1.0", ")", "bounder", "=", "args", "[", "'_ec'", "]", ".", "bounder", "children", "=", "[", "]", "if", "random", ".", "random", "(", ")", "<", "crossover_rate", ":", "bro", "=", "copy", ".", "copy", "(", "dad", ")", "sis", "=", "copy", ".", "copy", "(", "mom", ")", "if", "blx_points", "is", "None", ":", "blx_points", "=", "list", "(", "range", "(", "min", "(", "len", "(", "bro", ")", ",", "len", "(", "sis", ")", ")", ")", ")", "for", "i", "in", "blx_points", ":", "smallest", ",", "largest", "=", "min", "(", "mom", "[", "i", "]", ",", "dad", "[", "i", "]", ")", ",", "max", "(", "mom", "[", "i", "]", ",", "dad", "[", "i", "]", ")", "delta", "=", "blx_alpha", "*", "(", "largest", "-", "smallest", ")", "bro", "[", "i", "]", "=", "smallest", "-", "delta", "+", "random", ".", "random", "(", ")", "*", "(", "largest", "-", "smallest", "+", "2", "*", "delta", ")", "sis", "[", "i", "]", "=", "smallest", "-", "delta", "+", "random", ".", "random", "(", ")", "*", "(", "largest", "-", "smallest", "+", "2", "*", "delta", ")", "bro", "=", "bounder", "(", "bro", ",", "args", ")", "sis", "=", "bounder", "(", "sis", ",", "args", ")", "children", ".", "append", "(", "bro", ")", "children", ".", "append", "(", "sis", ")", "else", ":", "children", ".", "append", "(", "mom", ")", "children", ".", "append", "(", "dad", ")", "return", "children" ]
Return the offspring of blend crossover on the candidates. This function performs blend crossover (BLX), which is similar to arithmetic crossover with a bit of mutation. It creates offspring whose values are chosen randomly from a range bounded by the parent alleles but that is also extended by some amount proportional to the *blx_alpha* keyword argument. It is this extension of the range that provides the additional exploration. This averaging is only done on the alleles listed in the *blx_points* keyword argument. If this argument is ``None``, then all alleles are used. This function also makes use of the bounder function as specified in the EC's ``evolve`` method. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *blx_alpha* -- the blending rate (default 0.1) - *blx_points* -- a list of points specifying the alleles to recombine (default None)
[ "Return", "the", "offspring", "of", "blend", "crossover", "on", "the", "candidates", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/variators/crossovers.py#L269-L320
aarongarrett/inspyred
inspyred/ec/variators/crossovers.py
heuristic_crossover
def heuristic_crossover(random, candidates, args): """Return the offspring of heuristic crossover on the candidates. It performs heuristic crossover (HX), which is similar to the update rule used in particle swarm optimization. This function also makes use of the bounder function as specified in the EC's ``evolve`` method. .. note:: This function assumes that candidates can be pickled (for hashing as keys to a dictionary). .. Arguments: random -- the random number generator object candidates -- the candidate solutions args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) """ crossover_rate = args.setdefault('crossover_rate', 1.0) bounder = args['_ec'].bounder if len(candidates) % 2 == 1: candidates = candidates[:-1] # Since we don't have fitness information in the candidates, we need # to make a dictionary containing the candidate and its corresponding # individual in the population. population = list(args['_ec'].population) lookup = dict(zip([pickle.dumps(p.candidate, 1) for p in population], population)) moms = candidates[::2] dads = candidates[1::2] children = [] for mom, dad in zip(moms, dads): if random.random() < crossover_rate: bro = copy.copy(dad) sis = copy.copy(mom) mom_is_better = lookup[pickle.dumps(mom, 1)] > lookup[pickle.dumps(dad, 1)] for i, (m, d) in enumerate(zip(mom, dad)): negpos = 1 if mom_is_better else -1 val = d if mom_is_better else m bro[i] = val + random.random() * negpos * (m - d) sis[i] = val + random.random() * negpos * (m - d) bro = bounder(bro, args) sis = bounder(sis, args) children.append(bro) children.append(sis) else: children.append(mom) children.append(dad) return children
python
def heuristic_crossover(random, candidates, args): crossover_rate = args.setdefault('crossover_rate', 1.0) bounder = args['_ec'].bounder if len(candidates) % 2 == 1: candidates = candidates[:-1] population = list(args['_ec'].population) lookup = dict(zip([pickle.dumps(p.candidate, 1) for p in population], population)) moms = candidates[::2] dads = candidates[1::2] children = [] for mom, dad in zip(moms, dads): if random.random() < crossover_rate: bro = copy.copy(dad) sis = copy.copy(mom) mom_is_better = lookup[pickle.dumps(mom, 1)] > lookup[pickle.dumps(dad, 1)] for i, (m, d) in enumerate(zip(mom, dad)): negpos = 1 if mom_is_better else -1 val = d if mom_is_better else m bro[i] = val + random.random() * negpos * (m - d) sis[i] = val + random.random() * negpos * (m - d) bro = bounder(bro, args) sis = bounder(sis, args) children.append(bro) children.append(sis) else: children.append(mom) children.append(dad) return children
[ "def", "heuristic_crossover", "(", "random", ",", "candidates", ",", "args", ")", ":", "crossover_rate", "=", "args", ".", "setdefault", "(", "'crossover_rate'", ",", "1.0", ")", "bounder", "=", "args", "[", "'_ec'", "]", ".", "bounder", "if", "len", "(", "candidates", ")", "%", "2", "==", "1", ":", "candidates", "=", "candidates", "[", ":", "-", "1", "]", "# Since we don't have fitness information in the candidates, we need ", "# to make a dictionary containing the candidate and its corresponding ", "# individual in the population.", "population", "=", "list", "(", "args", "[", "'_ec'", "]", ".", "population", ")", "lookup", "=", "dict", "(", "zip", "(", "[", "pickle", ".", "dumps", "(", "p", ".", "candidate", ",", "1", ")", "for", "p", "in", "population", "]", ",", "population", ")", ")", "moms", "=", "candidates", "[", ":", ":", "2", "]", "dads", "=", "candidates", "[", "1", ":", ":", "2", "]", "children", "=", "[", "]", "for", "mom", ",", "dad", "in", "zip", "(", "moms", ",", "dads", ")", ":", "if", "random", ".", "random", "(", ")", "<", "crossover_rate", ":", "bro", "=", "copy", ".", "copy", "(", "dad", ")", "sis", "=", "copy", ".", "copy", "(", "mom", ")", "mom_is_better", "=", "lookup", "[", "pickle", ".", "dumps", "(", "mom", ",", "1", ")", "]", ">", "lookup", "[", "pickle", ".", "dumps", "(", "dad", ",", "1", ")", "]", "for", "i", ",", "(", "m", ",", "d", ")", "in", "enumerate", "(", "zip", "(", "mom", ",", "dad", ")", ")", ":", "negpos", "=", "1", "if", "mom_is_better", "else", "-", "1", "val", "=", "d", "if", "mom_is_better", "else", "m", "bro", "[", "i", "]", "=", "val", "+", "random", ".", "random", "(", ")", "*", "negpos", "*", "(", "m", "-", "d", ")", "sis", "[", "i", "]", "=", "val", "+", "random", ".", "random", "(", ")", "*", "negpos", "*", "(", "m", "-", "d", ")", "bro", "=", "bounder", "(", "bro", ",", "args", ")", "sis", "=", "bounder", "(", "sis", ",", "args", ")", "children", ".", "append", "(", "bro", ")", "children", ".", "append", "(", "sis", ")", "else", ":", "children", ".", "append", "(", "mom", ")", "children", ".", "append", "(", "dad", ")", "return", "children" ]
Return the offspring of heuristic crossover on the candidates. It performs heuristic crossover (HX), which is similar to the update rule used in particle swarm optimization. This function also makes use of the bounder function as specified in the EC's ``evolve`` method. .. note:: This function assumes that candidates can be pickled (for hashing as keys to a dictionary). .. Arguments: random -- the random number generator object candidates -- the candidate solutions args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0)
[ "Return", "the", "offspring", "of", "heuristic", "crossover", "on", "the", "candidates", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/variators/crossovers.py#L323-L379
aarongarrett/inspyred
inspyred/ec/variators/crossovers.py
simulated_binary_crossover
def simulated_binary_crossover(random, mom, dad, args): """Return the offspring of simulated binary crossover on the candidates. This function performs simulated binary crossover (SBX), following the implementation in NSGA-II `(Deb et al., ICANNGA 1999) <http://vision.ucsd.edu/~sagarwal/icannga.pdf>`_. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *sbx_distribution_index* -- the non-negative distribution index (default 10) A small value of the `sbx_distribution_index` optional argument allows solutions far away from parents to be created as child solutions, while a large value restricts only near-parent solutions to be created as child solutions. """ crossover_rate = args.setdefault('crossover_rate', 1.0) if random.random() < crossover_rate: di = args.setdefault('sbx_distribution_index', 10) bounder = args['_ec'].bounder bro = copy.copy(dad) sis = copy.copy(mom) for i, (m, d, lb, ub) in enumerate(zip(mom, dad, bounder.lower_bound, bounder.upper_bound)): try: if m > d: m, d = d, m beta = 1.0 + 2 * min(m - lb, ub - d) / float(d - m) alpha = 2.0 - 1.0 / beta**(di + 1.0) u = random.random() if u <= (1.0 / alpha): beta_q = (u * alpha)**(1.0 / float(di + 1.0)) else: beta_q = (1.0 / (2.0 - u * alpha))**(1.0 / float(di + 1.0)) bro_val = 0.5 * ((m + d) - beta_q * (d - m)) bro_val = max(min(bro_val, ub), lb) sis_val = 0.5 * ((m + d) + beta_q * (d - m)) sis_val = max(min(sis_val, ub), lb) if random.random() > 0.5: bro_val, sis_val = sis_val, bro_val bro[i] = bro_val sis[i] = sis_val except ZeroDivisionError: # The offspring already have legitimate values for every element, # so no need to take any special action here. pass return [bro, sis] else: return [mom, dad]
python
def simulated_binary_crossover(random, mom, dad, args): crossover_rate = args.setdefault('crossover_rate', 1.0) if random.random() < crossover_rate: di = args.setdefault('sbx_distribution_index', 10) bounder = args['_ec'].bounder bro = copy.copy(dad) sis = copy.copy(mom) for i, (m, d, lb, ub) in enumerate(zip(mom, dad, bounder.lower_bound, bounder.upper_bound)): try: if m > d: m, d = d, m beta = 1.0 + 2 * min(m - lb, ub - d) / float(d - m) alpha = 2.0 - 1.0 / beta**(di + 1.0) u = random.random() if u <= (1.0 / alpha): beta_q = (u * alpha)**(1.0 / float(di + 1.0)) else: beta_q = (1.0 / (2.0 - u * alpha))**(1.0 / float(di + 1.0)) bro_val = 0.5 * ((m + d) - beta_q * (d - m)) bro_val = max(min(bro_val, ub), lb) sis_val = 0.5 * ((m + d) + beta_q * (d - m)) sis_val = max(min(sis_val, ub), lb) if random.random() > 0.5: bro_val, sis_val = sis_val, bro_val bro[i] = bro_val sis[i] = sis_val except ZeroDivisionError: pass return [bro, sis] else: return [mom, dad]
[ "def", "simulated_binary_crossover", "(", "random", ",", "mom", ",", "dad", ",", "args", ")", ":", "crossover_rate", "=", "args", ".", "setdefault", "(", "'crossover_rate'", ",", "1.0", ")", "if", "random", ".", "random", "(", ")", "<", "crossover_rate", ":", "di", "=", "args", ".", "setdefault", "(", "'sbx_distribution_index'", ",", "10", ")", "bounder", "=", "args", "[", "'_ec'", "]", ".", "bounder", "bro", "=", "copy", ".", "copy", "(", "dad", ")", "sis", "=", "copy", ".", "copy", "(", "mom", ")", "for", "i", ",", "(", "m", ",", "d", ",", "lb", ",", "ub", ")", "in", "enumerate", "(", "zip", "(", "mom", ",", "dad", ",", "bounder", ".", "lower_bound", ",", "bounder", ".", "upper_bound", ")", ")", ":", "try", ":", "if", "m", ">", "d", ":", "m", ",", "d", "=", "d", ",", "m", "beta", "=", "1.0", "+", "2", "*", "min", "(", "m", "-", "lb", ",", "ub", "-", "d", ")", "/", "float", "(", "d", "-", "m", ")", "alpha", "=", "2.0", "-", "1.0", "/", "beta", "**", "(", "di", "+", "1.0", ")", "u", "=", "random", ".", "random", "(", ")", "if", "u", "<=", "(", "1.0", "/", "alpha", ")", ":", "beta_q", "=", "(", "u", "*", "alpha", ")", "**", "(", "1.0", "/", "float", "(", "di", "+", "1.0", ")", ")", "else", ":", "beta_q", "=", "(", "1.0", "/", "(", "2.0", "-", "u", "*", "alpha", ")", ")", "**", "(", "1.0", "/", "float", "(", "di", "+", "1.0", ")", ")", "bro_val", "=", "0.5", "*", "(", "(", "m", "+", "d", ")", "-", "beta_q", "*", "(", "d", "-", "m", ")", ")", "bro_val", "=", "max", "(", "min", "(", "bro_val", ",", "ub", ")", ",", "lb", ")", "sis_val", "=", "0.5", "*", "(", "(", "m", "+", "d", ")", "+", "beta_q", "*", "(", "d", "-", "m", ")", ")", "sis_val", "=", "max", "(", "min", "(", "sis_val", ",", "ub", ")", ",", "lb", ")", "if", "random", ".", "random", "(", ")", ">", "0.5", ":", "bro_val", ",", "sis_val", "=", "sis_val", ",", "bro_val", "bro", "[", "i", "]", "=", "bro_val", "sis", "[", "i", "]", "=", "sis_val", "except", "ZeroDivisionError", ":", "# The offspring already have legitimate values for every element,", "# so no need to take any special action here.", "pass", "return", "[", "bro", ",", "sis", "]", "else", ":", "return", "[", "mom", ",", "dad", "]" ]
Return the offspring of simulated binary crossover on the candidates. This function performs simulated binary crossover (SBX), following the implementation in NSGA-II `(Deb et al., ICANNGA 1999) <http://vision.ucsd.edu/~sagarwal/icannga.pdf>`_. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *sbx_distribution_index* -- the non-negative distribution index (default 10) A small value of the `sbx_distribution_index` optional argument allows solutions far away from parents to be created as child solutions, while a large value restricts only near-parent solutions to be created as child solutions.
[ "Return", "the", "offspring", "of", "simulated", "binary", "crossover", "on", "the", "candidates", ".", "This", "function", "performs", "simulated", "binary", "crossover", "(", "SBX", ")", "following", "the", "implementation", "in", "NSGA", "-", "II", "(", "Deb", "et", "al", ".", "ICANNGA", "1999", ")", "<http", ":", "//", "vision", ".", "ucsd", ".", "edu", "/", "~sagarwal", "/", "icannga", ".", "pdf", ">", "_", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "mom", "--", "the", "first", "parent", "candidate", "dad", "--", "the", "second", "parent", "candidate", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/variators/crossovers.py#L383-L440
aarongarrett/inspyred
inspyred/ec/variators/crossovers.py
laplace_crossover
def laplace_crossover(random, mom, dad, args): """Return the offspring of Laplace crossover on the candidates. This function performs Laplace crosssover (LX), following the implementation specified in (Deep and Thakur, "A new crossover operator for real coded genetic algorithms," Applied Mathematics and Computation, Volume 188, Issue 1, May 2007, pp. 895--911). This function also makes use of the bounder function as specified in the EC's ``evolve`` method. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *lx_location* -- the location parameter (default 0) - *lx_scale* -- the scale parameter (default 0.5) In some sense, the *lx_location* and *lx_scale* parameters can be thought of as analogs in a Laplace distribution to the mean and standard deviation of a Gaussian distribution. If *lx_scale* is near zero, offspring will be produced near the parents. If *lx_scale* is farther from zero, offspring will be produced far from the parents. """ crossover_rate = args.setdefault('crossover_rate', 1.0) if random.random() < crossover_rate: bounder = args['_ec'].bounder a = args.setdefault('lx_location', 0) b = args.setdefault('lx_scale', 0.5) bro = copy.copy(dad) sis = copy.copy(mom) for i, (m, d) in enumerate(zip(mom, dad)): u = random.random() if random.random() <= 0.5: beta = a - b * math.log(u) else: beta = a + b * math.log(u) bro[i] = m + beta * abs(m - d) sis[i] = d + beta * abs(m - d) bro = bounder(bro, args) sis = bounder(sis, args) return [bro, sis] else: return [mom, dad]
python
def laplace_crossover(random, mom, dad, args): crossover_rate = args.setdefault('crossover_rate', 1.0) if random.random() < crossover_rate: bounder = args['_ec'].bounder a = args.setdefault('lx_location', 0) b = args.setdefault('lx_scale', 0.5) bro = copy.copy(dad) sis = copy.copy(mom) for i, (m, d) in enumerate(zip(mom, dad)): u = random.random() if random.random() <= 0.5: beta = a - b * math.log(u) else: beta = a + b * math.log(u) bro[i] = m + beta * abs(m - d) sis[i] = d + beta * abs(m - d) bro = bounder(bro, args) sis = bounder(sis, args) return [bro, sis] else: return [mom, dad]
[ "def", "laplace_crossover", "(", "random", ",", "mom", ",", "dad", ",", "args", ")", ":", "crossover_rate", "=", "args", ".", "setdefault", "(", "'crossover_rate'", ",", "1.0", ")", "if", "random", ".", "random", "(", ")", "<", "crossover_rate", ":", "bounder", "=", "args", "[", "'_ec'", "]", ".", "bounder", "a", "=", "args", ".", "setdefault", "(", "'lx_location'", ",", "0", ")", "b", "=", "args", ".", "setdefault", "(", "'lx_scale'", ",", "0.5", ")", "bro", "=", "copy", ".", "copy", "(", "dad", ")", "sis", "=", "copy", ".", "copy", "(", "mom", ")", "for", "i", ",", "(", "m", ",", "d", ")", "in", "enumerate", "(", "zip", "(", "mom", ",", "dad", ")", ")", ":", "u", "=", "random", ".", "random", "(", ")", "if", "random", ".", "random", "(", ")", "<=", "0.5", ":", "beta", "=", "a", "-", "b", "*", "math", ".", "log", "(", "u", ")", "else", ":", "beta", "=", "a", "+", "b", "*", "math", ".", "log", "(", "u", ")", "bro", "[", "i", "]", "=", "m", "+", "beta", "*", "abs", "(", "m", "-", "d", ")", "sis", "[", "i", "]", "=", "d", "+", "beta", "*", "abs", "(", "m", "-", "d", ")", "bro", "=", "bounder", "(", "bro", ",", "args", ")", "sis", "=", "bounder", "(", "sis", ",", "args", ")", "return", "[", "bro", ",", "sis", "]", "else", ":", "return", "[", "mom", ",", "dad", "]" ]
Return the offspring of Laplace crossover on the candidates. This function performs Laplace crosssover (LX), following the implementation specified in (Deep and Thakur, "A new crossover operator for real coded genetic algorithms," Applied Mathematics and Computation, Volume 188, Issue 1, May 2007, pp. 895--911). This function also makes use of the bounder function as specified in the EC's ``evolve`` method. .. Arguments: random -- the random number generator object mom -- the first parent candidate dad -- the second parent candidate args -- a dictionary of keyword arguments Optional keyword arguments in args: - *crossover_rate* -- the rate at which crossover is performed (default 1.0) - *lx_location* -- the location parameter (default 0) - *lx_scale* -- the scale parameter (default 0.5) In some sense, the *lx_location* and *lx_scale* parameters can be thought of as analogs in a Laplace distribution to the mean and standard deviation of a Gaussian distribution. If *lx_scale* is near zero, offspring will be produced near the parents. If *lx_scale* is farther from zero, offspring will be produced far from the parents.
[ "Return", "the", "offspring", "of", "Laplace", "crossover", "on", "the", "candidates", ".", "This", "function", "performs", "Laplace", "crosssover", "(", "LX", ")", "following", "the", "implementation", "specified", "in", "(", "Deep", "and", "Thakur", "A", "new", "crossover", "operator", "for", "real", "coded", "genetic", "algorithms", "Applied", "Mathematics", "and", "Computation", "Volume", "188", "Issue", "1", "May", "2007", "pp", ".", "895", "--", "911", ")", ".", "This", "function", "also", "makes", "use", "of", "the", "bounder", "function", "as", "specified", "in", "the", "EC", "s", "evolve", "method", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "mom", "--", "the", "first", "parent", "candidate", "dad", "--", "the", "second", "parent", "candidate", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/variators/crossovers.py#L444-L493
aarongarrett/inspyred
inspyred/ec/analysis.py
fitness_statistics
def fitness_statistics(population): """Return the basic statistics of the population's fitness values. This function returns a dictionary containing the "best", "worst", "mean", "median", and "std" fitness values in the population. ("std" is the standard deviation.) A typical usage would be similar to the following:: stats = fitness_statistics(population) print(stats['best']) print(stats['worst']) print(stats['mean']) print(stats['median']) print(stats['std']) .. note:: This function makes use of the numpy library for calculations. If that library is not found, it attempts to complete the calculations internally. However, this second attempt will fail for multiobjective fitness values and will return ``nan`` for the mean, median, and standard deviation. Arguments: - *population* -- the population of individuals """ population.sort(reverse=True) worst_fit = population[-1].fitness best_fit = population[0].fitness try: import numpy f = [p.fitness for p in population] med_fit = numpy.median(f) avg_fit = numpy.mean(f) std_fit = numpy.std(f) except ImportError: try: plen = len(population) if plen % 2 == 1: med_fit = population[(plen - 1) // 2].fitness else: med_fit = float(population[plen // 2 - 1].fitness + population[plen // 2].fitness) / 2 avg_fit = sum([p.fitness for p in population]) / float(plen) if plen > 1: std_fit = math.sqrt(sum([(p.fitness - avg_fit)**2 for p in population]) / float(plen - 1)) else: std_fit = 0 except TypeError: med_fit = float('nan') avg_fit = float('nan') std_fit = float('nan') return {'best': best_fit, 'worst': worst_fit, 'mean': avg_fit, 'median': med_fit, 'std': std_fit}
python
def fitness_statistics(population): population.sort(reverse=True) worst_fit = population[-1].fitness best_fit = population[0].fitness try: import numpy f = [p.fitness for p in population] med_fit = numpy.median(f) avg_fit = numpy.mean(f) std_fit = numpy.std(f) except ImportError: try: plen = len(population) if plen % 2 == 1: med_fit = population[(plen - 1) // 2].fitness else: med_fit = float(population[plen // 2 - 1].fitness + population[plen // 2].fitness) / 2 avg_fit = sum([p.fitness for p in population]) / float(plen) if plen > 1: std_fit = math.sqrt(sum([(p.fitness - avg_fit)**2 for p in population]) / float(plen - 1)) else: std_fit = 0 except TypeError: med_fit = float('nan') avg_fit = float('nan') std_fit = float('nan') return {'best': best_fit, 'worst': worst_fit, 'mean': avg_fit, 'median': med_fit, 'std': std_fit}
[ "def", "fitness_statistics", "(", "population", ")", ":", "population", ".", "sort", "(", "reverse", "=", "True", ")", "worst_fit", "=", "population", "[", "-", "1", "]", ".", "fitness", "best_fit", "=", "population", "[", "0", "]", ".", "fitness", "try", ":", "import", "numpy", "f", "=", "[", "p", ".", "fitness", "for", "p", "in", "population", "]", "med_fit", "=", "numpy", ".", "median", "(", "f", ")", "avg_fit", "=", "numpy", ".", "mean", "(", "f", ")", "std_fit", "=", "numpy", ".", "std", "(", "f", ")", "except", "ImportError", ":", "try", ":", "plen", "=", "len", "(", "population", ")", "if", "plen", "%", "2", "==", "1", ":", "med_fit", "=", "population", "[", "(", "plen", "-", "1", ")", "//", "2", "]", ".", "fitness", "else", ":", "med_fit", "=", "float", "(", "population", "[", "plen", "//", "2", "-", "1", "]", ".", "fitness", "+", "population", "[", "plen", "//", "2", "]", ".", "fitness", ")", "/", "2", "avg_fit", "=", "sum", "(", "[", "p", ".", "fitness", "for", "p", "in", "population", "]", ")", "/", "float", "(", "plen", ")", "if", "plen", ">", "1", ":", "std_fit", "=", "math", ".", "sqrt", "(", "sum", "(", "[", "(", "p", ".", "fitness", "-", "avg_fit", ")", "**", "2", "for", "p", "in", "population", "]", ")", "/", "float", "(", "plen", "-", "1", ")", ")", "else", ":", "std_fit", "=", "0", "except", "TypeError", ":", "med_fit", "=", "float", "(", "'nan'", ")", "avg_fit", "=", "float", "(", "'nan'", ")", "std_fit", "=", "float", "(", "'nan'", ")", "return", "{", "'best'", ":", "best_fit", ",", "'worst'", ":", "worst_fit", ",", "'mean'", ":", "avg_fit", ",", "'median'", ":", "med_fit", ",", "'std'", ":", "std_fit", "}" ]
Return the basic statistics of the population's fitness values. This function returns a dictionary containing the "best", "worst", "mean", "median", and "std" fitness values in the population. ("std" is the standard deviation.) A typical usage would be similar to the following:: stats = fitness_statistics(population) print(stats['best']) print(stats['worst']) print(stats['mean']) print(stats['median']) print(stats['std']) .. note:: This function makes use of the numpy library for calculations. If that library is not found, it attempts to complete the calculations internally. However, this second attempt will fail for multiobjective fitness values and will return ``nan`` for the mean, median, and standard deviation. Arguments: - *population* -- the population of individuals
[ "Return", "the", "basic", "statistics", "of", "the", "population", "s", "fitness", "values", ".", "This", "function", "returns", "a", "dictionary", "containing", "the", "best", "worst", "mean", "median", "and", "std", "fitness", "values", "in", "the", "population", ".", "(", "std", "is", "the", "standard", "deviation", ".", ")", "A", "typical", "usage", "would", "be", "similar", "to", "the", "following", "::", "stats", "=", "fitness_statistics", "(", "population", ")", "print", "(", "stats", "[", "best", "]", ")", "print", "(", "stats", "[", "worst", "]", ")", "print", "(", "stats", "[", "mean", "]", ")", "print", "(", "stats", "[", "median", "]", ")", "print", "(", "stats", "[", "std", "]", ")", "..", "note", "::", "This", "function", "makes", "use", "of", "the", "numpy", "library", "for", "calculations", ".", "If", "that", "library", "is", "not", "found", "it", "attempts", "to", "complete", "the", "calculations", "internally", ".", "However", "this", "second", "attempt", "will", "fail", "for", "multiobjective", "fitness", "values", "and", "will", "return", "nan", "for", "the", "mean", "median", "and", "standard", "deviation", ".", "Arguments", ":", "-", "*", "population", "*", "--", "the", "population", "of", "individuals" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/analysis.py#L34-L88
aarongarrett/inspyred
inspyred/ec/analysis.py
generation_plot
def generation_plot(file, errorbars=True): """Plot the results of the algorithm using generation statistics. This function creates a plot of the generation fitness statistics (best, worst, median, and average). This function requires the matplotlib library. .. note:: This function only works for single-objective problems. .. figure:: _static/generation_plot.png :alt: Example generation plot :align: center An example image saved from the ``generation_plot`` function (without error bars). Arguments: - *file* -- a file-like object representing the statistics file produced by the file_observer - *errorbars* -- Boolean value stating whether standard error bars should be drawn (default True) """ import matplotlib.pyplot as plt import matplotlib.font_manager generation = [] psize = [] worst = [] best = [] median = [] average = [] stdev = [] reader = csv.reader(file) for row in reader: generation.append(int(row[0])) psize.append(int(row[1])) worst.append(float(row[2])) best.append(float(row[3])) median.append(float(row[4])) average.append(float(row[5])) stdev.append(float(row[6])) stderr = [s / math.sqrt(p) for s, p in zip(stdev, psize)] data = [average, median, best, worst] colors = ['black', 'blue', 'green', 'red'] labels = ['average', 'median', 'best', 'worst'] figure = plt.figure() if errorbars: plt.errorbar(generation, average, stderr, color=colors[0], label=labels[0]) else: plt.plot(generation, average, color=colors[0], label=labels[0]) for d, col, lab in zip(data[1:], colors[1:], labels[1:]): plt.plot(generation, d, color=col, label=lab) plt.fill_between(generation, data[2], data[3], color='#e6f2e6') plt.grid(True) ymin = min([min(d) for d in data]) ymax = max([max(d) for d in data]) yrange = ymax - ymin plt.ylim((ymin - 0.1*yrange, ymax + 0.1*yrange)) prop = matplotlib.font_manager.FontProperties(size=8) plt.legend(loc='upper left', prop=prop) plt.xlabel('Generation') plt.ylabel('Fitness') plt.show()
python
def generation_plot(file, errorbars=True): import matplotlib.pyplot as plt import matplotlib.font_manager generation = [] psize = [] worst = [] best = [] median = [] average = [] stdev = [] reader = csv.reader(file) for row in reader: generation.append(int(row[0])) psize.append(int(row[1])) worst.append(float(row[2])) best.append(float(row[3])) median.append(float(row[4])) average.append(float(row[5])) stdev.append(float(row[6])) stderr = [s / math.sqrt(p) for s, p in zip(stdev, psize)] data = [average, median, best, worst] colors = ['black', 'blue', 'green', 'red'] labels = ['average', 'median', 'best', 'worst'] figure = plt.figure() if errorbars: plt.errorbar(generation, average, stderr, color=colors[0], label=labels[0]) else: plt.plot(generation, average, color=colors[0], label=labels[0]) for d, col, lab in zip(data[1:], colors[1:], labels[1:]): plt.plot(generation, d, color=col, label=lab) plt.fill_between(generation, data[2], data[3], color=' plt.grid(True) ymin = min([min(d) for d in data]) ymax = max([max(d) for d in data]) yrange = ymax - ymin plt.ylim((ymin - 0.1*yrange, ymax + 0.1*yrange)) prop = matplotlib.font_manager.FontProperties(size=8) plt.legend(loc='upper left', prop=prop) plt.xlabel('Generation') plt.ylabel('Fitness') plt.show()
[ "def", "generation_plot", "(", "file", ",", "errorbars", "=", "True", ")", ":", "import", "matplotlib", ".", "pyplot", "as", "plt", "import", "matplotlib", ".", "font_manager", "generation", "=", "[", "]", "psize", "=", "[", "]", "worst", "=", "[", "]", "best", "=", "[", "]", "median", "=", "[", "]", "average", "=", "[", "]", "stdev", "=", "[", "]", "reader", "=", "csv", ".", "reader", "(", "file", ")", "for", "row", "in", "reader", ":", "generation", ".", "append", "(", "int", "(", "row", "[", "0", "]", ")", ")", "psize", ".", "append", "(", "int", "(", "row", "[", "1", "]", ")", ")", "worst", ".", "append", "(", "float", "(", "row", "[", "2", "]", ")", ")", "best", ".", "append", "(", "float", "(", "row", "[", "3", "]", ")", ")", "median", ".", "append", "(", "float", "(", "row", "[", "4", "]", ")", ")", "average", ".", "append", "(", "float", "(", "row", "[", "5", "]", ")", ")", "stdev", ".", "append", "(", "float", "(", "row", "[", "6", "]", ")", ")", "stderr", "=", "[", "s", "/", "math", ".", "sqrt", "(", "p", ")", "for", "s", ",", "p", "in", "zip", "(", "stdev", ",", "psize", ")", "]", "data", "=", "[", "average", ",", "median", ",", "best", ",", "worst", "]", "colors", "=", "[", "'black'", ",", "'blue'", ",", "'green'", ",", "'red'", "]", "labels", "=", "[", "'average'", ",", "'median'", ",", "'best'", ",", "'worst'", "]", "figure", "=", "plt", ".", "figure", "(", ")", "if", "errorbars", ":", "plt", ".", "errorbar", "(", "generation", ",", "average", ",", "stderr", ",", "color", "=", "colors", "[", "0", "]", ",", "label", "=", "labels", "[", "0", "]", ")", "else", ":", "plt", ".", "plot", "(", "generation", ",", "average", ",", "color", "=", "colors", "[", "0", "]", ",", "label", "=", "labels", "[", "0", "]", ")", "for", "d", ",", "col", ",", "lab", "in", "zip", "(", "data", "[", "1", ":", "]", ",", "colors", "[", "1", ":", "]", ",", "labels", "[", "1", ":", "]", ")", ":", "plt", ".", "plot", "(", "generation", ",", "d", ",", "color", "=", "col", ",", "label", "=", "lab", ")", "plt", ".", "fill_between", "(", "generation", ",", "data", "[", "2", "]", ",", "data", "[", "3", "]", ",", "color", "=", "'#e6f2e6'", ")", "plt", ".", "grid", "(", "True", ")", "ymin", "=", "min", "(", "[", "min", "(", "d", ")", "for", "d", "in", "data", "]", ")", "ymax", "=", "max", "(", "[", "max", "(", "d", ")", "for", "d", "in", "data", "]", ")", "yrange", "=", "ymax", "-", "ymin", "plt", ".", "ylim", "(", "(", "ymin", "-", "0.1", "*", "yrange", ",", "ymax", "+", "0.1", "*", "yrange", ")", ")", "prop", "=", "matplotlib", ".", "font_manager", ".", "FontProperties", "(", "size", "=", "8", ")", "plt", ".", "legend", "(", "loc", "=", "'upper left'", ",", "prop", "=", "prop", ")", "plt", ".", "xlabel", "(", "'Generation'", ")", "plt", ".", "ylabel", "(", "'Fitness'", ")", "plt", ".", "show", "(", ")" ]
Plot the results of the algorithm using generation statistics. This function creates a plot of the generation fitness statistics (best, worst, median, and average). This function requires the matplotlib library. .. note:: This function only works for single-objective problems. .. figure:: _static/generation_plot.png :alt: Example generation plot :align: center An example image saved from the ``generation_plot`` function (without error bars). Arguments: - *file* -- a file-like object representing the statistics file produced by the file_observer - *errorbars* -- Boolean value stating whether standard error bars should be drawn (default True)
[ "Plot", "the", "results", "of", "the", "algorithm", "using", "generation", "statistics", ".", "This", "function", "creates", "a", "plot", "of", "the", "generation", "fitness", "statistics", "(", "best", "worst", "median", "and", "average", ")", ".", "This", "function", "requires", "the", "matplotlib", "library", ".", "..", "note", "::", "This", "function", "only", "works", "for", "single", "-", "objective", "problems", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/analysis.py#L91-L157
aarongarrett/inspyred
inspyred/ec/analysis.py
allele_plot
def allele_plot(file, normalize=False, alleles=None, generations=None): """Plot the alleles from each generation from the individuals file. This function creates a plot of the individual allele values as they change through the generations. It creates three subplots, one for each of the best, median, and average individual. The best and median individuals are chosen using the fitness data for each generation. The average individual, on the other hand, is actually an individual created by averaging the alleles within a generation. This function requires the matplotlib library. .. note:: This function only works for single-objective problems. .. figure:: _static/allele_plot.png :alt: Example allele plot :align: center An example image saved from the ``allele_plot`` function. Arguments: - *file* -- a file-like object representing the individuals file produced by the file_observer - *normalize* -- Boolean value stating whether allele values should be normalized before plotting (default False) - *alleles* -- a list of allele index values that should be plotted (default None) - *generations* -- a list of generation numbers that should be plotted (default None) If *alleles* is ``None``, then all alleles are plotted. Similarly, if *generations* is ``None``, then all generations are plotted. """ import matplotlib.pyplot as plt generation_data = [] reader = csv.reader(open(file)) for row in reader: g = int(row[0]) row[3] = row[3].replace('[', '') row[-1] = row[-1].replace(']', '') individual = [float(r) for r in row[3:]] individual.append(float(row[2])) try: generation_data[g] except IndexError: generation_data.append([]) generation_data[g].append(individual) for gen in generation_data: gen.sort(key=lambda x: x[-1]) for j, g in enumerate(gen): gen[j] = g[:-1] best = [] median = [] average = [] for gen in generation_data: best.append(gen[0]) plen = len(gen) if plen % 2 == 1: med = gen[(plen - 1) // 2] else: med = [] for a, b in zip(gen[plen // 2 - 1], gen[plen // 2]): med.append(float(a + b) / 2) median.append(med) avg = [0] * len(gen[0]) for individual in gen: for i, allele in enumerate(individual): avg[i] += allele for i, a in enumerate(avg): avg[i] /= float(len(gen)) average.append(avg) for plot_num, (data, title) in enumerate(zip([best, median, average], ["Best", "Median", "Average"])): if alleles is None: alleles = list(range(len(data[0]))) if generations is None: generations = list(range(len(data))) if normalize: columns = list(zip(*data)) max_col = [max(c) for c in columns] min_col = [min(c) for c in columns] for dat in data: for i, d in enumerate(dat): dat[i] = (d - min_col[i]) / float(max_col[i] - min_col[i]) plot_data = [] for g in generations: plot_data.append([data[g][a] for a in alleles]) sub = plt.subplot(3, 1, plot_num + 1) plt.pcolor(plt.array(plot_data)) plt.colorbar() step_size = max(len(generations) // 7, 1) ytick_locs = list(range(step_size, len(generations), step_size)) ytick_labs = generations[step_size::step_size] plt.yticks(ytick_locs, ytick_labs) plt.ylabel('Generation') if plot_num == 2: xtick_locs = list(range(len(alleles))) xtick_labs = alleles plt.xticks(xtick_locs, xtick_labs) plt.xlabel('Allele') else: plt.setp(sub.get_xticklabels(), visible=False) plt.title(title) plt.show()
python
def allele_plot(file, normalize=False, alleles=None, generations=None): import matplotlib.pyplot as plt generation_data = [] reader = csv.reader(open(file)) for row in reader: g = int(row[0]) row[3] = row[3].replace('[', '') row[-1] = row[-1].replace(']', '') individual = [float(r) for r in row[3:]] individual.append(float(row[2])) try: generation_data[g] except IndexError: generation_data.append([]) generation_data[g].append(individual) for gen in generation_data: gen.sort(key=lambda x: x[-1]) for j, g in enumerate(gen): gen[j] = g[:-1] best = [] median = [] average = [] for gen in generation_data: best.append(gen[0]) plen = len(gen) if plen % 2 == 1: med = gen[(plen - 1) // 2] else: med = [] for a, b in zip(gen[plen // 2 - 1], gen[plen // 2]): med.append(float(a + b) / 2) median.append(med) avg = [0] * len(gen[0]) for individual in gen: for i, allele in enumerate(individual): avg[i] += allele for i, a in enumerate(avg): avg[i] /= float(len(gen)) average.append(avg) for plot_num, (data, title) in enumerate(zip([best, median, average], ["Best", "Median", "Average"])): if alleles is None: alleles = list(range(len(data[0]))) if generations is None: generations = list(range(len(data))) if normalize: columns = list(zip(*data)) max_col = [max(c) for c in columns] min_col = [min(c) for c in columns] for dat in data: for i, d in enumerate(dat): dat[i] = (d - min_col[i]) / float(max_col[i] - min_col[i]) plot_data = [] for g in generations: plot_data.append([data[g][a] for a in alleles]) sub = plt.subplot(3, 1, plot_num + 1) plt.pcolor(plt.array(plot_data)) plt.colorbar() step_size = max(len(generations) // 7, 1) ytick_locs = list(range(step_size, len(generations), step_size)) ytick_labs = generations[step_size::step_size] plt.yticks(ytick_locs, ytick_labs) plt.ylabel('Generation') if plot_num == 2: xtick_locs = list(range(len(alleles))) xtick_labs = alleles plt.xticks(xtick_locs, xtick_labs) plt.xlabel('Allele') else: plt.setp(sub.get_xticklabels(), visible=False) plt.title(title) plt.show()
[ "def", "allele_plot", "(", "file", ",", "normalize", "=", "False", ",", "alleles", "=", "None", ",", "generations", "=", "None", ")", ":", "import", "matplotlib", ".", "pyplot", "as", "plt", "generation_data", "=", "[", "]", "reader", "=", "csv", ".", "reader", "(", "open", "(", "file", ")", ")", "for", "row", "in", "reader", ":", "g", "=", "int", "(", "row", "[", "0", "]", ")", "row", "[", "3", "]", "=", "row", "[", "3", "]", ".", "replace", "(", "'['", ",", "''", ")", "row", "[", "-", "1", "]", "=", "row", "[", "-", "1", "]", ".", "replace", "(", "']'", ",", "''", ")", "individual", "=", "[", "float", "(", "r", ")", "for", "r", "in", "row", "[", "3", ":", "]", "]", "individual", ".", "append", "(", "float", "(", "row", "[", "2", "]", ")", ")", "try", ":", "generation_data", "[", "g", "]", "except", "IndexError", ":", "generation_data", ".", "append", "(", "[", "]", ")", "generation_data", "[", "g", "]", ".", "append", "(", "individual", ")", "for", "gen", "in", "generation_data", ":", "gen", ".", "sort", "(", "key", "=", "lambda", "x", ":", "x", "[", "-", "1", "]", ")", "for", "j", ",", "g", "in", "enumerate", "(", "gen", ")", ":", "gen", "[", "j", "]", "=", "g", "[", ":", "-", "1", "]", "best", "=", "[", "]", "median", "=", "[", "]", "average", "=", "[", "]", "for", "gen", "in", "generation_data", ":", "best", ".", "append", "(", "gen", "[", "0", "]", ")", "plen", "=", "len", "(", "gen", ")", "if", "plen", "%", "2", "==", "1", ":", "med", "=", "gen", "[", "(", "plen", "-", "1", ")", "//", "2", "]", "else", ":", "med", "=", "[", "]", "for", "a", ",", "b", "in", "zip", "(", "gen", "[", "plen", "//", "2", "-", "1", "]", ",", "gen", "[", "plen", "//", "2", "]", ")", ":", "med", ".", "append", "(", "float", "(", "a", "+", "b", ")", "/", "2", ")", "median", ".", "append", "(", "med", ")", "avg", "=", "[", "0", "]", "*", "len", "(", "gen", "[", "0", "]", ")", "for", "individual", "in", "gen", ":", "for", "i", ",", "allele", "in", "enumerate", "(", "individual", ")", ":", "avg", "[", "i", "]", "+=", "allele", "for", "i", ",", "a", "in", "enumerate", "(", "avg", ")", ":", "avg", "[", "i", "]", "/=", "float", "(", "len", "(", "gen", ")", ")", "average", ".", "append", "(", "avg", ")", "for", "plot_num", ",", "(", "data", ",", "title", ")", "in", "enumerate", "(", "zip", "(", "[", "best", ",", "median", ",", "average", "]", ",", "[", "\"Best\"", ",", "\"Median\"", ",", "\"Average\"", "]", ")", ")", ":", "if", "alleles", "is", "None", ":", "alleles", "=", "list", "(", "range", "(", "len", "(", "data", "[", "0", "]", ")", ")", ")", "if", "generations", "is", "None", ":", "generations", "=", "list", "(", "range", "(", "len", "(", "data", ")", ")", ")", "if", "normalize", ":", "columns", "=", "list", "(", "zip", "(", "*", "data", ")", ")", "max_col", "=", "[", "max", "(", "c", ")", "for", "c", "in", "columns", "]", "min_col", "=", "[", "min", "(", "c", ")", "for", "c", "in", "columns", "]", "for", "dat", "in", "data", ":", "for", "i", ",", "d", "in", "enumerate", "(", "dat", ")", ":", "dat", "[", "i", "]", "=", "(", "d", "-", "min_col", "[", "i", "]", ")", "/", "float", "(", "max_col", "[", "i", "]", "-", "min_col", "[", "i", "]", ")", "plot_data", "=", "[", "]", "for", "g", "in", "generations", ":", "plot_data", ".", "append", "(", "[", "data", "[", "g", "]", "[", "a", "]", "for", "a", "in", "alleles", "]", ")", "sub", "=", "plt", ".", "subplot", "(", "3", ",", "1", ",", "plot_num", "+", "1", ")", "plt", ".", "pcolor", "(", "plt", ".", "array", "(", "plot_data", ")", ")", "plt", ".", "colorbar", "(", ")", "step_size", "=", "max", "(", "len", "(", "generations", ")", "//", "7", ",", "1", ")", "ytick_locs", "=", "list", "(", "range", "(", "step_size", ",", "len", "(", "generations", ")", ",", "step_size", ")", ")", "ytick_labs", "=", "generations", "[", "step_size", ":", ":", "step_size", "]", "plt", ".", "yticks", "(", "ytick_locs", ",", "ytick_labs", ")", "plt", ".", "ylabel", "(", "'Generation'", ")", "if", "plot_num", "==", "2", ":", "xtick_locs", "=", "list", "(", "range", "(", "len", "(", "alleles", ")", ")", ")", "xtick_labs", "=", "alleles", "plt", ".", "xticks", "(", "xtick_locs", ",", "xtick_labs", ")", "plt", ".", "xlabel", "(", "'Allele'", ")", "else", ":", "plt", ".", "setp", "(", "sub", ".", "get_xticklabels", "(", ")", ",", "visible", "=", "False", ")", "plt", ".", "title", "(", "title", ")", "plt", ".", "show", "(", ")" ]
Plot the alleles from each generation from the individuals file. This function creates a plot of the individual allele values as they change through the generations. It creates three subplots, one for each of the best, median, and average individual. The best and median individuals are chosen using the fitness data for each generation. The average individual, on the other hand, is actually an individual created by averaging the alleles within a generation. This function requires the matplotlib library. .. note:: This function only works for single-objective problems. .. figure:: _static/allele_plot.png :alt: Example allele plot :align: center An example image saved from the ``allele_plot`` function. Arguments: - *file* -- a file-like object representing the individuals file produced by the file_observer - *normalize* -- Boolean value stating whether allele values should be normalized before plotting (default False) - *alleles* -- a list of allele index values that should be plotted (default None) - *generations* -- a list of generation numbers that should be plotted (default None) If *alleles* is ``None``, then all alleles are plotted. Similarly, if *generations* is ``None``, then all generations are plotted.
[ "Plot", "the", "alleles", "from", "each", "generation", "from", "the", "individuals", "file", ".", "This", "function", "creates", "a", "plot", "of", "the", "individual", "allele", "values", "as", "they", "change", "through", "the", "generations", ".", "It", "creates", "three", "subplots", "one", "for", "each", "of", "the", "best", "median", "and", "average", "individual", ".", "The", "best", "and", "median", "individuals", "are", "chosen", "using", "the", "fitness", "data", "for", "each", "generation", ".", "The", "average", "individual", "on", "the", "other", "hand", "is", "actually", "an", "individual", "created", "by", "averaging", "the", "alleles", "within", "a", "generation", ".", "This", "function", "requires", "the", "matplotlib", "library", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/analysis.py#L160-L269
aarongarrett/inspyred
inspyred/ec/analysis.py
hypervolume
def hypervolume(pareto_set, reference_point=None): """Calculates the hypervolume by slicing objectives (HSO). This function calculates the hypervolume (or S-measure) of a nondominated set using the Hypervolume by Slicing Objectives (HSO) procedure of `While, et al. (IEEE CEC 2005) <http://www.lania.mx/~ccoello/EMOO/while05a.pdf.gz>`_. The *pareto_set* should be a list of lists of objective values. The *reference_point* may be specified or it may be left as the default value of None. In that case, the reference point is calculated to be the maximum value in the set for all objectives (the ideal point). This function assumes that objectives are to be maximized. Arguments: - *pareto_set* -- the list or lists of objective values comprising the Pareto front - *reference_point* -- the reference point to be used (default None) """ def dominates(p, q, k=None): if k is None: k = len(p) d = True while d and k < len(p): d = not (q[k] > p[k]) k += 1 return d def insert(p, k, pl): ql = [] while pl and pl[0][k] > p[k]: ql.append(pl[0]) pl = pl[1:] ql.append(p) while pl: if not dominates(p, pl[0], k): ql.append(pl[0]) pl = pl[1:] return ql def slice(pl, k, ref): p = pl[0] pl = pl[1:] ql = [] s = [] while pl: ql = insert(p, k + 1, ql) p_prime = pl[0] s.append((math.fabs(p[k] - p_prime[k]), ql)) p = p_prime pl = pl[1:] ql = insert(p, k + 1, ql) s.append((math.fabs(p[k] - ref[k]), ql)) return s ps = pareto_set ref = reference_point n = min([len(p) for p in ps]) if ref is None: ref = [max(ps, key=lambda x: x[o])[o] for o in range(n)] pl = ps[:] pl.sort(key=lambda x: x[0], reverse=True) s = [(1, pl)] for k in range(n - 1): s_prime = [] for x, ql in s: for x_prime, ql_prime in slice(ql, k, ref): s_prime.append((x * x_prime, ql_prime)) s = s_prime vol = 0 for x, ql in s: vol = vol + x * math.fabs(ql[0][n - 1] - ref[n - 1]) return vol
python
def hypervolume(pareto_set, reference_point=None): def dominates(p, q, k=None): if k is None: k = len(p) d = True while d and k < len(p): d = not (q[k] > p[k]) k += 1 return d def insert(p, k, pl): ql = [] while pl and pl[0][k] > p[k]: ql.append(pl[0]) pl = pl[1:] ql.append(p) while pl: if not dominates(p, pl[0], k): ql.append(pl[0]) pl = pl[1:] return ql def slice(pl, k, ref): p = pl[0] pl = pl[1:] ql = [] s = [] while pl: ql = insert(p, k + 1, ql) p_prime = pl[0] s.append((math.fabs(p[k] - p_prime[k]), ql)) p = p_prime pl = pl[1:] ql = insert(p, k + 1, ql) s.append((math.fabs(p[k] - ref[k]), ql)) return s ps = pareto_set ref = reference_point n = min([len(p) for p in ps]) if ref is None: ref = [max(ps, key=lambda x: x[o])[o] for o in range(n)] pl = ps[:] pl.sort(key=lambda x: x[0], reverse=True) s = [(1, pl)] for k in range(n - 1): s_prime = [] for x, ql in s: for x_prime, ql_prime in slice(ql, k, ref): s_prime.append((x * x_prime, ql_prime)) s = s_prime vol = 0 for x, ql in s: vol = vol + x * math.fabs(ql[0][n - 1] - ref[n - 1]) return vol
[ "def", "hypervolume", "(", "pareto_set", ",", "reference_point", "=", "None", ")", ":", "def", "dominates", "(", "p", ",", "q", ",", "k", "=", "None", ")", ":", "if", "k", "is", "None", ":", "k", "=", "len", "(", "p", ")", "d", "=", "True", "while", "d", "and", "k", "<", "len", "(", "p", ")", ":", "d", "=", "not", "(", "q", "[", "k", "]", ">", "p", "[", "k", "]", ")", "k", "+=", "1", "return", "d", "def", "insert", "(", "p", ",", "k", ",", "pl", ")", ":", "ql", "=", "[", "]", "while", "pl", "and", "pl", "[", "0", "]", "[", "k", "]", ">", "p", "[", "k", "]", ":", "ql", ".", "append", "(", "pl", "[", "0", "]", ")", "pl", "=", "pl", "[", "1", ":", "]", "ql", ".", "append", "(", "p", ")", "while", "pl", ":", "if", "not", "dominates", "(", "p", ",", "pl", "[", "0", "]", ",", "k", ")", ":", "ql", ".", "append", "(", "pl", "[", "0", "]", ")", "pl", "=", "pl", "[", "1", ":", "]", "return", "ql", "def", "slice", "(", "pl", ",", "k", ",", "ref", ")", ":", "p", "=", "pl", "[", "0", "]", "pl", "=", "pl", "[", "1", ":", "]", "ql", "=", "[", "]", "s", "=", "[", "]", "while", "pl", ":", "ql", "=", "insert", "(", "p", ",", "k", "+", "1", ",", "ql", ")", "p_prime", "=", "pl", "[", "0", "]", "s", ".", "append", "(", "(", "math", ".", "fabs", "(", "p", "[", "k", "]", "-", "p_prime", "[", "k", "]", ")", ",", "ql", ")", ")", "p", "=", "p_prime", "pl", "=", "pl", "[", "1", ":", "]", "ql", "=", "insert", "(", "p", ",", "k", "+", "1", ",", "ql", ")", "s", ".", "append", "(", "(", "math", ".", "fabs", "(", "p", "[", "k", "]", "-", "ref", "[", "k", "]", ")", ",", "ql", ")", ")", "return", "s", "ps", "=", "pareto_set", "ref", "=", "reference_point", "n", "=", "min", "(", "[", "len", "(", "p", ")", "for", "p", "in", "ps", "]", ")", "if", "ref", "is", "None", ":", "ref", "=", "[", "max", "(", "ps", ",", "key", "=", "lambda", "x", ":", "x", "[", "o", "]", ")", "[", "o", "]", "for", "o", "in", "range", "(", "n", ")", "]", "pl", "=", "ps", "[", ":", "]", "pl", ".", "sort", "(", "key", "=", "lambda", "x", ":", "x", "[", "0", "]", ",", "reverse", "=", "True", ")", "s", "=", "[", "(", "1", ",", "pl", ")", "]", "for", "k", "in", "range", "(", "n", "-", "1", ")", ":", "s_prime", "=", "[", "]", "for", "x", ",", "ql", "in", "s", ":", "for", "x_prime", ",", "ql_prime", "in", "slice", "(", "ql", ",", "k", ",", "ref", ")", ":", "s_prime", ".", "append", "(", "(", "x", "*", "x_prime", ",", "ql_prime", ")", ")", "s", "=", "s_prime", "vol", "=", "0", "for", "x", ",", "ql", "in", "s", ":", "vol", "=", "vol", "+", "x", "*", "math", ".", "fabs", "(", "ql", "[", "0", "]", "[", "n", "-", "1", "]", "-", "ref", "[", "n", "-", "1", "]", ")", "return", "vol" ]
Calculates the hypervolume by slicing objectives (HSO). This function calculates the hypervolume (or S-measure) of a nondominated set using the Hypervolume by Slicing Objectives (HSO) procedure of `While, et al. (IEEE CEC 2005) <http://www.lania.mx/~ccoello/EMOO/while05a.pdf.gz>`_. The *pareto_set* should be a list of lists of objective values. The *reference_point* may be specified or it may be left as the default value of None. In that case, the reference point is calculated to be the maximum value in the set for all objectives (the ideal point). This function assumes that objectives are to be maximized. Arguments: - *pareto_set* -- the list or lists of objective values comprising the Pareto front - *reference_point* -- the reference point to be used (default None)
[ "Calculates", "the", "hypervolume", "by", "slicing", "objectives", "(", "HSO", ")", ".", "This", "function", "calculates", "the", "hypervolume", "(", "or", "S", "-", "measure", ")", "of", "a", "nondominated", "set", "using", "the", "Hypervolume", "by", "Slicing", "Objectives", "(", "HSO", ")", "procedure", "of", "While", "et", "al", ".", "(", "IEEE", "CEC", "2005", ")", "<http", ":", "//", "www", ".", "lania", ".", "mx", "/", "~ccoello", "/", "EMOO", "/", "while05a", ".", "pdf", ".", "gz", ">", "_", ".", "The", "*", "pareto_set", "*", "should", "be", "a", "list", "of", "lists", "of", "objective", "values", ".", "The", "*", "reference_point", "*", "may", "be", "specified", "or", "it", "may", "be", "left", "as", "the", "default", "value", "of", "None", ".", "In", "that", "case", "the", "reference", "point", "is", "calculated", "to", "be", "the", "maximum", "value", "in", "the", "set", "for", "all", "objectives", "(", "the", "ideal", "point", ")", ".", "This", "function", "assumes", "that", "objectives", "are", "to", "be", "maximized", ".", "Arguments", ":", "-", "*", "pareto_set", "*", "--", "the", "list", "or", "lists", "of", "objective", "values", "comprising", "the", "Pareto", "front", "-", "*", "reference_point", "*", "--", "the", "reference", "point", "to", "be", "used", "(", "default", "None", ")" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/analysis.py#L272-L343
aarongarrett/inspyred
docs/moonshot.py
gravitational_force
def gravitational_force(position_a, mass_a, position_b, mass_b): """Returns the gravitational force between the two bodies a and b.""" distance = distance_between(position_a, position_b) # Calculate the direction and magnitude of the force. angle = math.atan2(position_a[1] - position_b[1], position_a[0] - position_b[0]) magnitude = G * mass_a * mass_b / (distance**2) # Find the x and y components of the force. # Determine sign based on which one is the larger body. sign = -1 if mass_b > mass_a else 1 x_force = sign * magnitude * math.cos(angle) y_force = sign * magnitude * math.sin(angle) return x_force, y_force
python
def gravitational_force(position_a, mass_a, position_b, mass_b): distance = distance_between(position_a, position_b) angle = math.atan2(position_a[1] - position_b[1], position_a[0] - position_b[0]) magnitude = G * mass_a * mass_b / (distance**2) sign = -1 if mass_b > mass_a else 1 x_force = sign * magnitude * math.cos(angle) y_force = sign * magnitude * math.sin(angle) return x_force, y_force
[ "def", "gravitational_force", "(", "position_a", ",", "mass_a", ",", "position_b", ",", "mass_b", ")", ":", "distance", "=", "distance_between", "(", "position_a", ",", "position_b", ")", "# Calculate the direction and magnitude of the force.", "angle", "=", "math", ".", "atan2", "(", "position_a", "[", "1", "]", "-", "position_b", "[", "1", "]", ",", "position_a", "[", "0", "]", "-", "position_b", "[", "0", "]", ")", "magnitude", "=", "G", "*", "mass_a", "*", "mass_b", "/", "(", "distance", "**", "2", ")", "# Find the x and y components of the force.", "# Determine sign based on which one is the larger body.", "sign", "=", "-", "1", "if", "mass_b", ">", "mass_a", "else", "1", "x_force", "=", "sign", "*", "magnitude", "*", "math", ".", "cos", "(", "angle", ")", "y_force", "=", "sign", "*", "magnitude", "*", "math", ".", "sin", "(", "angle", ")", "return", "x_force", ",", "y_force" ]
Returns the gravitational force between the two bodies a and b.
[ "Returns", "the", "gravitational", "force", "between", "the", "two", "bodies", "a", "and", "b", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/docs/moonshot.py#L37-L50
aarongarrett/inspyred
docs/moonshot.py
force_on_satellite
def force_on_satellite(position, mass): """Returns the total gravitational force acting on the body from the Earth and Moon.""" earth_grav_force = gravitational_force(position, mass, earth_position, earth_mass) moon_grav_force = gravitational_force(position, mass, moon_position, moon_mass) F_x = earth_grav_force[0] + moon_grav_force[0] F_y = earth_grav_force[1] + moon_grav_force[1] return F_x, F_y
python
def force_on_satellite(position, mass): earth_grav_force = gravitational_force(position, mass, earth_position, earth_mass) moon_grav_force = gravitational_force(position, mass, moon_position, moon_mass) F_x = earth_grav_force[0] + moon_grav_force[0] F_y = earth_grav_force[1] + moon_grav_force[1] return F_x, F_y
[ "def", "force_on_satellite", "(", "position", ",", "mass", ")", ":", "earth_grav_force", "=", "gravitational_force", "(", "position", ",", "mass", ",", "earth_position", ",", "earth_mass", ")", "moon_grav_force", "=", "gravitational_force", "(", "position", ",", "mass", ",", "moon_position", ",", "moon_mass", ")", "F_x", "=", "earth_grav_force", "[", "0", "]", "+", "moon_grav_force", "[", "0", "]", "F_y", "=", "earth_grav_force", "[", "1", "]", "+", "moon_grav_force", "[", "1", "]", "return", "F_x", ",", "F_y" ]
Returns the total gravitational force acting on the body from the Earth and Moon.
[ "Returns", "the", "total", "gravitational", "force", "acting", "on", "the", "body", "from", "the", "Earth", "and", "Moon", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/docs/moonshot.py#L52-L58
aarongarrett/inspyred
docs/moonshot.py
acceleration_of_satellite
def acceleration_of_satellite(position, mass): """Returns the acceleration based on all forces acting upon the body.""" F_x, F_y = force_on_satellite(position, mass) return F_x / mass, F_y / mass
python
def acceleration_of_satellite(position, mass): F_x, F_y = force_on_satellite(position, mass) return F_x / mass, F_y / mass
[ "def", "acceleration_of_satellite", "(", "position", ",", "mass", ")", ":", "F_x", ",", "F_y", "=", "force_on_satellite", "(", "position", ",", "mass", ")", "return", "F_x", "/", "mass", ",", "F_y", "/", "mass" ]
Returns the acceleration based on all forces acting upon the body.
[ "Returns", "the", "acceleration", "based", "on", "all", "forces", "acting", "upon", "the", "body", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/docs/moonshot.py#L60-L63
aarongarrett/inspyred
inspyred/ec/replacers.py
truncation_replacement
def truncation_replacement(random, population, parents, offspring, args): """Replaces population with the best of the population and offspring. This function performs truncation replacement, which means that the entire existing population is replaced by the best from among the current population and offspring, keeping the existing population size fixed. This is similar to so-called "plus" replacement in the evolution strategies literature, except that "plus" replacement considers only parents and offspring for survival. However, if the entire population are parents (which is often the case in evolution strategies), then truncation replacement and plus-replacement are equivalent approaches. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments """ psize = len(population) population.extend(list(offspring)) population.sort(reverse=True) return population[:psize]
python
def truncation_replacement(random, population, parents, offspring, args): psize = len(population) population.extend(list(offspring)) population.sort(reverse=True) return population[:psize]
[ "def", "truncation_replacement", "(", "random", ",", "population", ",", "parents", ",", "offspring", ",", "args", ")", ":", "psize", "=", "len", "(", "population", ")", "population", ".", "extend", "(", "list", "(", "offspring", ")", ")", "population", ".", "sort", "(", "reverse", "=", "True", ")", "return", "population", "[", ":", "psize", "]" ]
Replaces population with the best of the population and offspring. This function performs truncation replacement, which means that the entire existing population is replaced by the best from among the current population and offspring, keeping the existing population size fixed. This is similar to so-called "plus" replacement in the evolution strategies literature, except that "plus" replacement considers only parents and offspring for survival. However, if the entire population are parents (which is often the case in evolution strategies), then truncation replacement and plus-replacement are equivalent approaches. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments
[ "Replaces", "population", "with", "the", "best", "of", "the", "population", "and", "offspring", ".", "This", "function", "performs", "truncation", "replacement", "which", "means", "that", "the", "entire", "existing", "population", "is", "replaced", "by", "the", "best", "from", "among", "the", "current", "population", "and", "offspring", "keeping", "the", "existing", "population", "size", "fixed", ".", "This", "is", "similar", "to", "so", "-", "called", "plus", "replacement", "in", "the", "evolution", "strategies", "literature", "except", "that", "plus", "replacement", "considers", "only", "parents", "and", "offspring", "for", "survival", ".", "However", "if", "the", "entire", "population", "are", "parents", "(", "which", "is", "often", "the", "case", "in", "evolution", "strategies", ")", "then", "truncation", "replacement", "and", "plus", "-", "replacement", "are", "equivalent", "approaches", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "population", "--", "the", "population", "of", "individuals", "parents", "--", "the", "list", "of", "parent", "individuals", "offspring", "--", "the", "list", "of", "offspring", "individuals", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/replacers.py#L58-L82
aarongarrett/inspyred
inspyred/ec/replacers.py
steady_state_replacement
def steady_state_replacement(random, population, parents, offspring, args): """Performs steady-state replacement for the offspring. This function performs steady-state replacement, which means that the offspring replace the least fit individuals in the existing population, even if those offspring are less fit than the individuals that they replace. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments """ population.sort() num_to_replace = min(len(offspring), len(population)) population[:num_to_replace] = offspring[:num_to_replace] return population
python
def steady_state_replacement(random, population, parents, offspring, args): population.sort() num_to_replace = min(len(offspring), len(population)) population[:num_to_replace] = offspring[:num_to_replace] return population
[ "def", "steady_state_replacement", "(", "random", ",", "population", ",", "parents", ",", "offspring", ",", "args", ")", ":", "population", ".", "sort", "(", ")", "num_to_replace", "=", "min", "(", "len", "(", "offspring", ")", ",", "len", "(", "population", ")", ")", "population", "[", ":", "num_to_replace", "]", "=", "offspring", "[", ":", "num_to_replace", "]", "return", "population" ]
Performs steady-state replacement for the offspring. This function performs steady-state replacement, which means that the offspring replace the least fit individuals in the existing population, even if those offspring are less fit than the individuals that they replace. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments
[ "Performs", "steady", "-", "state", "replacement", "for", "the", "offspring", ".", "This", "function", "performs", "steady", "-", "state", "replacement", "which", "means", "that", "the", "offspring", "replace", "the", "least", "fit", "individuals", "in", "the", "existing", "population", "even", "if", "those", "offspring", "are", "less", "fit", "than", "the", "individuals", "that", "they", "replace", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "population", "--", "the", "population", "of", "individuals", "parents", "--", "the", "list", "of", "parent", "individuals", "offspring", "--", "the", "list", "of", "offspring", "individuals", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/replacers.py#L85-L104
aarongarrett/inspyred
inspyred/ec/replacers.py
generational_replacement
def generational_replacement(random, population, parents, offspring, args): """Performs generational replacement with optional weak elitism. This function performs generational replacement, which means that the entire existing population is replaced by the offspring, truncating to the population size if the number of offspring is larger. Weak elitism may also be specified through the `num_elites` keyword argument in args. If this is used, the best `num_elites` individuals in the current population are allowed to survive if they are better than the worst `num_elites` offspring. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments Optional keyword arguments in args: - *num_elites* -- number of elites to consider (default 0) """ num_elites = args.setdefault('num_elites', 0) population.sort(reverse=True) offspring.extend(population[:num_elites]) offspring.sort(reverse=True) survivors = offspring[:len(population)] return survivors
python
def generational_replacement(random, population, parents, offspring, args): num_elites = args.setdefault('num_elites', 0) population.sort(reverse=True) offspring.extend(population[:num_elites]) offspring.sort(reverse=True) survivors = offspring[:len(population)] return survivors
[ "def", "generational_replacement", "(", "random", ",", "population", ",", "parents", ",", "offspring", ",", "args", ")", ":", "num_elites", "=", "args", ".", "setdefault", "(", "'num_elites'", ",", "0", ")", "population", ".", "sort", "(", "reverse", "=", "True", ")", "offspring", ".", "extend", "(", "population", "[", ":", "num_elites", "]", ")", "offspring", ".", "sort", "(", "reverse", "=", "True", ")", "survivors", "=", "offspring", "[", ":", "len", "(", "population", ")", "]", "return", "survivors" ]
Performs generational replacement with optional weak elitism. This function performs generational replacement, which means that the entire existing population is replaced by the offspring, truncating to the population size if the number of offspring is larger. Weak elitism may also be specified through the `num_elites` keyword argument in args. If this is used, the best `num_elites` individuals in the current population are allowed to survive if they are better than the worst `num_elites` offspring. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments Optional keyword arguments in args: - *num_elites* -- number of elites to consider (default 0)
[ "Performs", "generational", "replacement", "with", "optional", "weak", "elitism", ".", "This", "function", "performs", "generational", "replacement", "which", "means", "that", "the", "entire", "existing", "population", "is", "replaced", "by", "the", "offspring", "truncating", "to", "the", "population", "size", "if", "the", "number", "of", "offspring", "is", "larger", ".", "Weak", "elitism", "may", "also", "be", "specified", "through", "the", "num_elites", "keyword", "argument", "in", "args", ".", "If", "this", "is", "used", "the", "best", "num_elites", "individuals", "in", "the", "current", "population", "are", "allowed", "to", "survive", "if", "they", "are", "better", "than", "the", "worst", "num_elites", "offspring", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "population", "--", "the", "population", "of", "individuals", "parents", "--", "the", "list", "of", "parent", "individuals", "offspring", "--", "the", "list", "of", "offspring", "individuals", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/replacers.py#L107-L135
aarongarrett/inspyred
inspyred/ec/replacers.py
random_replacement
def random_replacement(random, population, parents, offspring, args): """Performs random replacement with optional weak elitism. This function performs random replacement, which means that the offspring replace random members of the population, keeping the population size constant. Weak elitism may also be specified through the `num_elites` keyword argument in args. If this is used, the best `num_elites` individuals in the current population are allowed to survive if they are better than the worst `num_elites` offspring. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments Optional keyword arguments in args: - *num_elites* -- number of elites to consider (default 0) """ num_elites = args.setdefault('num_elites', 0) population.sort(reverse=True) num_to_replace = min(len(offspring), len(population) - num_elites) valid_indices = range(num_elites, len(population)) rep_index = random.sample(valid_indices, num_to_replace) for i, repind in enumerate(rep_index): population[repind] = offspring[i] return population
python
def random_replacement(random, population, parents, offspring, args): num_elites = args.setdefault('num_elites', 0) population.sort(reverse=True) num_to_replace = min(len(offspring), len(population) - num_elites) valid_indices = range(num_elites, len(population)) rep_index = random.sample(valid_indices, num_to_replace) for i, repind in enumerate(rep_index): population[repind] = offspring[i] return population
[ "def", "random_replacement", "(", "random", ",", "population", ",", "parents", ",", "offspring", ",", "args", ")", ":", "num_elites", "=", "args", ".", "setdefault", "(", "'num_elites'", ",", "0", ")", "population", ".", "sort", "(", "reverse", "=", "True", ")", "num_to_replace", "=", "min", "(", "len", "(", "offspring", ")", ",", "len", "(", "population", ")", "-", "num_elites", ")", "valid_indices", "=", "range", "(", "num_elites", ",", "len", "(", "population", ")", ")", "rep_index", "=", "random", ".", "sample", "(", "valid_indices", ",", "num_to_replace", ")", "for", "i", ",", "repind", "in", "enumerate", "(", "rep_index", ")", ":", "population", "[", "repind", "]", "=", "offspring", "[", "i", "]", "return", "population" ]
Performs random replacement with optional weak elitism. This function performs random replacement, which means that the offspring replace random members of the population, keeping the population size constant. Weak elitism may also be specified through the `num_elites` keyword argument in args. If this is used, the best `num_elites` individuals in the current population are allowed to survive if they are better than the worst `num_elites` offspring. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments Optional keyword arguments in args: - *num_elites* -- number of elites to consider (default 0)
[ "Performs", "random", "replacement", "with", "optional", "weak", "elitism", ".", "This", "function", "performs", "random", "replacement", "which", "means", "that", "the", "offspring", "replace", "random", "members", "of", "the", "population", "keeping", "the", "population", "size", "constant", ".", "Weak", "elitism", "may", "also", "be", "specified", "through", "the", "num_elites", "keyword", "argument", "in", "args", ".", "If", "this", "is", "used", "the", "best", "num_elites", "individuals", "in", "the", "current", "population", "are", "allowed", "to", "survive", "if", "they", "are", "better", "than", "the", "worst", "num_elites", "offspring", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "population", "--", "the", "population", "of", "individuals", "parents", "--", "the", "list", "of", "parent", "individuals", "offspring", "--", "the", "list", "of", "offspring", "individuals", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/replacers.py#L138-L168
aarongarrett/inspyred
inspyred/ec/replacers.py
plus_replacement
def plus_replacement(random, population, parents, offspring, args): """Performs "plus" replacement. This function performs "plus" replacement, which means that the entire existing population is replaced by the best population-many elements from the combined set of parents and offspring. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments """ pool = list(offspring) pool.extend(parents) pool.sort(reverse=True) survivors = pool[:len(population)] return survivors
python
def plus_replacement(random, population, parents, offspring, args): pool = list(offspring) pool.extend(parents) pool.sort(reverse=True) survivors = pool[:len(population)] return survivors
[ "def", "plus_replacement", "(", "random", ",", "population", ",", "parents", ",", "offspring", ",", "args", ")", ":", "pool", "=", "list", "(", "offspring", ")", "pool", ".", "extend", "(", "parents", ")", "pool", ".", "sort", "(", "reverse", "=", "True", ")", "survivors", "=", "pool", "[", ":", "len", "(", "population", ")", "]", "return", "survivors" ]
Performs "plus" replacement. This function performs "plus" replacement, which means that the entire existing population is replaced by the best population-many elements from the combined set of parents and offspring. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments
[ "Performs", "plus", "replacement", ".", "This", "function", "performs", "plus", "replacement", "which", "means", "that", "the", "entire", "existing", "population", "is", "replaced", "by", "the", "best", "population", "-", "many", "elements", "from", "the", "combined", "set", "of", "parents", "and", "offspring", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "population", "--", "the", "population", "of", "individuals", "parents", "--", "the", "list", "of", "parent", "individuals", "offspring", "--", "the", "list", "of", "offspring", "individuals", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/replacers.py#L171-L191
aarongarrett/inspyred
inspyred/ec/replacers.py
comma_replacement
def comma_replacement(random, population, parents, offspring, args): """Performs "comma" replacement. This function performs "comma" replacement, which means that the entire existing population is replaced by the best population-many elements from the offspring. This function makes the assumption that the size of the offspring is at least as large as the original population. Otherwise, the population size will not be constant. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments """ offspring.sort(reverse=True) survivors = offspring[:len(population)] return survivors
python
def comma_replacement(random, population, parents, offspring, args): offspring.sort(reverse=True) survivors = offspring[:len(population)] return survivors
[ "def", "comma_replacement", "(", "random", ",", "population", ",", "parents", ",", "offspring", ",", "args", ")", ":", "offspring", ".", "sort", "(", "reverse", "=", "True", ")", "survivors", "=", "offspring", "[", ":", "len", "(", "population", ")", "]", "return", "survivors" ]
Performs "comma" replacement. This function performs "comma" replacement, which means that the entire existing population is replaced by the best population-many elements from the offspring. This function makes the assumption that the size of the offspring is at least as large as the original population. Otherwise, the population size will not be constant. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments
[ "Performs", "comma", "replacement", ".", "This", "function", "performs", "comma", "replacement", "which", "means", "that", "the", "entire", "existing", "population", "is", "replaced", "by", "the", "best", "population", "-", "many", "elements", "from", "the", "offspring", ".", "This", "function", "makes", "the", "assumption", "that", "the", "size", "of", "the", "offspring", "is", "at", "least", "as", "large", "as", "the", "original", "population", ".", "Otherwise", "the", "population", "size", "will", "not", "be", "constant", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "population", "--", "the", "population", "of", "individuals", "parents", "--", "the", "list", "of", "parent", "individuals", "offspring", "--", "the", "list", "of", "offspring", "individuals", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/replacers.py#L194-L214
aarongarrett/inspyred
inspyred/ec/replacers.py
crowding_replacement
def crowding_replacement(random, population, parents, offspring, args): """Performs crowding replacement as a form of niching. This function performs crowding replacement, which means that the members of the population are replaced one-at-a-time with each of the offspring. A random sample of `crowding_distance` individuals is pulled from the current population, and the closest individual to the current offspring (where "closest" is determined by the `distance_function`) is replaced by that offspring, if the offspring is better. It is possible for one offspring to replace an earlier offspring in the same generation, given the random sample that is taken of the current survivors for each offspring. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments Optional keyword arguments in args: - *distance_function* -- a function that accepts two candidate solutions and returns the distance between them (default Euclidean L2 distance) - *crowding_distance* -- a positive integer representing the number of closest solutions to consider as a "crowd" (default 2) """ def distance(x, y): return math.sqrt(sum([(a - b)**2 for a, b in zip(x, y)])) try: distance_function = args['distance_function'] except KeyError: distance_function = distance args['distance_function'] = distance_function crowding_distance = args.setdefault('crowding_distance', 2) survivors = population for o in offspring: pool = random.sample(survivors, crowding_distance) closest = min(pool, key=lambda x: distance_function(o.candidate, x.candidate)) if o > closest: survivors.remove(closest) survivors.append(o) return survivors
python
def crowding_replacement(random, population, parents, offspring, args): def distance(x, y): return math.sqrt(sum([(a - b)**2 for a, b in zip(x, y)])) try: distance_function = args['distance_function'] except KeyError: distance_function = distance args['distance_function'] = distance_function crowding_distance = args.setdefault('crowding_distance', 2) survivors = population for o in offspring: pool = random.sample(survivors, crowding_distance) closest = min(pool, key=lambda x: distance_function(o.candidate, x.candidate)) if o > closest: survivors.remove(closest) survivors.append(o) return survivors
[ "def", "crowding_replacement", "(", "random", ",", "population", ",", "parents", ",", "offspring", ",", "args", ")", ":", "def", "distance", "(", "x", ",", "y", ")", ":", "return", "math", ".", "sqrt", "(", "sum", "(", "[", "(", "a", "-", "b", ")", "**", "2", "for", "a", ",", "b", "in", "zip", "(", "x", ",", "y", ")", "]", ")", ")", "try", ":", "distance_function", "=", "args", "[", "'distance_function'", "]", "except", "KeyError", ":", "distance_function", "=", "distance", "args", "[", "'distance_function'", "]", "=", "distance_function", "crowding_distance", "=", "args", ".", "setdefault", "(", "'crowding_distance'", ",", "2", ")", "survivors", "=", "population", "for", "o", "in", "offspring", ":", "pool", "=", "random", ".", "sample", "(", "survivors", ",", "crowding_distance", ")", "closest", "=", "min", "(", "pool", ",", "key", "=", "lambda", "x", ":", "distance_function", "(", "o", ".", "candidate", ",", "x", ".", "candidate", ")", ")", "if", "o", ">", "closest", ":", "survivors", ".", "remove", "(", "closest", ")", "survivors", ".", "append", "(", "o", ")", "return", "survivors" ]
Performs crowding replacement as a form of niching. This function performs crowding replacement, which means that the members of the population are replaced one-at-a-time with each of the offspring. A random sample of `crowding_distance` individuals is pulled from the current population, and the closest individual to the current offspring (where "closest" is determined by the `distance_function`) is replaced by that offspring, if the offspring is better. It is possible for one offspring to replace an earlier offspring in the same generation, given the random sample that is taken of the current survivors for each offspring. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments Optional keyword arguments in args: - *distance_function* -- a function that accepts two candidate solutions and returns the distance between them (default Euclidean L2 distance) - *crowding_distance* -- a positive integer representing the number of closest solutions to consider as a "crowd" (default 2)
[ "Performs", "crowding", "replacement", "as", "a", "form", "of", "niching", ".", "This", "function", "performs", "crowding", "replacement", "which", "means", "that", "the", "members", "of", "the", "population", "are", "replaced", "one", "-", "at", "-", "a", "-", "time", "with", "each", "of", "the", "offspring", ".", "A", "random", "sample", "of", "crowding_distance", "individuals", "is", "pulled", "from", "the", "current", "population", "and", "the", "closest", "individual", "to", "the", "current", "offspring", "(", "where", "closest", "is", "determined", "by", "the", "distance_function", ")", "is", "replaced", "by", "that", "offspring", "if", "the", "offspring", "is", "better", ".", "It", "is", "possible", "for", "one", "offspring", "to", "replace", "an", "earlier", "offspring", "in", "the", "same", "generation", "given", "the", "random", "sample", "that", "is", "taken", "of", "the", "current", "survivors", "for", "each", "offspring", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "population", "--", "the", "population", "of", "individuals", "parents", "--", "the", "list", "of", "parent", "individuals", "offspring", "--", "the", "list", "of", "offspring", "individuals", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/replacers.py#L217-L262
aarongarrett/inspyred
inspyred/ec/replacers.py
simulated_annealing_replacement
def simulated_annealing_replacement(random, population, parents, offspring, args): """Replaces population using the simulated annealing schedule. This function performs simulated annealing replacement based on a temperature and a cooling rate. These can be specified by the keyword arguments `temperature`, which should be the initial temperature, and `cooling_rate`, which should be the coefficient by which the temperature is reduced. If these keyword arguments are not present, then the function will attempt to base the cooling schedule either on the ratio of evaluations to the maximum allowed evaluations or on the ratio of generations to the maximum allowed generations. Each of these ratios is of the form ``(max - current)/max`` so that the cooling schedule moves smoothly from 1 to 0. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments Optional keyword arguments in args: - *temperature* -- the initial temperature - *cooling_rate* -- a real-valued coefficient in the range (0, 1) by which the temperature should be reduced """ try: temp = args['temperature'] cooling_rate = args['cooling_rate'] temp = temp * cooling_rate args['temperature'] = temp except KeyError: try: num_evals = args['_ec'].num_evaluations max_evals = args['max_evaluations'] temp = float(max_evals - num_evals) / float(max_evals) except KeyError: num_gens = args['_ec'].num_generations max_gens = args['max_generations'] temp = 1 - float(max_gens - num_gens) / float(max_gens) new_pop = [] for p, o in zip(parents, offspring): if o >= p: new_pop.append(o) elif temp > 0 and random.random() < math.exp(-abs(p.fitness - o.fitness) / float(temp)): new_pop.append(o) else: new_pop.append(p) return new_pop
python
def simulated_annealing_replacement(random, population, parents, offspring, args): try: temp = args['temperature'] cooling_rate = args['cooling_rate'] temp = temp * cooling_rate args['temperature'] = temp except KeyError: try: num_evals = args['_ec'].num_evaluations max_evals = args['max_evaluations'] temp = float(max_evals - num_evals) / float(max_evals) except KeyError: num_gens = args['_ec'].num_generations max_gens = args['max_generations'] temp = 1 - float(max_gens - num_gens) / float(max_gens) new_pop = [] for p, o in zip(parents, offspring): if o >= p: new_pop.append(o) elif temp > 0 and random.random() < math.exp(-abs(p.fitness - o.fitness) / float(temp)): new_pop.append(o) else: new_pop.append(p) return new_pop
[ "def", "simulated_annealing_replacement", "(", "random", ",", "population", ",", "parents", ",", "offspring", ",", "args", ")", ":", "try", ":", "temp", "=", "args", "[", "'temperature'", "]", "cooling_rate", "=", "args", "[", "'cooling_rate'", "]", "temp", "=", "temp", "*", "cooling_rate", "args", "[", "'temperature'", "]", "=", "temp", "except", "KeyError", ":", "try", ":", "num_evals", "=", "args", "[", "'_ec'", "]", ".", "num_evaluations", "max_evals", "=", "args", "[", "'max_evaluations'", "]", "temp", "=", "float", "(", "max_evals", "-", "num_evals", ")", "/", "float", "(", "max_evals", ")", "except", "KeyError", ":", "num_gens", "=", "args", "[", "'_ec'", "]", ".", "num_generations", "max_gens", "=", "args", "[", "'max_generations'", "]", "temp", "=", "1", "-", "float", "(", "max_gens", "-", "num_gens", ")", "/", "float", "(", "max_gens", ")", "new_pop", "=", "[", "]", "for", "p", ",", "o", "in", "zip", "(", "parents", ",", "offspring", ")", ":", "if", "o", ">=", "p", ":", "new_pop", ".", "append", "(", "o", ")", "elif", "temp", ">", "0", "and", "random", ".", "random", "(", ")", "<", "math", ".", "exp", "(", "-", "abs", "(", "p", ".", "fitness", "-", "o", ".", "fitness", ")", "/", "float", "(", "temp", ")", ")", ":", "new_pop", ".", "append", "(", "o", ")", "else", ":", "new_pop", ".", "append", "(", "p", ")", "return", "new_pop" ]
Replaces population using the simulated annealing schedule. This function performs simulated annealing replacement based on a temperature and a cooling rate. These can be specified by the keyword arguments `temperature`, which should be the initial temperature, and `cooling_rate`, which should be the coefficient by which the temperature is reduced. If these keyword arguments are not present, then the function will attempt to base the cooling schedule either on the ratio of evaluations to the maximum allowed evaluations or on the ratio of generations to the maximum allowed generations. Each of these ratios is of the form ``(max - current)/max`` so that the cooling schedule moves smoothly from 1 to 0. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments Optional keyword arguments in args: - *temperature* -- the initial temperature - *cooling_rate* -- a real-valued coefficient in the range (0, 1) by which the temperature should be reduced
[ "Replaces", "population", "using", "the", "simulated", "annealing", "schedule", ".", "This", "function", "performs", "simulated", "annealing", "replacement", "based", "on", "a", "temperature", "and", "a", "cooling", "rate", ".", "These", "can", "be", "specified", "by", "the", "keyword", "arguments", "temperature", "which", "should", "be", "the", "initial", "temperature", "and", "cooling_rate", "which", "should", "be", "the", "coefficient", "by", "which", "the", "temperature", "is", "reduced", ".", "If", "these", "keyword", "arguments", "are", "not", "present", "then", "the", "function", "will", "attempt", "to", "base", "the", "cooling", "schedule", "either", "on", "the", "ratio", "of", "evaluations", "to", "the", "maximum", "allowed", "evaluations", "or", "on", "the", "ratio", "of", "generations", "to", "the", "maximum", "allowed", "generations", ".", "Each", "of", "these", "ratios", "is", "of", "the", "form", "(", "max", "-", "current", ")", "/", "max", "so", "that", "the", "cooling", "schedule", "moves", "smoothly", "from", "1", "to", "0", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "population", "--", "the", "population", "of", "individuals", "parents", "--", "the", "list", "of", "parent", "individuals", "offspring", "--", "the", "list", "of", "offspring", "individuals", "args", "--", "a", "dictionary", "of", "keyword", "arguments", "Optional", "keyword", "arguments", "in", "args", ":", "-", "*", "temperature", "*", "--", "the", "initial", "temperature", "-", "*", "cooling_rate", "*", "--", "a", "real", "-", "valued", "coefficient", "in", "the", "range", "(", "0", "1", ")", "by", "which", "the", "temperature", "should", "be", "reduced" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/replacers.py#L271-L324
aarongarrett/inspyred
inspyred/ec/replacers.py
nsga_replacement
def nsga_replacement(random, population, parents, offspring, args): """Replaces population using the non-dominated sorting technique from NSGA-II. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments """ survivors = [] combined = list(population) combined.extend(offspring) # Perform the non-dominated sorting to determine the fronts. fronts = [] pop = set(range(len(combined))) while len(pop) > 0: front = [] for p in pop: dominated = False for q in pop: if combined[p] < combined[q]: dominated = True break if not dominated: front.append(p) fronts.append([dict(individual=combined[f], index=f) for f in front]) pop = pop - set(front) # Go through each front and add all the elements until doing so # would put you above the population limit. At that point, fall # back to the crowding distance to determine who to put into the # next population. Individuals with higher crowding distances # (i.e., more distance between neighbors) are preferred. for i, front in enumerate(fronts): if len(survivors) + len(front) > len(population): # Determine the crowding distance. distance = [0 for _ in range(len(combined))] individuals = list(front) num_individuals = len(individuals) num_objectives = len(individuals[0]['individual'].fitness) for obj in range(num_objectives): individuals.sort(key=lambda x: x['individual'].fitness[obj]) distance[individuals[0]['index']] = float('inf') distance[individuals[-1]['index']] = float('inf') for i in range(1, num_individuals-1): distance[individuals[i]['index']] = (distance[individuals[i]['index']] + (individuals[i+1]['individual'].fitness[obj] - individuals[i-1]['individual'].fitness[obj])) crowd = [dict(dist=distance[f['index']], index=f['index']) for f in front] crowd.sort(key=lambda x: x['dist'], reverse=True) last_rank = [combined[c['index']] for c in crowd] r = 0 num_added = 0 num_left_to_add = len(population) - len(survivors) while r < len(last_rank) and num_added < num_left_to_add: if last_rank[r] not in survivors: survivors.append(last_rank[r]) num_added += 1 r += 1 # If we've filled out our survivor list, then stop. # Otherwise, process the next front in the list. if len(survivors) == len(population): break else: for f in front: if f['individual'] not in survivors: survivors.append(f['individual']) return survivors
python
def nsga_replacement(random, population, parents, offspring, args): survivors = [] combined = list(population) combined.extend(offspring) fronts = [] pop = set(range(len(combined))) while len(pop) > 0: front = [] for p in pop: dominated = False for q in pop: if combined[p] < combined[q]: dominated = True break if not dominated: front.append(p) fronts.append([dict(individual=combined[f], index=f) for f in front]) pop = pop - set(front) for i, front in enumerate(fronts): if len(survivors) + len(front) > len(population): distance = [0 for _ in range(len(combined))] individuals = list(front) num_individuals = len(individuals) num_objectives = len(individuals[0]['individual'].fitness) for obj in range(num_objectives): individuals.sort(key=lambda x: x['individual'].fitness[obj]) distance[individuals[0]['index']] = float('inf') distance[individuals[-1]['index']] = float('inf') for i in range(1, num_individuals-1): distance[individuals[i]['index']] = (distance[individuals[i]['index']] + (individuals[i+1]['individual'].fitness[obj] - individuals[i-1]['individual'].fitness[obj])) crowd = [dict(dist=distance[f['index']], index=f['index']) for f in front] crowd.sort(key=lambda x: x['dist'], reverse=True) last_rank = [combined[c['index']] for c in crowd] r = 0 num_added = 0 num_left_to_add = len(population) - len(survivors) while r < len(last_rank) and num_added < num_left_to_add: if last_rank[r] not in survivors: survivors.append(last_rank[r]) num_added += 1 r += 1 if len(survivors) == len(population): break else: for f in front: if f['individual'] not in survivors: survivors.append(f['individual']) return survivors
[ "def", "nsga_replacement", "(", "random", ",", "population", ",", "parents", ",", "offspring", ",", "args", ")", ":", "survivors", "=", "[", "]", "combined", "=", "list", "(", "population", ")", "combined", ".", "extend", "(", "offspring", ")", "# Perform the non-dominated sorting to determine the fronts.", "fronts", "=", "[", "]", "pop", "=", "set", "(", "range", "(", "len", "(", "combined", ")", ")", ")", "while", "len", "(", "pop", ")", ">", "0", ":", "front", "=", "[", "]", "for", "p", "in", "pop", ":", "dominated", "=", "False", "for", "q", "in", "pop", ":", "if", "combined", "[", "p", "]", "<", "combined", "[", "q", "]", ":", "dominated", "=", "True", "break", "if", "not", "dominated", ":", "front", ".", "append", "(", "p", ")", "fronts", ".", "append", "(", "[", "dict", "(", "individual", "=", "combined", "[", "f", "]", ",", "index", "=", "f", ")", "for", "f", "in", "front", "]", ")", "pop", "=", "pop", "-", "set", "(", "front", ")", "# Go through each front and add all the elements until doing so", "# would put you above the population limit. At that point, fall", "# back to the crowding distance to determine who to put into the", "# next population. Individuals with higher crowding distances", "# (i.e., more distance between neighbors) are preferred.", "for", "i", ",", "front", "in", "enumerate", "(", "fronts", ")", ":", "if", "len", "(", "survivors", ")", "+", "len", "(", "front", ")", ">", "len", "(", "population", ")", ":", "# Determine the crowding distance.", "distance", "=", "[", "0", "for", "_", "in", "range", "(", "len", "(", "combined", ")", ")", "]", "individuals", "=", "list", "(", "front", ")", "num_individuals", "=", "len", "(", "individuals", ")", "num_objectives", "=", "len", "(", "individuals", "[", "0", "]", "[", "'individual'", "]", ".", "fitness", ")", "for", "obj", "in", "range", "(", "num_objectives", ")", ":", "individuals", ".", "sort", "(", "key", "=", "lambda", "x", ":", "x", "[", "'individual'", "]", ".", "fitness", "[", "obj", "]", ")", "distance", "[", "individuals", "[", "0", "]", "[", "'index'", "]", "]", "=", "float", "(", "'inf'", ")", "distance", "[", "individuals", "[", "-", "1", "]", "[", "'index'", "]", "]", "=", "float", "(", "'inf'", ")", "for", "i", "in", "range", "(", "1", ",", "num_individuals", "-", "1", ")", ":", "distance", "[", "individuals", "[", "i", "]", "[", "'index'", "]", "]", "=", "(", "distance", "[", "individuals", "[", "i", "]", "[", "'index'", "]", "]", "+", "(", "individuals", "[", "i", "+", "1", "]", "[", "'individual'", "]", ".", "fitness", "[", "obj", "]", "-", "individuals", "[", "i", "-", "1", "]", "[", "'individual'", "]", ".", "fitness", "[", "obj", "]", ")", ")", "crowd", "=", "[", "dict", "(", "dist", "=", "distance", "[", "f", "[", "'index'", "]", "]", ",", "index", "=", "f", "[", "'index'", "]", ")", "for", "f", "in", "front", "]", "crowd", ".", "sort", "(", "key", "=", "lambda", "x", ":", "x", "[", "'dist'", "]", ",", "reverse", "=", "True", ")", "last_rank", "=", "[", "combined", "[", "c", "[", "'index'", "]", "]", "for", "c", "in", "crowd", "]", "r", "=", "0", "num_added", "=", "0", "num_left_to_add", "=", "len", "(", "population", ")", "-", "len", "(", "survivors", ")", "while", "r", "<", "len", "(", "last_rank", ")", "and", "num_added", "<", "num_left_to_add", ":", "if", "last_rank", "[", "r", "]", "not", "in", "survivors", ":", "survivors", ".", "append", "(", "last_rank", "[", "r", "]", ")", "num_added", "+=", "1", "r", "+=", "1", "# If we've filled out our survivor list, then stop.", "# Otherwise, process the next front in the list.", "if", "len", "(", "survivors", ")", "==", "len", "(", "population", ")", ":", "break", "else", ":", "for", "f", "in", "front", ":", "if", "f", "[", "'individual'", "]", "not", "in", "survivors", ":", "survivors", ".", "append", "(", "f", "[", "'individual'", "]", ")", "return", "survivors" ]
Replaces population using the non-dominated sorting technique from NSGA-II. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments
[ "Replaces", "population", "using", "the", "non", "-", "dominated", "sorting", "technique", "from", "NSGA", "-", "II", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "population", "--", "the", "population", "of", "individuals", "parents", "--", "the", "list", "of", "parent", "individuals", "offspring", "--", "the", "list", "of", "offspring", "individuals", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/replacers.py#L327-L398
aarongarrett/inspyred
inspyred/ec/replacers.py
paes_replacement
def paes_replacement(random, population, parents, offspring, args): """Replaces population using the Pareto Archived Evolution Strategy method. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments """ archive = args['_ec'].archive archiver = args['_ec'].archiver survivors = [] for p, o in zip(parents, offspring): if o == p: survivors.append(p) elif o in archive: survivors.append(p) elif o > p: archive = archiver(random, [o], archive, args) survivors.append(o) elif o >= p: for a in archive: if o > a or o < a: break if o >= a: archive = archiver(random, [o], archive, args) if o > a or archiver.grid_population[o.grid_location] <= archiver.grid_population[p.grid_location]: survivors.append(o) else: survivors.append(p) else: survivors.append(p) else: survivors.append(p) return survivors
python
def paes_replacement(random, population, parents, offspring, args): archive = args['_ec'].archive archiver = args['_ec'].archiver survivors = [] for p, o in zip(parents, offspring): if o == p: survivors.append(p) elif o in archive: survivors.append(p) elif o > p: archive = archiver(random, [o], archive, args) survivors.append(o) elif o >= p: for a in archive: if o > a or o < a: break if o >= a: archive = archiver(random, [o], archive, args) if o > a or archiver.grid_population[o.grid_location] <= archiver.grid_population[p.grid_location]: survivors.append(o) else: survivors.append(p) else: survivors.append(p) else: survivors.append(p) return survivors
[ "def", "paes_replacement", "(", "random", ",", "population", ",", "parents", ",", "offspring", ",", "args", ")", ":", "archive", "=", "args", "[", "'_ec'", "]", ".", "archive", "archiver", "=", "args", "[", "'_ec'", "]", ".", "archiver", "survivors", "=", "[", "]", "for", "p", ",", "o", "in", "zip", "(", "parents", ",", "offspring", ")", ":", "if", "o", "==", "p", ":", "survivors", ".", "append", "(", "p", ")", "elif", "o", "in", "archive", ":", "survivors", ".", "append", "(", "p", ")", "elif", "o", ">", "p", ":", "archive", "=", "archiver", "(", "random", ",", "[", "o", "]", ",", "archive", ",", "args", ")", "survivors", ".", "append", "(", "o", ")", "elif", "o", ">=", "p", ":", "for", "a", "in", "archive", ":", "if", "o", ">", "a", "or", "o", "<", "a", ":", "break", "if", "o", ">=", "a", ":", "archive", "=", "archiver", "(", "random", ",", "[", "o", "]", ",", "archive", ",", "args", ")", "if", "o", ">", "a", "or", "archiver", ".", "grid_population", "[", "o", ".", "grid_location", "]", "<=", "archiver", ".", "grid_population", "[", "p", ".", "grid_location", "]", ":", "survivors", ".", "append", "(", "o", ")", "else", ":", "survivors", ".", "append", "(", "p", ")", "else", ":", "survivors", ".", "append", "(", "p", ")", "else", ":", "survivors", ".", "append", "(", "p", ")", "return", "survivors" ]
Replaces population using the Pareto Archived Evolution Strategy method. .. Arguments: random -- the random number generator object population -- the population of individuals parents -- the list of parent individuals offspring -- the list of offspring individuals args -- a dictionary of keyword arguments
[ "Replaces", "population", "using", "the", "Pareto", "Archived", "Evolution", "Strategy", "method", ".", "..", "Arguments", ":", "random", "--", "the", "random", "number", "generator", "object", "population", "--", "the", "population", "of", "individuals", "parents", "--", "the", "list", "of", "parent", "individuals", "offspring", "--", "the", "list", "of", "offspring", "individuals", "args", "--", "a", "dictionary", "of", "keyword", "arguments" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/replacers.py#L401-L438
aarongarrett/inspyred
inspyred/ec/evaluators.py
evaluator
def evaluator(evaluate): """Return an inspyred evaluator function based on the given function. This function generator takes a function that evaluates only one candidate. The generator handles the iteration over each candidate to be evaluated. The given function ``evaluate`` must have the following signature:: fitness = evaluate(candidate, args) This function is most commonly used as a function decorator with the following usage:: @evaluator def evaluate(candidate, args): # Implementation of evaluation pass The generated function also contains an attribute named ``single_evaluation`` which holds the original evaluation function. In this way, the original single-candidate function can be retrieved if necessary. """ @functools.wraps(evaluate) def inspyred_evaluator(candidates, args): fitness = [] for candidate in candidates: fitness.append(evaluate(candidate, args)) return fitness inspyred_evaluator.single_evaluation = evaluate return inspyred_evaluator
python
def evaluator(evaluate): @functools.wraps(evaluate) def inspyred_evaluator(candidates, args): fitness = [] for candidate in candidates: fitness.append(evaluate(candidate, args)) return fitness inspyred_evaluator.single_evaluation = evaluate return inspyred_evaluator
[ "def", "evaluator", "(", "evaluate", ")", ":", "@", "functools", ".", "wraps", "(", "evaluate", ")", "def", "inspyred_evaluator", "(", "candidates", ",", "args", ")", ":", "fitness", "=", "[", "]", "for", "candidate", "in", "candidates", ":", "fitness", ".", "append", "(", "evaluate", "(", "candidate", ",", "args", ")", ")", "return", "fitness", "inspyred_evaluator", ".", "single_evaluation", "=", "evaluate", "return", "inspyred_evaluator" ]
Return an inspyred evaluator function based on the given function. This function generator takes a function that evaluates only one candidate. The generator handles the iteration over each candidate to be evaluated. The given function ``evaluate`` must have the following signature:: fitness = evaluate(candidate, args) This function is most commonly used as a function decorator with the following usage:: @evaluator def evaluate(candidate, args): # Implementation of evaluation pass The generated function also contains an attribute named ``single_evaluation`` which holds the original evaluation function. In this way, the original single-candidate function can be retrieved if necessary.
[ "Return", "an", "inspyred", "evaluator", "function", "based", "on", "the", "given", "function", ".", "This", "function", "generator", "takes", "a", "function", "that", "evaluates", "only", "one", "candidate", ".", "The", "generator", "handles", "the", "iteration", "over", "each", "candidate", "to", "be", "evaluated", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/evaluators.py#L45-L77
aarongarrett/inspyred
inspyred/ec/evaluators.py
parallel_evaluation_pp
def parallel_evaluation_pp(candidates, args): """Evaluate the candidates in parallel using Parallel Python. This function allows parallel evaluation of candidate solutions. It uses the `Parallel Python <http://www.parallelpython.com>`_ (pp) library to accomplish the parallelization. This library must already be installed in order to use this function. The function assigns the evaluation of each candidate to its own job, all of which are then distributed to the available processing units. .. note:: All arguments to the evaluation function must be pickleable. Those that are not will not be sent through the ``args`` variable and will be unavailable to your function. .. Arguments: candidates -- the candidate solutions args -- a dictionary of keyword arguments Required keyword arguments in args: - *pp_evaluator* -- actual evaluation function to be used (This function should have the same signature as any other inspyred evaluation function.) Optional keyword arguments in args: - *pp_dependencies* -- tuple of functional dependencies of the serial evaluator (default ()) - *pp_modules* -- tuple of modules that must be imported for the functional dependencies (default ()) - *pp_servers* -- tuple of servers (on a cluster) that will be used for parallel processing (default ("*",)) - *pp_secret* -- string representing the secret key needed to authenticate on a worker node (default "inspyred") - *pp_nprocs* -- integer representing the number of worker processes to start on the local machine (default "autodetect", which sets it to the number of processors in the system) For more information about these arguments, please consult the documentation for `Parallel Python <http://www.parallelpython.com>`_. """ import pp logger = args['_ec'].logger try: evaluator = args['pp_evaluator'] except KeyError: logger.error('parallel_evaluation_pp requires \'pp_evaluator\' be defined in the keyword arguments list') raise secret_key = args.setdefault('pp_secret', 'inspyred') try: job_server = args['_pp_job_server'] except KeyError: pp_servers = args.get('pp_servers', ("*",)) pp_nprocs = args.get('pp_nprocs', 'autodetect') job_server = pp.Server(ncpus=pp_nprocs, ppservers=pp_servers, secret=secret_key) args['_pp_job_server'] = job_server pp_depends = args.setdefault('pp_dependencies', ()) pp_modules = args.setdefault('pp_modules', ()) pickled_args = {} for key in args: try: pickle.dumps(args[key]) pickled_args[key] = args[key] except (TypeError, pickle.PickleError, pickle.PicklingError): logger.debug('unable to pickle args parameter {0} in parallel_evaluation_pp'.format(key)) pass func_template = pp.Template(job_server, evaluator, pp_depends, pp_modules) jobs = [func_template.submit([c], pickled_args) for c in candidates] fitness = [] for i, job in enumerate(jobs): r = job() try: fitness.append(r[0]) except TypeError: logger.warning('parallel_evaluation_pp generated an invalid fitness for candidate {0}'.format(candidates[i])) fitness.append(None) return fitness
python
def parallel_evaluation_pp(candidates, args): import pp logger = args['_ec'].logger try: evaluator = args['pp_evaluator'] except KeyError: logger.error('parallel_evaluation_pp requires \'pp_evaluator\' be defined in the keyword arguments list') raise secret_key = args.setdefault('pp_secret', 'inspyred') try: job_server = args['_pp_job_server'] except KeyError: pp_servers = args.get('pp_servers', ("*",)) pp_nprocs = args.get('pp_nprocs', 'autodetect') job_server = pp.Server(ncpus=pp_nprocs, ppservers=pp_servers, secret=secret_key) args['_pp_job_server'] = job_server pp_depends = args.setdefault('pp_dependencies', ()) pp_modules = args.setdefault('pp_modules', ()) pickled_args = {} for key in args: try: pickle.dumps(args[key]) pickled_args[key] = args[key] except (TypeError, pickle.PickleError, pickle.PicklingError): logger.debug('unable to pickle args parameter {0} in parallel_evaluation_pp'.format(key)) pass func_template = pp.Template(job_server, evaluator, pp_depends, pp_modules) jobs = [func_template.submit([c], pickled_args) for c in candidates] fitness = [] for i, job in enumerate(jobs): r = job() try: fitness.append(r[0]) except TypeError: logger.warning('parallel_evaluation_pp generated an invalid fitness for candidate {0}'.format(candidates[i])) fitness.append(None) return fitness
[ "def", "parallel_evaluation_pp", "(", "candidates", ",", "args", ")", ":", "import", "pp", "logger", "=", "args", "[", "'_ec'", "]", ".", "logger", "try", ":", "evaluator", "=", "args", "[", "'pp_evaluator'", "]", "except", "KeyError", ":", "logger", ".", "error", "(", "'parallel_evaluation_pp requires \\'pp_evaluator\\' be defined in the keyword arguments list'", ")", "raise", "secret_key", "=", "args", ".", "setdefault", "(", "'pp_secret'", ",", "'inspyred'", ")", "try", ":", "job_server", "=", "args", "[", "'_pp_job_server'", "]", "except", "KeyError", ":", "pp_servers", "=", "args", ".", "get", "(", "'pp_servers'", ",", "(", "\"*\"", ",", ")", ")", "pp_nprocs", "=", "args", ".", "get", "(", "'pp_nprocs'", ",", "'autodetect'", ")", "job_server", "=", "pp", ".", "Server", "(", "ncpus", "=", "pp_nprocs", ",", "ppservers", "=", "pp_servers", ",", "secret", "=", "secret_key", ")", "args", "[", "'_pp_job_server'", "]", "=", "job_server", "pp_depends", "=", "args", ".", "setdefault", "(", "'pp_dependencies'", ",", "(", ")", ")", "pp_modules", "=", "args", ".", "setdefault", "(", "'pp_modules'", ",", "(", ")", ")", "pickled_args", "=", "{", "}", "for", "key", "in", "args", ":", "try", ":", "pickle", ".", "dumps", "(", "args", "[", "key", "]", ")", "pickled_args", "[", "key", "]", "=", "args", "[", "key", "]", "except", "(", "TypeError", ",", "pickle", ".", "PickleError", ",", "pickle", ".", "PicklingError", ")", ":", "logger", ".", "debug", "(", "'unable to pickle args parameter {0} in parallel_evaluation_pp'", ".", "format", "(", "key", ")", ")", "pass", "func_template", "=", "pp", ".", "Template", "(", "job_server", ",", "evaluator", ",", "pp_depends", ",", "pp_modules", ")", "jobs", "=", "[", "func_template", ".", "submit", "(", "[", "c", "]", ",", "pickled_args", ")", "for", "c", "in", "candidates", "]", "fitness", "=", "[", "]", "for", "i", ",", "job", "in", "enumerate", "(", "jobs", ")", ":", "r", "=", "job", "(", ")", "try", ":", "fitness", ".", "append", "(", "r", "[", "0", "]", ")", "except", "TypeError", ":", "logger", ".", "warning", "(", "'parallel_evaluation_pp generated an invalid fitness for candidate {0}'", ".", "format", "(", "candidates", "[", "i", "]", ")", ")", "fitness", ".", "append", "(", "None", ")", "return", "fitness" ]
Evaluate the candidates in parallel using Parallel Python. This function allows parallel evaluation of candidate solutions. It uses the `Parallel Python <http://www.parallelpython.com>`_ (pp) library to accomplish the parallelization. This library must already be installed in order to use this function. The function assigns the evaluation of each candidate to its own job, all of which are then distributed to the available processing units. .. note:: All arguments to the evaluation function must be pickleable. Those that are not will not be sent through the ``args`` variable and will be unavailable to your function. .. Arguments: candidates -- the candidate solutions args -- a dictionary of keyword arguments Required keyword arguments in args: - *pp_evaluator* -- actual evaluation function to be used (This function should have the same signature as any other inspyred evaluation function.) Optional keyword arguments in args: - *pp_dependencies* -- tuple of functional dependencies of the serial evaluator (default ()) - *pp_modules* -- tuple of modules that must be imported for the functional dependencies (default ()) - *pp_servers* -- tuple of servers (on a cluster) that will be used for parallel processing (default ("*",)) - *pp_secret* -- string representing the secret key needed to authenticate on a worker node (default "inspyred") - *pp_nprocs* -- integer representing the number of worker processes to start on the local machine (default "autodetect", which sets it to the number of processors in the system) For more information about these arguments, please consult the documentation for `Parallel Python <http://www.parallelpython.com>`_.
[ "Evaluate", "the", "candidates", "in", "parallel", "using", "Parallel", "Python", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/evaluators.py#L80-L162
aarongarrett/inspyred
inspyred/ec/evaluators.py
parallel_evaluation_mp
def parallel_evaluation_mp(candidates, args): """Evaluate the candidates in parallel using ``multiprocessing``. This function allows parallel evaluation of candidate solutions. It uses the standard multiprocessing library to accomplish the parallelization. The function assigns the evaluation of each candidate to its own job, all of which are then distributed to the available processing units. .. note:: All arguments to the evaluation function must be pickleable. Those that are not will not be sent through the ``args`` variable and will be unavailable to your function. .. Arguments: candidates -- the candidate solutions args -- a dictionary of keyword arguments Required keyword arguments in args: - *mp_evaluator* -- actual evaluation function to be used (This function should have the same signature as any other inspyred evaluation function.) Optional keyword arguments in args: - *mp_nprocs* -- number of processors that will be used (default machine cpu count) """ import time import multiprocessing logger = args['_ec'].logger try: evaluator = args['mp_evaluator'] except KeyError: logger.error('parallel_evaluation_mp requires \'mp_evaluator\' be defined in the keyword arguments list') raise try: nprocs = args['mp_nprocs'] except KeyError: nprocs = multiprocessing.cpu_count() pickled_args = {} for key in args: try: pickle.dumps(args[key]) pickled_args[key] = args[key] except (TypeError, pickle.PickleError, pickle.PicklingError): logger.debug('unable to pickle args parameter {0} in parallel_evaluation_mp'.format(key)) pass start = time.time() try: pool = multiprocessing.Pool(processes=nprocs) results = [pool.apply_async(evaluator, ([c], pickled_args)) for c in candidates] pool.close() pool.join() return [r.get()[0] for r in results] except (OSError, RuntimeError) as e: logger.error('failed parallel_evaluation_mp: {0}'.format(str(e))) raise else: end = time.time() logger.debug('completed parallel_evaluation_mp in {0} seconds'.format(end - start))
python
def parallel_evaluation_mp(candidates, args): import time import multiprocessing logger = args['_ec'].logger try: evaluator = args['mp_evaluator'] except KeyError: logger.error('parallel_evaluation_mp requires \'mp_evaluator\' be defined in the keyword arguments list') raise try: nprocs = args['mp_nprocs'] except KeyError: nprocs = multiprocessing.cpu_count() pickled_args = {} for key in args: try: pickle.dumps(args[key]) pickled_args[key] = args[key] except (TypeError, pickle.PickleError, pickle.PicklingError): logger.debug('unable to pickle args parameter {0} in parallel_evaluation_mp'.format(key)) pass start = time.time() try: pool = multiprocessing.Pool(processes=nprocs) results = [pool.apply_async(evaluator, ([c], pickled_args)) for c in candidates] pool.close() pool.join() return [r.get()[0] for r in results] except (OSError, RuntimeError) as e: logger.error('failed parallel_evaluation_mp: {0}'.format(str(e))) raise else: end = time.time() logger.debug('completed parallel_evaluation_mp in {0} seconds'.format(end - start))
[ "def", "parallel_evaluation_mp", "(", "candidates", ",", "args", ")", ":", "import", "time", "import", "multiprocessing", "logger", "=", "args", "[", "'_ec'", "]", ".", "logger", "try", ":", "evaluator", "=", "args", "[", "'mp_evaluator'", "]", "except", "KeyError", ":", "logger", ".", "error", "(", "'parallel_evaluation_mp requires \\'mp_evaluator\\' be defined in the keyword arguments list'", ")", "raise", "try", ":", "nprocs", "=", "args", "[", "'mp_nprocs'", "]", "except", "KeyError", ":", "nprocs", "=", "multiprocessing", ".", "cpu_count", "(", ")", "pickled_args", "=", "{", "}", "for", "key", "in", "args", ":", "try", ":", "pickle", ".", "dumps", "(", "args", "[", "key", "]", ")", "pickled_args", "[", "key", "]", "=", "args", "[", "key", "]", "except", "(", "TypeError", ",", "pickle", ".", "PickleError", ",", "pickle", ".", "PicklingError", ")", ":", "logger", ".", "debug", "(", "'unable to pickle args parameter {0} in parallel_evaluation_mp'", ".", "format", "(", "key", ")", ")", "pass", "start", "=", "time", ".", "time", "(", ")", "try", ":", "pool", "=", "multiprocessing", ".", "Pool", "(", "processes", "=", "nprocs", ")", "results", "=", "[", "pool", ".", "apply_async", "(", "evaluator", ",", "(", "[", "c", "]", ",", "pickled_args", ")", ")", "for", "c", "in", "candidates", "]", "pool", ".", "close", "(", ")", "pool", ".", "join", "(", ")", "return", "[", "r", ".", "get", "(", ")", "[", "0", "]", "for", "r", "in", "results", "]", "except", "(", "OSError", ",", "RuntimeError", ")", "as", "e", ":", "logger", ".", "error", "(", "'failed parallel_evaluation_mp: {0}'", ".", "format", "(", "str", "(", "e", ")", ")", ")", "raise", "else", ":", "end", "=", "time", ".", "time", "(", ")", "logger", ".", "debug", "(", "'completed parallel_evaluation_mp in {0} seconds'", ".", "format", "(", "end", "-", "start", ")", ")" ]
Evaluate the candidates in parallel using ``multiprocessing``. This function allows parallel evaluation of candidate solutions. It uses the standard multiprocessing library to accomplish the parallelization. The function assigns the evaluation of each candidate to its own job, all of which are then distributed to the available processing units. .. note:: All arguments to the evaluation function must be pickleable. Those that are not will not be sent through the ``args`` variable and will be unavailable to your function. .. Arguments: candidates -- the candidate solutions args -- a dictionary of keyword arguments Required keyword arguments in args: - *mp_evaluator* -- actual evaluation function to be used (This function should have the same signature as any other inspyred evaluation function.) Optional keyword arguments in args: - *mp_nprocs* -- number of processors that will be used (default machine cpu count)
[ "Evaluate", "the", "candidates", "in", "parallel", "using", "multiprocessing", "." ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/evaluators.py#L165-L230
aarongarrett/inspyred
inspyred/ec/generators.py
strategize
def strategize(generator): """Add strategy parameters to candidates created by a generator. This function decorator is used to provide a means of adding strategy parameters to candidates created by a generator. The generator function is modifed to extend the candidate with ``len(candidate)`` strategy parameters (one per candidate element). Each strategy parameter is initialized to a random value in the range [0, 1]. The typical usage is as follows:: @strategize def generator_function(random, args): # Normal generator function pass """ @functools.wraps(generator) def strategy_generator(random, args): candidate = generator(random, args) n = len(candidate) candidate.extend([random.random() for _ in range(n)]) return candidate return strategy_generator
python
def strategize(generator): @functools.wraps(generator) def strategy_generator(random, args): candidate = generator(random, args) n = len(candidate) candidate.extend([random.random() for _ in range(n)]) return candidate return strategy_generator
[ "def", "strategize", "(", "generator", ")", ":", "@", "functools", ".", "wraps", "(", "generator", ")", "def", "strategy_generator", "(", "random", ",", "args", ")", ":", "candidate", "=", "generator", "(", "random", ",", "args", ")", "n", "=", "len", "(", "candidate", ")", "candidate", ".", "extend", "(", "[", "random", ".", "random", "(", ")", "for", "_", "in", "range", "(", "n", ")", "]", ")", "return", "candidate", "return", "strategy_generator" ]
Add strategy parameters to candidates created by a generator. This function decorator is used to provide a means of adding strategy parameters to candidates created by a generator. The generator function is modifed to extend the candidate with ``len(candidate)`` strategy parameters (one per candidate element). Each strategy parameter is initialized to a random value in the range [0, 1]. The typical usage is as follows:: @strategize def generator_function(random, args): # Normal generator function pass
[ "Add", "strategy", "parameters", "to", "candidates", "created", "by", "a", "generator", ".", "This", "function", "decorator", "is", "used", "to", "provide", "a", "means", "of", "adding", "strategy", "parameters", "to", "candidates", "created", "by", "a", "generator", ".", "The", "generator", "function", "is", "modifed", "to", "extend", "the", "candidate", "with", "len", "(", "candidate", ")", "strategy", "parameters", "(", "one", "per", "candidate", "element", ")", ".", "Each", "strategy", "parameter", "is", "initialized", "to", "a", "random", "value", "in", "the", "range", "[", "0", "1", "]", ".", "The", "typical", "usage", "is", "as", "follows", "::" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/generators.py#L40-L62
aarongarrett/inspyred
inspyred/ec/utilities.py
memoize
def memoize(func=None, maxlen=None): """Cache a function's return value each time it is called. This function serves as a function decorator to provide a caching of evaluated fitness values. If called later with the same arguments, the cached value is returned instead of being re-evaluated. This decorator assumes that candidates are individually pickleable, and their pickled values are used for hashing into a dictionary. It should be used when evaluating an *expensive* fitness function to avoid costly re-evaluation of those fitnesses. The typical usage is as follows:: @memoize def expensive_fitness_function(candidates, args): # Implementation of expensive fitness calculation pass It is also possible to provide the named argument *maxlen*, which specifies the size of the memoization cache to use. (If *maxlen* is ``None``, then an unbounded cache is used.) Once the size of the cache has reached *maxlen*, the oldest element is replaced by the newest element in order to keep the size constant. This usage is as follows:: @memoize(maxlen=100) def expensive_fitness_function(candidates, args): # Implementation of expensive fitness calculation pass .. warning:: The ``maxlen`` parameter must be passed as a named keyword argument, or an ``AttributeError`` will be raised (e.g., saying ``@memoize(100)`` will cause an error). """ if func is not None: cache = BoundedOrderedDict(maxlen=maxlen) @functools.wraps(func) def memo_target(candidates, args): fitness = [] for candidate in candidates: lookup_value = pickle.dumps(candidate, 1) if lookup_value not in cache: cache[lookup_value] = func([candidate], args)[0] fitness.append(cache[lookup_value]) return fitness return memo_target else: def memoize_factory(func): return memoize(func, maxlen=maxlen) return memoize_factory
python
def memoize(func=None, maxlen=None): if func is not None: cache = BoundedOrderedDict(maxlen=maxlen) @functools.wraps(func) def memo_target(candidates, args): fitness = [] for candidate in candidates: lookup_value = pickle.dumps(candidate, 1) if lookup_value not in cache: cache[lookup_value] = func([candidate], args)[0] fitness.append(cache[lookup_value]) return fitness return memo_target else: def memoize_factory(func): return memoize(func, maxlen=maxlen) return memoize_factory
[ "def", "memoize", "(", "func", "=", "None", ",", "maxlen", "=", "None", ")", ":", "if", "func", "is", "not", "None", ":", "cache", "=", "BoundedOrderedDict", "(", "maxlen", "=", "maxlen", ")", "@", "functools", ".", "wraps", "(", "func", ")", "def", "memo_target", "(", "candidates", ",", "args", ")", ":", "fitness", "=", "[", "]", "for", "candidate", "in", "candidates", ":", "lookup_value", "=", "pickle", ".", "dumps", "(", "candidate", ",", "1", ")", "if", "lookup_value", "not", "in", "cache", ":", "cache", "[", "lookup_value", "]", "=", "func", "(", "[", "candidate", "]", ",", "args", ")", "[", "0", "]", "fitness", ".", "append", "(", "cache", "[", "lookup_value", "]", ")", "return", "fitness", "return", "memo_target", "else", ":", "def", "memoize_factory", "(", "func", ")", ":", "return", "memoize", "(", "func", ",", "maxlen", "=", "maxlen", ")", "return", "memoize_factory" ]
Cache a function's return value each time it is called. This function serves as a function decorator to provide a caching of evaluated fitness values. If called later with the same arguments, the cached value is returned instead of being re-evaluated. This decorator assumes that candidates are individually pickleable, and their pickled values are used for hashing into a dictionary. It should be used when evaluating an *expensive* fitness function to avoid costly re-evaluation of those fitnesses. The typical usage is as follows:: @memoize def expensive_fitness_function(candidates, args): # Implementation of expensive fitness calculation pass It is also possible to provide the named argument *maxlen*, which specifies the size of the memoization cache to use. (If *maxlen* is ``None``, then an unbounded cache is used.) Once the size of the cache has reached *maxlen*, the oldest element is replaced by the newest element in order to keep the size constant. This usage is as follows:: @memoize(maxlen=100) def expensive_fitness_function(candidates, args): # Implementation of expensive fitness calculation pass .. warning:: The ``maxlen`` parameter must be passed as a named keyword argument, or an ``AttributeError`` will be raised (e.g., saying ``@memoize(100)`` will cause an error).
[ "Cache", "a", "function", "s", "return", "value", "each", "time", "it", "is", "called", ".", "This", "function", "serves", "as", "a", "function", "decorator", "to", "provide", "a", "caching", "of", "evaluated", "fitness", "values", ".", "If", "called", "later", "with", "the", "same", "arguments", "the", "cached", "value", "is", "returned", "instead", "of", "being", "re", "-", "evaluated", ".", "This", "decorator", "assumes", "that", "candidates", "are", "individually", "pickleable", "and", "their", "pickled", "values", "are", "used", "for", "hashing", "into", "a", "dictionary", ".", "It", "should", "be", "used", "when", "evaluating", "an", "*", "expensive", "*", "fitness", "function", "to", "avoid", "costly", "re", "-", "evaluation", "of", "those", "fitnesses", ".", "The", "typical", "usage", "is", "as", "follows", "::" ]
train
https://github.com/aarongarrett/inspyred/blob/d5976ab503cc9d51c6f586cbb7bb601a38c01128/inspyred/ec/utilities.py#L252-L301
djsutho/django-debug-toolbar-request-history
ddt_request_history/panels/request_history.py
allow_ajax
def allow_ajax(request): """ Default function to determine whether to show the toolbar on a given page. """ if request.META.get('REMOTE_ADDR', None) not in settings.INTERNAL_IPS: return False if toolbar_version < LooseVersion('1.8') \ and request.get_full_path().startswith(DEBUG_TOOLBAR_URL_PREFIX) \ and request.GET.get('panel_id', None) != 'RequestHistoryPanel': return False return bool(settings.DEBUG)
python
def allow_ajax(request): if request.META.get('REMOTE_ADDR', None) not in settings.INTERNAL_IPS: return False if toolbar_version < LooseVersion('1.8') \ and request.get_full_path().startswith(DEBUG_TOOLBAR_URL_PREFIX) \ and request.GET.get('panel_id', None) != 'RequestHistoryPanel': return False return bool(settings.DEBUG)
[ "def", "allow_ajax", "(", "request", ")", ":", "if", "request", ".", "META", ".", "get", "(", "'REMOTE_ADDR'", ",", "None", ")", "not", "in", "settings", ".", "INTERNAL_IPS", ":", "return", "False", "if", "toolbar_version", "<", "LooseVersion", "(", "'1.8'", ")", "and", "request", ".", "get_full_path", "(", ")", ".", "startswith", "(", "DEBUG_TOOLBAR_URL_PREFIX", ")", "and", "request", ".", "GET", ".", "get", "(", "'panel_id'", ",", "None", ")", "!=", "'RequestHistoryPanel'", ":", "return", "False", "return", "bool", "(", "settings", ".", "DEBUG", ")" ]
Default function to determine whether to show the toolbar on a given page.
[ "Default", "function", "to", "determine", "whether", "to", "show", "the", "toolbar", "on", "a", "given", "page", "." ]
train
https://github.com/djsutho/django-debug-toolbar-request-history/blob/b3da3e12762d68c23a307ffb279e6047f80ba695/ddt_request_history/panels/request_history.py#L104-L114
djsutho/django-debug-toolbar-request-history
ddt_request_history/panels/request_history.py
RequestHistoryPanel.content
def content(self): """ Content of the panel when it's displayed in full screen. """ toolbars = OrderedDict() for id, toolbar in DebugToolbar._store.items(): content = {} for panel in toolbar.panels: panel_id = None nav_title = '' nav_subtitle = '' try: panel_id = panel.panel_id nav_title = panel.nav_title nav_subtitle = panel.nav_subtitle() if isinstance( panel.nav_subtitle, Callable) else panel.nav_subtitle except Exception: logger.debug('Error parsing panel info:', exc_info=True) if panel_id is not None: content.update({ panel_id: { 'panel_id': panel_id, 'nav_title': nav_title, 'nav_subtitle': nav_subtitle, } }) toolbars[id] = { 'toolbar': toolbar, 'content': content } return get_template().render(Context({ 'toolbars': OrderedDict(reversed(list(toolbars.items()))), 'trunc_length': CONFIG.get('RH_POST_TRUNC_LENGTH', 0) }))
python
def content(self): toolbars = OrderedDict() for id, toolbar in DebugToolbar._store.items(): content = {} for panel in toolbar.panels: panel_id = None nav_title = '' nav_subtitle = '' try: panel_id = panel.panel_id nav_title = panel.nav_title nav_subtitle = panel.nav_subtitle() if isinstance( panel.nav_subtitle, Callable) else panel.nav_subtitle except Exception: logger.debug('Error parsing panel info:', exc_info=True) if panel_id is not None: content.update({ panel_id: { 'panel_id': panel_id, 'nav_title': nav_title, 'nav_subtitle': nav_subtitle, } }) toolbars[id] = { 'toolbar': toolbar, 'content': content } return get_template().render(Context({ 'toolbars': OrderedDict(reversed(list(toolbars.items()))), 'trunc_length': CONFIG.get('RH_POST_TRUNC_LENGTH', 0) }))
[ "def", "content", "(", "self", ")", ":", "toolbars", "=", "OrderedDict", "(", ")", "for", "id", ",", "toolbar", "in", "DebugToolbar", ".", "_store", ".", "items", "(", ")", ":", "content", "=", "{", "}", "for", "panel", "in", "toolbar", ".", "panels", ":", "panel_id", "=", "None", "nav_title", "=", "''", "nav_subtitle", "=", "''", "try", ":", "panel_id", "=", "panel", ".", "panel_id", "nav_title", "=", "panel", ".", "nav_title", "nav_subtitle", "=", "panel", ".", "nav_subtitle", "(", ")", "if", "isinstance", "(", "panel", ".", "nav_subtitle", ",", "Callable", ")", "else", "panel", ".", "nav_subtitle", "except", "Exception", ":", "logger", ".", "debug", "(", "'Error parsing panel info:'", ",", "exc_info", "=", "True", ")", "if", "panel_id", "is", "not", "None", ":", "content", ".", "update", "(", "{", "panel_id", ":", "{", "'panel_id'", ":", "panel_id", ",", "'nav_title'", ":", "nav_title", ",", "'nav_subtitle'", ":", "nav_subtitle", ",", "}", "}", ")", "toolbars", "[", "id", "]", "=", "{", "'toolbar'", ":", "toolbar", ",", "'content'", ":", "content", "}", "return", "get_template", "(", ")", ".", "render", "(", "Context", "(", "{", "'toolbars'", ":", "OrderedDict", "(", "reversed", "(", "list", "(", "toolbars", ".", "items", "(", ")", ")", ")", ")", ",", "'trunc_length'", ":", "CONFIG", ".", "get", "(", "'RH_POST_TRUNC_LENGTH'", ",", "0", ")", "}", ")", ")" ]
Content of the panel when it's displayed in full screen.
[ "Content", "of", "the", "panel", "when", "it", "s", "displayed", "in", "full", "screen", "." ]
train
https://github.com/djsutho/django-debug-toolbar-request-history/blob/b3da3e12762d68c23a307ffb279e6047f80ba695/ddt_request_history/panels/request_history.py#L184-L215
AlexMathew/scrapple
scrapple/commands/web.py
WebCommand.execute_command
def execute_command(self): """ The web command runs the Scrapple web interface through a simple \ `Flask <http://flask.pocoo.org>`_ app. When the execute_command() method is called from the \ :ref:`runCLI() <implementation-cli>` function, it starts of two simultaneous \ processes : - Calls the run_flask() method to start the Flask app on port 5000 of localhost - Opens the web interface on a web browser The '/' view of the Flask app, opens up the Scrapple web interface. This \ provides a basic form, to fill in the required configuration file. On submitting \ the form, it makes a POST request, passing in the form in the request header. \ This form is passed to the form_to_json() \ :ref:`utility function <implementation-utils>`, where the form is converted into \ the resultant JSON configuration file. Currently, closing the web command execution requires making a keyboard interrupt \ on the command line after the web interface has been closed. """ print(Back.GREEN + Fore.BLACK + "Scrapple Web Interface") print(Back.RESET + Fore.RESET) p1 = Process(target = self.run_flask) p2 = Process(target = lambda : webbrowser.open('http://127.0.0.1:5000')) p1.start() p2.start()
python
def execute_command(self): print(Back.GREEN + Fore.BLACK + "Scrapple Web Interface") print(Back.RESET + Fore.RESET) p1 = Process(target = self.run_flask) p2 = Process(target = lambda : webbrowser.open('http://127.0.0.1:5000')) p1.start() p2.start()
[ "def", "execute_command", "(", "self", ")", ":", "print", "(", "Back", ".", "GREEN", "+", "Fore", ".", "BLACK", "+", "\"Scrapple Web Interface\"", ")", "print", "(", "Back", ".", "RESET", "+", "Fore", ".", "RESET", ")", "p1", "=", "Process", "(", "target", "=", "self", ".", "run_flask", ")", "p2", "=", "Process", "(", "target", "=", "lambda", ":", "webbrowser", ".", "open", "(", "'http://127.0.0.1:5000'", ")", ")", "p1", ".", "start", "(", ")", "p2", ".", "start", "(", ")" ]
The web command runs the Scrapple web interface through a simple \ `Flask <http://flask.pocoo.org>`_ app. When the execute_command() method is called from the \ :ref:`runCLI() <implementation-cli>` function, it starts of two simultaneous \ processes : - Calls the run_flask() method to start the Flask app on port 5000 of localhost - Opens the web interface on a web browser The '/' view of the Flask app, opens up the Scrapple web interface. This \ provides a basic form, to fill in the required configuration file. On submitting \ the form, it makes a POST request, passing in the form in the request header. \ This form is passed to the form_to_json() \ :ref:`utility function <implementation-utils>`, where the form is converted into \ the resultant JSON configuration file. Currently, closing the web command execution requires making a keyboard interrupt \ on the command line after the web interface has been closed.
[ "The", "web", "command", "runs", "the", "Scrapple", "web", "interface", "through", "a", "simple", "\\", "Flask", "<http", ":", "//", "flask", ".", "pocoo", ".", "org", ">", "_", "app", "." ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/commands/web.py#L39-L67
AlexMathew/scrapple
scrapple/commands/generate.py
GenerateCommand.execute_command
def execute_command(self): """ The generate command uses `Jinja2 <http://jinja.pocoo.org/>`_ templates \ to create Python scripts, according to the specification in the configuration \ file. The predefined templates use the extract_content() method of the \ :ref:`selector classes <implementation-selectors>` to implement linear extractors \ and use recursive for loops to implement multiple levels of link crawlers. This \ implementation is effectively a representation of the traverse_next() \ :ref:`utility function <implementation-utils>`, using the loop depth to \ differentiate between levels of the crawler execution. According to the --output_type argument in the CLI input, the results are \ written into a JSON document or a CSV document. The Python script is written into <output_filename>.py - running this file \ is the equivalent of using the Scrapple :ref:`run command <command-run>`. """ print(Back.GREEN + Fore.BLACK + "Scrapple Generate") print(Back.RESET + Fore.RESET) directory = os.path.join(scrapple.__path__[0], 'templates', 'scripts') with open(os.path.join(directory, 'generate.txt'), 'r') as f: template_content = f.read() template = Template(template_content) try: with open(self.args['<projectname>'] + '.json', 'r') as f: config = json.load(f) if self.args['--output_type'] == 'csv': from scrapple.utils.config import extract_fieldnames config['fields'] = str(extract_fieldnames(config)) config['output_file'] = self.args['<output_filename>'] config['output_type'] = self.args['--output_type'] rendered = template.render(config=config) with open(self.args['<output_filename>'] + '.py', 'w') as f: f.write(rendered) print(Back.WHITE + Fore.RED + self.args['<output_filename>'], \ ".py has been created" + Back.RESET + Fore.RESET, sep="") except IOError: print(Back.WHITE + Fore.RED + self.args['<projectname>'], ".json does not ", \ "exist. Use ``scrapple genconfig``." + Back.RESET + Fore.RESET, sep="")
python
def execute_command(self): print(Back.GREEN + Fore.BLACK + "Scrapple Generate") print(Back.RESET + Fore.RESET) directory = os.path.join(scrapple.__path__[0], 'templates', 'scripts') with open(os.path.join(directory, 'generate.txt'), 'r') as f: template_content = f.read() template = Template(template_content) try: with open(self.args['<projectname>'] + '.json', 'r') as f: config = json.load(f) if self.args['--output_type'] == 'csv': from scrapple.utils.config import extract_fieldnames config['fields'] = str(extract_fieldnames(config)) config['output_file'] = self.args['<output_filename>'] config['output_type'] = self.args['--output_type'] rendered = template.render(config=config) with open(self.args['<output_filename>'] + '.py', 'w') as f: f.write(rendered) print(Back.WHITE + Fore.RED + self.args['<output_filename>'], \ ".py has been created" + Back.RESET + Fore.RESET, sep="") except IOError: print(Back.WHITE + Fore.RED + self.args['<projectname>'], ".json does not ", \ "exist. Use ``scrapple genconfig``." + Back.RESET + Fore.RESET, sep="")
[ "def", "execute_command", "(", "self", ")", ":", "print", "(", "Back", ".", "GREEN", "+", "Fore", ".", "BLACK", "+", "\"Scrapple Generate\"", ")", "print", "(", "Back", ".", "RESET", "+", "Fore", ".", "RESET", ")", "directory", "=", "os", ".", "path", ".", "join", "(", "scrapple", ".", "__path__", "[", "0", "]", ",", "'templates'", ",", "'scripts'", ")", "with", "open", "(", "os", ".", "path", ".", "join", "(", "directory", ",", "'generate.txt'", ")", ",", "'r'", ")", "as", "f", ":", "template_content", "=", "f", ".", "read", "(", ")", "template", "=", "Template", "(", "template_content", ")", "try", ":", "with", "open", "(", "self", ".", "args", "[", "'<projectname>'", "]", "+", "'.json'", ",", "'r'", ")", "as", "f", ":", "config", "=", "json", ".", "load", "(", "f", ")", "if", "self", ".", "args", "[", "'--output_type'", "]", "==", "'csv'", ":", "from", "scrapple", ".", "utils", ".", "config", "import", "extract_fieldnames", "config", "[", "'fields'", "]", "=", "str", "(", "extract_fieldnames", "(", "config", ")", ")", "config", "[", "'output_file'", "]", "=", "self", ".", "args", "[", "'<output_filename>'", "]", "config", "[", "'output_type'", "]", "=", "self", ".", "args", "[", "'--output_type'", "]", "rendered", "=", "template", ".", "render", "(", "config", "=", "config", ")", "with", "open", "(", "self", ".", "args", "[", "'<output_filename>'", "]", "+", "'.py'", ",", "'w'", ")", "as", "f", ":", "f", ".", "write", "(", "rendered", ")", "print", "(", "Back", ".", "WHITE", "+", "Fore", ".", "RED", "+", "self", ".", "args", "[", "'<output_filename>'", "]", ",", "\".py has been created\"", "+", "Back", ".", "RESET", "+", "Fore", ".", "RESET", ",", "sep", "=", "\"\"", ")", "except", "IOError", ":", "print", "(", "Back", ".", "WHITE", "+", "Fore", ".", "RED", "+", "self", ".", "args", "[", "'<projectname>'", "]", ",", "\".json does not \"", ",", "\"exist. Use ``scrapple genconfig``.\"", "+", "Back", ".", "RESET", "+", "Fore", ".", "RESET", ",", "sep", "=", "\"\"", ")" ]
The generate command uses `Jinja2 <http://jinja.pocoo.org/>`_ templates \ to create Python scripts, according to the specification in the configuration \ file. The predefined templates use the extract_content() method of the \ :ref:`selector classes <implementation-selectors>` to implement linear extractors \ and use recursive for loops to implement multiple levels of link crawlers. This \ implementation is effectively a representation of the traverse_next() \ :ref:`utility function <implementation-utils>`, using the loop depth to \ differentiate between levels of the crawler execution. According to the --output_type argument in the CLI input, the results are \ written into a JSON document or a CSV document. The Python script is written into <output_filename>.py - running this file \ is the equivalent of using the Scrapple :ref:`run command <command-run>`.
[ "The", "generate", "command", "uses", "Jinja2", "<http", ":", "//", "jinja", ".", "pocoo", ".", "org", "/", ">", "_", "templates", "\\", "to", "create", "Python", "scripts", "according", "to", "the", "specification", "in", "the", "configuration", "\\", "file", ".", "The", "predefined", "templates", "use", "the", "extract_content", "()", "method", "of", "the", "\\", ":", "ref", ":", "selector", "classes", "<implementation", "-", "selectors", ">", "to", "implement", "linear", "extractors", "\\", "and", "use", "recursive", "for", "loops", "to", "implement", "multiple", "levels", "of", "link", "crawlers", ".", "This", "\\", "implementation", "is", "effectively", "a", "representation", "of", "the", "traverse_next", "()", "\\", ":", "ref", ":", "utility", "function", "<implementation", "-", "utils", ">", "using", "the", "loop", "depth", "to", "\\", "differentiate", "between", "levels", "of", "the", "crawler", "execution", "." ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/commands/generate.py#L28-L67
AlexMathew/scrapple
scrapple/selectors/selector.py
Selector.extract_content
def extract_content(self, selector='', attr='', default='', connector='', *args, **kwargs): """ Method for performing the content extraction for the particular selector type. \ If the selector is "url", the URL of the current web page is returned. Otherwise, the selector expression is used to extract content. The particular \ attribute to be extracted ("text", "href", etc.) is specified in the method \ arguments, and this is used to extract the required content. If the content \ extracted is a link (from an attr value of "href" or "src"), the URL is parsed \ to convert the relative path into an absolute path. If the selector does not fetch any content, the default value is returned. \ If no default value is specified, an exception is raised. :param selector: The XPath expression :param attr: The attribute to be extracted from the selected tag :param default: The default value to be used if the selector does not return any data :param connector: String connector for list of data returned for a particular selector :return: The extracted content """ try: if selector.lower() == "url": return self.url if attr.lower() == "text": tag = self.get_tree_tag(selector=selector, get_one=True) content = connector.join([make_ascii(x).strip() for x in tag.itertext()]) content = content.replace("\n", " ").strip() else: tag = self.get_tree_tag(selector=selector, get_one=True) content = tag.get(attr) if attr in ["href", "src"]: content = urljoin(self.url, content) return content except IndexError: if default is not "": return default raise Exception("There is no content for the %s selector - %s" % (self.__selector_type__, selector)) except XPathError: raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector))
python
def extract_content(self, selector='', attr='', default='', connector='', *args, **kwargs): try: if selector.lower() == "url": return self.url if attr.lower() == "text": tag = self.get_tree_tag(selector=selector, get_one=True) content = connector.join([make_ascii(x).strip() for x in tag.itertext()]) content = content.replace("\n", " ").strip() else: tag = self.get_tree_tag(selector=selector, get_one=True) content = tag.get(attr) if attr in ["href", "src"]: content = urljoin(self.url, content) return content except IndexError: if default is not "": return default raise Exception("There is no content for the %s selector - %s" % (self.__selector_type__, selector)) except XPathError: raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector))
[ "def", "extract_content", "(", "self", ",", "selector", "=", "''", ",", "attr", "=", "''", ",", "default", "=", "''", ",", "connector", "=", "''", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "try", ":", "if", "selector", ".", "lower", "(", ")", "==", "\"url\"", ":", "return", "self", ".", "url", "if", "attr", ".", "lower", "(", ")", "==", "\"text\"", ":", "tag", "=", "self", ".", "get_tree_tag", "(", "selector", "=", "selector", ",", "get_one", "=", "True", ")", "content", "=", "connector", ".", "join", "(", "[", "make_ascii", "(", "x", ")", ".", "strip", "(", ")", "for", "x", "in", "tag", ".", "itertext", "(", ")", "]", ")", "content", "=", "content", ".", "replace", "(", "\"\\n\"", ",", "\" \"", ")", ".", "strip", "(", ")", "else", ":", "tag", "=", "self", ".", "get_tree_tag", "(", "selector", "=", "selector", ",", "get_one", "=", "True", ")", "content", "=", "tag", ".", "get", "(", "attr", ")", "if", "attr", "in", "[", "\"href\"", ",", "\"src\"", "]", ":", "content", "=", "urljoin", "(", "self", ".", "url", ",", "content", ")", "return", "content", "except", "IndexError", ":", "if", "default", "is", "not", "\"\"", ":", "return", "default", "raise", "Exception", "(", "\"There is no content for the %s selector - %s\"", "%", "(", "self", ".", "__selector_type__", ",", "selector", ")", ")", "except", "XPathError", ":", "raise", "Exception", "(", "\"Invalid %s selector - %s\"", "%", "(", "self", ".", "__selector_type__", ",", "selector", ")", ")" ]
Method for performing the content extraction for the particular selector type. \ If the selector is "url", the URL of the current web page is returned. Otherwise, the selector expression is used to extract content. The particular \ attribute to be extracted ("text", "href", etc.) is specified in the method \ arguments, and this is used to extract the required content. If the content \ extracted is a link (from an attr value of "href" or "src"), the URL is parsed \ to convert the relative path into an absolute path. If the selector does not fetch any content, the default value is returned. \ If no default value is specified, an exception is raised. :param selector: The XPath expression :param attr: The attribute to be extracted from the selected tag :param default: The default value to be used if the selector does not return any data :param connector: String connector for list of data returned for a particular selector :return: The extracted content
[ "Method", "for", "performing", "the", "content", "extraction", "for", "the", "particular", "selector", "type", ".", "\\" ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/selectors/selector.py#L81-L119
AlexMathew/scrapple
scrapple/selectors/selector.py
Selector.extract_links
def extract_links(self, selector='', *args, **kwargs): """ Method for performing the link extraction for the crawler. \ The selector passed as the argument is a selector to point to the anchor tags \ that the crawler should pass through. A list of links is obtained, and the links \ are iterated through. The relative paths are converted into absolute paths and \ a ``XpathSelector``/``CssSelector`` object (as is the case) is created with the URL of the next page as the argument \ and this created object is yielded. The extract_links method basically generates ``XpathSelector``/``CssSelector`` objects for all of \ the links to be crawled through. :param selector: The selector for the anchor tags to be crawled through :return: A ``XpathSelector``/``CssSelector`` object for every page to be crawled through """ try: links = self.get_tree_tag(selector=selector) for link in links: next_url = urljoin(self.url, link.get('href')) yield type(self)(next_url) except XPathError: raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector)) except Exception: raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector))
python
def extract_links(self, selector='', *args, **kwargs): try: links = self.get_tree_tag(selector=selector) for link in links: next_url = urljoin(self.url, link.get('href')) yield type(self)(next_url) except XPathError: raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector)) except Exception: raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector))
[ "def", "extract_links", "(", "self", ",", "selector", "=", "''", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "try", ":", "links", "=", "self", ".", "get_tree_tag", "(", "selector", "=", "selector", ")", "for", "link", "in", "links", ":", "next_url", "=", "urljoin", "(", "self", ".", "url", ",", "link", ".", "get", "(", "'href'", ")", ")", "yield", "type", "(", "self", ")", "(", "next_url", ")", "except", "XPathError", ":", "raise", "Exception", "(", "\"Invalid %s selector - %s\"", "%", "(", "self", ".", "__selector_type__", ",", "selector", ")", ")", "except", "Exception", ":", "raise", "Exception", "(", "\"Invalid %s selector - %s\"", "%", "(", "self", ".", "__selector_type__", ",", "selector", ")", ")" ]
Method for performing the link extraction for the crawler. \ The selector passed as the argument is a selector to point to the anchor tags \ that the crawler should pass through. A list of links is obtained, and the links \ are iterated through. The relative paths are converted into absolute paths and \ a ``XpathSelector``/``CssSelector`` object (as is the case) is created with the URL of the next page as the argument \ and this created object is yielded. The extract_links method basically generates ``XpathSelector``/``CssSelector`` objects for all of \ the links to be crawled through. :param selector: The selector for the anchor tags to be crawled through :return: A ``XpathSelector``/``CssSelector`` object for every page to be crawled through
[ "Method", "for", "performing", "the", "link", "extraction", "for", "the", "crawler", ".", "\\" ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/selectors/selector.py#L122-L147
AlexMathew/scrapple
scrapple/selectors/selector.py
Selector.extract_tabular
def extract_tabular(self, header='', prefix='', suffix='', table_type='', *args, **kwargs): """ Method for performing the tabular data extraction. \ :param result: A dictionary containing the extracted data so far :param table_type: Can be "rows" or "columns". This determines the type of table to be extracted. \ A row extraction is when there is a single row to be extracted and mapped to a set of headers. \ A column extraction is when a set of rows have to be extracted, giving a list of header-value mappings. :param header: The headers to be used for the table. This can be a list of headers, or a selector that gives the list of headers :param prefix: A prefix to be added to each header :param suffix: A suffix to be added to each header :param selector: For row extraction, this is a selector that gives the row to be extracted. \ For column extraction, this is a list of selectors for each column. :param attr: The attribute to be extracted from the selected tag :param default: The default value to be used if the selector does not return any data :param verbosity: The verbosity set as the argument for scrapple run :return: A 2-tuple containing the list of all the column headers extracted and the list of \ dictionaries which contain (header, content) pairs """ if type(header) in [str, unicode]: try: header_list = self.get_tree_tag(header) table_headers = [prefix + h.text + suffix for h in header_list] except XPathError: raise Exception("Invalid %s selector for table header - %s" % (self.__selector_type__, header)) except Exception: raise Exception("Invalid %s selector for table header - %s" % (self.__selector_type__, header)) else: table_headers = [prefix + h + suffix for h in header] if len(table_headers) == 0: raise Exception("Invalid %s selector for table header - %s" % (self.__selector_type__, header)) if table_type not in ["rows", "columns"]: raise Exception("Specify 'rows' or 'columns' in table_type") if table_type == "rows": result_list = self.extract_rows(table_headers=table_headers, *args, **kwargs) else: result_list = self.extract_columns(table_headers=table_headers, *args, **kwargs) return table_headers, result_list
python
def extract_tabular(self, header='', prefix='', suffix='', table_type='', *args, **kwargs): if type(header) in [str, unicode]: try: header_list = self.get_tree_tag(header) table_headers = [prefix + h.text + suffix for h in header_list] except XPathError: raise Exception("Invalid %s selector for table header - %s" % (self.__selector_type__, header)) except Exception: raise Exception("Invalid %s selector for table header - %s" % (self.__selector_type__, header)) else: table_headers = [prefix + h + suffix for h in header] if len(table_headers) == 0: raise Exception("Invalid %s selector for table header - %s" % (self.__selector_type__, header)) if table_type not in ["rows", "columns"]: raise Exception("Specify 'rows' or 'columns' in table_type") if table_type == "rows": result_list = self.extract_rows(table_headers=table_headers, *args, **kwargs) else: result_list = self.extract_columns(table_headers=table_headers, *args, **kwargs) return table_headers, result_list
[ "def", "extract_tabular", "(", "self", ",", "header", "=", "''", ",", "prefix", "=", "''", ",", "suffix", "=", "''", ",", "table_type", "=", "''", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "type", "(", "header", ")", "in", "[", "str", ",", "unicode", "]", ":", "try", ":", "header_list", "=", "self", ".", "get_tree_tag", "(", "header", ")", "table_headers", "=", "[", "prefix", "+", "h", ".", "text", "+", "suffix", "for", "h", "in", "header_list", "]", "except", "XPathError", ":", "raise", "Exception", "(", "\"Invalid %s selector for table header - %s\"", "%", "(", "self", ".", "__selector_type__", ",", "header", ")", ")", "except", "Exception", ":", "raise", "Exception", "(", "\"Invalid %s selector for table header - %s\"", "%", "(", "self", ".", "__selector_type__", ",", "header", ")", ")", "else", ":", "table_headers", "=", "[", "prefix", "+", "h", "+", "suffix", "for", "h", "in", "header", "]", "if", "len", "(", "table_headers", ")", "==", "0", ":", "raise", "Exception", "(", "\"Invalid %s selector for table header - %s\"", "%", "(", "self", ".", "__selector_type__", ",", "header", ")", ")", "if", "table_type", "not", "in", "[", "\"rows\"", ",", "\"columns\"", "]", ":", "raise", "Exception", "(", "\"Specify 'rows' or 'columns' in table_type\"", ")", "if", "table_type", "==", "\"rows\"", ":", "result_list", "=", "self", ".", "extract_rows", "(", "table_headers", "=", "table_headers", ",", "*", "args", ",", "*", "*", "kwargs", ")", "else", ":", "result_list", "=", "self", ".", "extract_columns", "(", "table_headers", "=", "table_headers", ",", "*", "args", ",", "*", "*", "kwargs", ")", "return", "table_headers", ",", "result_list" ]
Method for performing the tabular data extraction. \ :param result: A dictionary containing the extracted data so far :param table_type: Can be "rows" or "columns". This determines the type of table to be extracted. \ A row extraction is when there is a single row to be extracted and mapped to a set of headers. \ A column extraction is when a set of rows have to be extracted, giving a list of header-value mappings. :param header: The headers to be used for the table. This can be a list of headers, or a selector that gives the list of headers :param prefix: A prefix to be added to each header :param suffix: A suffix to be added to each header :param selector: For row extraction, this is a selector that gives the row to be extracted. \ For column extraction, this is a list of selectors for each column. :param attr: The attribute to be extracted from the selected tag :param default: The default value to be used if the selector does not return any data :param verbosity: The verbosity set as the argument for scrapple run :return: A 2-tuple containing the list of all the column headers extracted and the list of \ dictionaries which contain (header, content) pairs
[ "Method", "for", "performing", "the", "tabular", "data", "extraction", ".", "\\" ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/selectors/selector.py#L150-L187
AlexMathew/scrapple
scrapple/selectors/selector.py
Selector.extract_rows
def extract_rows(self, result={}, selector='', table_headers=[], attr='', connector='', default='', verbosity=0, *args, **kwargs): """ Row data extraction for extract_tabular """ result_list = [] try: values = self.get_tree_tag(selector) if len(table_headers) >= len(values): from itertools import izip_longest pairs = izip_longest(table_headers, values, fillvalue=default) else: from itertools import izip pairs = izip(table_headers, values) for head, val in pairs: if verbosity > 1: print("\nExtracting", head, "attribute", sep=' ', end='') if attr.lower() == "text": try: content = connector.join([make_ascii(x).strip() for x in val.itertext()]) except Exception: content = default content = content.replace("\n", " ").strip() else: content = val.get(attr) if attr in ["href", "src"]: content = urljoin(self.url, content) result[head] = content result_list.append(result) except XPathError: raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector)) except TypeError: raise Exception("Selector expression string to be provided. Got " + selector) return result_list
python
def extract_rows(self, result={}, selector='', table_headers=[], attr='', connector='', default='', verbosity=0, *args, **kwargs): result_list = [] try: values = self.get_tree_tag(selector) if len(table_headers) >= len(values): from itertools import izip_longest pairs = izip_longest(table_headers, values, fillvalue=default) else: from itertools import izip pairs = izip(table_headers, values) for head, val in pairs: if verbosity > 1: print("\nExtracting", head, "attribute", sep=' ', end='') if attr.lower() == "text": try: content = connector.join([make_ascii(x).strip() for x in val.itertext()]) except Exception: content = default content = content.replace("\n", " ").strip() else: content = val.get(attr) if attr in ["href", "src"]: content = urljoin(self.url, content) result[head] = content result_list.append(result) except XPathError: raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector)) except TypeError: raise Exception("Selector expression string to be provided. Got " + selector) return result_list
[ "def", "extract_rows", "(", "self", ",", "result", "=", "{", "}", ",", "selector", "=", "''", ",", "table_headers", "=", "[", "]", ",", "attr", "=", "''", ",", "connector", "=", "''", ",", "default", "=", "''", ",", "verbosity", "=", "0", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "result_list", "=", "[", "]", "try", ":", "values", "=", "self", ".", "get_tree_tag", "(", "selector", ")", "if", "len", "(", "table_headers", ")", ">=", "len", "(", "values", ")", ":", "from", "itertools", "import", "izip_longest", "pairs", "=", "izip_longest", "(", "table_headers", ",", "values", ",", "fillvalue", "=", "default", ")", "else", ":", "from", "itertools", "import", "izip", "pairs", "=", "izip", "(", "table_headers", ",", "values", ")", "for", "head", ",", "val", "in", "pairs", ":", "if", "verbosity", ">", "1", ":", "print", "(", "\"\\nExtracting\"", ",", "head", ",", "\"attribute\"", ",", "sep", "=", "' '", ",", "end", "=", "''", ")", "if", "attr", ".", "lower", "(", ")", "==", "\"text\"", ":", "try", ":", "content", "=", "connector", ".", "join", "(", "[", "make_ascii", "(", "x", ")", ".", "strip", "(", ")", "for", "x", "in", "val", ".", "itertext", "(", ")", "]", ")", "except", "Exception", ":", "content", "=", "default", "content", "=", "content", ".", "replace", "(", "\"\\n\"", ",", "\" \"", ")", ".", "strip", "(", ")", "else", ":", "content", "=", "val", ".", "get", "(", "attr", ")", "if", "attr", "in", "[", "\"href\"", ",", "\"src\"", "]", ":", "content", "=", "urljoin", "(", "self", ".", "url", ",", "content", ")", "result", "[", "head", "]", "=", "content", "result_list", ".", "append", "(", "result", ")", "except", "XPathError", ":", "raise", "Exception", "(", "\"Invalid %s selector - %s\"", "%", "(", "self", ".", "__selector_type__", ",", "selector", ")", ")", "except", "TypeError", ":", "raise", "Exception", "(", "\"Selector expression string to be provided. Got \"", "+", "selector", ")", "return", "result_list" ]
Row data extraction for extract_tabular
[ "Row", "data", "extraction", "for", "extract_tabular" ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/selectors/selector.py#L190-L224
AlexMathew/scrapple
scrapple/selectors/selector.py
Selector.extract_columns
def extract_columns(self, result={}, selector='', table_headers=[], attr='', connector='', default='', verbosity=0, *args, **kwargs): """ Column data extraction for extract_tabular """ result_list = [] try: if type(selector) in [str, unicode]: selectors = [selector] elif type(selector) == list: selectors = selector[:] else: raise Exception("Use a list of selector expressions for the various columns") from itertools import izip, count pairs = izip(table_headers, selectors) columns = {} for head, selector in pairs: columns[head] = self.get_tree_tag(selector) try: for i in count(start=0): r = result.copy() for head in columns.keys(): if verbosity > 1: print("\nExtracting", head, "attribute", sep=' ', end='') col = columns[head][i] if attr == "text": try: content = connector.join([make_ascii(x).strip() for x in col.itertext()]) except Exception: content = default content = content.replace("\n", " ").strip() else: content = col.get(attr) if attr in ["href", "src"]: content = urljoin(self.url, content) r[head] = content result_list.append(r) except IndexError: pass except XPathError: raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector)) except TypeError: raise Exception("Selector expression string to be provided. Got " + selector) return result_list
python
def extract_columns(self, result={}, selector='', table_headers=[], attr='', connector='', default='', verbosity=0, *args, **kwargs): result_list = [] try: if type(selector) in [str, unicode]: selectors = [selector] elif type(selector) == list: selectors = selector[:] else: raise Exception("Use a list of selector expressions for the various columns") from itertools import izip, count pairs = izip(table_headers, selectors) columns = {} for head, selector in pairs: columns[head] = self.get_tree_tag(selector) try: for i in count(start=0): r = result.copy() for head in columns.keys(): if verbosity > 1: print("\nExtracting", head, "attribute", sep=' ', end='') col = columns[head][i] if attr == "text": try: content = connector.join([make_ascii(x).strip() for x in col.itertext()]) except Exception: content = default content = content.replace("\n", " ").strip() else: content = col.get(attr) if attr in ["href", "src"]: content = urljoin(self.url, content) r[head] = content result_list.append(r) except IndexError: pass except XPathError: raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector)) except TypeError: raise Exception("Selector expression string to be provided. Got " + selector) return result_list
[ "def", "extract_columns", "(", "self", ",", "result", "=", "{", "}", ",", "selector", "=", "''", ",", "table_headers", "=", "[", "]", ",", "attr", "=", "''", ",", "connector", "=", "''", ",", "default", "=", "''", ",", "verbosity", "=", "0", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "result_list", "=", "[", "]", "try", ":", "if", "type", "(", "selector", ")", "in", "[", "str", ",", "unicode", "]", ":", "selectors", "=", "[", "selector", "]", "elif", "type", "(", "selector", ")", "==", "list", ":", "selectors", "=", "selector", "[", ":", "]", "else", ":", "raise", "Exception", "(", "\"Use a list of selector expressions for the various columns\"", ")", "from", "itertools", "import", "izip", ",", "count", "pairs", "=", "izip", "(", "table_headers", ",", "selectors", ")", "columns", "=", "{", "}", "for", "head", ",", "selector", "in", "pairs", ":", "columns", "[", "head", "]", "=", "self", ".", "get_tree_tag", "(", "selector", ")", "try", ":", "for", "i", "in", "count", "(", "start", "=", "0", ")", ":", "r", "=", "result", ".", "copy", "(", ")", "for", "head", "in", "columns", ".", "keys", "(", ")", ":", "if", "verbosity", ">", "1", ":", "print", "(", "\"\\nExtracting\"", ",", "head", ",", "\"attribute\"", ",", "sep", "=", "' '", ",", "end", "=", "''", ")", "col", "=", "columns", "[", "head", "]", "[", "i", "]", "if", "attr", "==", "\"text\"", ":", "try", ":", "content", "=", "connector", ".", "join", "(", "[", "make_ascii", "(", "x", ")", ".", "strip", "(", ")", "for", "x", "in", "col", ".", "itertext", "(", ")", "]", ")", "except", "Exception", ":", "content", "=", "default", "content", "=", "content", ".", "replace", "(", "\"\\n\"", ",", "\" \"", ")", ".", "strip", "(", ")", "else", ":", "content", "=", "col", ".", "get", "(", "attr", ")", "if", "attr", "in", "[", "\"href\"", ",", "\"src\"", "]", ":", "content", "=", "urljoin", "(", "self", ".", "url", ",", "content", ")", "r", "[", "head", "]", "=", "content", "result_list", ".", "append", "(", "r", ")", "except", "IndexError", ":", "pass", "except", "XPathError", ":", "raise", "Exception", "(", "\"Invalid %s selector - %s\"", "%", "(", "self", ".", "__selector_type__", ",", "selector", ")", ")", "except", "TypeError", ":", "raise", "Exception", "(", "\"Selector expression string to be provided. Got \"", "+", "selector", ")", "return", "result_list" ]
Column data extraction for extract_tabular
[ "Column", "data", "extraction", "for", "extract_tabular" ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/selectors/selector.py#L227-L271
AlexMathew/scrapple
scrapple/cmd.py
runCLI
def runCLI(): """ The starting point for the execution of the Scrapple command line tool. runCLI uses the docstring as the usage description for the scrapple command. \ The class for the required command is selected by a dynamic dispatch, and the \ command is executed through the execute_command() method of the command class. """ args = docopt(__doc__, version='0.3.0') try: check_arguments(args) command_list = ['genconfig', 'run', 'generate'] select = itemgetter('genconfig', 'run', 'generate') selectedCommand = command_list[select(args).index(True)] cmdClass = get_command_class(selectedCommand) obj = cmdClass(args) obj.execute_command() except POSSIBLE_EXCEPTIONS as e: print('\n', e, '\n')
python
def runCLI(): args = docopt(__doc__, version='0.3.0') try: check_arguments(args) command_list = ['genconfig', 'run', 'generate'] select = itemgetter('genconfig', 'run', 'generate') selectedCommand = command_list[select(args).index(True)] cmdClass = get_command_class(selectedCommand) obj = cmdClass(args) obj.execute_command() except POSSIBLE_EXCEPTIONS as e: print('\n', e, '\n')
[ "def", "runCLI", "(", ")", ":", "args", "=", "docopt", "(", "__doc__", ",", "version", "=", "'0.3.0'", ")", "try", ":", "check_arguments", "(", "args", ")", "command_list", "=", "[", "'genconfig'", ",", "'run'", ",", "'generate'", "]", "select", "=", "itemgetter", "(", "'genconfig'", ",", "'run'", ",", "'generate'", ")", "selectedCommand", "=", "command_list", "[", "select", "(", "args", ")", ".", "index", "(", "True", ")", "]", "cmdClass", "=", "get_command_class", "(", "selectedCommand", ")", "obj", "=", "cmdClass", "(", "args", ")", "obj", ".", "execute_command", "(", ")", "except", "POSSIBLE_EXCEPTIONS", "as", "e", ":", "print", "(", "'\\n'", ",", "e", ",", "'\\n'", ")" ]
The starting point for the execution of the Scrapple command line tool. runCLI uses the docstring as the usage description for the scrapple command. \ The class for the required command is selected by a dynamic dispatch, and the \ command is executed through the execute_command() method of the command class.
[ "The", "starting", "point", "for", "the", "execution", "of", "the", "Scrapple", "command", "line", "tool", "." ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/cmd.py#L49-L67
AlexMathew/scrapple
scrapple/utils/exceptions.py
check_arguments
def check_arguments(args): """ Validates the arguments passed through the CLI commands. :param args: The arguments passed in the CLI, parsed by the docopt module :return: None """ projectname_re = re.compile(r'[^a-zA-Z0-9_]') if args['genconfig']: if args['--type'] not in ['scraper', 'crawler']: raise InvalidType("--type has to be 'scraper' or 'crawler'") if args['--selector'] not in ['xpath', 'css']: raise InvalidSelector("--selector has to be 'xpath' or 'css'") if args['generate'] or args['run']: if args['--output_type'] not in ['json', 'csv']: raise InvalidOutputType("--output_type has to be 'json' or 'csv'") if args['genconfig'] or args['generate'] or args['run']: if projectname_re.search(args['<projectname>']) is not None: message = "<projectname> should consist of letters, digits or _" raise InvalidProjectName(message) try: if int(args['--levels']) < 1: message = "--levels should be greater than, or equal to 1" raise InvalidLevels(message) except (TypeError, ValueError): message = " ".join([ "--levels should be an integer and not of type", "{}".format(type(args['--levels'])) ]) raise InvalidLevels(message)
python
def check_arguments(args): projectname_re = re.compile(r'[^a-zA-Z0-9_]') if args['genconfig']: if args['--type'] not in ['scraper', 'crawler']: raise InvalidType("--type has to be 'scraper' or 'crawler'") if args['--selector'] not in ['xpath', 'css']: raise InvalidSelector("--selector has to be 'xpath' or 'css'") if args['generate'] or args['run']: if args['--output_type'] not in ['json', 'csv']: raise InvalidOutputType("--output_type has to be 'json' or 'csv'") if args['genconfig'] or args['generate'] or args['run']: if projectname_re.search(args['<projectname>']) is not None: message = "<projectname> should consist of letters, digits or _" raise InvalidProjectName(message) try: if int(args['--levels']) < 1: message = "--levels should be greater than, or equal to 1" raise InvalidLevels(message) except (TypeError, ValueError): message = " ".join([ "--levels should be an integer and not of type", "{}".format(type(args['--levels'])) ]) raise InvalidLevels(message)
[ "def", "check_arguments", "(", "args", ")", ":", "projectname_re", "=", "re", ".", "compile", "(", "r'[^a-zA-Z0-9_]'", ")", "if", "args", "[", "'genconfig'", "]", ":", "if", "args", "[", "'--type'", "]", "not", "in", "[", "'scraper'", ",", "'crawler'", "]", ":", "raise", "InvalidType", "(", "\"--type has to be 'scraper' or 'crawler'\"", ")", "if", "args", "[", "'--selector'", "]", "not", "in", "[", "'xpath'", ",", "'css'", "]", ":", "raise", "InvalidSelector", "(", "\"--selector has to be 'xpath' or 'css'\"", ")", "if", "args", "[", "'generate'", "]", "or", "args", "[", "'run'", "]", ":", "if", "args", "[", "'--output_type'", "]", "not", "in", "[", "'json'", ",", "'csv'", "]", ":", "raise", "InvalidOutputType", "(", "\"--output_type has to be 'json' or 'csv'\"", ")", "if", "args", "[", "'genconfig'", "]", "or", "args", "[", "'generate'", "]", "or", "args", "[", "'run'", "]", ":", "if", "projectname_re", ".", "search", "(", "args", "[", "'<projectname>'", "]", ")", "is", "not", "None", ":", "message", "=", "\"<projectname> should consist of letters, digits or _\"", "raise", "InvalidProjectName", "(", "message", ")", "try", ":", "if", "int", "(", "args", "[", "'--levels'", "]", ")", "<", "1", ":", "message", "=", "\"--levels should be greater than, or equal to 1\"", "raise", "InvalidLevels", "(", "message", ")", "except", "(", "TypeError", ",", "ValueError", ")", ":", "message", "=", "\" \"", ".", "join", "(", "[", "\"--levels should be an integer and not of type\"", ",", "\"{}\"", ".", "format", "(", "type", "(", "args", "[", "'--levels'", "]", ")", ")", "]", ")", "raise", "InvalidLevels", "(", "message", ")" ]
Validates the arguments passed through the CLI commands. :param args: The arguments passed in the CLI, parsed by the docopt module :return: None
[ "Validates", "the", "arguments", "passed", "through", "the", "CLI", "commands", "." ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/utils/exceptions.py#L36-L66
AlexMathew/scrapple
scrapple/utils/dynamicdispatch.py
get_command_class
def get_command_class(command): """ Called from runCLI() to select the command class for the selected command. :param command: The command to be implemented :return: The command class corresponding to the selected command """ from scrapple.commands import genconfig, generate, run, web commandMapping = { 'genconfig': genconfig, 'generate': generate, 'run': run, 'web': web } cmdClass = getattr(commandMapping.get(command), command.title() + 'Command') return cmdClass
python
def get_command_class(command): from scrapple.commands import genconfig, generate, run, web commandMapping = { 'genconfig': genconfig, 'generate': generate, 'run': run, 'web': web } cmdClass = getattr(commandMapping.get(command), command.title() + 'Command') return cmdClass
[ "def", "get_command_class", "(", "command", ")", ":", "from", "scrapple", ".", "commands", "import", "genconfig", ",", "generate", ",", "run", ",", "web", "commandMapping", "=", "{", "'genconfig'", ":", "genconfig", ",", "'generate'", ":", "generate", ",", "'run'", ":", "run", ",", "'web'", ":", "web", "}", "cmdClass", "=", "getattr", "(", "commandMapping", ".", "get", "(", "command", ")", ",", "command", ".", "title", "(", ")", "+", "'Command'", ")", "return", "cmdClass" ]
Called from runCLI() to select the command class for the selected command. :param command: The command to be implemented :return: The command class corresponding to the selected command
[ "Called", "from", "runCLI", "()", "to", "select", "the", "command", "class", "for", "the", "selected", "command", "." ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/utils/dynamicdispatch.py#L8-L23
AlexMathew/scrapple
scrapple/utils/form.py
form_to_json
def form_to_json(form): """ Takes the form from the POST request in the web interface, and generates the JSON config\ file :param form: The form from the POST request :return: None """ config = dict() if form['project_name'] == "": raise Exception('Project name cannot be empty.') if form['selector_type'] not in ["css", "xpath"]: raise Exception('Selector type has to css or xpath') config['project_name'] = form['project_name'] config['selector_type'] = form['selector_type'] config['scraping'] = dict() if form['url'] == "": raise Exception('URL cannot be empty') config['scraping']['url'] = form['url'] config['scraping']['data'] = list() for i in itertools.count(start=1): try: data = { 'field': form['field_' + str(i)], 'selector': form['selector_' + str(i)], 'attr': form['attribute_' + str(i)], 'default': form['default_' + str(i)] } config['scraping']['data'].append(data) except KeyError: break # TODO : Crawler 'next' parameter handling with open(os.path.join(os.getcwd(), form['project_name'] + '.json'), 'w') as f: json.dump(config, f) return
python
def form_to_json(form): config = dict() if form['project_name'] == "": raise Exception('Project name cannot be empty.') if form['selector_type'] not in ["css", "xpath"]: raise Exception('Selector type has to css or xpath') config['project_name'] = form['project_name'] config['selector_type'] = form['selector_type'] config['scraping'] = dict() if form['url'] == "": raise Exception('URL cannot be empty') config['scraping']['url'] = form['url'] config['scraping']['data'] = list() for i in itertools.count(start=1): try: data = { 'field': form['field_' + str(i)], 'selector': form['selector_' + str(i)], 'attr': form['attribute_' + str(i)], 'default': form['default_' + str(i)] } config['scraping']['data'].append(data) except KeyError: break with open(os.path.join(os.getcwd(), form['project_name'] + '.json'), 'w') as f: json.dump(config, f) return
[ "def", "form_to_json", "(", "form", ")", ":", "config", "=", "dict", "(", ")", "if", "form", "[", "'project_name'", "]", "==", "\"\"", ":", "raise", "Exception", "(", "'Project name cannot be empty.'", ")", "if", "form", "[", "'selector_type'", "]", "not", "in", "[", "\"css\"", ",", "\"xpath\"", "]", ":", "raise", "Exception", "(", "'Selector type has to css or xpath'", ")", "config", "[", "'project_name'", "]", "=", "form", "[", "'project_name'", "]", "config", "[", "'selector_type'", "]", "=", "form", "[", "'selector_type'", "]", "config", "[", "'scraping'", "]", "=", "dict", "(", ")", "if", "form", "[", "'url'", "]", "==", "\"\"", ":", "raise", "Exception", "(", "'URL cannot be empty'", ")", "config", "[", "'scraping'", "]", "[", "'url'", "]", "=", "form", "[", "'url'", "]", "config", "[", "'scraping'", "]", "[", "'data'", "]", "=", "list", "(", ")", "for", "i", "in", "itertools", ".", "count", "(", "start", "=", "1", ")", ":", "try", ":", "data", "=", "{", "'field'", ":", "form", "[", "'field_'", "+", "str", "(", "i", ")", "]", ",", "'selector'", ":", "form", "[", "'selector_'", "+", "str", "(", "i", ")", "]", ",", "'attr'", ":", "form", "[", "'attribute_'", "+", "str", "(", "i", ")", "]", ",", "'default'", ":", "form", "[", "'default_'", "+", "str", "(", "i", ")", "]", "}", "config", "[", "'scraping'", "]", "[", "'data'", "]", ".", "append", "(", "data", ")", "except", "KeyError", ":", "break", "# TODO : Crawler 'next' parameter handling", "with", "open", "(", "os", ".", "path", ".", "join", "(", "os", ".", "getcwd", "(", ")", ",", "form", "[", "'project_name'", "]", "+", "'.json'", ")", ",", "'w'", ")", "as", "f", ":", "json", ".", "dump", "(", "config", ",", "f", ")", "return" ]
Takes the form from the POST request in the web interface, and generates the JSON config\ file :param form: The form from the POST request :return: None
[ "Takes", "the", "form", "from", "the", "POST", "request", "in", "the", "web", "interface", "and", "generates", "the", "JSON", "config", "\\", "file" ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/utils/form.py#L13-L48
AlexMathew/scrapple
scrapple/commands/run.py
RunCommand.execute_command
def execute_command(self): """ The run command implements the web content extractor corresponding to the given \ configuration file. The execute_command() validates the input project name and opens the JSON \ configuration file. The run() method handles the execution of the extractor run. The extractor implementation follows these primary steps : 1. Selects the appropriate :ref:`selector class <implementation-selectors>` through \ a dynamic dispatch, with the selector_type argument from the CLI input. #. Iterate through the data section in level-0 of the configuration file. \ On each data item, call the extract_content() method from the selector class to \ extract the content according to the specified extractor rule. #. If there are multiple levels of the extractor, i.e, if there is a 'next' \ attribute in the configuration file, call the traverse_next() \ :ref:`utility function <implementation-utils>` and parse through successive levels \ of the configuration file. #. According to the --output_type argument, the result data is saved in a JSON \ document or a CSV document. """ try: self.args['--verbosity'] = int(self.args['--verbosity']) if self.args['--verbosity'] not in [0, 1, 2]: raise ValueError if self.args['--verbosity'] > 0: print(Back.GREEN + Fore.BLACK + "Scrapple Run") print(Back.RESET + Fore.RESET) import json with open(self.args['<projectname>'] + '.json', 'r') as f: self.config = json.load(f) validate_config(self.config) self.run() except ValueError: print(Back.WHITE + Fore.RED + "Use 0, 1 or 2 for verbosity." \ + Back.RESET + Fore.RESET, sep="") except IOError: print(Back.WHITE + Fore.RED + self.args['<projectname>'], ".json does not ", \ "exist. Use ``scrapple genconfig``." + Back.RESET + Fore.RESET, sep="") except InvalidConfigException as e: print(Back.WHITE + Fore.RED + e + Back.RESET + Fore.RESET, sep="")
python
def execute_command(self): try: self.args['--verbosity'] = int(self.args['--verbosity']) if self.args['--verbosity'] not in [0, 1, 2]: raise ValueError if self.args['--verbosity'] > 0: print(Back.GREEN + Fore.BLACK + "Scrapple Run") print(Back.RESET + Fore.RESET) import json with open(self.args['<projectname>'] + '.json', 'r') as f: self.config = json.load(f) validate_config(self.config) self.run() except ValueError: print(Back.WHITE + Fore.RED + "Use 0, 1 or 2 for verbosity." \ + Back.RESET + Fore.RESET, sep="") except IOError: print(Back.WHITE + Fore.RED + self.args['<projectname>'], ".json does not ", \ "exist. Use ``scrapple genconfig``." + Back.RESET + Fore.RESET, sep="") except InvalidConfigException as e: print(Back.WHITE + Fore.RED + e + Back.RESET + Fore.RESET, sep="")
[ "def", "execute_command", "(", "self", ")", ":", "try", ":", "self", ".", "args", "[", "'--verbosity'", "]", "=", "int", "(", "self", ".", "args", "[", "'--verbosity'", "]", ")", "if", "self", ".", "args", "[", "'--verbosity'", "]", "not", "in", "[", "0", ",", "1", ",", "2", "]", ":", "raise", "ValueError", "if", "self", ".", "args", "[", "'--verbosity'", "]", ">", "0", ":", "print", "(", "Back", ".", "GREEN", "+", "Fore", ".", "BLACK", "+", "\"Scrapple Run\"", ")", "print", "(", "Back", ".", "RESET", "+", "Fore", ".", "RESET", ")", "import", "json", "with", "open", "(", "self", ".", "args", "[", "'<projectname>'", "]", "+", "'.json'", ",", "'r'", ")", "as", "f", ":", "self", ".", "config", "=", "json", ".", "load", "(", "f", ")", "validate_config", "(", "self", ".", "config", ")", "self", ".", "run", "(", ")", "except", "ValueError", ":", "print", "(", "Back", ".", "WHITE", "+", "Fore", ".", "RED", "+", "\"Use 0, 1 or 2 for verbosity.\"", "+", "Back", ".", "RESET", "+", "Fore", ".", "RESET", ",", "sep", "=", "\"\"", ")", "except", "IOError", ":", "print", "(", "Back", ".", "WHITE", "+", "Fore", ".", "RED", "+", "self", ".", "args", "[", "'<projectname>'", "]", ",", "\".json does not \"", ",", "\"exist. Use ``scrapple genconfig``.\"", "+", "Back", ".", "RESET", "+", "Fore", ".", "RESET", ",", "sep", "=", "\"\"", ")", "except", "InvalidConfigException", "as", "e", ":", "print", "(", "Back", ".", "WHITE", "+", "Fore", ".", "RED", "+", "e", "+", "Back", ".", "RESET", "+", "Fore", ".", "RESET", ",", "sep", "=", "\"\"", ")" ]
The run command implements the web content extractor corresponding to the given \ configuration file. The execute_command() validates the input project name and opens the JSON \ configuration file. The run() method handles the execution of the extractor run. The extractor implementation follows these primary steps : 1. Selects the appropriate :ref:`selector class <implementation-selectors>` through \ a dynamic dispatch, with the selector_type argument from the CLI input. #. Iterate through the data section in level-0 of the configuration file. \ On each data item, call the extract_content() method from the selector class to \ extract the content according to the specified extractor rule. #. If there are multiple levels of the extractor, i.e, if there is a 'next' \ attribute in the configuration file, call the traverse_next() \ :ref:`utility function <implementation-utils>` and parse through successive levels \ of the configuration file. #. According to the --output_type argument, the result data is saved in a JSON \ document or a CSV document.
[ "The", "run", "command", "implements", "the", "web", "content", "extractor", "corresponding", "to", "the", "given", "\\", "configuration", "file", "." ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/commands/run.py#L29-L74
AlexMathew/scrapple
scrapple/utils/config.py
traverse_next
def traverse_next(page, nextx, results, tabular_data_headers=[], verbosity=0): """ Recursive generator to traverse through the next attribute and \ crawl through the links to be followed. :param page: The current page being parsed :param next: The next attribute of the current scraping dict :param results: The current extracted content, stored in a dict :return: The extracted content, through a generator """ for link in page.extract_links(selector=nextx['follow_link']): if verbosity > 0: print('\n') print(Back.YELLOW + Fore.BLUE + "Loading page ", link.url + Back.RESET + Fore.RESET, end='') r = results.copy() for attribute in nextx['scraping'].get('data'): if attribute['field'] != "": if verbosity > 1: print("\nExtracting", attribute['field'], "attribute", sep=' ', end='') r[attribute['field']] = link.extract_content(**attribute) if not nextx['scraping'].get('table'): result_list = [r] else: tables = nextx['scraping'].get('table', []) for table in tables: table.update({ 'result': r, 'verbosity': verbosity }) table_headers, result_list = link.extract_tabular(**table) tabular_data_headers.extend(table_headers) if not nextx['scraping'].get('next'): for r in result_list: yield (tabular_data_headers, r) else: for nextx2 in nextx['scraping'].get('next'): for tdh, result in traverse_next(link, nextx2, r, tabular_data_headers=tabular_data_headers, verbosity=verbosity): yield (tdh, result)
python
def traverse_next(page, nextx, results, tabular_data_headers=[], verbosity=0): for link in page.extract_links(selector=nextx['follow_link']): if verbosity > 0: print('\n') print(Back.YELLOW + Fore.BLUE + "Loading page ", link.url + Back.RESET + Fore.RESET, end='') r = results.copy() for attribute in nextx['scraping'].get('data'): if attribute['field'] != "": if verbosity > 1: print("\nExtracting", attribute['field'], "attribute", sep=' ', end='') r[attribute['field']] = link.extract_content(**attribute) if not nextx['scraping'].get('table'): result_list = [r] else: tables = nextx['scraping'].get('table', []) for table in tables: table.update({ 'result': r, 'verbosity': verbosity }) table_headers, result_list = link.extract_tabular(**table) tabular_data_headers.extend(table_headers) if not nextx['scraping'].get('next'): for r in result_list: yield (tabular_data_headers, r) else: for nextx2 in nextx['scraping'].get('next'): for tdh, result in traverse_next(link, nextx2, r, tabular_data_headers=tabular_data_headers, verbosity=verbosity): yield (tdh, result)
[ "def", "traverse_next", "(", "page", ",", "nextx", ",", "results", ",", "tabular_data_headers", "=", "[", "]", ",", "verbosity", "=", "0", ")", ":", "for", "link", "in", "page", ".", "extract_links", "(", "selector", "=", "nextx", "[", "'follow_link'", "]", ")", ":", "if", "verbosity", ">", "0", ":", "print", "(", "'\\n'", ")", "print", "(", "Back", ".", "YELLOW", "+", "Fore", ".", "BLUE", "+", "\"Loading page \"", ",", "link", ".", "url", "+", "Back", ".", "RESET", "+", "Fore", ".", "RESET", ",", "end", "=", "''", ")", "r", "=", "results", ".", "copy", "(", ")", "for", "attribute", "in", "nextx", "[", "'scraping'", "]", ".", "get", "(", "'data'", ")", ":", "if", "attribute", "[", "'field'", "]", "!=", "\"\"", ":", "if", "verbosity", ">", "1", ":", "print", "(", "\"\\nExtracting\"", ",", "attribute", "[", "'field'", "]", ",", "\"attribute\"", ",", "sep", "=", "' '", ",", "end", "=", "''", ")", "r", "[", "attribute", "[", "'field'", "]", "]", "=", "link", ".", "extract_content", "(", "*", "*", "attribute", ")", "if", "not", "nextx", "[", "'scraping'", "]", ".", "get", "(", "'table'", ")", ":", "result_list", "=", "[", "r", "]", "else", ":", "tables", "=", "nextx", "[", "'scraping'", "]", ".", "get", "(", "'table'", ",", "[", "]", ")", "for", "table", "in", "tables", ":", "table", ".", "update", "(", "{", "'result'", ":", "r", ",", "'verbosity'", ":", "verbosity", "}", ")", "table_headers", ",", "result_list", "=", "link", ".", "extract_tabular", "(", "*", "*", "table", ")", "tabular_data_headers", ".", "extend", "(", "table_headers", ")", "if", "not", "nextx", "[", "'scraping'", "]", ".", "get", "(", "'next'", ")", ":", "for", "r", "in", "result_list", ":", "yield", "(", "tabular_data_headers", ",", "r", ")", "else", ":", "for", "nextx2", "in", "nextx", "[", "'scraping'", "]", ".", "get", "(", "'next'", ")", ":", "for", "tdh", ",", "result", "in", "traverse_next", "(", "link", ",", "nextx2", ",", "r", ",", "tabular_data_headers", "=", "tabular_data_headers", ",", "verbosity", "=", "verbosity", ")", ":", "yield", "(", "tdh", ",", "result", ")" ]
Recursive generator to traverse through the next attribute and \ crawl through the links to be followed. :param page: The current page being parsed :param next: The next attribute of the current scraping dict :param results: The current extracted content, stored in a dict :return: The extracted content, through a generator
[ "Recursive", "generator", "to", "traverse", "through", "the", "next", "attribute", "and", "\\", "crawl", "through", "the", "links", "to", "be", "followed", "." ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/utils/config.py#L20-L58
AlexMathew/scrapple
scrapple/utils/config.py
validate_config
def validate_config(config): """ Validates the extractor configuration file. Ensures that there are no duplicate field names, etc. :param config: The configuration file that contains the specification of the extractor :return: True if config is valid, else raises a exception that specifies the correction to be made """ fields = [f for f in get_fields(config)] if len(fields) != len(set(fields)): raise InvalidConfigException( "Invalid configuration file - %d duplicate field names" % len(fields) - len(set(fields)) ) return True
python
def validate_config(config): fields = [f for f in get_fields(config)] if len(fields) != len(set(fields)): raise InvalidConfigException( "Invalid configuration file - %d duplicate field names" % len(fields) - len(set(fields)) ) return True
[ "def", "validate_config", "(", "config", ")", ":", "fields", "=", "[", "f", "for", "f", "in", "get_fields", "(", "config", ")", "]", "if", "len", "(", "fields", ")", "!=", "len", "(", "set", "(", "fields", ")", ")", ":", "raise", "InvalidConfigException", "(", "\"Invalid configuration file - %d duplicate field names\"", "%", "len", "(", "fields", ")", "-", "len", "(", "set", "(", "fields", ")", ")", ")", "return", "True" ]
Validates the extractor configuration file. Ensures that there are no duplicate field names, etc. :param config: The configuration file that contains the specification of the extractor :return: True if config is valid, else raises a exception that specifies the correction to be made
[ "Validates", "the", "extractor", "configuration", "file", ".", "Ensures", "that", "there", "are", "no", "duplicate", "field", "names", "etc", "." ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/utils/config.py#L61-L74
AlexMathew/scrapple
scrapple/utils/config.py
get_fields
def get_fields(config): """ Recursive generator that yields the field names in the config file :param config: The configuration file that contains the specification of the extractor :return: The field names in the config file, through a generator """ for data in config['scraping']['data']: if data['field'] != '': yield data['field'] if 'next' in config['scraping']: for n in config['scraping']['next']: for f in get_fields(n): yield f
python
def get_fields(config): for data in config['scraping']['data']: if data['field'] != '': yield data['field'] if 'next' in config['scraping']: for n in config['scraping']['next']: for f in get_fields(n): yield f
[ "def", "get_fields", "(", "config", ")", ":", "for", "data", "in", "config", "[", "'scraping'", "]", "[", "'data'", "]", ":", "if", "data", "[", "'field'", "]", "!=", "''", ":", "yield", "data", "[", "'field'", "]", "if", "'next'", "in", "config", "[", "'scraping'", "]", ":", "for", "n", "in", "config", "[", "'scraping'", "]", "[", "'next'", "]", ":", "for", "f", "in", "get_fields", "(", "n", ")", ":", "yield", "f" ]
Recursive generator that yields the field names in the config file :param config: The configuration file that contains the specification of the extractor :return: The field names in the config file, through a generator
[ "Recursive", "generator", "that", "yields", "the", "field", "names", "in", "the", "config", "file" ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/utils/config.py#L77-L91
AlexMathew/scrapple
scrapple/utils/config.py
extract_fieldnames
def extract_fieldnames(config): """ Function to return a list of unique field names from the config file :param config: The configuration file that contains the specification of the extractor :return: A list of field names from the config file """ fields = [] for x in get_fields(config): if x in fields: fields.append(x + '_' + str(fields.count(x) + 1)) else: fields.append(x) return fields
python
def extract_fieldnames(config): fields = [] for x in get_fields(config): if x in fields: fields.append(x + '_' + str(fields.count(x) + 1)) else: fields.append(x) return fields
[ "def", "extract_fieldnames", "(", "config", ")", ":", "fields", "=", "[", "]", "for", "x", "in", "get_fields", "(", "config", ")", ":", "if", "x", "in", "fields", ":", "fields", ".", "append", "(", "x", "+", "'_'", "+", "str", "(", "fields", ".", "count", "(", "x", ")", "+", "1", ")", ")", "else", ":", "fields", ".", "append", "(", "x", ")", "return", "fields" ]
Function to return a list of unique field names from the config file :param config: The configuration file that contains the specification of the extractor :return: A list of field names from the config file
[ "Function", "to", "return", "a", "list", "of", "unique", "field", "names", "from", "the", "config", "file" ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/utils/config.py#L94-L108
AlexMathew/scrapple
scrapple/commands/genconfig.py
GenconfigCommand.execute_command
def execute_command(self): """ The genconfig command depends on predefined `Jinja2 <http://jinja.pocoo.org/>`_ \ templates for the skeleton configuration files. Taking the --type argument from the \ CLI input, the corresponding template file is used. Settings for the configuration file, like project name, selector type and URL \ are taken from the CLI input and using these as parameters, the template is \ rendered. This rendered JSON document is saved as <project_name>.json. """ print(Back.GREEN + Fore.BLACK + "Scrapple Genconfig") print(Back.RESET + Fore.RESET) directory = os.path.join(scrapple.__path__[0], 'templates', 'configs') with open(os.path.join(directory, self.args['--type'] + '.txt'), 'r') as f: template_content = f.read() print("\n\nUsing the", self.args['--type'], "template\n\n") template = Template(template_content) settings = { 'projectname': self.args['<projectname>'], 'selector_type': self.args['--selector'], 'url': self.args['<url>'], 'levels': int(self.args['--levels']) } rendered = template.render(settings=settings) with open(self.args['<projectname>'] + '.json', 'w') as f: rendered_data = json.loads(rendered) json.dump(rendered_data, f, indent=3) print(Back.WHITE + Fore.RED + self.args['<projectname>'], ".json has been created" \ + Back.RESET + Fore.RESET, sep="")
python
def execute_command(self): print(Back.GREEN + Fore.BLACK + "Scrapple Genconfig") print(Back.RESET + Fore.RESET) directory = os.path.join(scrapple.__path__[0], 'templates', 'configs') with open(os.path.join(directory, self.args['--type'] + '.txt'), 'r') as f: template_content = f.read() print("\n\nUsing the", self.args['--type'], "template\n\n") template = Template(template_content) settings = { 'projectname': self.args['<projectname>'], 'selector_type': self.args['--selector'], 'url': self.args['<url>'], 'levels': int(self.args['--levels']) } rendered = template.render(settings=settings) with open(self.args['<projectname>'] + '.json', 'w') as f: rendered_data = json.loads(rendered) json.dump(rendered_data, f, indent=3) print(Back.WHITE + Fore.RED + self.args['<projectname>'], ".json has been created" \ + Back.RESET + Fore.RESET, sep="")
[ "def", "execute_command", "(", "self", ")", ":", "print", "(", "Back", ".", "GREEN", "+", "Fore", ".", "BLACK", "+", "\"Scrapple Genconfig\"", ")", "print", "(", "Back", ".", "RESET", "+", "Fore", ".", "RESET", ")", "directory", "=", "os", ".", "path", ".", "join", "(", "scrapple", ".", "__path__", "[", "0", "]", ",", "'templates'", ",", "'configs'", ")", "with", "open", "(", "os", ".", "path", ".", "join", "(", "directory", ",", "self", ".", "args", "[", "'--type'", "]", "+", "'.txt'", ")", ",", "'r'", ")", "as", "f", ":", "template_content", "=", "f", ".", "read", "(", ")", "print", "(", "\"\\n\\nUsing the\"", ",", "self", ".", "args", "[", "'--type'", "]", ",", "\"template\\n\\n\"", ")", "template", "=", "Template", "(", "template_content", ")", "settings", "=", "{", "'projectname'", ":", "self", ".", "args", "[", "'<projectname>'", "]", ",", "'selector_type'", ":", "self", ".", "args", "[", "'--selector'", "]", ",", "'url'", ":", "self", ".", "args", "[", "'<url>'", "]", ",", "'levels'", ":", "int", "(", "self", ".", "args", "[", "'--levels'", "]", ")", "}", "rendered", "=", "template", ".", "render", "(", "settings", "=", "settings", ")", "with", "open", "(", "self", ".", "args", "[", "'<projectname>'", "]", "+", "'.json'", ",", "'w'", ")", "as", "f", ":", "rendered_data", "=", "json", ".", "loads", "(", "rendered", ")", "json", ".", "dump", "(", "rendered_data", ",", "f", ",", "indent", "=", "3", ")", "print", "(", "Back", ".", "WHITE", "+", "Fore", ".", "RED", "+", "self", ".", "args", "[", "'<projectname>'", "]", ",", "\".json has been created\"", "+", "Back", ".", "RESET", "+", "Fore", ".", "RESET", ",", "sep", "=", "\"\"", ")" ]
The genconfig command depends on predefined `Jinja2 <http://jinja.pocoo.org/>`_ \ templates for the skeleton configuration files. Taking the --type argument from the \ CLI input, the corresponding template file is used. Settings for the configuration file, like project name, selector type and URL \ are taken from the CLI input and using these as parameters, the template is \ rendered. This rendered JSON document is saved as <project_name>.json.
[ "The", "genconfig", "command", "depends", "on", "predefined", "Jinja2", "<http", ":", "//", "jinja", ".", "pocoo", ".", "org", "/", ">", "_", "\\", "templates", "for", "the", "skeleton", "configuration", "files", ".", "Taking", "the", "--", "type", "argument", "from", "the", "\\", "CLI", "input", "the", "corresponding", "template", "file", "is", "used", "." ]
train
https://github.com/AlexMathew/scrapple/blob/eeb604601b155d6cc7e035855ff4d3f48f8bed74/scrapple/commands/genconfig.py#L28-L57
CQCL/pytket
pytket/chemistry/aqua/qse_subs.py
_jordan_wigner_mode
def _jordan_wigner_mode(n): """ Jordan_Wigner mode. Args: n (int): number of modes """ a = [] for i in range(n): xv = np.asarray([1] * i + [0] + [0] * (n - i - 1)) xw = np.asarray([0] * i + [1] + [0] * (n - i - 1)) yv = np.asarray([1] * i + [1] + [0] * (n - i - 1)) yw = np.asarray([0] * i + [1] + [0] * (n - i - 1)) a.append((Pauli(xv, xw), Pauli(yv, yw))) return a
python
def _jordan_wigner_mode(n): a = [] for i in range(n): xv = np.asarray([1] * i + [0] + [0] * (n - i - 1)) xw = np.asarray([0] * i + [1] + [0] * (n - i - 1)) yv = np.asarray([1] * i + [1] + [0] * (n - i - 1)) yw = np.asarray([0] * i + [1] + [0] * (n - i - 1)) a.append((Pauli(xv, xw), Pauli(yv, yw))) return a
[ "def", "_jordan_wigner_mode", "(", "n", ")", ":", "a", "=", "[", "]", "for", "i", "in", "range", "(", "n", ")", ":", "xv", "=", "np", ".", "asarray", "(", "[", "1", "]", "*", "i", "+", "[", "0", "]", "+", "[", "0", "]", "*", "(", "n", "-", "i", "-", "1", ")", ")", "xw", "=", "np", ".", "asarray", "(", "[", "0", "]", "*", "i", "+", "[", "1", "]", "+", "[", "0", "]", "*", "(", "n", "-", "i", "-", "1", ")", ")", "yv", "=", "np", ".", "asarray", "(", "[", "1", "]", "*", "i", "+", "[", "1", "]", "+", "[", "0", "]", "*", "(", "n", "-", "i", "-", "1", ")", ")", "yw", "=", "np", ".", "asarray", "(", "[", "0", "]", "*", "i", "+", "[", "1", "]", "+", "[", "0", "]", "*", "(", "n", "-", "i", "-", "1", ")", ")", "a", ".", "append", "(", "(", "Pauli", "(", "xv", ",", "xw", ")", ",", "Pauli", "(", "yv", ",", "yw", ")", ")", ")", "return", "a" ]
Jordan_Wigner mode. Args: n (int): number of modes
[ "Jordan_Wigner", "mode", ".", "Args", ":", "n", "(", "int", ")", ":", "number", "of", "modes" ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/chemistry/aqua/qse_subs.py#L24-L37
CQCL/pytket
pytket/chemistry/aqua/qse_subs.py
_one_body_mapping
def _one_body_mapping(a_i, a_j, threshold=0.000001): """ Subroutine for one body mapping. Args: a_i (Pauli): pauli at index i a_j (Pauli): pauli at index j threshold: (float): threshold to remove a pauli Returns: Operator: Operator for those paulis """ pauli_list = [] for alpha in range(2): for beta in range(2): pauli_prod = Pauli.sgn_prod(a_i[alpha], a_j[beta]) coeff = 1.0/4 * pauli_prod[1] * np.power(-1j, alpha) * np.power(1j, beta) pauli_term = [coeff, pauli_prod[0]] if np.absolute(pauli_term[0]) > threshold: pauli_list.append(pauli_term) return Operator(paulis=pauli_list)
python
def _one_body_mapping(a_i, a_j, threshold=0.000001): pauli_list = [] for alpha in range(2): for beta in range(2): pauli_prod = Pauli.sgn_prod(a_i[alpha], a_j[beta]) coeff = 1.0/4 * pauli_prod[1] * np.power(-1j, alpha) * np.power(1j, beta) pauli_term = [coeff, pauli_prod[0]] if np.absolute(pauli_term[0]) > threshold: pauli_list.append(pauli_term) return Operator(paulis=pauli_list)
[ "def", "_one_body_mapping", "(", "a_i", ",", "a_j", ",", "threshold", "=", "0.000001", ")", ":", "pauli_list", "=", "[", "]", "for", "alpha", "in", "range", "(", "2", ")", ":", "for", "beta", "in", "range", "(", "2", ")", ":", "pauli_prod", "=", "Pauli", ".", "sgn_prod", "(", "a_i", "[", "alpha", "]", ",", "a_j", "[", "beta", "]", ")", "coeff", "=", "1.0", "/", "4", "*", "pauli_prod", "[", "1", "]", "*", "np", ".", "power", "(", "-", "1j", ",", "alpha", ")", "*", "np", ".", "power", "(", "1j", ",", "beta", ")", "pauli_term", "=", "[", "coeff", ",", "pauli_prod", "[", "0", "]", "]", "if", "np", ".", "absolute", "(", "pauli_term", "[", "0", "]", ")", ">", "threshold", ":", "pauli_list", ".", "append", "(", "pauli_term", ")", "return", "Operator", "(", "paulis", "=", "pauli_list", ")" ]
Subroutine for one body mapping. Args: a_i (Pauli): pauli at index i a_j (Pauli): pauli at index j threshold: (float): threshold to remove a pauli Returns: Operator: Operator for those paulis
[ "Subroutine", "for", "one", "body", "mapping", ".", "Args", ":", "a_i", "(", "Pauli", ")", ":", "pauli", "at", "index", "i", "a_j", "(", "Pauli", ")", ":", "pauli", "at", "index", "j", "threshold", ":", "(", "float", ")", ":", "threshold", "to", "remove", "a", "pauli", "Returns", ":", "Operator", ":", "Operator", "for", "those", "paulis" ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/chemistry/aqua/qse_subs.py#L39-L57
CQCL/pytket
pytket/cirq/cirq_convert.py
get_grid_qubits
def get_grid_qubits(arc: SquareGrid, nodes: Iterator[int]) -> List[cirq.GridQubit]: """Gets a list of :py:class:GridQubit` s corresponding to the qubit nodes provided on the given Architecture. :param arc: The grid Architecture :param nodes: An iterator of node index values :return: The list of qubits """ return [cirq.GridQubit(*arc.qind_to_squind(i)) for i in nodes]
python
def get_grid_qubits(arc: SquareGrid, nodes: Iterator[int]) -> List[cirq.GridQubit]: return [cirq.GridQubit(*arc.qind_to_squind(i)) for i in nodes]
[ "def", "get_grid_qubits", "(", "arc", ":", "SquareGrid", ",", "nodes", ":", "Iterator", "[", "int", "]", ")", "->", "List", "[", "cirq", ".", "GridQubit", "]", ":", "return", "[", "cirq", ".", "GridQubit", "(", "*", "arc", ".", "qind_to_squind", "(", "i", ")", ")", "for", "i", "in", "nodes", "]" ]
Gets a list of :py:class:GridQubit` s corresponding to the qubit nodes provided on the given Architecture. :param arc: The grid Architecture :param nodes: An iterator of node index values :return: The list of qubits
[ "Gets", "a", "list", "of", ":", "py", ":", "class", ":", "GridQubit", "s", "corresponding", "to", "the", "qubit", "nodes", "provided", "on", "the", "given", "Architecture", "." ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/cirq/cirq_convert.py#L62-L71
CQCL/pytket
pytket/cirq/cirq_convert.py
cirq_to_tk
def cirq_to_tk(circuit: cirq.Circuit) -> Circuit: """Converts a Cirq :py:class:`Circuit` to a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` object. :param circuit: The input Cirq :py:class:`Circuit` :raises NotImplementedError: If the input contains a Cirq :py:class:`Circuit` operation which is not yet supported by pytket :return: The :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` corresponding to the input circuit """ qubit_list = _indexed_qubits_from_circuit(circuit) qid_to_num = {q : i for i, q in enumerate(qubit_list)} n_qubits = len(circuit.all_qubits()) tkcirc = Circuit(n_qubits) for moment in circuit: for op in moment.operations: gate = op.gate gatetype = type(gate) qb_lst = [qid_to_num[q] for q in op.qubits] n_qubits = len(op.qubits) if gatetype == cirq_common.HPowGate and gate.exponent == 1: gatetype = cirq_common.H elif gatetype == cirq_common.CNotPowGate and gate.exponent == 1: gatetype = cirq_common.CNOT try: optype = _cirq2ops_mapping[gatetype] except KeyError as error: raise NotImplementedError("Operation not supported by tket: " + str(op.gate)) from error if isinstance(gate, _rotation_types): o = tkcirc._get_op(optype,n_qubits,n_qubits,gate.exponent) elif isinstance(gate, cirq_common.MeasurementGate) : o = tkcirc._get_op(optype,n_qubits,n_qubits,gate.key) else: o = tkcirc._get_op(optype) tkcirc._add_operation(o,qb_lst) return tkcirc
python
def cirq_to_tk(circuit: cirq.Circuit) -> Circuit: qubit_list = _indexed_qubits_from_circuit(circuit) qid_to_num = {q : i for i, q in enumerate(qubit_list)} n_qubits = len(circuit.all_qubits()) tkcirc = Circuit(n_qubits) for moment in circuit: for op in moment.operations: gate = op.gate gatetype = type(gate) qb_lst = [qid_to_num[q] for q in op.qubits] n_qubits = len(op.qubits) if gatetype == cirq_common.HPowGate and gate.exponent == 1: gatetype = cirq_common.H elif gatetype == cirq_common.CNotPowGate and gate.exponent == 1: gatetype = cirq_common.CNOT try: optype = _cirq2ops_mapping[gatetype] except KeyError as error: raise NotImplementedError("Operation not supported by tket: " + str(op.gate)) from error if isinstance(gate, _rotation_types): o = tkcirc._get_op(optype,n_qubits,n_qubits,gate.exponent) elif isinstance(gate, cirq_common.MeasurementGate) : o = tkcirc._get_op(optype,n_qubits,n_qubits,gate.key) else: o = tkcirc._get_op(optype) tkcirc._add_operation(o,qb_lst) return tkcirc
[ "def", "cirq_to_tk", "(", "circuit", ":", "cirq", ".", "Circuit", ")", "->", "Circuit", ":", "qubit_list", "=", "_indexed_qubits_from_circuit", "(", "circuit", ")", "qid_to_num", "=", "{", "q", ":", "i", "for", "i", ",", "q", "in", "enumerate", "(", "qubit_list", ")", "}", "n_qubits", "=", "len", "(", "circuit", ".", "all_qubits", "(", ")", ")", "tkcirc", "=", "Circuit", "(", "n_qubits", ")", "for", "moment", "in", "circuit", ":", "for", "op", "in", "moment", ".", "operations", ":", "gate", "=", "op", ".", "gate", "gatetype", "=", "type", "(", "gate", ")", "qb_lst", "=", "[", "qid_to_num", "[", "q", "]", "for", "q", "in", "op", ".", "qubits", "]", "n_qubits", "=", "len", "(", "op", ".", "qubits", ")", "if", "gatetype", "==", "cirq_common", ".", "HPowGate", "and", "gate", ".", "exponent", "==", "1", ":", "gatetype", "=", "cirq_common", ".", "H", "elif", "gatetype", "==", "cirq_common", ".", "CNotPowGate", "and", "gate", ".", "exponent", "==", "1", ":", "gatetype", "=", "cirq_common", ".", "CNOT", "try", ":", "optype", "=", "_cirq2ops_mapping", "[", "gatetype", "]", "except", "KeyError", "as", "error", ":", "raise", "NotImplementedError", "(", "\"Operation not supported by tket: \"", "+", "str", "(", "op", ".", "gate", ")", ")", "from", "error", "if", "isinstance", "(", "gate", ",", "_rotation_types", ")", ":", "o", "=", "tkcirc", ".", "_get_op", "(", "optype", ",", "n_qubits", ",", "n_qubits", ",", "gate", ".", "exponent", ")", "elif", "isinstance", "(", "gate", ",", "cirq_common", ".", "MeasurementGate", ")", ":", "o", "=", "tkcirc", ".", "_get_op", "(", "optype", ",", "n_qubits", ",", "n_qubits", ",", "gate", ".", "key", ")", "else", ":", "o", "=", "tkcirc", ".", "_get_op", "(", "optype", ")", "tkcirc", ".", "_add_operation", "(", "o", ",", "qb_lst", ")", "return", "tkcirc" ]
Converts a Cirq :py:class:`Circuit` to a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` object. :param circuit: The input Cirq :py:class:`Circuit` :raises NotImplementedError: If the input contains a Cirq :py:class:`Circuit` operation which is not yet supported by pytket :return: The :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` corresponding to the input circuit
[ "Converts", "a", "Cirq", ":", "py", ":", "class", ":", "Circuit", "to", "a", ":", "math", ":", "\\\\", "mathrm", "{", "t|ket", "}", "\\\\", "rangle", ":", "py", ":", "class", ":", "Circuit", "object", ".", ":", "param", "circuit", ":", "The", "input", "Cirq", ":", "py", ":", "class", ":", "Circuit" ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/cirq/cirq_convert.py#L73-L111
CQCL/pytket
pytket/cirq/cirq_convert.py
tk_to_cirq
def tk_to_cirq(tkcirc: Circuit, indexed_qubits: List[QubitId]) -> cirq.Circuit: """Converts a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` object to a Cirq :py:class:`Circuit`. :param tkcirc: The input :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` :param indexed_qubits: Map from :math:`\\mathrm{t|ket}\\rangle` qubit indices to Cirq :py:class:`QubitId` s :return: The Cirq :py:class:`Circuit` corresponding to the input circuit """ grid = tkcirc._int_routing_grid() qubits = _grid_to_qubits(grid) oplst = [] slices = [] for s in grid: news = set() for pair in s: if pair[0]>-1: news.add(pair[0]) slices.append(news) for s in slices: for v in s: op = tkcirc._unsigned_to_op(v) optype = op.get_type() if optype == OpType.Input or optype == OpType.Output: continue try: gatetype = _ops2cirq_mapping[optype] except KeyError as error: raise NotImplementedError("Cannot convert tket Op to cirq gate: " + op.get_name()) from error n_qubits = op.get_n_inputs() qids = [] for i in range(n_qubits): qbit = qubits[(v,i)] qids.append(indexed_qubits[qbit]) params = op.get_params() if gatetype in _rotation_types: cirqop = gatetype(exponent=params[0])(*qids) elif gatetype == cirq_common.MeasurementGate: for q in qids: cirqop = cirq_common.measure(q, key=op.get_desc()) else: cirqop = gatetype(*qids) oplst.append(cirqop) return cirq.Circuit.from_ops(*oplst)
python
def tk_to_cirq(tkcirc: Circuit, indexed_qubits: List[QubitId]) -> cirq.Circuit: grid = tkcirc._int_routing_grid() qubits = _grid_to_qubits(grid) oplst = [] slices = [] for s in grid: news = set() for pair in s: if pair[0]>-1: news.add(pair[0]) slices.append(news) for s in slices: for v in s: op = tkcirc._unsigned_to_op(v) optype = op.get_type() if optype == OpType.Input or optype == OpType.Output: continue try: gatetype = _ops2cirq_mapping[optype] except KeyError as error: raise NotImplementedError("Cannot convert tket Op to cirq gate: " + op.get_name()) from error n_qubits = op.get_n_inputs() qids = [] for i in range(n_qubits): qbit = qubits[(v,i)] qids.append(indexed_qubits[qbit]) params = op.get_params() if gatetype in _rotation_types: cirqop = gatetype(exponent=params[0])(*qids) elif gatetype == cirq_common.MeasurementGate: for q in qids: cirqop = cirq_common.measure(q, key=op.get_desc()) else: cirqop = gatetype(*qids) oplst.append(cirqop) return cirq.Circuit.from_ops(*oplst)
[ "def", "tk_to_cirq", "(", "tkcirc", ":", "Circuit", ",", "indexed_qubits", ":", "List", "[", "QubitId", "]", ")", "->", "cirq", ".", "Circuit", ":", "grid", "=", "tkcirc", ".", "_int_routing_grid", "(", ")", "qubits", "=", "_grid_to_qubits", "(", "grid", ")", "oplst", "=", "[", "]", "slices", "=", "[", "]", "for", "s", "in", "grid", ":", "news", "=", "set", "(", ")", "for", "pair", "in", "s", ":", "if", "pair", "[", "0", "]", ">", "-", "1", ":", "news", ".", "add", "(", "pair", "[", "0", "]", ")", "slices", ".", "append", "(", "news", ")", "for", "s", "in", "slices", ":", "for", "v", "in", "s", ":", "op", "=", "tkcirc", ".", "_unsigned_to_op", "(", "v", ")", "optype", "=", "op", ".", "get_type", "(", ")", "if", "optype", "==", "OpType", ".", "Input", "or", "optype", "==", "OpType", ".", "Output", ":", "continue", "try", ":", "gatetype", "=", "_ops2cirq_mapping", "[", "optype", "]", "except", "KeyError", "as", "error", ":", "raise", "NotImplementedError", "(", "\"Cannot convert tket Op to cirq gate: \"", "+", "op", ".", "get_name", "(", ")", ")", "from", "error", "n_qubits", "=", "op", ".", "get_n_inputs", "(", ")", "qids", "=", "[", "]", "for", "i", "in", "range", "(", "n_qubits", ")", ":", "qbit", "=", "qubits", "[", "(", "v", ",", "i", ")", "]", "qids", ".", "append", "(", "indexed_qubits", "[", "qbit", "]", ")", "params", "=", "op", ".", "get_params", "(", ")", "if", "gatetype", "in", "_rotation_types", ":", "cirqop", "=", "gatetype", "(", "exponent", "=", "params", "[", "0", "]", ")", "(", "*", "qids", ")", "elif", "gatetype", "==", "cirq_common", ".", "MeasurementGate", ":", "for", "q", "in", "qids", ":", "cirqop", "=", "cirq_common", ".", "measure", "(", "q", ",", "key", "=", "op", ".", "get_desc", "(", ")", ")", "else", ":", "cirqop", "=", "gatetype", "(", "*", "qids", ")", "oplst", ".", "append", "(", "cirqop", ")", "return", "cirq", ".", "Circuit", ".", "from_ops", "(", "*", "oplst", ")" ]
Converts a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` object to a Cirq :py:class:`Circuit`. :param tkcirc: The input :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` :param indexed_qubits: Map from :math:`\\mathrm{t|ket}\\rangle` qubit indices to Cirq :py:class:`QubitId` s :return: The Cirq :py:class:`Circuit` corresponding to the input circuit
[ "Converts", "a", ":", "math", ":", "\\\\", "mathrm", "{", "t|ket", "}", "\\\\", "rangle", ":", "py", ":", "class", ":", "Circuit", "object", "to", "a", "Cirq", ":", "py", ":", "class", ":", "Circuit", ".", ":", "param", "tkcirc", ":", "The", "input", ":", "math", ":", "\\\\", "mathrm", "{", "t|ket", "}", "\\\\", "rangle", ":", "py", ":", "class", ":", "Circuit", ":", "param", "indexed_qubits", ":", "Map", "from", ":", "math", ":", "\\\\", "mathrm", "{", "t|ket", "}", "\\\\", "rangle", "qubit", "indices", "to", "Cirq", ":", "py", ":", "class", ":", "QubitId", "s", ":", "return", ":", "The", "Cirq", ":", "py", ":", "class", ":", "Circuit", "corresponding", "to", "the", "input", "circuit" ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/cirq/cirq_convert.py#L113-L157
CQCL/pytket
pytket/qiskit/dagcircuit_convert.py
dagcircuit_to_tk
def dagcircuit_to_tk(dag:DAGCircuit, _BOX_UNKNOWN:bool=BOX_UNKNOWN, _DROP_CONDS:bool=DROP_CONDS) -> Circuit : """Converts a :py:class:`qiskit.DAGCircuit` into a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit`. Note that not all Qiskit operations are currently supported by pytket. Classical registers are supported only as the output of measurements. This does not attempt to preserve the structure of the quantum registers, instead creating one big quantum register. :param dag: A circuit to be converted :return: The converted circuit """ qs = dag.get_qubits() qnames = ["%s[%d]" % (r.name, i) for r, i in qs] g = dag.multi_graph circ = Circuit() if DEBUG : print("new graph w " + str(len(qs)) + " qubits") print(str(qs)) # process vertices tk_vs = [ None for _ in range(dag.node_counter + 1) ] for n in g.nodes : node = g.nodes[n] if DEBUG : print(str(n) + " " + str(node["type"])+ " " + str(node["name"])) if ((node["type"]=="in" or node["type"]=="out") and not node["name"] in qnames) : # don't create vertices for in/outs of classical registers if DEBUG: print("Dropping node " + str(n)) continue else : tk_vs[n] = circ._add_vertex(_node_converter(circ, node, _BOX_UNKNOWN=_BOX_UNKNOWN, _DROP_CONDS=_DROP_CONDS)) if DEBUG: print("qiskit vertex " + str(n) + " is t|ket> vertex " +str(tk_vs[n])) # process edges for e in g.edges(data=True) : wire = e[2]["wire"] if wire in qs : # ignore classical wires src_port = _get_port_for_edge(g.node[e[0]], wire) tgt_port = _get_port_for_edge(g.node[e[1]], wire) if DEBUG : print(_make_edge_str(tk_vs[e[0]],src_port,tk_vs[e[1]],tgt_port)) circ._add_edge(tk_vs[e[0]],src_port,tk_vs[e[1]],tgt_port) return circ
python
def dagcircuit_to_tk(dag:DAGCircuit, _BOX_UNKNOWN:bool=BOX_UNKNOWN, _DROP_CONDS:bool=DROP_CONDS) -> Circuit : qs = dag.get_qubits() qnames = ["%s[%d]" % (r.name, i) for r, i in qs] g = dag.multi_graph circ = Circuit() if DEBUG : print("new graph w " + str(len(qs)) + " qubits") print(str(qs)) tk_vs = [ None for _ in range(dag.node_counter + 1) ] for n in g.nodes : node = g.nodes[n] if DEBUG : print(str(n) + " " + str(node["type"])+ " " + str(node["name"])) if ((node["type"]=="in" or node["type"]=="out") and not node["name"] in qnames) : if DEBUG: print("Dropping node " + str(n)) continue else : tk_vs[n] = circ._add_vertex(_node_converter(circ, node, _BOX_UNKNOWN=_BOX_UNKNOWN, _DROP_CONDS=_DROP_CONDS)) if DEBUG: print("qiskit vertex " + str(n) + " is t|ket> vertex " +str(tk_vs[n])) for e in g.edges(data=True) : wire = e[2]["wire"] if wire in qs : src_port = _get_port_for_edge(g.node[e[0]], wire) tgt_port = _get_port_for_edge(g.node[e[1]], wire) if DEBUG : print(_make_edge_str(tk_vs[e[0]],src_port,tk_vs[e[1]],tgt_port)) circ._add_edge(tk_vs[e[0]],src_port,tk_vs[e[1]],tgt_port) return circ
[ "def", "dagcircuit_to_tk", "(", "dag", ":", "DAGCircuit", ",", "_BOX_UNKNOWN", ":", "bool", "=", "BOX_UNKNOWN", ",", "_DROP_CONDS", ":", "bool", "=", "DROP_CONDS", ")", "->", "Circuit", ":", "qs", "=", "dag", ".", "get_qubits", "(", ")", "qnames", "=", "[", "\"%s[%d]\"", "%", "(", "r", ".", "name", ",", "i", ")", "for", "r", ",", "i", "in", "qs", "]", "g", "=", "dag", ".", "multi_graph", "circ", "=", "Circuit", "(", ")", "if", "DEBUG", ":", "print", "(", "\"new graph w \"", "+", "str", "(", "len", "(", "qs", ")", ")", "+", "\" qubits\"", ")", "print", "(", "str", "(", "qs", ")", ")", "# process vertices", "tk_vs", "=", "[", "None", "for", "_", "in", "range", "(", "dag", ".", "node_counter", "+", "1", ")", "]", "for", "n", "in", "g", ".", "nodes", ":", "node", "=", "g", ".", "nodes", "[", "n", "]", "if", "DEBUG", ":", "print", "(", "str", "(", "n", ")", "+", "\" \"", "+", "str", "(", "node", "[", "\"type\"", "]", ")", "+", "\" \"", "+", "str", "(", "node", "[", "\"name\"", "]", ")", ")", "if", "(", "(", "node", "[", "\"type\"", "]", "==", "\"in\"", "or", "node", "[", "\"type\"", "]", "==", "\"out\"", ")", "and", "not", "node", "[", "\"name\"", "]", "in", "qnames", ")", ":", "# don't create vertices for in/outs of classical registers", "if", "DEBUG", ":", "print", "(", "\"Dropping node \"", "+", "str", "(", "n", ")", ")", "continue", "else", ":", "tk_vs", "[", "n", "]", "=", "circ", ".", "_add_vertex", "(", "_node_converter", "(", "circ", ",", "node", ",", "_BOX_UNKNOWN", "=", "_BOX_UNKNOWN", ",", "_DROP_CONDS", "=", "_DROP_CONDS", ")", ")", "if", "DEBUG", ":", "print", "(", "\"qiskit vertex \"", "+", "str", "(", "n", ")", "+", "\" is t|ket> vertex \"", "+", "str", "(", "tk_vs", "[", "n", "]", ")", ")", "# process edges", "for", "e", "in", "g", ".", "edges", "(", "data", "=", "True", ")", ":", "wire", "=", "e", "[", "2", "]", "[", "\"wire\"", "]", "if", "wire", "in", "qs", ":", "# ignore classical wires", "src_port", "=", "_get_port_for_edge", "(", "g", ".", "node", "[", "e", "[", "0", "]", "]", ",", "wire", ")", "tgt_port", "=", "_get_port_for_edge", "(", "g", ".", "node", "[", "e", "[", "1", "]", "]", ",", "wire", ")", "if", "DEBUG", ":", "print", "(", "_make_edge_str", "(", "tk_vs", "[", "e", "[", "0", "]", "]", ",", "src_port", ",", "tk_vs", "[", "e", "[", "1", "]", "]", ",", "tgt_port", ")", ")", "circ", ".", "_add_edge", "(", "tk_vs", "[", "e", "[", "0", "]", "]", ",", "src_port", ",", "tk_vs", "[", "e", "[", "1", "]", "]", ",", "tgt_port", ")", "return", "circ" ]
Converts a :py:class:`qiskit.DAGCircuit` into a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit`. Note that not all Qiskit operations are currently supported by pytket. Classical registers are supported only as the output of measurements. This does not attempt to preserve the structure of the quantum registers, instead creating one big quantum register. :param dag: A circuit to be converted :return: The converted circuit
[ "Converts", "a", ":", "py", ":", "class", ":", "qiskit", ".", "DAGCircuit", "into", "a", ":", "math", ":", "\\\\", "mathrm", "{", "t|ket", "}", "\\\\", "rangle", ":", "py", ":", "class", ":", "Circuit", ".", "Note", "that", "not", "all", "Qiskit", "operations", "are", "currently", "supported", "by", "pytket", ".", "Classical", "registers", "are", "supported", "only", "as", "the", "output", "of", "measurements", ".", "This", "does", "not", "attempt", "to", "preserve", "the", "structure", "of", "the", "quantum", "registers", "instead", "creating", "one", "big", "quantum", "register", "." ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/qiskit/dagcircuit_convert.py#L61-L104
CQCL/pytket
pytket/qiskit/dagcircuit_convert.py
tk_to_dagcircuit
def tk_to_dagcircuit(circ:Circuit,_qreg_name:str="q") -> DAGCircuit : """ Convert a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` to a :py:class:`qiskit.DAGCircuit` . Requires that the circuit only conatins :py:class:`OpType` s from the qelib set. :param circ: A circuit to be converted :return: The converted circuit """ dc = DAGCircuit() qreg = QuantumRegister(circ.n_qubits(), name=_qreg_name) dc.add_qreg(qreg) grid = circ._int_routing_grid() slices = _grid_to_slices(grid) qubits = _grid_to_qubits(grid, qreg) in_boundary = circ._get_boundary()[0] out_boundary = circ._get_boundary()[1] for s in slices : for v in s: o = circ._unsigned_to_op(v) qargs = [ qubits[(v,i)] for i in range(o.get_n_inputs()) ] name, cargs, params = _translate_ops(circ,v) if cargs : _extend_cregs(dc,cargs) if name : dc.add_basis_element(name,o.get_n_inputs(),number_classical=len(cargs),number_parameters=len(params)) ins = Instruction(name, list(map(_normalise_param_out, params)), qargs, cargs) dc.apply_operation_back(ins ,qargs=qargs, cargs=cargs) tk2dg_outs = {} for v in out_boundary: tk2dg_outs[v] = dc.output_map[qubits[(v,0)]] for i, v in enumerate(out_boundary): dc.multi_graph.node[tk2dg_outs[v]]["wire"] = [qubits[(in_boundary[i],0)]] dc.output_map[qubits[(in_boundary[i],0)]] = tk2dg_outs[v] return dc
python
def tk_to_dagcircuit(circ:Circuit,_qreg_name:str="q") -> DAGCircuit : dc = DAGCircuit() qreg = QuantumRegister(circ.n_qubits(), name=_qreg_name) dc.add_qreg(qreg) grid = circ._int_routing_grid() slices = _grid_to_slices(grid) qubits = _grid_to_qubits(grid, qreg) in_boundary = circ._get_boundary()[0] out_boundary = circ._get_boundary()[1] for s in slices : for v in s: o = circ._unsigned_to_op(v) qargs = [ qubits[(v,i)] for i in range(o.get_n_inputs()) ] name, cargs, params = _translate_ops(circ,v) if cargs : _extend_cregs(dc,cargs) if name : dc.add_basis_element(name,o.get_n_inputs(),number_classical=len(cargs),number_parameters=len(params)) ins = Instruction(name, list(map(_normalise_param_out, params)), qargs, cargs) dc.apply_operation_back(ins ,qargs=qargs, cargs=cargs) tk2dg_outs = {} for v in out_boundary: tk2dg_outs[v] = dc.output_map[qubits[(v,0)]] for i, v in enumerate(out_boundary): dc.multi_graph.node[tk2dg_outs[v]]["wire"] = [qubits[(in_boundary[i],0)]] dc.output_map[qubits[(in_boundary[i],0)]] = tk2dg_outs[v] return dc
[ "def", "tk_to_dagcircuit", "(", "circ", ":", "Circuit", ",", "_qreg_name", ":", "str", "=", "\"q\"", ")", "->", "DAGCircuit", ":", "dc", "=", "DAGCircuit", "(", ")", "qreg", "=", "QuantumRegister", "(", "circ", ".", "n_qubits", "(", ")", ",", "name", "=", "_qreg_name", ")", "dc", ".", "add_qreg", "(", "qreg", ")", "grid", "=", "circ", ".", "_int_routing_grid", "(", ")", "slices", "=", "_grid_to_slices", "(", "grid", ")", "qubits", "=", "_grid_to_qubits", "(", "grid", ",", "qreg", ")", "in_boundary", "=", "circ", ".", "_get_boundary", "(", ")", "[", "0", "]", "out_boundary", "=", "circ", ".", "_get_boundary", "(", ")", "[", "1", "]", "for", "s", "in", "slices", ":", "for", "v", "in", "s", ":", "o", "=", "circ", ".", "_unsigned_to_op", "(", "v", ")", "qargs", "=", "[", "qubits", "[", "(", "v", ",", "i", ")", "]", "for", "i", "in", "range", "(", "o", ".", "get_n_inputs", "(", ")", ")", "]", "name", ",", "cargs", ",", "params", "=", "_translate_ops", "(", "circ", ",", "v", ")", "if", "cargs", ":", "_extend_cregs", "(", "dc", ",", "cargs", ")", "if", "name", ":", "dc", ".", "add_basis_element", "(", "name", ",", "o", ".", "get_n_inputs", "(", ")", ",", "number_classical", "=", "len", "(", "cargs", ")", ",", "number_parameters", "=", "len", "(", "params", ")", ")", "ins", "=", "Instruction", "(", "name", ",", "list", "(", "map", "(", "_normalise_param_out", ",", "params", ")", ")", ",", "qargs", ",", "cargs", ")", "dc", ".", "apply_operation_back", "(", "ins", ",", "qargs", "=", "qargs", ",", "cargs", "=", "cargs", ")", "tk2dg_outs", "=", "{", "}", "for", "v", "in", "out_boundary", ":", "tk2dg_outs", "[", "v", "]", "=", "dc", ".", "output_map", "[", "qubits", "[", "(", "v", ",", "0", ")", "]", "]", "for", "i", ",", "v", "in", "enumerate", "(", "out_boundary", ")", ":", "dc", ".", "multi_graph", ".", "node", "[", "tk2dg_outs", "[", "v", "]", "]", "[", "\"wire\"", "]", "=", "[", "qubits", "[", "(", "in_boundary", "[", "i", "]", ",", "0", ")", "]", "]", "dc", ".", "output_map", "[", "qubits", "[", "(", "in_boundary", "[", "i", "]", ",", "0", ")", "]", "]", "=", "tk2dg_outs", "[", "v", "]", "return", "dc" ]
Convert a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` to a :py:class:`qiskit.DAGCircuit` . Requires that the circuit only conatins :py:class:`OpType` s from the qelib set. :param circ: A circuit to be converted :return: The converted circuit
[ "Convert", "a", ":", "math", ":", "\\\\", "mathrm", "{", "t|ket", "}", "\\\\", "rangle", ":", "py", ":", "class", ":", "Circuit", "to", "a", ":", "py", ":", "class", ":", "qiskit", ".", "DAGCircuit", ".", "Requires", "that", "the", "circuit", "only", "conatins", ":", "py", ":", "class", ":", "OpType", "s", "from", "the", "qelib", "set", ".", ":", "param", "circ", ":", "A", "circuit", "to", "be", "converted" ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/qiskit/dagcircuit_convert.py#L258-L293
CQCL/pytket
pytket/qiskit/dagcircuit_convert.py
coupling_to_arc
def coupling_to_arc(coupling_map:List[List[int]]) -> Architecture: """ Produces a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Architecture` corresponding to a (directed) coupling map, stating the pairs of qubits between which two-qubit interactions (e.g. CXs) can be applied. :param coupling_map: Pairs of indices where each pair [control, target] permits the use of CXs between them :return: The :math:`\\mathrm{t|ket}\\rangle` :py:class:`Architecture` capturing the behaviour of the coupling map """ coupling = CouplingMap(couplinglist=coupling_map) return DirectedGraph(coupling_map,coupling.size())
python
def coupling_to_arc(coupling_map:List[List[int]]) -> Architecture: coupling = CouplingMap(couplinglist=coupling_map) return DirectedGraph(coupling_map,coupling.size())
[ "def", "coupling_to_arc", "(", "coupling_map", ":", "List", "[", "List", "[", "int", "]", "]", ")", "->", "Architecture", ":", "coupling", "=", "CouplingMap", "(", "couplinglist", "=", "coupling_map", ")", "return", "DirectedGraph", "(", "coupling_map", ",", "coupling", ".", "size", "(", ")", ")" ]
Produces a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Architecture` corresponding to a (directed) coupling map, stating the pairs of qubits between which two-qubit interactions (e.g. CXs) can be applied. :param coupling_map: Pairs of indices where each pair [control, target] permits the use of CXs between them :return: The :math:`\\mathrm{t|ket}\\rangle` :py:class:`Architecture` capturing the behaviour of the coupling map
[ "Produces", "a", ":", "math", ":", "\\\\", "mathrm", "{", "t|ket", "}", "\\\\", "rangle", ":", "py", ":", "class", ":", "Architecture", "corresponding", "to", "a", "(", "directed", ")", "coupling", "map", "stating", "the", "pairs", "of", "qubits", "between", "which", "two", "-", "qubit", "interactions", "(", "e", ".", "g", ".", "CXs", ")", "can", "be", "applied", "." ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/qiskit/dagcircuit_convert.py#L368-L380
CQCL/pytket
pytket/qiskit/tket_pass.py
TketPass.run
def run(self, dag:DAGCircuit) -> DAGCircuit: """ Run one pass of optimisation on the circuit and route for the given backend. :param dag: The circuit to optimise and route :return: The modified circuit """ circ = dagcircuit_to_tk(dag, _DROP_CONDS=self.DROP_CONDS,_BOX_UNKNOWN=self.BOX_UNKNOWN) circ, circlay = self.process_circ(circ) newdag = tk_to_dagcircuit(circ) newdag.name = dag.name finlay = dict() for i, qi in enumerate(circlay): finlay[('q', i)] = ('q', qi) newdag.final_layout = finlay return newdag
python
def run(self, dag:DAGCircuit) -> DAGCircuit: circ = dagcircuit_to_tk(dag, _DROP_CONDS=self.DROP_CONDS,_BOX_UNKNOWN=self.BOX_UNKNOWN) circ, circlay = self.process_circ(circ) newdag = tk_to_dagcircuit(circ) newdag.name = dag.name finlay = dict() for i, qi in enumerate(circlay): finlay[('q', i)] = ('q', qi) newdag.final_layout = finlay return newdag
[ "def", "run", "(", "self", ",", "dag", ":", "DAGCircuit", ")", "->", "DAGCircuit", ":", "circ", "=", "dagcircuit_to_tk", "(", "dag", ",", "_DROP_CONDS", "=", "self", ".", "DROP_CONDS", ",", "_BOX_UNKNOWN", "=", "self", ".", "BOX_UNKNOWN", ")", "circ", ",", "circlay", "=", "self", ".", "process_circ", "(", "circ", ")", "newdag", "=", "tk_to_dagcircuit", "(", "circ", ")", "newdag", ".", "name", "=", "dag", ".", "name", "finlay", "=", "dict", "(", ")", "for", "i", ",", "qi", "in", "enumerate", "(", "circlay", ")", ":", "finlay", "[", "(", "'q'", ",", "i", ")", "]", "=", "(", "'q'", ",", "qi", ")", "newdag", ".", "final_layout", "=", "finlay", "return", "newdag" ]
Run one pass of optimisation on the circuit and route for the given backend. :param dag: The circuit to optimise and route :return: The modified circuit
[ "Run", "one", "pass", "of", "optimisation", "on", "the", "circuit", "and", "route", "for", "the", "given", "backend", "." ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/qiskit/tket_pass.py#L51-L68
CQCL/pytket
pytket/cirq/qubits.py
_sort_row_col
def _sort_row_col(qubits: Iterator[GridQubit]) -> List[GridQubit]: """Sort grid qubits first by row then by column""" return sorted(qubits, key=lambda x: (x.row, x.col))
python
def _sort_row_col(qubits: Iterator[GridQubit]) -> List[GridQubit]: return sorted(qubits, key=lambda x: (x.row, x.col))
[ "def", "_sort_row_col", "(", "qubits", ":", "Iterator", "[", "GridQubit", "]", ")", "->", "List", "[", "GridQubit", "]", ":", "return", "sorted", "(", "qubits", ",", "key", "=", "lambda", "x", ":", "(", "x", ".", "row", ",", "x", ".", "col", ")", ")" ]
Sort grid qubits first by row then by column
[ "Sort", "grid", "qubits", "first", "by", "row", "then", "by", "column" ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/cirq/qubits.py#L51-L54
CQCL/pytket
pytket/cirq/qubits.py
xmon_to_arc
def xmon_to_arc(xmon: XmonDevice) -> Architecture: """Generates a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Architecture` object for a Cirq :py:class:`XmonDevice` . :param xmon: The device to convert :return: The corresponding :math:`\\mathrm{t|ket}\\rangle` :py:class:`Architecture` """ nodes = len(xmon.qubits) indexed_qubits = _sort_row_col(xmon.qubits) pairs = [] for qb in indexed_qubits: neighbours = xmon.neighbors_of(qb) #filter only higher index neighbours to avoid double counting edges forward_neighbours = filter(lambda x: indexed_qubits.index(x)>indexed_qubits.index(qb), neighbours) for x in forward_neighbours: pairs.append((indexed_qubits.index(qb), indexed_qubits.index(x))) return Architecture(pairs, nodes)
python
def xmon_to_arc(xmon: XmonDevice) -> Architecture: nodes = len(xmon.qubits) indexed_qubits = _sort_row_col(xmon.qubits) pairs = [] for qb in indexed_qubits: neighbours = xmon.neighbors_of(qb) forward_neighbours = filter(lambda x: indexed_qubits.index(x)>indexed_qubits.index(qb), neighbours) for x in forward_neighbours: pairs.append((indexed_qubits.index(qb), indexed_qubits.index(x))) return Architecture(pairs, nodes)
[ "def", "xmon_to_arc", "(", "xmon", ":", "XmonDevice", ")", "->", "Architecture", ":", "nodes", "=", "len", "(", "xmon", ".", "qubits", ")", "indexed_qubits", "=", "_sort_row_col", "(", "xmon", ".", "qubits", ")", "pairs", "=", "[", "]", "for", "qb", "in", "indexed_qubits", ":", "neighbours", "=", "xmon", ".", "neighbors_of", "(", "qb", ")", "#filter only higher index neighbours to avoid double counting edges", "forward_neighbours", "=", "filter", "(", "lambda", "x", ":", "indexed_qubits", ".", "index", "(", "x", ")", ">", "indexed_qubits", ".", "index", "(", "qb", ")", ",", "neighbours", ")", "for", "x", "in", "forward_neighbours", ":", "pairs", ".", "append", "(", "(", "indexed_qubits", ".", "index", "(", "qb", ")", ",", "indexed_qubits", ".", "index", "(", "x", ")", ")", ")", "return", "Architecture", "(", "pairs", ",", "nodes", ")" ]
Generates a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Architecture` object for a Cirq :py:class:`XmonDevice` . :param xmon: The device to convert :return: The corresponding :math:`\\mathrm{t|ket}\\rangle` :py:class:`Architecture`
[ "Generates", "a", ":", "math", ":", "\\\\", "mathrm", "{", "t|ket", "}", "\\\\", "rangle", ":", "py", ":", "class", ":", "Architecture", "object", "for", "a", "Cirq", ":", "py", ":", "class", ":", "XmonDevice", ".", ":", "param", "xmon", ":", "The", "device", "to", "convert" ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/cirq/qubits.py#L56-L73
CQCL/pytket
pytket/pyquil/pyquil_convert.py
pyquil_to_tk
def pyquil_to_tk(prog: Program) -> Circuit: """ Convert a :py:class:`pyquil.Program` to a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` . Note that not all pyQuil operations are currently supported by pytket. :param prog: A circuit to be converted :return: The converted circuit """ reg_name = None qubits = prog.get_qubits() n_qubits = max(qubits) + 1 tkc = Circuit(n_qubits) for i in prog.instructions: if isinstance(i, Gate): name = i.name try: optype = _known_quil_gate[name] except KeyError as error: raise NotImplementedError("Operation not supported by tket: " + str(i)) from error if len(i.params) == 0: tkc.add_operation(optype, [q.index for q in i.qubits]) else: params = [p/PI for p in i.params] op = tkc._get_op(optype,len(i.qubits),len(i.qubits),params) tkc._add_operation(op, [q.index for q in i.qubits]) elif isinstance(i, Measurement): if not i.classical_reg: raise NotImplementedError("Program has no defined classical register for measurement on qubit: ", i.qubits[0]) reg = i.classical_reg if reg_name and reg_name != reg.name: raise NotImplementedError("Program has multiple classical registers: ", reg_name, reg.name) reg_name = reg.name op = tkc._get_op(OpType.Measure,1,1,str(reg.offset)) tkc._add_operation(op, [i.qubit.index]) elif isinstance(i, Declare): continue elif isinstance(i, Pragma): continue elif isinstance(i, Halt): return tkc else: raise NotImplementedError("Pyquil instruction is not a gate: " + str(i)) return tkc
python
def pyquil_to_tk(prog: Program) -> Circuit: reg_name = None qubits = prog.get_qubits() n_qubits = max(qubits) + 1 tkc = Circuit(n_qubits) for i in prog.instructions: if isinstance(i, Gate): name = i.name try: optype = _known_quil_gate[name] except KeyError as error: raise NotImplementedError("Operation not supported by tket: " + str(i)) from error if len(i.params) == 0: tkc.add_operation(optype, [q.index for q in i.qubits]) else: params = [p/PI for p in i.params] op = tkc._get_op(optype,len(i.qubits),len(i.qubits),params) tkc._add_operation(op, [q.index for q in i.qubits]) elif isinstance(i, Measurement): if not i.classical_reg: raise NotImplementedError("Program has no defined classical register for measurement on qubit: ", i.qubits[0]) reg = i.classical_reg if reg_name and reg_name != reg.name: raise NotImplementedError("Program has multiple classical registers: ", reg_name, reg.name) reg_name = reg.name op = tkc._get_op(OpType.Measure,1,1,str(reg.offset)) tkc._add_operation(op, [i.qubit.index]) elif isinstance(i, Declare): continue elif isinstance(i, Pragma): continue elif isinstance(i, Halt): return tkc else: raise NotImplementedError("Pyquil instruction is not a gate: " + str(i)) return tkc
[ "def", "pyquil_to_tk", "(", "prog", ":", "Program", ")", "->", "Circuit", ":", "reg_name", "=", "None", "qubits", "=", "prog", ".", "get_qubits", "(", ")", "n_qubits", "=", "max", "(", "qubits", ")", "+", "1", "tkc", "=", "Circuit", "(", "n_qubits", ")", "for", "i", "in", "prog", ".", "instructions", ":", "if", "isinstance", "(", "i", ",", "Gate", ")", ":", "name", "=", "i", ".", "name", "try", ":", "optype", "=", "_known_quil_gate", "[", "name", "]", "except", "KeyError", "as", "error", ":", "raise", "NotImplementedError", "(", "\"Operation not supported by tket: \"", "+", "str", "(", "i", ")", ")", "from", "error", "if", "len", "(", "i", ".", "params", ")", "==", "0", ":", "tkc", ".", "add_operation", "(", "optype", ",", "[", "q", ".", "index", "for", "q", "in", "i", ".", "qubits", "]", ")", "else", ":", "params", "=", "[", "p", "/", "PI", "for", "p", "in", "i", ".", "params", "]", "op", "=", "tkc", ".", "_get_op", "(", "optype", ",", "len", "(", "i", ".", "qubits", ")", ",", "len", "(", "i", ".", "qubits", ")", ",", "params", ")", "tkc", ".", "_add_operation", "(", "op", ",", "[", "q", ".", "index", "for", "q", "in", "i", ".", "qubits", "]", ")", "elif", "isinstance", "(", "i", ",", "Measurement", ")", ":", "if", "not", "i", ".", "classical_reg", ":", "raise", "NotImplementedError", "(", "\"Program has no defined classical register for measurement on qubit: \"", ",", "i", ".", "qubits", "[", "0", "]", ")", "reg", "=", "i", ".", "classical_reg", "if", "reg_name", "and", "reg_name", "!=", "reg", ".", "name", ":", "raise", "NotImplementedError", "(", "\"Program has multiple classical registers: \"", ",", "reg_name", ",", "reg", ".", "name", ")", "reg_name", "=", "reg", ".", "name", "op", "=", "tkc", ".", "_get_op", "(", "OpType", ".", "Measure", ",", "1", ",", "1", ",", "str", "(", "reg", ".", "offset", ")", ")", "tkc", ".", "_add_operation", "(", "op", ",", "[", "i", ".", "qubit", ".", "index", "]", ")", "elif", "isinstance", "(", "i", ",", "Declare", ")", ":", "continue", "elif", "isinstance", "(", "i", ",", "Pragma", ")", ":", "continue", "elif", "isinstance", "(", "i", ",", "Halt", ")", ":", "return", "tkc", "else", ":", "raise", "NotImplementedError", "(", "\"Pyquil instruction is not a gate: \"", "+", "str", "(", "i", ")", ")", "return", "tkc" ]
Convert a :py:class:`pyquil.Program` to a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` . Note that not all pyQuil operations are currently supported by pytket. :param prog: A circuit to be converted :return: The converted circuit
[ "Convert", "a", ":", "py", ":", "class", ":", "pyquil", ".", "Program", "to", "a", ":", "math", ":", "\\\\", "mathrm", "{", "t|ket", "}", "\\\\", "rangle", ":", "py", ":", "class", ":", "Circuit", ".", "Note", "that", "not", "all", "pyQuil", "operations", "are", "currently", "supported", "by", "pytket", ".", ":", "param", "prog", ":", "A", "circuit", "to", "be", "converted" ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/pyquil/pyquil_convert.py#L47-L90
CQCL/pytket
pytket/pyquil/pyquil_convert.py
tk_to_pyquil
def tk_to_pyquil(circ: Circuit) -> Program: """ Convert a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` to a :py:class:`pyquil.Program` . :param circ: A circuit to be converted :return: The converted circuit """ p = Program() ro = p.declare('ro', 'BIT', circ.n_qubits()) grid = circ._int_routing_grid() qubits = _grid_to_qubits(grid) slices = [] for s in grid: news = set() for pair in s: if pair[0]>-1: news.add(pair[0]) slices.append(news) for s in slices: for v in s: op = circ._unsigned_to_op(v) optype = op.get_type() if optype == OpType.Input or optype == OpType.Output: continue elif optype == OpType.Measure: p += Measurement(qubits[(v, 0)], ro[int(op.get_desc())]) continue try: gatetype = _known_quil_gate_rev[optype] except KeyError as error: raise NotImplementedError("Cannot convert tket Op to pyquil gate: " + op.get_name()) from error params = [p*PI for p in op.get_params()] g = Gate(gatetype, params, [qubits[(v,port)] for port in range(op.get_n_inputs())]) p += g return p
python
def tk_to_pyquil(circ: Circuit) -> Program: p = Program() ro = p.declare('ro', 'BIT', circ.n_qubits()) grid = circ._int_routing_grid() qubits = _grid_to_qubits(grid) slices = [] for s in grid: news = set() for pair in s: if pair[0]>-1: news.add(pair[0]) slices.append(news) for s in slices: for v in s: op = circ._unsigned_to_op(v) optype = op.get_type() if optype == OpType.Input or optype == OpType.Output: continue elif optype == OpType.Measure: p += Measurement(qubits[(v, 0)], ro[int(op.get_desc())]) continue try: gatetype = _known_quil_gate_rev[optype] except KeyError as error: raise NotImplementedError("Cannot convert tket Op to pyquil gate: " + op.get_name()) from error params = [p*PI for p in op.get_params()] g = Gate(gatetype, params, [qubits[(v,port)] for port in range(op.get_n_inputs())]) p += g return p
[ "def", "tk_to_pyquil", "(", "circ", ":", "Circuit", ")", "->", "Program", ":", "p", "=", "Program", "(", ")", "ro", "=", "p", ".", "declare", "(", "'ro'", ",", "'BIT'", ",", "circ", ".", "n_qubits", "(", ")", ")", "grid", "=", "circ", ".", "_int_routing_grid", "(", ")", "qubits", "=", "_grid_to_qubits", "(", "grid", ")", "slices", "=", "[", "]", "for", "s", "in", "grid", ":", "news", "=", "set", "(", ")", "for", "pair", "in", "s", ":", "if", "pair", "[", "0", "]", ">", "-", "1", ":", "news", ".", "add", "(", "pair", "[", "0", "]", ")", "slices", ".", "append", "(", "news", ")", "for", "s", "in", "slices", ":", "for", "v", "in", "s", ":", "op", "=", "circ", ".", "_unsigned_to_op", "(", "v", ")", "optype", "=", "op", ".", "get_type", "(", ")", "if", "optype", "==", "OpType", ".", "Input", "or", "optype", "==", "OpType", ".", "Output", ":", "continue", "elif", "optype", "==", "OpType", ".", "Measure", ":", "p", "+=", "Measurement", "(", "qubits", "[", "(", "v", ",", "0", ")", "]", ",", "ro", "[", "int", "(", "op", ".", "get_desc", "(", ")", ")", "]", ")", "continue", "try", ":", "gatetype", "=", "_known_quil_gate_rev", "[", "optype", "]", "except", "KeyError", "as", "error", ":", "raise", "NotImplementedError", "(", "\"Cannot convert tket Op to pyquil gate: \"", "+", "op", ".", "get_name", "(", ")", ")", "from", "error", "params", "=", "[", "p", "*", "PI", "for", "p", "in", "op", ".", "get_params", "(", ")", "]", "g", "=", "Gate", "(", "gatetype", ",", "params", ",", "[", "qubits", "[", "(", "v", ",", "port", ")", "]", "for", "port", "in", "range", "(", "op", ".", "get_n_inputs", "(", ")", ")", "]", ")", "p", "+=", "g", "return", "p" ]
Convert a :math:`\\mathrm{t|ket}\\rangle` :py:class:`Circuit` to a :py:class:`pyquil.Program` . :param circ: A circuit to be converted :return: The converted circuit
[ "Convert", "a", ":", "math", ":", "\\\\", "mathrm", "{", "t|ket", "}", "\\\\", "rangle", ":", "py", ":", "class", ":", "Circuit", "to", "a", ":", "py", ":", "class", ":", "pyquil", ".", "Program", ".", ":", "param", "circ", ":", "A", "circuit", "to", "be", "converted" ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/pyquil/pyquil_convert.py#L100-L135
CQCL/pytket
pytket/chemistry/aqua/qse.py
QSE.print_setting
def print_setting(self) -> str: """ Presents the QSE settings as a string. :return: The formatted settings of the QSE instance """ ret = "\n" ret += "==================== Setting of {} ============================\n".format(self.configuration['name']) ret += "{}".format(self.setting) ret += "===============================================================\n" ret += "{}".format(self._var_form.setting) ret += "===============================================================\n" return ret
python
def print_setting(self) -> str: ret = "\n" ret += "==================== Setting of {} ============================\n".format(self.configuration['name']) ret += "{}".format(self.setting) ret += "===============================================================\n" ret += "{}".format(self._var_form.setting) ret += "===============================================================\n" return ret
[ "def", "print_setting", "(", "self", ")", "->", "str", ":", "ret", "=", "\"\\n\"", "ret", "+=", "\"==================== Setting of {} ============================\\n\"", ".", "format", "(", "self", ".", "configuration", "[", "'name'", "]", ")", "ret", "+=", "\"{}\"", ".", "format", "(", "self", ".", "setting", ")", "ret", "+=", "\"===============================================================\\n\"", "ret", "+=", "\"{}\"", ".", "format", "(", "self", ".", "_var_form", ".", "setting", ")", "ret", "+=", "\"===============================================================\\n\"", "return", "ret" ]
Presents the QSE settings as a string. :return: The formatted settings of the QSE instance
[ "Presents", "the", "QSE", "settings", "as", "a", "string", "." ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/chemistry/aqua/qse.py#L117-L129
CQCL/pytket
pytket/chemistry/aqua/qse.py
QSE._energy_evaluation
def _energy_evaluation(self, operator): """ Evaluate the energy of the current input circuit with respect to the given operator. :param operator: Hamiltonian of the system :return: Energy of the Hamiltonian """ if self._quantum_state is not None: input_circuit = self._quantum_state else: input_circuit = [self.opt_circuit] if operator._paulis: mean_energy, std_energy = operator.evaluate_with_result(self._operator_mode, input_circuit, self._quantum_instance.backend, self.ret) else: mean_energy = 0.0 std_energy = 0.0 operator.disable_summarize_circuits() logger.debug('Energy evaluation {} returned {}'.format(self._eval_count, np.real(mean_energy))) return np.real(mean_energy), np.real(std_energy)
python
def _energy_evaluation(self, operator): if self._quantum_state is not None: input_circuit = self._quantum_state else: input_circuit = [self.opt_circuit] if operator._paulis: mean_energy, std_energy = operator.evaluate_with_result(self._operator_mode, input_circuit, self._quantum_instance.backend, self.ret) else: mean_energy = 0.0 std_energy = 0.0 operator.disable_summarize_circuits() logger.debug('Energy evaluation {} returned {}'.format(self._eval_count, np.real(mean_energy))) return np.real(mean_energy), np.real(std_energy)
[ "def", "_energy_evaluation", "(", "self", ",", "operator", ")", ":", "if", "self", ".", "_quantum_state", "is", "not", "None", ":", "input_circuit", "=", "self", ".", "_quantum_state", "else", ":", "input_circuit", "=", "[", "self", ".", "opt_circuit", "]", "if", "operator", ".", "_paulis", ":", "mean_energy", ",", "std_energy", "=", "operator", ".", "evaluate_with_result", "(", "self", ".", "_operator_mode", ",", "input_circuit", ",", "self", ".", "_quantum_instance", ".", "backend", ",", "self", ".", "ret", ")", "else", ":", "mean_energy", "=", "0.0", "std_energy", "=", "0.0", "operator", ".", "disable_summarize_circuits", "(", ")", "logger", ".", "debug", "(", "'Energy evaluation {} returned {}'", ".", "format", "(", "self", ".", "_eval_count", ",", "np", ".", "real", "(", "mean_energy", ")", ")", ")", "return", "np", ".", "real", "(", "mean_energy", ")", ",", "np", ".", "real", "(", "std_energy", ")" ]
Evaluate the energy of the current input circuit with respect to the given operator. :param operator: Hamiltonian of the system :return: Energy of the Hamiltonian
[ "Evaluate", "the", "energy", "of", "the", "current", "input", "circuit", "with", "respect", "to", "the", "given", "operator", "." ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/chemistry/aqua/qse.py#L131-L151
CQCL/pytket
pytket/chemistry/aqua/qse.py
QSE._run
def _run(self) -> dict: """ Runs the QSE algorithm to compute the eigenvalues of the Hamiltonian. :return: Dictionary of results """ if not self._quantum_instance.is_statevector: raise AquaError("Can only calculate state for QSE with statevector backends") ret = self._quantum_instance.execute(self.opt_circuit) self.ret = ret self._eval_count = 0 self._solve() self._ret['eval_count'] = self._eval_count self._ret['eval_time'] = self._eval_time return self._ret
python
def _run(self) -> dict: if not self._quantum_instance.is_statevector: raise AquaError("Can only calculate state for QSE with statevector backends") ret = self._quantum_instance.execute(self.opt_circuit) self.ret = ret self._eval_count = 0 self._solve() self._ret['eval_count'] = self._eval_count self._ret['eval_time'] = self._eval_time return self._ret
[ "def", "_run", "(", "self", ")", "->", "dict", ":", "if", "not", "self", ".", "_quantum_instance", ".", "is_statevector", ":", "raise", "AquaError", "(", "\"Can only calculate state for QSE with statevector backends\"", ")", "ret", "=", "self", ".", "_quantum_instance", ".", "execute", "(", "self", ".", "opt_circuit", ")", "self", ".", "ret", "=", "ret", "self", ".", "_eval_count", "=", "0", "self", ".", "_solve", "(", ")", "self", ".", "_ret", "[", "'eval_count'", "]", "=", "self", ".", "_eval_count", "self", ".", "_ret", "[", "'eval_time'", "]", "=", "self", ".", "_eval_time", "return", "self", ".", "_ret" ]
Runs the QSE algorithm to compute the eigenvalues of the Hamiltonian. :return: Dictionary of results
[ "Runs", "the", "QSE", "algorithm", "to", "compute", "the", "eigenvalues", "of", "the", "Hamiltonian", "." ]
train
https://github.com/CQCL/pytket/blob/ae68f7402dcb5fb45221832cc6185d267bdd7a71/pytket/chemistry/aqua/qse.py#L260-L274
bkabrda/flask-whooshee
flask_whooshee.py
WhoosheeQuery.whooshee_search
def whooshee_search(self, search_string, group=whoosh.qparser.OrGroup, whoosheer=None, match_substrings=True, limit=None, order_by_relevance=10): """Do a fulltext search on the query. Returns a query filtered with results of the fulltext search. :param search_string: The string to search for. :param group: The whoosh group to use for searching. Defaults to :class:`whoosh.qparser.OrGroup` which searches for all words in all columns. :param match_substrings: ``True`` if you want to match substrings, ``False`` otherwise :param limit: The number of the top records to be returned. Defaults to ``None`` and returns all records. """ if not whoosheer: ### inspiration taken from flask-WhooshAlchemy # find out all entities in join entities = set() # directly queried entities for cd in self.column_descriptions: entities.add(cd['type']) # joined entities if self._join_entities and isinstance(self._join_entities[0], Mapper): # SQLAlchemy >= 0.8.0 entities.update(set([x.entity for x in self._join_entities])) else: # SQLAlchemy < 0.8.0 entities.update(set(self._join_entities)) # make sure we can work with aliased entities unaliased = set() for entity in entities: if isinstance(entity, (AliasedClass, AliasedInsp)): unaliased.add(inspect(entity).mapper.class_) else: unaliased.add(entity) whoosheer = next(w for w in _get_config(self)['whoosheers'] if set(w.models) == unaliased) # TODO what if unique field doesn't exist or there are multiple? for fname, field in list(whoosheer.schema._fields.items()): if field.unique: uniq = fname # TODO: use something more general than id res = whoosheer.search(search_string=search_string, values_of=uniq, group=group, match_substrings=match_substrings, limit=limit) if not res: return self.filter(text('null')) # transform unique field name into model attribute field attr = None if hasattr(whoosheer, '_is_model_whoosheer'): attr = getattr(whoosheer.models[0], uniq) else: # non-model whoosheers must have unique field named # model.__name__.lower + '_' + attr for m in whoosheer.models: if m.__name__.lower() == uniq.split('_')[0]: attr = getattr(m, uniq.split('_')[1]) search_query = self.filter(attr.in_(res)) if order_by_relevance < 0: # we want all returned rows ordered search_query = search_query.order_by(sqlalchemy.sql.expression.case( [(attr == uniq_val, index) for index, uniq_val in enumerate(res)], )) elif order_by_relevance > 0: # we want only number of specified rows ordered search_query = search_query.order_by(sqlalchemy.sql.expression.case( [(attr == uniq_val, index) for index, uniq_val in enumerate(res) if index < order_by_relevance], else_=order_by_relevance )) else: # no ordering pass return search_query
python
def whooshee_search(self, search_string, group=whoosh.qparser.OrGroup, whoosheer=None, match_substrings=True, limit=None, order_by_relevance=10): if not whoosheer: entities = set() for cd in self.column_descriptions: entities.add(cd['type']) if self._join_entities and isinstance(self._join_entities[0], Mapper): entities.update(set([x.entity for x in self._join_entities])) else: entities.update(set(self._join_entities)) unaliased = set() for entity in entities: if isinstance(entity, (AliasedClass, AliasedInsp)): unaliased.add(inspect(entity).mapper.class_) else: unaliased.add(entity) whoosheer = next(w for w in _get_config(self)['whoosheers'] if set(w.models) == unaliased) for fname, field in list(whoosheer.schema._fields.items()): if field.unique: uniq = fname res = whoosheer.search(search_string=search_string, values_of=uniq, group=group, match_substrings=match_substrings, limit=limit) if not res: return self.filter(text('null')) attr = None if hasattr(whoosheer, '_is_model_whoosheer'): attr = getattr(whoosheer.models[0], uniq) else: for m in whoosheer.models: if m.__name__.lower() == uniq.split('_')[0]: attr = getattr(m, uniq.split('_')[1]) search_query = self.filter(attr.in_(res)) if order_by_relevance < 0: search_query = search_query.order_by(sqlalchemy.sql.expression.case( [(attr == uniq_val, index) for index, uniq_val in enumerate(res)], )) elif order_by_relevance > 0: search_query = search_query.order_by(sqlalchemy.sql.expression.case( [(attr == uniq_val, index) for index, uniq_val in enumerate(res) if index < order_by_relevance], else_=order_by_relevance )) else: pass return search_query
[ "def", "whooshee_search", "(", "self", ",", "search_string", ",", "group", "=", "whoosh", ".", "qparser", ".", "OrGroup", ",", "whoosheer", "=", "None", ",", "match_substrings", "=", "True", ",", "limit", "=", "None", ",", "order_by_relevance", "=", "10", ")", ":", "if", "not", "whoosheer", ":", "### inspiration taken from flask-WhooshAlchemy", "# find out all entities in join", "entities", "=", "set", "(", ")", "# directly queried entities", "for", "cd", "in", "self", ".", "column_descriptions", ":", "entities", ".", "add", "(", "cd", "[", "'type'", "]", ")", "# joined entities", "if", "self", ".", "_join_entities", "and", "isinstance", "(", "self", ".", "_join_entities", "[", "0", "]", ",", "Mapper", ")", ":", "# SQLAlchemy >= 0.8.0", "entities", ".", "update", "(", "set", "(", "[", "x", ".", "entity", "for", "x", "in", "self", ".", "_join_entities", "]", ")", ")", "else", ":", "# SQLAlchemy < 0.8.0", "entities", ".", "update", "(", "set", "(", "self", ".", "_join_entities", ")", ")", "# make sure we can work with aliased entities", "unaliased", "=", "set", "(", ")", "for", "entity", "in", "entities", ":", "if", "isinstance", "(", "entity", ",", "(", "AliasedClass", ",", "AliasedInsp", ")", ")", ":", "unaliased", ".", "add", "(", "inspect", "(", "entity", ")", ".", "mapper", ".", "class_", ")", "else", ":", "unaliased", ".", "add", "(", "entity", ")", "whoosheer", "=", "next", "(", "w", "for", "w", "in", "_get_config", "(", "self", ")", "[", "'whoosheers'", "]", "if", "set", "(", "w", ".", "models", ")", "==", "unaliased", ")", "# TODO what if unique field doesn't exist or there are multiple?", "for", "fname", ",", "field", "in", "list", "(", "whoosheer", ".", "schema", ".", "_fields", ".", "items", "(", ")", ")", ":", "if", "field", ".", "unique", ":", "uniq", "=", "fname", "# TODO: use something more general than id", "res", "=", "whoosheer", ".", "search", "(", "search_string", "=", "search_string", ",", "values_of", "=", "uniq", ",", "group", "=", "group", ",", "match_substrings", "=", "match_substrings", ",", "limit", "=", "limit", ")", "if", "not", "res", ":", "return", "self", ".", "filter", "(", "text", "(", "'null'", ")", ")", "# transform unique field name into model attribute field", "attr", "=", "None", "if", "hasattr", "(", "whoosheer", ",", "'_is_model_whoosheer'", ")", ":", "attr", "=", "getattr", "(", "whoosheer", ".", "models", "[", "0", "]", ",", "uniq", ")", "else", ":", "# non-model whoosheers must have unique field named", "# model.__name__.lower + '_' + attr", "for", "m", "in", "whoosheer", ".", "models", ":", "if", "m", ".", "__name__", ".", "lower", "(", ")", "==", "uniq", ".", "split", "(", "'_'", ")", "[", "0", "]", ":", "attr", "=", "getattr", "(", "m", ",", "uniq", ".", "split", "(", "'_'", ")", "[", "1", "]", ")", "search_query", "=", "self", ".", "filter", "(", "attr", ".", "in_", "(", "res", ")", ")", "if", "order_by_relevance", "<", "0", ":", "# we want all returned rows ordered", "search_query", "=", "search_query", ".", "order_by", "(", "sqlalchemy", ".", "sql", ".", "expression", ".", "case", "(", "[", "(", "attr", "==", "uniq_val", ",", "index", ")", "for", "index", ",", "uniq_val", "in", "enumerate", "(", "res", ")", "]", ",", ")", ")", "elif", "order_by_relevance", ">", "0", ":", "# we want only number of specified rows ordered", "search_query", "=", "search_query", ".", "order_by", "(", "sqlalchemy", ".", "sql", ".", "expression", ".", "case", "(", "[", "(", "attr", "==", "uniq_val", ",", "index", ")", "for", "index", ",", "uniq_val", "in", "enumerate", "(", "res", ")", "if", "index", "<", "order_by_relevance", "]", ",", "else_", "=", "order_by_relevance", ")", ")", "else", ":", "# no ordering", "pass", "return", "search_query" ]
Do a fulltext search on the query. Returns a query filtered with results of the fulltext search. :param search_string: The string to search for. :param group: The whoosh group to use for searching. Defaults to :class:`whoosh.qparser.OrGroup` which searches for all words in all columns. :param match_substrings: ``True`` if you want to match substrings, ``False`` otherwise :param limit: The number of the top records to be returned. Defaults to ``None`` and returns all records.
[ "Do", "a", "fulltext", "search", "on", "the", "query", ".", "Returns", "a", "query", "filtered", "with", "results", "of", "the", "fulltext", "search", "." ]
train
https://github.com/bkabrda/flask-whooshee/blob/773fc51ed53043bd5e92c65eadef5663845ae8c4/flask_whooshee.py#L44-L123
bkabrda/flask-whooshee
flask_whooshee.py
AbstractWhoosheer.search
def search(cls, search_string, values_of='', group=whoosh.qparser.OrGroup, match_substrings=True, limit=None): """Searches the fields for given search_string. Returns the found records if 'values_of' is left empty, else the values of the given columns. :param search_string: The string to search for. :param values_of: If given, the method will not return the whole records, but only values of given column. Defaults to returning whole records. :param group: The whoosh group to use for searching. Defaults to :class:`whoosh.qparser.OrGroup` which searches for all words in all columns. :param match_substrings: ``True`` if you want to match substrings, ``False`` otherwise. :param limit: The number of the top records to be returned. Defaults to ``None`` and returns all records. """ index = Whooshee.get_or_create_index(_get_app(cls), cls) prepped_string = cls.prep_search_string(search_string, match_substrings) with index.searcher() as searcher: parser = whoosh.qparser.MultifieldParser(cls.schema.names(), index.schema, group=group) query = parser.parse(prepped_string) results = searcher.search(query, limit=limit) if values_of: return [x[values_of] for x in results] return results
python
def search(cls, search_string, values_of='', group=whoosh.qparser.OrGroup, match_substrings=True, limit=None): index = Whooshee.get_or_create_index(_get_app(cls), cls) prepped_string = cls.prep_search_string(search_string, match_substrings) with index.searcher() as searcher: parser = whoosh.qparser.MultifieldParser(cls.schema.names(), index.schema, group=group) query = parser.parse(prepped_string) results = searcher.search(query, limit=limit) if values_of: return [x[values_of] for x in results] return results
[ "def", "search", "(", "cls", ",", "search_string", ",", "values_of", "=", "''", ",", "group", "=", "whoosh", ".", "qparser", ".", "OrGroup", ",", "match_substrings", "=", "True", ",", "limit", "=", "None", ")", ":", "index", "=", "Whooshee", ".", "get_or_create_index", "(", "_get_app", "(", "cls", ")", ",", "cls", ")", "prepped_string", "=", "cls", ".", "prep_search_string", "(", "search_string", ",", "match_substrings", ")", "with", "index", ".", "searcher", "(", ")", "as", "searcher", ":", "parser", "=", "whoosh", ".", "qparser", ".", "MultifieldParser", "(", "cls", ".", "schema", ".", "names", "(", ")", ",", "index", ".", "schema", ",", "group", "=", "group", ")", "query", "=", "parser", ".", "parse", "(", "prepped_string", ")", "results", "=", "searcher", ".", "search", "(", "query", ",", "limit", "=", "limit", ")", "if", "values_of", ":", "return", "[", "x", "[", "values_of", "]", "for", "x", "in", "results", "]", "return", "results" ]
Searches the fields for given search_string. Returns the found records if 'values_of' is left empty, else the values of the given columns. :param search_string: The string to search for. :param values_of: If given, the method will not return the whole records, but only values of given column. Defaults to returning whole records. :param group: The whoosh group to use for searching. Defaults to :class:`whoosh.qparser.OrGroup` which searches for all words in all columns. :param match_substrings: ``True`` if you want to match substrings, ``False`` otherwise. :param limit: The number of the top records to be returned. Defaults to ``None`` and returns all records.
[ "Searches", "the", "fields", "for", "given", "search_string", ".", "Returns", "the", "found", "records", "if", "values_of", "is", "left", "empty", "else", "the", "values", "of", "the", "given", "columns", "." ]
train
https://github.com/bkabrda/flask-whooshee/blob/773fc51ed53043bd5e92c65eadef5663845ae8c4/flask_whooshee.py#L138-L163
bkabrda/flask-whooshee
flask_whooshee.py
AbstractWhoosheer.prep_search_string
def prep_search_string(cls, search_string, match_substrings): """Prepares search string as a proper whoosh search string. :param search_string: The search string which should be prepared. :param match_substrings: ``True`` if you want to match substrings, ``False`` otherwise. """ if sys.version < '3' and not isinstance(search_string, unicode): search_string = search_string.decode('utf-8') s = search_string.strip() # we don't want stars from user s = s.replace('*', '') if len(s) < _get_config(cls)['search_string_min_len']: raise ValueError('Search string must have at least 3 characters') # replace multiple with star space star if match_substrings: s = u'*{0}*'.format(re.sub('[\s]+', '* *', s)) # TODO: some sanitization return s
python
def prep_search_string(cls, search_string, match_substrings): if sys.version < '3' and not isinstance(search_string, unicode): search_string = search_string.decode('utf-8') s = search_string.strip() s = s.replace('*', '') if len(s) < _get_config(cls)['search_string_min_len']: raise ValueError('Search string must have at least 3 characters') if match_substrings: s = u'*{0}*'.format(re.sub('[\s]+', '* *', s)) return s
[ "def", "prep_search_string", "(", "cls", ",", "search_string", ",", "match_substrings", ")", ":", "if", "sys", ".", "version", "<", "'3'", "and", "not", "isinstance", "(", "search_string", ",", "unicode", ")", ":", "search_string", "=", "search_string", ".", "decode", "(", "'utf-8'", ")", "s", "=", "search_string", ".", "strip", "(", ")", "# we don't want stars from user", "s", "=", "s", ".", "replace", "(", "'*'", ",", "''", ")", "if", "len", "(", "s", ")", "<", "_get_config", "(", "cls", ")", "[", "'search_string_min_len'", "]", ":", "raise", "ValueError", "(", "'Search string must have at least 3 characters'", ")", "# replace multiple with star space star", "if", "match_substrings", ":", "s", "=", "u'*{0}*'", ".", "format", "(", "re", ".", "sub", "(", "'[\\s]+'", ",", "'* *'", ",", "s", ")", ")", "# TODO: some sanitization", "return", "s" ]
Prepares search string as a proper whoosh search string. :param search_string: The search string which should be prepared. :param match_substrings: ``True`` if you want to match substrings, ``False`` otherwise.
[ "Prepares", "search", "string", "as", "a", "proper", "whoosh", "search", "string", "." ]
train
https://github.com/bkabrda/flask-whooshee/blob/773fc51ed53043bd5e92c65eadef5663845ae8c4/flask_whooshee.py#L166-L184
bkabrda/flask-whooshee
flask_whooshee.py
Whooshee.init_app
def init_app(self, app): """Initialize the extension. It will create the `index_path_root` directory upon initalization but it will **not** create the index. Please use :meth:`reindex` for this. :param app: The application instance for which the extension should be initialized. """ if not hasattr(app, 'extensions'): app.extensions = {} config = app.extensions.setdefault('whooshee', {}) # mapping that caches whoosheers to their indexes; used by `get_or_create_index` config['whoosheers_indexes'] = {} # store a reference to self whoosheers; this way, even whoosheers created after init_app # was called will be found config['whoosheers'] = self.whoosheers config['index_path_root'] = app.config.get('WHOOSHEE_DIR', '') or 'whooshee' config['writer_timeout'] = app.config.get('WHOOSHEE_WRITER_TIMEOUT', 2) config['search_string_min_len'] = app.config.get('WHOOSHEE_MIN_STRING_LEN', 3) config['memory_storage'] = app.config.get("WHOOSHEE_MEMORY_STORAGE", False) config['enable_indexing'] = app.config.get('WHOOSHEE_ENABLE_INDEXING', True) if app.config.get('WHOOSHE_MIN_STRING_LEN', None) is not None: warnings.warn(WhoosheeDeprecationWarning("The config key WHOOSHE_MIN_STRING_LEN has been renamed to WHOOSHEE_MIN_STRING_LEN. The mispelled config key is deprecated and will be removed in upcoming releases. Change it to WHOOSHEE_MIN_STRING_LEN to suppress this warning")) config['search_string_min_len'] = app.config.get('WHOOSHE_MIN_STRING_LEN') if not os.path.exists(config['index_path_root']): os.makedirs(config['index_path_root'])
python
def init_app(self, app): if not hasattr(app, 'extensions'): app.extensions = {} config = app.extensions.setdefault('whooshee', {}) config['whoosheers_indexes'] = {} config['whoosheers'] = self.whoosheers config['index_path_root'] = app.config.get('WHOOSHEE_DIR', '') or 'whooshee' config['writer_timeout'] = app.config.get('WHOOSHEE_WRITER_TIMEOUT', 2) config['search_string_min_len'] = app.config.get('WHOOSHEE_MIN_STRING_LEN', 3) config['memory_storage'] = app.config.get("WHOOSHEE_MEMORY_STORAGE", False) config['enable_indexing'] = app.config.get('WHOOSHEE_ENABLE_INDEXING', True) if app.config.get('WHOOSHE_MIN_STRING_LEN', None) is not None: warnings.warn(WhoosheeDeprecationWarning("The config key WHOOSHE_MIN_STRING_LEN has been renamed to WHOOSHEE_MIN_STRING_LEN. The mispelled config key is deprecated and will be removed in upcoming releases. Change it to WHOOSHEE_MIN_STRING_LEN to suppress this warning")) config['search_string_min_len'] = app.config.get('WHOOSHE_MIN_STRING_LEN') if not os.path.exists(config['index_path_root']): os.makedirs(config['index_path_root'])
[ "def", "init_app", "(", "self", ",", "app", ")", ":", "if", "not", "hasattr", "(", "app", ",", "'extensions'", ")", ":", "app", ".", "extensions", "=", "{", "}", "config", "=", "app", ".", "extensions", ".", "setdefault", "(", "'whooshee'", ",", "{", "}", ")", "# mapping that caches whoosheers to their indexes; used by `get_or_create_index`", "config", "[", "'whoosheers_indexes'", "]", "=", "{", "}", "# store a reference to self whoosheers; this way, even whoosheers created after init_app", "# was called will be found", "config", "[", "'whoosheers'", "]", "=", "self", ".", "whoosheers", "config", "[", "'index_path_root'", "]", "=", "app", ".", "config", ".", "get", "(", "'WHOOSHEE_DIR'", ",", "''", ")", "or", "'whooshee'", "config", "[", "'writer_timeout'", "]", "=", "app", ".", "config", ".", "get", "(", "'WHOOSHEE_WRITER_TIMEOUT'", ",", "2", ")", "config", "[", "'search_string_min_len'", "]", "=", "app", ".", "config", ".", "get", "(", "'WHOOSHEE_MIN_STRING_LEN'", ",", "3", ")", "config", "[", "'memory_storage'", "]", "=", "app", ".", "config", ".", "get", "(", "\"WHOOSHEE_MEMORY_STORAGE\"", ",", "False", ")", "config", "[", "'enable_indexing'", "]", "=", "app", ".", "config", ".", "get", "(", "'WHOOSHEE_ENABLE_INDEXING'", ",", "True", ")", "if", "app", ".", "config", ".", "get", "(", "'WHOOSHE_MIN_STRING_LEN'", ",", "None", ")", "is", "not", "None", ":", "warnings", ".", "warn", "(", "WhoosheeDeprecationWarning", "(", "\"The config key WHOOSHE_MIN_STRING_LEN has been renamed to WHOOSHEE_MIN_STRING_LEN. The mispelled config key is deprecated and will be removed in upcoming releases. Change it to WHOOSHEE_MIN_STRING_LEN to suppress this warning\"", ")", ")", "config", "[", "'search_string_min_len'", "]", "=", "app", ".", "config", ".", "get", "(", "'WHOOSHE_MIN_STRING_LEN'", ")", "if", "not", "os", ".", "path", ".", "exists", "(", "config", "[", "'index_path_root'", "]", ")", ":", "os", ".", "makedirs", "(", "config", "[", "'index_path_root'", "]", ")" ]
Initialize the extension. It will create the `index_path_root` directory upon initalization but it will **not** create the index. Please use :meth:`reindex` for this. :param app: The application instance for which the extension should be initialized.
[ "Initialize", "the", "extension", ".", "It", "will", "create", "the", "index_path_root", "directory", "upon", "initalization", "but", "it", "will", "**", "not", "**", "create", "the", "index", ".", "Please", "use", ":", "meth", ":", "reindex", "for", "this", "." ]
train
https://github.com/bkabrda/flask-whooshee/blob/773fc51ed53043bd5e92c65eadef5663845ae8c4/flask_whooshee.py#L230-L257
bkabrda/flask-whooshee
flask_whooshee.py
Whooshee.register_whoosheer
def register_whoosheer(self, wh): """This will register the given whoosher on `whoosheers`, create the neccessary SQLAlchemy event listeners, replace the `query_class` with our own query class which will provide the search functionality and store the app on the whoosheer, so that we can always work with that. :param wh: The whoosher which should be registered. """ self.whoosheers.append(wh) for model in wh.models: event.listen(model, 'after_{0}'.format(INSERT_KWD), self.after_insert) event.listen(model, 'after_{0}'.format(UPDATE_KWD), self.after_update) event.listen(model, 'after_{0}'.format(DELETE_KWD), self.after_delete) query_class = getattr(model, 'query_class', None) if query_class is not None and isclass(query_class): # already a subclass, ignore it if issubclass(query_class, self.query): pass # ensure there can be a stable MRO elif query_class not in (BaseQuery, SQLAQuery, WhoosheeQuery): query_class_name = query_class.__name__ model.query_class = type( "Whooshee{}".format(query_class_name), (query_class, self.query), {} ) else: model.query_class = self.query else: model.query_class = self.query if self.app: wh.app = self.app return wh
python
def register_whoosheer(self, wh): self.whoosheers.append(wh) for model in wh.models: event.listen(model, 'after_{0}'.format(INSERT_KWD), self.after_insert) event.listen(model, 'after_{0}'.format(UPDATE_KWD), self.after_update) event.listen(model, 'after_{0}'.format(DELETE_KWD), self.after_delete) query_class = getattr(model, 'query_class', None) if query_class is not None and isclass(query_class): if issubclass(query_class, self.query): pass elif query_class not in (BaseQuery, SQLAQuery, WhoosheeQuery): query_class_name = query_class.__name__ model.query_class = type( "Whooshee{}".format(query_class_name), (query_class, self.query), {} ) else: model.query_class = self.query else: model.query_class = self.query if self.app: wh.app = self.app return wh
[ "def", "register_whoosheer", "(", "self", ",", "wh", ")", ":", "self", ".", "whoosheers", ".", "append", "(", "wh", ")", "for", "model", "in", "wh", ".", "models", ":", "event", ".", "listen", "(", "model", ",", "'after_{0}'", ".", "format", "(", "INSERT_KWD", ")", ",", "self", ".", "after_insert", ")", "event", ".", "listen", "(", "model", ",", "'after_{0}'", ".", "format", "(", "UPDATE_KWD", ")", ",", "self", ".", "after_update", ")", "event", ".", "listen", "(", "model", ",", "'after_{0}'", ".", "format", "(", "DELETE_KWD", ")", ",", "self", ".", "after_delete", ")", "query_class", "=", "getattr", "(", "model", ",", "'query_class'", ",", "None", ")", "if", "query_class", "is", "not", "None", "and", "isclass", "(", "query_class", ")", ":", "# already a subclass, ignore it", "if", "issubclass", "(", "query_class", ",", "self", ".", "query", ")", ":", "pass", "# ensure there can be a stable MRO", "elif", "query_class", "not", "in", "(", "BaseQuery", ",", "SQLAQuery", ",", "WhoosheeQuery", ")", ":", "query_class_name", "=", "query_class", ".", "__name__", "model", ".", "query_class", "=", "type", "(", "\"Whooshee{}\"", ".", "format", "(", "query_class_name", ")", ",", "(", "query_class", ",", "self", ".", "query", ")", ",", "{", "}", ")", "else", ":", "model", ".", "query_class", "=", "self", ".", "query", "else", ":", "model", ".", "query_class", "=", "self", ".", "query", "if", "self", ".", "app", ":", "wh", ".", "app", "=", "self", ".", "app", "return", "wh" ]
This will register the given whoosher on `whoosheers`, create the neccessary SQLAlchemy event listeners, replace the `query_class` with our own query class which will provide the search functionality and store the app on the whoosheer, so that we can always work with that. :param wh: The whoosher which should be registered.
[ "This", "will", "register", "the", "given", "whoosher", "on", "whoosheers", "create", "the", "neccessary", "SQLAlchemy", "event", "listeners", "replace", "the", "query_class", "with", "our", "own", "query", "class", "which", "will", "provide", "the", "search", "functionality", "and", "store", "the", "app", "on", "the", "whoosheer", "so", "that", "we", "can", "always", "work", "with", "that", ".", ":", "param", "wh", ":", "The", "whoosher", "which", "should", "be", "registered", "." ]
train
https://github.com/bkabrda/flask-whooshee/blob/773fc51ed53043bd5e92c65eadef5663845ae8c4/flask_whooshee.py#L259-L292
bkabrda/flask-whooshee
flask_whooshee.py
Whooshee.register_model
def register_model(self, *index_fields, **kw): """Registers a single model for fulltext search. This basically creates a simple Whoosheer for the model and calls :func:`register_whoosheer` on it. """ # construct subclass of AbstractWhoosheer for a model class ModelWhoosheer(AbstractWhoosheerMeta): @classmethod def _assign_primary(cls, primary, primary_is_numeric, attrs, model): attrs[primary] = getattr(model, primary) if not primary_is_numeric: if sys.version < '3': attrs[primary] = unicode(attrs[primary]) else: attrs[primary] = str(attrs[primary]) mwh = ModelWhoosheer def inner(model): mwh.index_subdir = model.__tablename__ mwh.models = [model] schema_attrs = {} for field in model.__table__.columns: if field.primary_key: primary = field.name primary_is_numeric = True if isinstance(field.type, SQLInteger): schema_attrs[field.name] = whoosh.fields.NUMERIC(stored=True, unique=True) else: primary_is_numeric = False schema_attrs[field.name] = whoosh.fields.ID(stored=True, unique=True) elif field.name in index_fields: schema_attrs[field.name] = whoosh.fields.TEXT(**kw) mwh.schema = whoosh.fields.Schema(**schema_attrs) # we can't check with isinstance, because ModelWhoosheer is private # so use this attribute to find out mwh._is_model_whoosheer = True @classmethod def update_model(cls, writer, model): attrs = {} cls._assign_primary(primary, primary_is_numeric, attrs, model) for f in index_fields: attrs[f] = getattr(model, f) if not isinstance(attrs[f], int): if sys.version < '3': attrs[f] = unicode(attrs[f]) else: attrs[f] = str(attrs[f]) writer.update_document(**attrs) @classmethod def insert_model(cls, writer, model): attrs = {} cls._assign_primary(primary, primary_is_numeric, attrs, model) for f in index_fields: attrs[f] = getattr(model, f) if not isinstance(attrs[f], int): if sys.version < '3': attrs[f] = unicode(attrs[f]) else: attrs[f] = str(attrs[f]) writer.add_document(**attrs) @classmethod def delete_model(cls, writer, model): writer.delete_by_term(primary, getattr(model, primary)) setattr(mwh, '{0}_{1}'.format(UPDATE_KWD, model.__name__.lower()), update_model) setattr(mwh, '{0}_{1}'.format(INSERT_KWD, model.__name__.lower()), insert_model) setattr(mwh, '{0}_{1}'.format(DELETE_KWD, model.__name__.lower()), delete_model) model._whoosheer_ = mwh model.whoosh_search = mwh.search self.register_whoosheer(mwh) return model return inner
python
def register_model(self, *index_fields, **kw): class ModelWhoosheer(AbstractWhoosheerMeta): @classmethod def _assign_primary(cls, primary, primary_is_numeric, attrs, model): attrs[primary] = getattr(model, primary) if not primary_is_numeric: if sys.version < '3': attrs[primary] = unicode(attrs[primary]) else: attrs[primary] = str(attrs[primary]) mwh = ModelWhoosheer def inner(model): mwh.index_subdir = model.__tablename__ mwh.models = [model] schema_attrs = {} for field in model.__table__.columns: if field.primary_key: primary = field.name primary_is_numeric = True if isinstance(field.type, SQLInteger): schema_attrs[field.name] = whoosh.fields.NUMERIC(stored=True, unique=True) else: primary_is_numeric = False schema_attrs[field.name] = whoosh.fields.ID(stored=True, unique=True) elif field.name in index_fields: schema_attrs[field.name] = whoosh.fields.TEXT(**kw) mwh.schema = whoosh.fields.Schema(**schema_attrs) mwh._is_model_whoosheer = True @classmethod def update_model(cls, writer, model): attrs = {} cls._assign_primary(primary, primary_is_numeric, attrs, model) for f in index_fields: attrs[f] = getattr(model, f) if not isinstance(attrs[f], int): if sys.version < '3': attrs[f] = unicode(attrs[f]) else: attrs[f] = str(attrs[f]) writer.update_document(**attrs) @classmethod def insert_model(cls, writer, model): attrs = {} cls._assign_primary(primary, primary_is_numeric, attrs, model) for f in index_fields: attrs[f] = getattr(model, f) if not isinstance(attrs[f], int): if sys.version < '3': attrs[f] = unicode(attrs[f]) else: attrs[f] = str(attrs[f]) writer.add_document(**attrs) @classmethod def delete_model(cls, writer, model): writer.delete_by_term(primary, getattr(model, primary)) setattr(mwh, '{0}_{1}'.format(UPDATE_KWD, model.__name__.lower()), update_model) setattr(mwh, '{0}_{1}'.format(INSERT_KWD, model.__name__.lower()), insert_model) setattr(mwh, '{0}_{1}'.format(DELETE_KWD, model.__name__.lower()), delete_model) model._whoosheer_ = mwh model.whoosh_search = mwh.search self.register_whoosheer(mwh) return model return inner
[ "def", "register_model", "(", "self", ",", "*", "index_fields", ",", "*", "*", "kw", ")", ":", "# construct subclass of AbstractWhoosheer for a model", "class", "ModelWhoosheer", "(", "AbstractWhoosheerMeta", ")", ":", "@", "classmethod", "def", "_assign_primary", "(", "cls", ",", "primary", ",", "primary_is_numeric", ",", "attrs", ",", "model", ")", ":", "attrs", "[", "primary", "]", "=", "getattr", "(", "model", ",", "primary", ")", "if", "not", "primary_is_numeric", ":", "if", "sys", ".", "version", "<", "'3'", ":", "attrs", "[", "primary", "]", "=", "unicode", "(", "attrs", "[", "primary", "]", ")", "else", ":", "attrs", "[", "primary", "]", "=", "str", "(", "attrs", "[", "primary", "]", ")", "mwh", "=", "ModelWhoosheer", "def", "inner", "(", "model", ")", ":", "mwh", ".", "index_subdir", "=", "model", ".", "__tablename__", "mwh", ".", "models", "=", "[", "model", "]", "schema_attrs", "=", "{", "}", "for", "field", "in", "model", ".", "__table__", ".", "columns", ":", "if", "field", ".", "primary_key", ":", "primary", "=", "field", ".", "name", "primary_is_numeric", "=", "True", "if", "isinstance", "(", "field", ".", "type", ",", "SQLInteger", ")", ":", "schema_attrs", "[", "field", ".", "name", "]", "=", "whoosh", ".", "fields", ".", "NUMERIC", "(", "stored", "=", "True", ",", "unique", "=", "True", ")", "else", ":", "primary_is_numeric", "=", "False", "schema_attrs", "[", "field", ".", "name", "]", "=", "whoosh", ".", "fields", ".", "ID", "(", "stored", "=", "True", ",", "unique", "=", "True", ")", "elif", "field", ".", "name", "in", "index_fields", ":", "schema_attrs", "[", "field", ".", "name", "]", "=", "whoosh", ".", "fields", ".", "TEXT", "(", "*", "*", "kw", ")", "mwh", ".", "schema", "=", "whoosh", ".", "fields", ".", "Schema", "(", "*", "*", "schema_attrs", ")", "# we can't check with isinstance, because ModelWhoosheer is private", "# so use this attribute to find out", "mwh", ".", "_is_model_whoosheer", "=", "True", "@", "classmethod", "def", "update_model", "(", "cls", ",", "writer", ",", "model", ")", ":", "attrs", "=", "{", "}", "cls", ".", "_assign_primary", "(", "primary", ",", "primary_is_numeric", ",", "attrs", ",", "model", ")", "for", "f", "in", "index_fields", ":", "attrs", "[", "f", "]", "=", "getattr", "(", "model", ",", "f", ")", "if", "not", "isinstance", "(", "attrs", "[", "f", "]", ",", "int", ")", ":", "if", "sys", ".", "version", "<", "'3'", ":", "attrs", "[", "f", "]", "=", "unicode", "(", "attrs", "[", "f", "]", ")", "else", ":", "attrs", "[", "f", "]", "=", "str", "(", "attrs", "[", "f", "]", ")", "writer", ".", "update_document", "(", "*", "*", "attrs", ")", "@", "classmethod", "def", "insert_model", "(", "cls", ",", "writer", ",", "model", ")", ":", "attrs", "=", "{", "}", "cls", ".", "_assign_primary", "(", "primary", ",", "primary_is_numeric", ",", "attrs", ",", "model", ")", "for", "f", "in", "index_fields", ":", "attrs", "[", "f", "]", "=", "getattr", "(", "model", ",", "f", ")", "if", "not", "isinstance", "(", "attrs", "[", "f", "]", ",", "int", ")", ":", "if", "sys", ".", "version", "<", "'3'", ":", "attrs", "[", "f", "]", "=", "unicode", "(", "attrs", "[", "f", "]", ")", "else", ":", "attrs", "[", "f", "]", "=", "str", "(", "attrs", "[", "f", "]", ")", "writer", ".", "add_document", "(", "*", "*", "attrs", ")", "@", "classmethod", "def", "delete_model", "(", "cls", ",", "writer", ",", "model", ")", ":", "writer", ".", "delete_by_term", "(", "primary", ",", "getattr", "(", "model", ",", "primary", ")", ")", "setattr", "(", "mwh", ",", "'{0}_{1}'", ".", "format", "(", "UPDATE_KWD", ",", "model", ".", "__name__", ".", "lower", "(", ")", ")", ",", "update_model", ")", "setattr", "(", "mwh", ",", "'{0}_{1}'", ".", "format", "(", "INSERT_KWD", ",", "model", ".", "__name__", ".", "lower", "(", ")", ")", ",", "insert_model", ")", "setattr", "(", "mwh", ",", "'{0}_{1}'", ".", "format", "(", "DELETE_KWD", ",", "model", ".", "__name__", ".", "lower", "(", ")", ")", ",", "delete_model", ")", "model", ".", "_whoosheer_", "=", "mwh", "model", ".", "whoosh_search", "=", "mwh", ".", "search", "self", ".", "register_whoosheer", "(", "mwh", ")", "return", "model", "return", "inner" ]
Registers a single model for fulltext search. This basically creates a simple Whoosheer for the model and calls :func:`register_whoosheer` on it.
[ "Registers", "a", "single", "model", "for", "fulltext", "search", ".", "This", "basically", "creates", "a", "simple", "Whoosheer", "for", "the", "model", "and", "calls", ":", "func", ":", "register_whoosheer", "on", "it", "." ]
train
https://github.com/bkabrda/flask-whooshee/blob/773fc51ed53043bd5e92c65eadef5663845ae8c4/flask_whooshee.py#L294-L372
bkabrda/flask-whooshee
flask_whooshee.py
Whooshee.create_index
def create_index(cls, app, wh): """Creates and opens an index for the given whoosheer and app. If the index already exists, it just opens it, otherwise it creates it first. :param app: The application instance. :param wh: The whoosheer instance for which a index should be created. """ # TODO: do we really want/need to use camel casing? # everywhere else, there is just .lower() if app.extensions['whooshee']['memory_storage']: storage = RamStorage() index = storage.create_index(wh.schema) assert index return index else: index_path = os.path.join(app.extensions['whooshee']['index_path_root'], getattr(wh, 'index_subdir', cls.camel_to_snake(wh.__name__))) if whoosh.index.exists_in(index_path): index = whoosh.index.open_dir(index_path) else: if not os.path.exists(index_path): os.makedirs(index_path) index = whoosh.index.create_in(index_path, wh.schema) return index
python
def create_index(cls, app, wh): if app.extensions['whooshee']['memory_storage']: storage = RamStorage() index = storage.create_index(wh.schema) assert index return index else: index_path = os.path.join(app.extensions['whooshee']['index_path_root'], getattr(wh, 'index_subdir', cls.camel_to_snake(wh.__name__))) if whoosh.index.exists_in(index_path): index = whoosh.index.open_dir(index_path) else: if not os.path.exists(index_path): os.makedirs(index_path) index = whoosh.index.create_in(index_path, wh.schema) return index
[ "def", "create_index", "(", "cls", ",", "app", ",", "wh", ")", ":", "# TODO: do we really want/need to use camel casing?", "# everywhere else, there is just .lower()", "if", "app", ".", "extensions", "[", "'whooshee'", "]", "[", "'memory_storage'", "]", ":", "storage", "=", "RamStorage", "(", ")", "index", "=", "storage", ".", "create_index", "(", "wh", ".", "schema", ")", "assert", "index", "return", "index", "else", ":", "index_path", "=", "os", ".", "path", ".", "join", "(", "app", ".", "extensions", "[", "'whooshee'", "]", "[", "'index_path_root'", "]", ",", "getattr", "(", "wh", ",", "'index_subdir'", ",", "cls", ".", "camel_to_snake", "(", "wh", ".", "__name__", ")", ")", ")", "if", "whoosh", ".", "index", ".", "exists_in", "(", "index_path", ")", ":", "index", "=", "whoosh", ".", "index", ".", "open_dir", "(", "index_path", ")", "else", ":", "if", "not", "os", ".", "path", ".", "exists", "(", "index_path", ")", ":", "os", ".", "makedirs", "(", "index_path", ")", "index", "=", "whoosh", ".", "index", ".", "create_in", "(", "index_path", ",", "wh", ".", "schema", ")", "return", "index" ]
Creates and opens an index for the given whoosheer and app. If the index already exists, it just opens it, otherwise it creates it first. :param app: The application instance. :param wh: The whoosheer instance for which a index should be created.
[ "Creates", "and", "opens", "an", "index", "for", "the", "given", "whoosheer", "and", "app", ".", "If", "the", "index", "already", "exists", "it", "just", "opens", "it", "otherwise", "it", "creates", "it", "first", "." ]
train
https://github.com/bkabrda/flask-whooshee/blob/773fc51ed53043bd5e92c65eadef5663845ae8c4/flask_whooshee.py#L375-L399
bkabrda/flask-whooshee
flask_whooshee.py
Whooshee.camel_to_snake
def camel_to_snake(self, s): """Constructs nice dir name from class name, e.g. FooBar => foo_bar. :param s: The string which should be converted to snake_case. """ return self._underscore_re2.sub(r'\1_\2', self._underscore_re1.sub(r'\1_\2', s)).lower()
python
def camel_to_snake(self, s): return self._underscore_re2.sub(r'\1_\2', self._underscore_re1.sub(r'\1_\2', s)).lower()
[ "def", "camel_to_snake", "(", "self", ",", "s", ")", ":", "return", "self", ".", "_underscore_re2", ".", "sub", "(", "r'\\1_\\2'", ",", "self", ".", "_underscore_re1", ".", "sub", "(", "r'\\1_\\2'", ",", "s", ")", ")", ".", "lower", "(", ")" ]
Constructs nice dir name from class name, e.g. FooBar => foo_bar. :param s: The string which should be converted to snake_case.
[ "Constructs", "nice", "dir", "name", "from", "class", "name", "e", ".", "g", ".", "FooBar", "=", ">", "foo_bar", "." ]
train
https://github.com/bkabrda/flask-whooshee/blob/773fc51ed53043bd5e92c65eadef5663845ae8c4/flask_whooshee.py#L402-L407
bkabrda/flask-whooshee
flask_whooshee.py
Whooshee.get_or_create_index
def get_or_create_index(cls, app, wh): """Gets a previously cached index or creates a new one for the given app and whoosheer. :param app: The application instance. :param wh: The whoosheer instance for which the index should be retrieved or created. """ if wh in app.extensions['whooshee']['whoosheers_indexes']: return app.extensions['whooshee']['whoosheers_indexes'][wh] index = cls.create_index(app, wh) app.extensions['whooshee']['whoosheers_indexes'][wh] = index return index
python
def get_or_create_index(cls, app, wh): if wh in app.extensions['whooshee']['whoosheers_indexes']: return app.extensions['whooshee']['whoosheers_indexes'][wh] index = cls.create_index(app, wh) app.extensions['whooshee']['whoosheers_indexes'][wh] = index return index
[ "def", "get_or_create_index", "(", "cls", ",", "app", ",", "wh", ")", ":", "if", "wh", "in", "app", ".", "extensions", "[", "'whooshee'", "]", "[", "'whoosheers_indexes'", "]", ":", "return", "app", ".", "extensions", "[", "'whooshee'", "]", "[", "'whoosheers_indexes'", "]", "[", "wh", "]", "index", "=", "cls", ".", "create_index", "(", "app", ",", "wh", ")", "app", ".", "extensions", "[", "'whooshee'", "]", "[", "'whoosheers_indexes'", "]", "[", "wh", "]", "=", "index", "return", "index" ]
Gets a previously cached index or creates a new one for the given app and whoosheer. :param app: The application instance. :param wh: The whoosheer instance for which the index should be retrieved or created.
[ "Gets", "a", "previously", "cached", "index", "or", "creates", "a", "new", "one", "for", "the", "given", "app", "and", "whoosheer", "." ]
train
https://github.com/bkabrda/flask-whooshee/blob/773fc51ed53043bd5e92c65eadef5663845ae8c4/flask_whooshee.py#L410-L422
bkabrda/flask-whooshee
flask_whooshee.py
Whooshee.on_commit
def on_commit(self, changes): """Method that gets called when a model is changed. This serves to do the actual index writing. """ if _get_config(self)['enable_indexing'] is False: return None for wh in self.whoosheers: if not wh.auto_update: continue writer = None for change in changes: if change[0].__class__ in wh.models: method_name = '{0}_{1}'.format(change[1], change[0].__class__.__name__.lower()) method = getattr(wh, method_name, None) if method: if not writer: writer = type(self).get_or_create_index(_get_app(self), wh).\ writer(timeout=_get_config(self)['writer_timeout']) method(writer, change[0]) if writer: writer.commit()
python
def on_commit(self, changes): if _get_config(self)['enable_indexing'] is False: return None for wh in self.whoosheers: if not wh.auto_update: continue writer = None for change in changes: if change[0].__class__ in wh.models: method_name = '{0}_{1}'.format(change[1], change[0].__class__.__name__.lower()) method = getattr(wh, method_name, None) if method: if not writer: writer = type(self).get_or_create_index(_get_app(self), wh).\ writer(timeout=_get_config(self)['writer_timeout']) method(writer, change[0]) if writer: writer.commit()
[ "def", "on_commit", "(", "self", ",", "changes", ")", ":", "if", "_get_config", "(", "self", ")", "[", "'enable_indexing'", "]", "is", "False", ":", "return", "None", "for", "wh", "in", "self", ".", "whoosheers", ":", "if", "not", "wh", ".", "auto_update", ":", "continue", "writer", "=", "None", "for", "change", "in", "changes", ":", "if", "change", "[", "0", "]", ".", "__class__", "in", "wh", ".", "models", ":", "method_name", "=", "'{0}_{1}'", ".", "format", "(", "change", "[", "1", "]", ",", "change", "[", "0", "]", ".", "__class__", ".", "__name__", ".", "lower", "(", ")", ")", "method", "=", "getattr", "(", "wh", ",", "method_name", ",", "None", ")", "if", "method", ":", "if", "not", "writer", ":", "writer", "=", "type", "(", "self", ")", ".", "get_or_create_index", "(", "_get_app", "(", "self", ")", ",", "wh", ")", ".", "writer", "(", "timeout", "=", "_get_config", "(", "self", ")", "[", "'writer_timeout'", "]", ")", "method", "(", "writer", ",", "change", "[", "0", "]", ")", "if", "writer", ":", "writer", ".", "commit", "(", ")" ]
Method that gets called when a model is changed. This serves to do the actual index writing.
[ "Method", "that", "gets", "called", "when", "a", "model", "is", "changed", ".", "This", "serves", "to", "do", "the", "actual", "index", "writing", "." ]
train
https://github.com/bkabrda/flask-whooshee/blob/773fc51ed53043bd5e92c65eadef5663845ae8c4/flask_whooshee.py#L433-L454
bkabrda/flask-whooshee
flask_whooshee.py
Whooshee.reindex
def reindex(self): """Reindex all data This method retrieves all the data from the registered models and calls the ``update_<model>()`` function for every instance of such model. """ for wh in self.whoosheers: index = type(self).get_or_create_index(_get_app(self), wh) writer = index.writer(timeout=_get_config(self)['writer_timeout']) for model in wh.models: method_name = "{0}_{1}".format(UPDATE_KWD, model.__name__.lower()) for item in model.query.all(): getattr(wh, method_name)(writer, item) writer.commit()
python
def reindex(self): for wh in self.whoosheers: index = type(self).get_or_create_index(_get_app(self), wh) writer = index.writer(timeout=_get_config(self)['writer_timeout']) for model in wh.models: method_name = "{0}_{1}".format(UPDATE_KWD, model.__name__.lower()) for item in model.query.all(): getattr(wh, method_name)(writer, item) writer.commit()
[ "def", "reindex", "(", "self", ")", ":", "for", "wh", "in", "self", ".", "whoosheers", ":", "index", "=", "type", "(", "self", ")", ".", "get_or_create_index", "(", "_get_app", "(", "self", ")", ",", "wh", ")", "writer", "=", "index", ".", "writer", "(", "timeout", "=", "_get_config", "(", "self", ")", "[", "'writer_timeout'", "]", ")", "for", "model", "in", "wh", ".", "models", ":", "method_name", "=", "\"{0}_{1}\"", ".", "format", "(", "UPDATE_KWD", ",", "model", ".", "__name__", ".", "lower", "(", ")", ")", "for", "item", "in", "model", ".", "query", ".", "all", "(", ")", ":", "getattr", "(", "wh", ",", "method_name", ")", "(", "writer", ",", "item", ")", "writer", ".", "commit", "(", ")" ]
Reindex all data This method retrieves all the data from the registered models and calls the ``update_<model>()`` function for every instance of such model.
[ "Reindex", "all", "data" ]
train
https://github.com/bkabrda/flask-whooshee/blob/773fc51ed53043bd5e92c65eadef5663845ae8c4/flask_whooshee.py#L456-L470
spry-group/python-vultr
vultr/v1_dns.py
VultrDNS.create_domain
def create_domain(self, domain, ipaddr, params=None): ''' /v1/dns/create_domain POST - account Create a domain name in DNS Link: https://www.vultr.com/api/#dns_create_domain ''' params = update_params(params, { 'domain': domain, 'ip': ipaddr }) return self.request('/v1/dns/create_domain', params, 'POST')
python
def create_domain(self, domain, ipaddr, params=None): params = update_params(params, { 'domain': domain, 'ip': ipaddr }) return self.request('/v1/dns/create_domain', params, 'POST')
[ "def", "create_domain", "(", "self", ",", "domain", ",", "ipaddr", ",", "params", "=", "None", ")", ":", "params", "=", "update_params", "(", "params", ",", "{", "'domain'", ":", "domain", ",", "'ip'", ":", "ipaddr", "}", ")", "return", "self", ".", "request", "(", "'/v1/dns/create_domain'", ",", "params", ",", "'POST'", ")" ]
/v1/dns/create_domain POST - account Create a domain name in DNS Link: https://www.vultr.com/api/#dns_create_domain
[ "/", "v1", "/", "dns", "/", "create_domain", "POST", "-", "account", "Create", "a", "domain", "name", "in", "DNS" ]
train
https://github.com/spry-group/python-vultr/blob/bad1448f1df7b5dba70fd3d11434f32580f0b850/vultr/v1_dns.py#L10-L21
spry-group/python-vultr
vultr/v1_dns.py
VultrDNS.create_record
def create_record(self, domain, name, _type, data, params=None): ''' /v1/dns/create_domain POST - account Add a DNS record Link: https://www.vultr.com/api/#dns_create_record ''' params = update_params(params, { 'domain': domain, 'name': name, 'type': _type, 'data': data }) return self.request('/v1/dns/create_record', params, 'POST')
python
def create_record(self, domain, name, _type, data, params=None): params = update_params(params, { 'domain': domain, 'name': name, 'type': _type, 'data': data }) return self.request('/v1/dns/create_record', params, 'POST')
[ "def", "create_record", "(", "self", ",", "domain", ",", "name", ",", "_type", ",", "data", ",", "params", "=", "None", ")", ":", "params", "=", "update_params", "(", "params", ",", "{", "'domain'", ":", "domain", ",", "'name'", ":", "name", ",", "'type'", ":", "_type", ",", "'data'", ":", "data", "}", ")", "return", "self", ".", "request", "(", "'/v1/dns/create_record'", ",", "params", ",", "'POST'", ")" ]
/v1/dns/create_domain POST - account Add a DNS record Link: https://www.vultr.com/api/#dns_create_record
[ "/", "v1", "/", "dns", "/", "create_domain", "POST", "-", "account", "Add", "a", "DNS", "record" ]
train
https://github.com/spry-group/python-vultr/blob/bad1448f1df7b5dba70fd3d11434f32580f0b850/vultr/v1_dns.py#L23-L36
spry-group/python-vultr
vultr/v1_dns.py
VultrDNS.delete_domain
def delete_domain(self, domain, params=None): ''' /v1/dns/delete_domain POST - account Delete a domain name (and all associated records) Link: https://www.vultr.com/api/#dns_delete_domain ''' params = update_params(params, {'domain': domain}) return self.request('/v1/dns/delete_domain', params, 'POST')
python
def delete_domain(self, domain, params=None): params = update_params(params, {'domain': domain}) return self.request('/v1/dns/delete_domain', params, 'POST')
[ "def", "delete_domain", "(", "self", ",", "domain", ",", "params", "=", "None", ")", ":", "params", "=", "update_params", "(", "params", ",", "{", "'domain'", ":", "domain", "}", ")", "return", "self", ".", "request", "(", "'/v1/dns/delete_domain'", ",", "params", ",", "'POST'", ")" ]
/v1/dns/delete_domain POST - account Delete a domain name (and all associated records) Link: https://www.vultr.com/api/#dns_delete_domain
[ "/", "v1", "/", "dns", "/", "delete_domain", "POST", "-", "account", "Delete", "a", "domain", "name", "(", "and", "all", "associated", "records", ")" ]
train
https://github.com/spry-group/python-vultr/blob/bad1448f1df7b5dba70fd3d11434f32580f0b850/vultr/v1_dns.py#L38-L46
spry-group/python-vultr
vultr/v1_dns.py
VultrDNS.delete_record
def delete_record(self, domain, recordid, params=None): ''' /v1/dns/delete_record POST - account Deletes an individual DNS record Link: https://www.vultr.com/api/#dns_delete_record ''' params = update_params(params, { 'domain': domain, 'RECORDID': recordid }) return self.request('/v1/dns/delete_record', params, 'POST')
python
def delete_record(self, domain, recordid, params=None): params = update_params(params, { 'domain': domain, 'RECORDID': recordid }) return self.request('/v1/dns/delete_record', params, 'POST')
[ "def", "delete_record", "(", "self", ",", "domain", ",", "recordid", ",", "params", "=", "None", ")", ":", "params", "=", "update_params", "(", "params", ",", "{", "'domain'", ":", "domain", ",", "'RECORDID'", ":", "recordid", "}", ")", "return", "self", ".", "request", "(", "'/v1/dns/delete_record'", ",", "params", ",", "'POST'", ")" ]
/v1/dns/delete_record POST - account Deletes an individual DNS record Link: https://www.vultr.com/api/#dns_delete_record
[ "/", "v1", "/", "dns", "/", "delete_record", "POST", "-", "account", "Deletes", "an", "individual", "DNS", "record" ]
train
https://github.com/spry-group/python-vultr/blob/bad1448f1df7b5dba70fd3d11434f32580f0b850/vultr/v1_dns.py#L48-L59
spry-group/python-vultr
vultr/v1_dns.py
VultrDNS.records
def records(self, domain, params=None): ''' /v1/dns/records GET - account List all the records associated with a particular domain Link: https://www.vultr.com/api/#dns_records ''' params = update_params(params, {'domain': domain}) return self.request('/v1/dns/records', params, 'GET')
python
def records(self, domain, params=None): params = update_params(params, {'domain': domain}) return self.request('/v1/dns/records', params, 'GET')
[ "def", "records", "(", "self", ",", "domain", ",", "params", "=", "None", ")", ":", "params", "=", "update_params", "(", "params", ",", "{", "'domain'", ":", "domain", "}", ")", "return", "self", ".", "request", "(", "'/v1/dns/records'", ",", "params", ",", "'GET'", ")" ]
/v1/dns/records GET - account List all the records associated with a particular domain Link: https://www.vultr.com/api/#dns_records
[ "/", "v1", "/", "dns", "/", "records", "GET", "-", "account", "List", "all", "the", "records", "associated", "with", "a", "particular", "domain" ]
train
https://github.com/spry-group/python-vultr/blob/bad1448f1df7b5dba70fd3d11434f32580f0b850/vultr/v1_dns.py#L71-L79
spry-group/python-vultr
vultr/v1_dns.py
VultrDNS.update_record
def update_record(self, domain, recordid, params=None): ''' /v1/dns/update_record POST - account Update a DNS record Link: https://www.vultr.com/api/#dns_update_record ''' params = update_params(params, { 'domain': domain, 'RECORDID': recordid }) return self.request('/v1/dns/update_record', params, 'POST')
python
def update_record(self, domain, recordid, params=None): params = update_params(params, { 'domain': domain, 'RECORDID': recordid }) return self.request('/v1/dns/update_record', params, 'POST')
[ "def", "update_record", "(", "self", ",", "domain", ",", "recordid", ",", "params", "=", "None", ")", ":", "params", "=", "update_params", "(", "params", ",", "{", "'domain'", ":", "domain", ",", "'RECORDID'", ":", "recordid", "}", ")", "return", "self", ".", "request", "(", "'/v1/dns/update_record'", ",", "params", ",", "'POST'", ")" ]
/v1/dns/update_record POST - account Update a DNS record Link: https://www.vultr.com/api/#dns_update_record
[ "/", "v1", "/", "dns", "/", "update_record", "POST", "-", "account", "Update", "a", "DNS", "record" ]
train
https://github.com/spry-group/python-vultr/blob/bad1448f1df7b5dba70fd3d11434f32580f0b850/vultr/v1_dns.py#L81-L92