This file contains 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
import operator | |
import random | |
def computePerfPopulation(population, password): | |
populationPerf = {} | |
for individual in population: | |
populationPerf[individual] = fitness(password, individual) | |
return sorted(populationPerf.items(), key = operator.itemgetter(1), reverse=True) | |
def selectFromPopulation(populationSorted, best_sample, lucky_few): |
This file contains 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
import random | |
def createChild(individual1, individual2): | |
child = "" | |
for i in range(len(individual1)): | |
if (int(100 * random.random()) < 50): | |
child += individual1[i] | |
else: | |
child += individual2[i] | |
return child |
This file contains 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
import random | |
def generateAWord (length): | |
i = 0 | |
result = "" | |
while i < length: | |
letter = chr(97 + int(26 * random.random())) | |
result += letter | |
i +=1 | |
return result |
This file contains 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 fitness (password, test_word): | |
if (len(test_word) != len(password)): | |
print("taille incompatible") | |
return | |
else: | |
score = 0 | |
i = 0 | |
while (i < len(password)): | |
if (password[i] == test_word[i]): |
This file contains 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
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 | |
This file contains 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
#!/usr/bin/python3.5 | |
# -*-coding:Utf-8 -* | |
import random | |
import operator | |
import time | |
import matplotlib.pyplot as plt | |
temps1 = time.time() |
This file contains 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 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): |
This file contains 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 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 | |
This file contains 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 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 |
This file contains 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 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 |
OlderNewer