Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
#
# https://github.com/Nyr/openvpn-install
#
# Copyright (c) 2013 Nyr. Released under the MIT License.
# Detect Debian users running the script with "sh" instead of bash
if readlink /proc/$$/exe | grep -q "dash"; then
echo 'This installer needs to be run with "bash", not "sh".'
@rocreguant
rocreguant / roulette_wheel_selection_minimization.py
Created March 14, 2021 10:26
Roulette wheel selection example in python for minimisation problems
import numpy as np
def roulette_wheel_selection(population):
# Computes the totallity of the population fitness
population_fitness = sum([chromosome.fitness for chromosome in population])
# Computes for each chromosome the probability
chromosome_probabilities = [chromosome.fitness/population_fitness for chromosome in population]
@rocreguant
rocreguant / roulette_wheel_selection.py
Last active July 25, 2021 11:39
roulette wheel selection example in python
import numpy as np
def roulette_wheel_selection(population):
# Computes the totallity of the population fitness
population_fitness = sum([chromosome.fitness for chromosome in population])
# Computes for each chromosome the probability
chromosome_probabilities = [chromosome.fitness/population_fitness for chromosome in population]
best_solution = [-1,-np.inf,np.array([])]
for i in range(10000):
if i%100==0: print(i, fitnes_list.min(), fitnes_list.mean(), datetime.now().strftime("%d/%m/%y %H:%M"))
fitnes_list = get_all_fitnes(mutated_pop,cities_dict)
#Saving the best solution
if fitnes_list.max() > best_solution[1]:
best_solution[0] = i
best_solution[1] = fitnes_list.max()
best_solution[2] = np.array(mutated_pop)[fitnes_list.max() == fitnes_list]
def mutate_offspring(offspring, max_weight, object_list, objects_dict):
for mutation_number in range(int(len(offspring)*mutation_rate)):
a = np.random.randint(0,len(object_list))
b = np.random.randint(0,len(offspring))
offspring.insert(b, object_list[a])
offspring = fit_in_knapsack(offspring, max_weight, objects_dict)
def mate_progenitors(prog_a, prog_b, max_weight, objects_dict):
offspring = []
for i in zip(prog_a, prog_b):
offspring.extend(i)
offspring = list(dict.fromkeys(offspring)) #Removing duplicates
offspring = fit_in_knapsack(offspring, max_weight, objects_dict)
return offspring
def progenitor_selection(population_set,fitnes_list):
total_fit = fitnes_list.sum()
prob_list = fitnes_list/total_fit
#Notice there is the chance that a progenitor. mates with oneself
progenitor_list_a = np.random.choice(list(range(len(population_set))), len(population_set),p=prob_list, replace=True)
progenitor_list_b = np.random.choice(list(range(len(population_set))), len(population_set),p=prob_list, replace=True)
progenitor_list_a = population_set[progenitor_list_a]
progenitor_list_b = population_set[progenitor_list_b]
def get_all_fitnes(population_set, objects_dict):
fitnes_list = np.zeros(n_population)
#Looping over all solutions computing the fitness for each solution
for i in range(n_population):
_, fitnes_list[i] = get_current_weight_value(population_set[i], objects_dict)
return fitnes_list
# First step: Create the first population set
def fit_in_knapsack(objects_list, max_weight, objects_dict):
r = []
for o in objects_list:
r.append(o)
weight, value = get_current_weight_value(r, objects_dict)
if weight > max_weight:
r.pop()
return r
return r
# Everything put together
best_solution = [-1,np.inf,np.array([])]
for i in range(100000):
if i%100==0: print(i, fitnes_list.min(), fitnes_list.mean(), datetime.now().strftime("%d/%m/%y %H:%M"))
fitnes_list = get_all_fitnes(mutated_pop,cities_dict)
#Saving the best solution
if fitnes_list.min() < best_solution[1]:
best_solution[0] = i
best_solution[1] = fitnes_list.min()