This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Solve problem using the genetic algorithm | |
| best_state, best_fitness = mlrose.genetic_alg(problem_fit, mutation_prob = 0.2, | |
| max_attempts = 100, random_state = 2) | |
| print('The best state found is: ', best_state) | |
| print('The fitness at the best state is: ', best_fitness) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Solve problem using the genetic algorithm | |
| best_state, best_fitness = mlrose.genetic_alg(problem_fit, random_state = 2) | |
| print('The best state found is: ', best_state) | |
| print('The fitness at the best state is: ', best_fitness) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Solve problem using simulated annealing | |
| best_state, best_fitness = mlrose.simulated_annealing(problem, schedule = schedule, | |
| max_attempts = 100, max_iters = 1000, | |
| init_state = init_state, random_state = 1) | |
| print('The best state found is: ', best_state) | |
| print('The fitness at the best state is: ', best_fitness) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Define decay schedule | |
| schedule = mlrose.ExpDecay() | |
| # Define initial state | |
| init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7]) | |
| # Solve problem using simulated annealing | |
| best_state, best_fitness = mlrose.simulated_annealing(problem, schedule = schedule, | |
| max_attempts = 10, max_iters = 1000, | |
| init_state = init_state random_state = 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Define alternative N-Queens fitness function for maximization problem | |
| def queens_max(state): | |
| # Initialize counter | |
| fitness = 0 | |
| # For all pairs of queens | |
| for i in range(len(state) - 1): | |
| for j in range(i + 1, len(state)): | |
NewerOlder