corrigés du mastermind hors publication, liens vers les fichiers utiles
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from problem_sup import *
|
||||
from _problem_sup import *
|
||||
|
||||
BEST_KEPT = 5
|
||||
|
||||
@@ -1,84 +1,84 @@
|
||||
from problem_interface import *
|
||||
from combinaison import *
|
||||
|
||||
class Problem_5(Problem_Interface) :
|
||||
"""
|
||||
classe probleme n°5 : Combinaison Coffre-Fort
|
||||
"""
|
||||
|
||||
def __init__(self, calcul):
|
||||
self.__size = len(calcul)
|
||||
self.__combinaison = calcul
|
||||
|
||||
|
||||
|
||||
def create_individual(self):
|
||||
'''
|
||||
create a randomly generated indidivual for this problem
|
||||
|
||||
:return: a randomly generated individual gfor this problem
|
||||
:rtype: an Individual object
|
||||
'''
|
||||
return Combinaison(self.__size)
|
||||
|
||||
def evaluate_fitness(self, individual):
|
||||
'''
|
||||
compute the fitness of individual for this problem
|
||||
|
||||
:param individual: the individual to consider
|
||||
:type individual: an Individual object
|
||||
:return: the fitness of individual for this problem
|
||||
:rtype: an Individual object
|
||||
'''
|
||||
|
||||
return sum([1 if calcul[chiffre]==self.__combinaison[chiffre] else 0 for chiffre in range(len(calcul))])
|
||||
|
||||
|
||||
def sort_population(self, population):
|
||||
'''
|
||||
sort population from best fitted to worst fitted individuals.
|
||||
Depending on the problem, it can correspond to ascending or descending order with respect to the fitness function.
|
||||
|
||||
side effect : population is modified by this method
|
||||
|
||||
:param population: the list of individuals to sort
|
||||
:type population: list(Individual)
|
||||
'''
|
||||
population.sort(key = lambda x: x.get_score())
|
||||
|
||||
def best_individual(self, population):
|
||||
'''
|
||||
return the best fitted individual from population.
|
||||
Depending on the problem, it can correspond to the individual with the highest or the lowest fitness value.
|
||||
|
||||
|
||||
:param population: the list of individuals to sort
|
||||
:type population: list(Individual)
|
||||
:return: the best fitted individual of population
|
||||
:rtype: an Individual object
|
||||
'''
|
||||
self.sort_population(population)
|
||||
return population[len(population) -1 ]
|
||||
|
||||
def tournament(self, first, second):
|
||||
'''
|
||||
perform a rounament between two individuals, the winner is the most fitted one, it is returned
|
||||
|
||||
|
||||
:param first: an individual
|
||||
:type first: an Individual object
|
||||
:param second: an individual
|
||||
:type second: an Individual object
|
||||
:return: the winner of the tournament
|
||||
:rtype: Individual object
|
||||
'''
|
||||
if first.get_score() < second.get_score():
|
||||
res = first
|
||||
else:
|
||||
res = second
|
||||
return res
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
from problem_interface import *
|
||||
from _combinaison import *
|
||||
|
||||
class Problem_5(Problem_Interface) :
|
||||
"""
|
||||
classe probleme n°5 : Combinaison Coffre-Fort
|
||||
"""
|
||||
|
||||
def __init__(self, calcul):
|
||||
self.__size = len(calcul)
|
||||
self.__combinaison = calcul
|
||||
|
||||
|
||||
|
||||
def create_individual(self):
|
||||
'''
|
||||
create a randomly generated indidivual for this problem
|
||||
|
||||
:return: a randomly generated individual gfor this problem
|
||||
:rtype: an Individual object
|
||||
'''
|
||||
return Combinaison(self.__size)
|
||||
|
||||
def evaluate_fitness(self, individual):
|
||||
'''
|
||||
compute the fitness of individual for this problem
|
||||
|
||||
:param individual: the individual to consider
|
||||
:type individual: an Individual object
|
||||
:return: the fitness of individual for this problem
|
||||
:rtype: an Individual object
|
||||
'''
|
||||
|
||||
return sum([1 if calcul[chiffre]==self.__combinaison[chiffre] else 0 for chiffre in range(len(calcul))])
|
||||
|
||||
|
||||
def sort_population(self, population):
|
||||
'''
|
||||
sort population from best fitted to worst fitted individuals.
|
||||
Depending on the problem, it can correspond to ascending or descending order with respect to the fitness function.
|
||||
|
||||
side effect : population is modified by this method
|
||||
|
||||
:param population: the list of individuals to sort
|
||||
:type population: list(Individual)
|
||||
'''
|
||||
population.sort(key = lambda x: x.get_score())
|
||||
|
||||
def best_individual(self, population):
|
||||
'''
|
||||
return the best fitted individual from population.
|
||||
Depending on the problem, it can correspond to the individual with the highest or the lowest fitness value.
|
||||
|
||||
|
||||
:param population: the list of individuals to sort
|
||||
:type population: list(Individual)
|
||||
:return: the best fitted individual of population
|
||||
:rtype: an Individual object
|
||||
'''
|
||||
self.sort_population(population)
|
||||
return population[len(population) -1 ]
|
||||
|
||||
def tournament(self, first, second):
|
||||
'''
|
||||
perform a rounament between two individuals, the winner is the most fitted one, it is returned
|
||||
|
||||
|
||||
:param first: an individual
|
||||
:type first: an Individual object
|
||||
:param second: an individual
|
||||
:type second: an Individual object
|
||||
:return: the winner of the tournament
|
||||
:rtype: Individual object
|
||||
'''
|
||||
if first.get_score() < second.get_score():
|
||||
res = first
|
||||
else:
|
||||
res = second
|
||||
return res
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+15
-15
@@ -1,16 +1,16 @@
|
||||
from algo_gen import *
|
||||
from problem_4 import *
|
||||
|
||||
if __name__=="__main__":
|
||||
p = Problem_4([
|
||||
['Lille', 15, 15],
|
||||
['Tourcoing', 10, 20],
|
||||
['Paris', 5, 20],
|
||||
['Alsace', 5, 30],
|
||||
['Londre', 30, 15],
|
||||
['Strasburg', 30, 30],
|
||||
['Moskou', 5, 5],
|
||||
['San Francisco', 10, 15]
|
||||
])
|
||||
a = AlgoGen(p, 1000, 0, 0.1)
|
||||
from _algo_gen import *
|
||||
from _problem_4 import *
|
||||
|
||||
if __name__=="__main__":
|
||||
p = Problem_4([
|
||||
['Lille', 15, 15],
|
||||
['Tourcoing', 10, 20],
|
||||
['Paris', 5, 20],
|
||||
['Alsace', 5, 30],
|
||||
['Londre', 30, 15],
|
||||
['Strasburg', 30, 30],
|
||||
['Moskou', 5, 5],
|
||||
['San Francisco', 10, 15]
|
||||
])
|
||||
a = AlgoGen(p, 1000, 0, 0.1)
|
||||
a.solve(20)
|
||||
Reference in New Issue
Block a user