<jupyter_start><jupyter_text>Finding the Roots of Himmelblau's function using Simulated Annealing  Simulated Annealing Algorithm Search Space- $ x_0 $ - Initial Solution- $ x_i $ - solution i - $ x_f $ - final solution- $ f(x_i) $ - objective function evaluated at $ x_i $ Variables- $ T_o $ - initial temperature- $ T_t $ - temperature at stage $ t $- $ T_f $ - final temperature- $ \alpha $ - cooling parameter- $ M $ - number of temperatures- $ N(t) $ - number of moves at temperature $ T_t $- $ \sigma $ - move operator Methodology1. Set $ T_0 $, $ M $, $ N $, $ \alpha $, and type of move operator2. start $ m = 1 $3. Start at a random point in the search space, $ x_i $4. Move to another location using the move operator, $ x_t $5. Look around the neighborhood (points around your location) and move to one of them, $ n = 1 $6. Check if the Objective function is better    - If yes, take it    - if no, take a random number and check if the random number is less than: $$ \frac{1}{\exp(\frac{f(x_{tmp}) - f(x_i))}{T_t})} $$    - if less the number evaluated is less than the number from the formula, take it    - if greater, don't take it and stay where you are, and look for another point    - $ n = n + 1 $7. Do this $ N $ times8. $ m = m + 1 $9. $ T_{t+1} = \alpha * T_t $10. Repeat steps 5 to 9 $M - 1 $ times11. Get $x_{final}$, note: keep track of the best solution.  Himmelblau's function Equation:$$ f(x,y) = (x^2 + y - 11)^2 + (x+y^2-7)^2 $$Local Minimas- $ f(3.0,2.0) = 0.0 $- $ f(-2.805118, 3.131312) = 0.0 $- $ f(-3.779310, -3.283186) = 0.0 $- $ f(3.584428, -1.848126) = 0.0 $- $ f(-0.270845, -0.923039) = 181.617 $  Step 1: Importing statements<jupyter_code>import numpy as np 
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Step 2: Define the Objective Function<jupyter_code>def Objective_Function(z):
    x = z[0]
    y = z[1]
    return (x**2 + y - 11)**2 + (x + y**2 - 7)**2 
def Random_Number_Check(objective_old, objective_new, Init_temp):
    return 1/((np.exp(objective_old - objective_new)) / Init_temp)<jupyter_output><empty_output><jupyter_text>Step 3: Initialize design variables<jupyter_code># design variables related to the obejective function
x0 = -5
y0 = -5

# intermediate variables 
k = 0.05 # move variable's constant
M = 300 # number of loops to be performed
T0 = 10000 # initial temperature
N = 15 # initial number of neighbors per search space loop
alpha = 0.85 # cooling parameter
temp = [] # storing of values for the temperature per loop M
min_z = [] # storing best value of the objective function per loop M
x = [] # storing x values per loop for plotting purposes
y = [] # storing y values per loop for plotting purposes<jupyter_output><empty_output><jupyter_text>Step 4: Simulated Annealing Algorithm<jupyter_code>for m in range(M):
    for n in range(N):
        # xt = 0 # initialize random point in search space
        # yt = 0 # initialize random point in search space

        random_x = np.random.rand(1)
        random_y = np.random.rand(1)
        
        if random_x >= 0.5:
            x_new = k*random_x
        else:
            x_new = -k*random_x
        
        if random_y >= 0.5:
            y_new = k*random_y
        else:
            y_new = -k*random_y  

        x_new = x0 + x_new
        y_new = y0 + y_new     

        z_computed = Objective_Function([x_new, y_new])
        z_old = Objective_Function([x0, y0])

        check = Random_Number_Check(z_computed, z_old, T0)
        random_number = np.random.rand(1)

        if z_computed <= z_old:
            x0 = x_new
            y0 = y_new
        elif random_number <= check:
            x0 = x_new
            x0 = y_new
        else:
            x0 = x0
            y0 = y0

    temp.append(T0)
    min_z.append(z_old)
    x.append(x0)
    y.append(y0)
    
    T0 = alpha * T0<jupyter_output><empty_output><jupyter_text>Step 5: Plots and Results<jupyter_code>plt.xlim(1000, 0)
plt.plot(temp, min_z)
plt.xlim(-5,5)
plt.ylim(-5,5)
plt.scatter(x,y)
print("final x location: {}".format(np.round(x0,4)))
print("final y location: {}".format(np.round(y0,4)))
print("final Objective function: {}".format(min_z[-1]))<jupyter_output>final x location: [-3.7835]
final y location: [-3.295]
final Objective function: [0.00584953]<jupyter_text>Using Scipy Optimize Package<jupyter_code>from scipy.optimize import minimize
minimize(Objective_Function,[x0,y0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12376014427412083, 'fraction_numerical': 0.045536519386834985, 'mean_word_length': 2.9829443447037702, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '356465', 'license': 'MIT', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1534, 'n_words': 480, 'path': "01 Simulated Annealing/SA 01 Himmelblau's function.ipynb", 'repo_name': 'mjamezquidilla/Optimization-Practices', 'size': '4436'}