<jupyter_start><jupyter_text>Optimisation of the Schwefel Function<jupyter_code>def fitness_fn(x):
    """
    Schwefel Function
    https://www.sfu.ca/~ssurjano/schwef.html
    """
    dimension = tf.cast(tf.shape(x)[1], tf.float64)
    return 418.9829 * dimension - tf.reduce_sum(x * tf.sin(tf.sqrt(tf.abs(x))), axis=1)
xlim, ylim = [-500, 500], [-500, 500]

plot_3d_surface(fitness_fn, xlim, ylim);
plot_2d_contour(fitness_fn, xlim, ylim);
%%time
cma = CMA(
    initial_solution=[0., 0.],
    initial_step_size=1000.0,
    fitness_function=fitness_fn,
    store_trace=True,
    # Ensure that the fitness function is never called 
    # outisde the specified bounds:
    enforce_bounds=[xlim, ylim],
);
cma.search();

num_generations = cma.generation
best_solution = cma.best_solution()
best_fitness = cma.best_fitness()

print('Number of generations:', num_generations)
print(f'Best solution: [{best_solution[0]:.5f}, {best_solution[1]:.5f}]')
print(f'Best fitness: {best_fitness:.4f}')
print()
sns.set(palette='colorblind', font_scale=1.2)  # better default style for matplotlib
plot_mean_coordinates(cma.trace);
generations = [0, 8, 16, 20, 25, 30]
fig, _ = plot_generations(
    generations,
    cma.trace,
    fitness_fn,
    xlim=xlim, 
    ylim=ylim,
);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14860681114551083, 'fraction_numerical': 0.037925696594427245, 'mean_word_length': 4.824324324324325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '282509', 'license': 'MIT', 'n_tokens_mistral': 538, 'n_tokens_neox': 503, 'n_words': 94, 'path': 'notebook/Example 2 - Schwefel Function.ipynb', 'repo_name': 'JugglingNumbers/cma-es', 'size': '1292'}