コード例 #1
0
ファイル: snake_app.py プロジェクト: DongPhamBK/git
    def _crossover(
        self, parent1_weights: np.ndarray, parent2_weights: np.ndarray,
        parent1_bias: np.ndarray, parent2_bias: np.ndarray
    ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
        rand_crossover = random.random()
        crossover_bucket = np.digitize(rand_crossover, self._crossover_bins)
        child1_weights, child2_weights = None, None
        child1_bias, child2_bias = None, None

        # SBX
        if crossover_bucket == 0:
            child1_weights, child2_weights = SBX(parent1_weights,
                                                 parent2_weights,
                                                 self._SBX_eta)
            child1_bias, child2_bias = SBX(parent1_bias, parent2_bias,
                                           self._SBX_eta)

        # Crossover nhị phân một điểm (SPBX)
        elif crossover_bucket == 1:
            child1_weights, child2_weights = single_point_binary_crossover(
                parent1_weights, parent2_weights, major=self._SPBX_type)
            child1_bias, child2_bias = single_point_binary_crossover(
                parent1_bias, parent2_bias, major=self._SPBX_type)

        else:
            raise Exception(
                'Không thể xác định lai chéo hợp lệ dựa trên xác suất')

        return child1_weights, child2_weights, child1_bias, child2_bias
コード例 #2
0
ファイル: snake_app.py プロジェクト: zyadbenameur/SnakeAI
    def _crossover(
        self, parent1_weights: np.ndarray, parent2_weights: np.ndarray,
        parent1_bias: np.ndarray, parent2_bias: np.ndarray
    ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
        rand_crossover = random.random()
        crossover_bucket = np.digitize(rand_crossover, self._crossover_bins)
        child1_weights, child2_weights = None, None
        child1_bias, child2_bias = None, None

        # SBX
        if crossover_bucket == 0:
            child1_weights, child2_weights = SBX(parent1_weights,
                                                 parent2_weights,
                                                 self._SBX_eta)
            child1_bias, child2_bias = SBX(parent1_bias, parent2_bias,
                                           self._SBX_eta)

        # Single point binary crossover (SPBX)
        elif crossover_bucket == 1:
            child1_weights, child2_weights = single_point_binary_crossover(
                parent1_weights, parent2_weights, major=self._SPBX_type)
            child1_bias, child2_bias = single_point_binary_crossover(
                parent1_bias, parent2_bias, major=self._SPBX_type)

        else:
            raise Exception(
                'Unable to determine valid crossover based off probabilities')

        return child1_weights, child2_weights, child1_bias, child2_bias
コード例 #3
0
    def _crossover(self, parent1_weights: np.ndarray, parent2_weights: np.ndarray,
                   parent1_bias: np.ndarray, parent2_bias: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
        eta = self.config.Crossover.sbx_eta

        # SBX weights and bias
        child1_weights, child2_weights = SBX(parent1_weights, parent2_weights, eta)
        child1_bias, child2_bias =  SBX(parent1_bias, parent2_bias, eta)

        return child1_weights, child2_weights, child1_bias, child2_bias
コード例 #4
0
ファイル: PyGenoCar.py プロジェクト: gridl/PyGenoCar
    def _crossover(self, p1_chromosome: np.ndarray,
                   p2_chromosome: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
        """
        Perform crossover between two parent chromosomes and return TWO child chromosomes
        """
        rand_crossover = random.random()
        crossover_bucket = np.digitize(rand_crossover, self._crossover_bins)

        # SBX
        if crossover_bucket == 0:
            c1_chromosome, c2_chromosome = SBX(p1_chromosome, p2_chromosome,
                                               get_ga_constant('SBX_eta'))
        else:
            raise Exception(
                'Unable to determine valid crossover based off probabilities')

        return c1_chromosome, c2_chromosome