Skip to content

Instantly share code, notes, and snippets.

View gkhayes's full-sized avatar

Dr Genevieve Hayes gkhayes

View GitHub Profile
@gkhayes
gkhayes / TSP_Opt2.py
Last active March 3, 2019 04:31
TSP Optimization Using a Genetic Algorithm - Attempt 2
# 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)
@gkhayes
gkhayes / TSP_Opt1.py
Last active March 3, 2019 04:30
TSP Optimization Using a Genetic Algorithm - Attempt 1
# 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)
@gkhayes
gkhayes / TSP_Fitness_Distance.py
Created January 17, 2019 19:32
Initialize TSP fitness function using distance method
# 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
@gkhayes
gkhayes / TSP_Fitness_Coords.py
Last active March 10, 2022 15:49
Initialize TSP fitness function using coordinates method
# 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)
@gkhayes
gkhayes / 8_Queens_Opt2.py
Last active March 3, 2019 04:28
8-Queens Optimization Using Simulated Annealing - Attempt 2
# 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)
@gkhayes
gkhayes / 8_Queens_Opt1.py
Last active March 3, 2019 04:26
8-Queens Optimization Using Simulated Annealing - Attempt 1
# 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)
@gkhayes
gkhayes / 8_Queens_Custom_Fitness.py
Last active January 13, 2019 02:06
8-Queens Custom Fitness Function
# 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)):