<jupyter_start><jupyter_text>Numerical Simulation Laboratory (NSL)    Numerical exercises 10  Exercise 10.1By adapting your Genetic Algorithm code, developed during the Numerical Exercise 9, write a C++ code to solve the TSP with a **Simulated Annealing** (SA) algorithm. Apply your code to the optimization of a path among - 30 cities randomly placed on a circumferenceShow your results via:- a picture of the length of the best path as a function of the iteration of your algorithm- a picture of the best path<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from os import system
from matplotlib.pyplot import figure
from time import time
start = time()<jupyter_output><empty_output><jupyter_text>CIRCUMFERENCE<jupyter_code>form = 0
system('sh clean.sh')
system('./genetic.exe '+str(form))
results = np.loadtxt("results.txt", skiprows=1)
temps = results[:,0]
paths = results[:,1]
figure(figsize=(10,7), dpi=70)
plt.plot(temps, paths)
plt.axhline(2*np.pi, color='red', label='2π')
plt.xlabel("Temperature")
plt.ylabel("Path lengths")
plt.legend(loc='best')
plt.grid(True)
plt.show()
print('-- Best path reached:', paths[-1])
positions = np.loadtxt("best_conf.txt", skiprows=1)
x = positions[:,0]
y = positions[:,1]
figure(figsize=(10,10), dpi=70)
plt.plot(x, y, marker="o")
plt.xlabel('x')
plt.ylabel('y')
plt.show()<jupyter_output><empty_output><jupyter_text>SQUARE<jupyter_code>form = 1
system('sh clean.sh')
system('./genetic.exe '+str(form))
results = np.loadtxt("results.txt", skiprows=1)
temps = results[:,0]
paths = results[:,1]
figure(figsize=(10,7), dpi=70)
plt.plot(temps, paths)
plt.xlabel("Temperature")
plt.ylabel("Path lengths")
plt.grid(True)
plt.show()
print('-- Best path reached:', paths[-1])
positions = np.loadtxt("best_conf.txt", skiprows=1)
x = positions[:,0]
y = positions[:,1]
figure(figsize=(10,10), dpi=70)
plt.plot(x, y, marker="o")
plt.xlabel('x')
plt.ylabel('y')
plt.show()
end = time()
print("-- Time for computation: ", int((end-start)*100.)/100., 'sec')<jupyter_output>-- Time for computation:  6.15 sec<jupyter_text>Exercise 10.2Parallelize with MPI libraries your Simulated Annealing code in order to solve the TSP by performing a *Random Search* with **parallel SA searches of the optimal path**:each node should perform an independent SA search and only in the end you will compare the results of each node.Apply your code to the *usual* TSP problems above. _I run only on the square, bacause the circumference is too easy and every node gets the same result._ _This part of the exercise has been run separately using MPI. I didn't dare to attempt to use ```os.system``` with it._<jupyter_code>best_conf = np.zeros((4, 31, 2))
results = np.zeros((4, 1000, 2))
for rank in range(0,4):
    init = np.loadtxt("Parallel/best_conf"+str(rank)+".txt", skiprows=1)
    init_2 = np.loadtxt("Parallel/results"+str(rank)+".txt", skiprows=1)
    best_conf[rank, :, :] = init[:,:]
    results[rank,:,:] = init_2[:,:]
for rank in range(0,4):
    figure(figsize=(10,5), dpi=100)
    
    plt.subplot(1,2,1)
    plt.plot(best_conf[rank,:,0], best_conf[rank, :, 1], marker="o")
    plt.title("Path rank: "+str(rank))
    plt.xlabel("x")
    plt.ylabel("y")
    
    plt.subplot(1,2,2)
    plt.plot(results[rank,:,0], results[rank,:,1])
    plt.title("Path length to temperature, rank: "+str(rank))
    plt.xlabel("Temperature")
    plt.ylabel("Path length")
    plt.show()
    print("BEST: ", results[rank,-1,1], "------------ \n \n")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16257142857142856, 'fraction_numerical': 0.027428571428571427, 'mean_word_length': 5.262969588550984, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '3039', 'license': 'MIT', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1217, 'n_words': 347, 'path': 'LSN_exercise_10/Ex_10.ipynb', 'repo_name': 'LeonardoAlchieri/LSN_exercises', 'size': '3500'}