Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Testing 18.04
Views: 1431
Kernel: Python 3 (Ubuntu Linux)

MLROSE: Machine Learning, Randomized Optimization and SEarch

Kernel: Python 3 (Ubuntu Linux)

https://mlrose.readthedocs.io

import mlrose import numpy as np
# Create list of city coordinates coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)] # Initialize fitness function object using coords_list fitness_coords = mlrose.TravellingSales(coords = coords_list)
# Create list of distances between pairs of cities dist_list = [(0, 1, 3.1623), (0, 2, 4.1231), (0, 3, 5.8310), (0, 4, 4.2426), \ (0, 5, 5.3852), (0, 6, 4.0000), (0, 7, 2.2361), (1, 2, 1.0000), \ (1, 3, 2.8284), (1, 4, 2.0000), (1, 5, 4.1231), (1, 6, 4.2426), \ (1, 7, 2.2361), (2, 3, 2.2361), (2, 4, 2.2361), (2, 5, 4.4721), \ (2, 6, 5.0000), (2, 7, 3.1623), (3, 4, 2.0000), (3, 5, 3.6056), \ (3, 6, 5.0990), (3, 7, 4.1231), (4, 5, 2.2361), (4, 6, 3.1623), \ (4, 7, 2.2361), (5, 6, 2.2361), (5, 7, 3.1623), (6, 7, 2.2361)] # Initialize fitness function object using dist_list fitness_dists = mlrose.TravellingSales(distances = dist_list)
# Define optimization problem object problem_fit = mlrose.TSPOpt(length = 8, fitness_fn = fitness_coords, maximize=False)
# Create list of city coordinates coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)] # Define optimization problem object problem_no_fit = mlrose.TSPOpt(length = 8, coords = coords_list, maximize=False)
# Set random seed np.random.seed(2) # Solve problem using the genetic algorithm best_state, best_fitness = mlrose.genetic_alg(problem_fit)
print(best_state)
[1 3 4 5 6 7 0 2]
print(best_fitness)
18.89580466036301