<jupyter_start><jupyter_text>Table of Contents1&nbsp;&nbsp;The Problem2&nbsp;&nbsp;Monte-Carlo Optimization3&nbsp;&nbsp;Documentation  The Problem<jupyter_code>import matplotlib as mpl
import matplotlib.pyplot as plt
import math
import numpy as np
def f(x,y):
    return 45+(x*y+y**2)/100   \
            -80*np.exp(-(x-20)**2/50-(y-50)**2/50)   \
            -150*np.exp(-(x-70)**2/50-(y-80)**2/50)<jupyter_output><empty_output><jupyter_text>Monte-Carlo Optimization<jupyter_code>import random
random.seed(0)<jupyter_output><empty_output><jupyter_text>Randomly generate points $(x_i, y_i)$. Whenever $f(x_i, y_i)$ is smaller than the minimum found so far, consider this point.<jupyter_code>def monte_carlo(n, xmin=0, xmax=99, ymin=0, ymax=99):
    
    x = [ random.randint(xmin, xmax) for i in range(n)]
    y = [ random.randint(ymin, ymax) for i in range(n)]
    
    xp = [ x[0] ]
    yp = [ y[0] ]
    fmin = f(xp[0], yp[0])
    for i in range(1, len(x)):
        fi = f(x[i], y[i])
        if fi < fmin:
            xp += [x[i]]
            yp += [y[i]]
            fmin = fi
            
    xs = np.linspace(xmin, xmax, 100)
    ys = np.linspace(ymin, ymax, 100)
    xx, yy = np.meshgrid(xs, ys)
    z=f(xx, yy)
      
    fig, ax = plt.subplots(1, 1)
    fig.set_figwidth(6)
    fig.set_figheight(5)
    cs = ax.contourf(xs, ys, z, 100)
    fig.colorbar(cs, ax=ax)
    
    plt.scatter(x, y, c='red', marker='.')

    plt.plot(xp, yp, c='red')
    return len(xp), xp[-1], yp[-1], f(xp[-1], yp[-1])
random.seed(41)
monte_carlo(100)
random.seed(40)
monte_carlo(100)
monte_carlo(100)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17541692402717726, 'fraction_numerical': 0.04694255713403336, 'mean_word_length': 3.1012658227848102, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '529540', 'license': 'MIT', 'n_tokens_mistral': 738, 'n_tokens_neox': 695, 'n_words': 135, 'path': 'LAB4/MonteCarlo.ipynb', 'repo_name': 'sobil-dalal/Model-Simulation-Optimization', 'size': '1619'}