<jupyter_start><jupyter_text>AI: Simulated Annealing $f(x_1 ,  x_2) = -( sin(x_1) cos(x_2) + \frac{4}{5} exp(1- \sqrt{(x_1^2 + x_2^2)})$Find the minimum value of $f(x_1, x_2)$, where $-10 \leq x_1 \leq 10$ and $-10 \leq x_2 \leq 10$  Objective Functionimplement the function above to python code<jupyter_code>import numpy as np
import seaborn as sns

def f(x1, x2):
    return -((np.sin(x1) * np.cos(x2)) + ((4/5) * np.exp(1 - np.sqrt((x1 ** 2) + (x2 ** 2)))))<jupyter_output><empty_output><jupyter_text>Visualize the Functionthis is to describe our function look where $-10 \leq x_1 \leq 10$ and $-10 \leq x_2 \leq 10$<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

# Design variables at mesh points
i1 = np.arange(-10.0, 10.0, 0.05)
i2 = np.arange(-10.0, 10.0, 0.05)
x1m, x2m = np.meshgrid(i1, i2)
fm = np.zeros(x1m.shape)
for i in range(x1m.shape[0]):
    for j in range(x1m.shape[1]):
        fm[i][j] = f(x1m[i][j], x2m[i][j])

# Create a contour plot
plt.figure(num=None, figsize=(10,10), dpi=100)#blank space
# Specify contour lines
# Plot contours
CS = plt.contour(x1m, x2m, fm)
# Label contours
plt.colorbar(CS)
plt.clabel(CS, inline=1, fontsize=10)
# Add some text to the plot
plt.title('F')
plt.xlabel('x1')
plt.ylabel('x2')

sns.kdeplot(x1m, virginica.sepal_length,
                 cmap="Blues", shade=True, shade_lowest=False)<jupyter_output><empty_output><jupyter_text>Defining Cost Function<jupyter_code>def cost(state):
    return f(state[0], state[1])<jupyter_output><empty_output><jupyter_text>Annealing Schedule<jupyter_code>def annealing_schedule(temperature, alpha):
    return temperature * alpha<jupyter_output><empty_output><jupyter_text>Probability Acceptance<jupyter_code>def probability_acceptance(new_cost, current_cost, temperature):
    delta_e = new_cost - current_cost
    return np.exp(-(delta_e)/temperature)<jupyter_output><empty_output><jupyter_text>Simulated Annealing Function<jupyter_code>import random

def simmulated_annealing(initial_state, temperature, final_temperature, alpha):
    solutions = {}

    current_state = initial_state
    current_cost = cost(initial_state)

    #initialize best solution
    best_solution = initial_state
    best_cost = current_cost

    j = 1
    while (temperature > final_temperature):
        for i in range(100):
            x1 = random.uniform(-10, 10)
            x2 = random.uniform(-10, 10)
            new_state = [x1, x2]
            new_cost = cost(new_state)

        if (new_cost < current_cost):
            current_state = new_state
            current_cost = new_cost
            if (current_cost < best_cost):
                best_solution = current_state
                best_cost = current_cost
        else:
            prob = probability_acceptance(new_cost, current_cost, temperature)
            if (prob > random.uniform(0,1)):
                current_state = new_state
                current_cost = new_cost
        solutions[j] = [temperature, best_solution, best_cost]
        j += 1
        temperature = annealing_schedule(temperature, alpha)

    return solutions<jupyter_output><empty_output><jupyter_text>Searching Result<jupyter_code>temperature = 100 # initial temperature
final_temperature = 0.0001 # loops will end while temperature = final_temperature
alpha = 0.9999 # parameter of annealing schedule

#initialize state
x1 = random.uniform(-10, 10)
x2 = random.uniform(-10, 10)
initial_state = [x1 ,x2]

#find the solution
solutions = simmulated_annealing(initial_state, temperature, final_temperature, alpha)
print("Minimum value:",solutions[list(solutions)[-1]][-1])
print("Nilai x1 dan x2:",solutions[list(solutions)[-1]][1])<jupyter_output>Minimum value: -2.129010204751578
Nilai x1 dan x2: [0.03869204090833733, -0.008120161349911115]<jupyter_text>Analyze the Result<jupyter_code>temperature = []
min_val = []
for x in range(1,len(solutions)):
    temperature.append(solutions[x][0])
    min_val.append(solutions[x][-1])
solutions = np.array(solutions)
plt.figure(figsize=(10,4), dpi=100)
plt.plot(min_val, temperature)

plt.xlabel('Cost (f(x))')
plt.ylabel('decreasing temperature (T)')
plt.title('Annealing Cost Movement')
plt.gca().invert_xaxis()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12732846026880454, 'fraction_numerical': 0.0485734496580995, 'mean_word_length': 4.282689912826899, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '33121', 'license': 'MIT', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1501, 'n_words': 325, 'path': 'course_assignments/Global Minima Problem/.ipynb_checkpoints/Minimum Value Problem-checkpoint.ipynb', 'repo_name': 'Maxalmina/maru', 'size': '4241'}