Skip to content

Instantly share code, notes, and snippets.

@NicolleLouis
NicolleLouis / import.py
Created November 27, 2017 17:51
import knapsack
import random
import operator
import time
import matplotlib.pyplot as plt
import functools
@NicolleLouis
NicolleLouis / graphKnapsack.py
Last active November 27, 2017 17:49
Print graphe
def evolve_several_generation_with_limited_time(item_set, size_of_population, number_of_child, time_limit,
mutationRate):
temps_init = time.time()
value0 = 0
result = []
population = generate_first_population(item_set, size_of_population)
value0 = max(value0, value(get_best_individual_in_population(population), item_set))
result.append(value0)
while (time.time() - temps_init < time_limit):
population_sorted = sort_population_by_fitness(population, item_set)
@NicolleLouis
NicolleLouis / intramutationKnapsack.py
Created November 27, 2017 17:25
Intramutation Knapsack
def mutate_one_individual(individual):
i = int(len(individual) * random.random())
individual[i] = not individual[i]
return individual
def mutate_population(population, mutationRate):
for i in population:
if (100 * random.random() < mutationRate):
i = mutate_one_individual(i)
return (population)
@NicolleLouis
NicolleLouis / intermutationKnapsack.py
Created November 27, 2017 17:21
Intermutation Knapsack
def mutate_one_individual(individual, mutationRate):
for geneIndex in range(len(individual)):
if (100 * random.random() < mutationRate):
individual[geneIndex] = not individual[geneIndex]
return individual
def mutate_population(population, mutationRate):
for individual in population:
individual = mutate_one_individual(individual, mutationRate)
@NicolleLouis
NicolleLouis / fitnessKnapsack.py
Created November 27, 2017 17:18
Fitness Knapsack
def fitness(individual, item_set):
Knapsack_Capacity = round(total_weight_of_item_set(item_set) / 2)
result = 0
if (weight_of_individual(individual, item_set) <= Knapsack_Capacity):
result = 2 * value_of_individual(individual, item_set) - weight_of_individual(individual, item_set)
return result
@NicolleLouis
NicolleLouis / reproductionKnapsack.py
Created November 27, 2017 17:17
Reproduction Knapsack
def select_breeders(population_sorted, size_of_population):
result = []
best_individuals = size_of_population / 5
lucky_few = size_of_population / 5
for i in range(best_individuals):
result.append(population_sorted[i])
for i in range(lucky_few):
result.append(random.choice(population_sorted))
random.shuffle(result)
return result
@NicolleLouis
NicolleLouis / KnapsackIndividuals.py
Created November 27, 2017 17:08
Create individuals
def generate_one_individual(item_set):
individual = []
for i in range(len(item_set)):
if (100 * random.random() < 50):
individual.append(True)
else:
individual.append(False)
return individual
@NicolleLouis
NicolleLouis / createItemSet.py
Last active November 27, 2017 17:07
create Item set
def generate_one_item(Max_weight, Max_value):
item = []
item.append(round(Max_weight * random.random()))
item.append(round(Max_value * random.random()))
return item
def generate_all_items(Number_of_item, Max_weight, Max_value):
list_item = []
for i in range(Number_of_item):
#!/usr/bin/python3.5
# -*-coding:Utf-8 -*
import random
import operator
import time
import matplotlib.pyplot as plt
temps1 = time.time()
import random
def mutateWord(word):
index_modification = int(random.random() * len(word))
if (index_modification == 0):
word = chr(97 + int(26 * random.random())) + word[1:]
else:
word = word[:index_modification] + chr(97 + int(26 * random.random())) + word[index_modification+1:]
return word