<jupyter_start><jupyter_text>All localsearch algorithms examples + plotting  imports<jupyter_code># read data
from lclpy.io.tsplib import read_tsplib

# move functions
from lclpy.localsearch.move.tsp_array_swap import TspArraySwap
from lclpy.localsearch.move.array_reverse_order import ArrayReverseOrder
from lclpy.localsearch.move.multi_neighbourhood import MultiNeighbourhood

# evaluation function
from lclpy.evaluation.tsp_evaluation_function import TspEvaluationFunction

# Problem class
from lclpy.problem.array_problem import ArrayProblem

# termination functions
from lclpy.termination.min_temperature_termination_criterion import MinTemperatureTerminationCriterion
from lclpy.termination.max_seconds_termination_criterion import MaxSecondsTerminationCriterion

# simulated annealing
from lclpy.localsearch.simulatedannealing.simulated_annealing import SimulatedAnnealing
from lclpy.localsearch.simulatedannealing.geometric_cooling_function import GeometricCoolingFunction
from lclpy.localsearch.simulatedannealing.cnst_iterations_temp_function import CnstIterationsTempFunction

# steepest descent 
from lclpy.localsearch.steepestdescent.steepest_descent import SteepestDescent

# tabu search
from lclpy.localsearch.tabusearch.sum_diff_state import SumDiffState
from lclpy.localsearch.tabusearch.tabu_search import TabuSearch

# variable neighbourhood search
from lclpy.localsearch.vns.variable_neighbourhood import VariableNeighbourhood


# plotting
from lclpy.plot.plot import plot, iterations_plot

# matplotlib init (only needed when using matplotlib)

%matplotlib inline
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Reading the data<jupyter_code>data = read_tsplib('data/gr48.tsp')
distance_matrix = data[0]
print(distance_matrix)

size = distance_matrix.shape[0]
print(size)<jupyter_output>[[  0 593 409 ... 235 369 121]
 [593   0 258 ... 371 205 570]
 [409 258   0 ... 187 289 386]
 ...
 [235 371 187 ...   0 149 212]
 [369 205 289 ... 149   0 347]
 [121 570 386 ... 212 347   0]]
48<jupyter_text>simulated annealing<jupyter_code>move_1 = TspArraySwap(size)

evaluation_1 = TspEvaluationFunction(distance_matrix, move_1)

problem_1 = ArrayProblem(evaluation_1, move_1, size)

termination_criterion_1 = MinTemperatureTerminationCriterion()

cooling_func = GeometricCoolingFunction(0.95)
i_for_temp = CnstIterationsTempFunction(1500)

algorithm_1 = SimulatedAnnealing(problem_1, termination_criterion_1,
                                 cooling_func, i_for_temp, benchmarking=True, logging=False)
results_1 = algorithm_1.run()
print(results_1)<jupyter_output>Results(best_order=array([ 0, 12, 15, 10, 47, 28,  6, 27, 43, 40, 45, 17, 33, 22, 24,  2, 18,
        3, 29, 42, 44,  1, 37, 19, 34, 41, 38, 39, 36, 46, 25,  5, 14, 23,
        9, 11, 30,  4, 32,  7, 21, 31, 26, 16, 20,  8, 13, 35]), best_value=5374, data=Data(time=array([0.0000000e+00, 5.6700000e-05, 1.1320000e-04, ..., 2.2324651e+00,
       2.2350800e+00, 2.2368162e+00]), iteration=array([     0,      0,      1, ..., 155684, 155875, 156000]), temperature=array([2000.        , 2000.        , 2000.        , ...,   10.15222748,
         10.15222748,    9.64461611]), value=array([19837, 19714, 20924, ...,  5405,  5396,  5396]), best_value=array([19837, 19714, 19714, ...,  5374,  5374,  5374])))<jupyter_text>simulated annealing plotting data with lspy's plot function<jupyter_code># value-time plot

plot(results_1.data)
# value-iterations plot

iterations_plot(results_1.data)<jupyter_output><empty_output><jupyter_text>simulated annealing plotting data with matplotlib<jupyter_code># getting data from results (only used for plotting with matplotlib)

data = results_1.data

time = data.time
iterations = data.iteration
temperature = data.temperature
values = data.value
best_values = data.best_value
# plotting a value-time plot with matplotlib

plt.figure(figsize=(20,10))
plt.plot(time, values,label='value')
plt.plot(time, best_values, label='best value found')

plt.xlabel('time (s)')
plt.ylabel('evaluation value')

plt.title("Simple Plot")

plt.legend()
# plotting a value-iteration plot with matplotlib

plt.figure(figsize=(20,10))
plt.plot(iterations, values,label='value')
plt.plot(iterations, best_values, label='best value found')

plt.xlabel('iteration')
plt.ylabel('evaluation value')

plt.title("Plot with iterations instead of time")

plt.legend()
# plotting a temperature-iteration plot with matplotlib

plt.figure(figsize=(20,10))
plt.plot(iterations, temperature)

plt.xlabel('iteration')
plt.ylabel('temperature')

plt.title("Temperature in function of the amount of iterations")<jupyter_output><empty_output><jupyter_text>steepest descent<jupyter_code>move_2 = TspArraySwap(size)

evaluation_2 = TspEvaluationFunction(distance_matrix, move_2)

problem_2 = ArrayProblem(evaluation_2, move_2, size)


algorithm_2 = SteepestDescent(problem_2, benchmarking=True, logging=False)
results_2 = algorithm_2.run()
print(results_2)
# value-time plot

plot(results_2.data)
# value-iterations plot

iterations_plot(results_2.data)<jupyter_output><empty_output><jupyter_text>tabu search<jupyter_code>move_3 = TspArraySwap(size)

evaluation_3 = TspEvaluationFunction(distance_matrix, move_3)

problem_3 = ArrayProblem(evaluation_3, move_3, size)

termination_3 = MaxSecondsTerminationCriterion(10)

diff_state_3 = SumDiffState()

algorithm_3 = TabuSearch(problem_3, termination_3, diff_state_3, 11, benchmarking=True, logging=False)
results_3 = algorithm_3.run()
print(results_3)
# value-time plot

plot(results_3.data)
# value-iterations plot

iterations_plot(results_3.data)<jupyter_output><empty_output><jupyter_text>variable neighbourhood search<jupyter_code>move_a = TspArraySwap(size)
move_b = ArrayReverseOrder(size)

moves = [move_a, move_b]

move_4 = MultiNeighbourhood(moves)

evaluation_4 = TspEvaluationFunction(distance_matrix, move_4)

problem_4 = ArrayProblem(evaluation_4, move_4, size)


algorithm_4 = VariableNeighbourhood(problem_4, benchmarking=True, logging=False)
results_4 = algorithm_4.run()
print(results_4)
# value-time plot

plot(results_4.data)
# value-iterations plot

iterations_plot(results_4.data)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10199125789218066, 'fraction_numerical': 0.07285089849441477, 'mean_word_length': 6.276796230859835, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '177068', 'license': 'MIT', 'n_tokens_mistral': 2517, 'n_tokens_neox': 2125, 'n_words': 440, 'path': 'notebooks/all_algorithms_examples_and_plot.ipynb', 'repo_name': 'nobody1570/lspy', 'size': '6177'}