def selection_rand(self, selection_size):
     n = len(self.population)
     if selection_size > n:
         raise SrflpError(
             f'Cannot create bigger sample({selection_size}) than the original population ({n})'
         )
     return Population(random.choices(self.population, k=selection_size))
 def __init__(self, population=None):
     if population and not all(
             isinstance(x, Chromosome) for x in population):
         raise SrflpError(
             f'All members of a population must be Chromosomes')
     self.population = population if population else []
     self.n = len(population) if population else 0
 def crossover(self, other: Chromosome, type: str):
     type = type.lower()
     if type == 'pmx':
         return self.partially_mapped_crossover(other)
     elif type == 'order':
         return self.order_crossover(other)
     else:
         raise SrflpError(f'Incorrect crossover{type}')
 def selection_roulette_wheel(self, selection_size):
     n = len(self.population)
     if selection_size > n:
         raise SrflpError(
             f'Cannot create bigger sample({selection_size}) than the original population ({n})'
         )
     return Population(
         random.choices(self.population,
                        weights=[x.fitness for x in self.population],
                        k=selection_size))
 def __init__(self, n, L, C, F=None):
     super().__init__(n)
     if not L or not C:
         raise SrflpError('Empty dataset provided')
     if len(L) != n or len(C) != n or [x for x in C if len(x) != n] != []:
         raise SrflpError(
             f'Incorrect dimensions provided: {type(self)} : {self}')
     for i in range(n):
         if C[i][i] != -1:
             raise SrflpError(f'Incorrect cost matrix provided {self}')
     for i in range(n):
         for j in range(n):
             if i != j and C[i][j] < 0:
                 raise SrflpError(f'Inccorect cost matrix provided {self}')
     self.n = n
     self.__F = [x for x in range(n)] if not F else F
     self.L = L
     self.C = C
     self.fitness = self.get_fitness()
Exemplo n.º 6
0
 def solve_simple(srflp_chromosome: SrflpChromosome,
                  N,
                  algorithm='RANDOM_PERMUTATION'):
     algorithm = algorithm.upper()
     if algorithm not in SrflpAlgorithm.ALGORITHMS:
         raise SrflpError(f'Incorrect algorithm provided {algorithm}')
     print(f'Using alogirthm {algorithm}')
     if algorithm == 'RANDOM_PERMUTATION':
         return SrflpAlgorithm.random_permutations(srflp_chromosome, N)
     if algorithm == 'BRUTE_FORCE':
         return SrflpAlgorithm.brute_force(srflp_chromosome, N)
 def selection_rank(self, selection_size):
     n = len(self.population)
     if selection_size > n:
         raise SrflpError(
             f'Cannot create bigger sample({selection_size}) than the original population ({n})'
         )
     indices = np.argsort([x.fitness for x in self.population
                           ])[-selection_size:].tolist()
     pop = []
     for i in indices:
         pop.append(self.population[i])
     return Population(pop)
 def mutation(self, type: str):
     type = type.lower()
     if type == 'reverse':
         self.reverse_mutation()
     elif type == 'scramble':
         self.scramble_mutation()
     elif type == 'insert':
         self.insert_mutation()
     elif type == 'swap':
         self.swap_mutation()
     else:
         raise SrflpError(f'Incorrect mutation {type}')
 def generate_sample(cls, n=6, sample_size=10**4):
     from srflp.algorithm import SrflpChromosome
     if n < 3 or sample_size < 1 or n > cls.MAX_LENGTH - cls.MIN_LENGTH:
         raise SrflpError(
             f'Incorrect input provided n should be between {cls.MIN_LENGTH} and {cls.MAX_LENGTH}'
         )
     for i in range(sample_size):
         L = sample(range(cls.MIN_LENGTH, cls.MAX_LENGTH), n)
         C = np.random.random_integers(cls.MIN_COST,
                                       cls.MAX_COST,
                                       size=(n, n))
         C = (C + C.T) / 2
         for i in range(n):
             C[i][i] = -1
         C = C.tolist()
         yield SrflpChromosome(n, L, C)
 def F(self, F):
     if len(F) != len(self.__F):
         raise SrflpError(f'New F value should be same length as original')
     self.__F = F
     self.fitness = self.get_fitness()
 def remove(self, index):
     n = len(self.population)
     if index < n:
         raise SrflpError(f'Invalid index({index}) in array of length {n}')
     del self.population[index]
 def __init__(self, n, F=None):
     if n < self.MIN_N or n > self.MAX_N:
         raise SrflpError(
             f'Incorrect input provided for chromosome (n={n} should be between {self.MIN_N} and {self.MAX_N})'
         )
     self._F = [x for x in range(n)] if not F else F