<jupyter_start><jupyter_text>The goal is to find a minimum of a function.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize, differential_evolution
from numpy import sin, exp<jupyter_output><empty_output><jupyter_text>Define initial functions.<jupyter_code># Defining a functions to search minimum of.
def f(x):
    return sin(x / 5) * exp(x / 10) + 5 * exp(-x / 2)

def h(x: int):
    return f(x).astype(int)
# Defining range.
x = np.arange(0, 30, 0.1)
# Displaying our function.
plt.plot(x, f(x), label="f(x)")
plt.plot(x, h(x), '.', label="h(x)")
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Smooth function gradient optimization<jupyter_code># Finding 1st local minimum.
local_min_1 = minimize(f, 2, method="BFGS")
print(local_min_1)
value_local_min_1 = local_min_1.fun
# Finding 2nd local minimum.
local_min_2 = minimize(f, 30, method="BFGS")
print(local_min_2)
value_local_min_2 = local_min_2.fun
# Writing function values in local minimums to file.
with open('output/submission-1.txt', 'w') as file_obj:
    file_obj.write("{} {}".format(round(value_local_min_1, 2), 
                                  round(value_local_min_2, 2)))
    
!cd output/ && cat submission-1.txt<jupyter_output>1.75 -11.9<jupyter_text>Smooth function global optimization<jupyter_code># Finding global minimum on (1, 30) with differential evolution.
global_min = differential_evolution(f, [(1, 30)])
print(global_min)
value_global_min = global_min.fun
# Writing function value in global minimum to file.
with open('output/submission-2.txt', 'w') as file_obj:
    file_obj.write("{}".format(round(value_global_min[0], 2)))  
    
!cd output/ && cat submission-2.txt<jupyter_output>-11.9<jupyter_text>Non-smooth function optimization<jupyter_code># Trying to find local minimum.
local_min_non_smooth = minimize(h, 30, method="BFGS")
print(local_min_non_smooth)
value_local_min_non_smooth = local_min_non_smooth.fun
# Finding global minimum.
global_min_non_smooth = differential_evolution(h, [(1,30)])
print(global_min_non_smooth)
value_global_min_non_smooth = global_min_non_smooth.fun
# Writing function values in minimums to file.
with open('output/submission-3.txt', 'w') as file_obj:
    file_obj.write("{} {}".format(round(value_local_min_non_smooth[0]), 
                                  round(value_global_min_non_smooth, 2)))
    
!cd output/ && cat submission-3.txt<jupyter_output>-5 -11.0
{'fraction_non_alphanumeric': 0.12601957585644372, 'fraction_numerical': 0.024877650897226752, 'mean_word_length': 5.147869674185464, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '356264', 'license': 'MIT', 'n_tokens_mistral': 929, 'n_tokens_neox': 866, 'n_words': 198, 'path': 'MathematicsAndPython/Optimizations/FunctionsMinimization.ipynb', 'repo_name': 'SmirnovAlexander/MachineLearningDataAnalysis', 'size': '2452'}