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
def vectorised_dfo(solution_size=5, population_size=1000000, iteration_count=100, disturbance_threshold=0.1, lower=-300.0, upper=300.0, print_results=False): | |
target_solution = tf.zeros((solution_size,)) | |
target_size = int(target_solution.get_shape()[0]) | |
mean = np.mean([lower, upper]) | |
std = np.std([lower, upper]) | |
population = tf.random_normal(shape=[population_size, target_size], mean=mean, stddev=std) | |
for _ in range(iteration_count): |
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
def naive_dfo(solution_size=5, population_size=1000000, iteration_count=100): | |
size = population_size | |
disturbance_threshold = 0.01 | |
target_solution = np.zeros((solution_size,)) | |
lower = -1.0 | |
upper = 1.0 | |
population = np.array([np.random.uniform(lower, upper, solution_size) | |
for _ in range(size)]) | |
all_time_best = None |
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
def create_random_population(size): | |
return np.random.randint(10, size=(size, 8)) | |
def get_fitness(dna): | |
return (dna[0] + dna[1]) - (dna[2] + dna[3]) + (dna[4] + dna[5]) - (dna[6] + dna[7]) | |
def mutate(dna, mutation_rate): | |
for i in range(len(dna)): |
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
def create_random_population(size): | |
return np.random.random((size, 10)) | |
for iteration in range(50): | |
size = 100 | |
disturbance_threshold = 0.01 | |
lower = -1.0 | |
upper = 1.0 |
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
def get_fitness(fly): | |
# Get the indices that would sort the values | |
# flies position. This turns the flies continious | |
# position vector into a set of integers from 0 | |
# to len(fly). e.g | |
# [0.4, 0.53, 0.2, 0.7, 0.1] would turn into | |
# [4, 2, 0, 1, 3]. | |
fly_sorted_indices = np.argsort(fly) | |
# Turn indices into cards |
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
elitism = 0.1 | |
population_size = 500 | |
mutation_rate = 0.1 | |
mutation_sigma = 0.1 | |
mutation_decay = 0.999 | |
mutation_limit = 0.01 | |
amount_optimisation_steps = 250 | |
sizes = [10, 5, 6, 7, 8] |
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
def knapsack_evaluate(position, challenge): | |
if challenge == 1: | |
profits = [92, 57, 49, 68, 60, 43, 67, 84, 87, 72] | |
weights = [23, 31, 29, 44, 53, 38, 63, 85, 89, 82] | |
max_weight = 165 | |
solution = [1, 1, 1, 1, 0, 1, 0, 0, 0, 0] | |
elif challenge == 2: | |
profits = [24, 13, 23, 15, 16] |
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
class GA(): | |
def __init__(self, | |
dna_size, | |
elitism=0.01, | |
population_size=200, | |
mutation_rate=0.01, | |
mutation_sigma=0.1, | |
mutation_decay=0.999, | |
mutation_limit=0.01, |
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
population_size = 100 | |
mutation_rate = 0.1 | |
amount_iterations = 1000 | |
amount_experiments = 50 | |
elitism = 0.1 | |
data = run_experiments(population_size, | |
mutation_rate, | |
elitism, | |
amount_iterations, |
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
def run_experiments(population_size, | |
mutation_rate, | |
elitism, | |
amount_iteration, | |
amount_experiements): | |
experiment_data = [] | |
for experiment in range(amount_experiements): | |
population = create_random_population(population_size) |