def test_tree_based_non_dominated_sort(self): print("Testing T-ENS...") F = np.ones((1000, 3)) F[:, 1:] = np.random.random((1000, 2)) _fronts = load_function("fast_non_dominated_sort", _type="python")(F) fronts = load_function("tree_based_non_dominated_sort", _type="python")(F) assert_fronts_equal(_fronts, fronts)
def test_non_dominated_sorting(self): F = np.random.random((100,2)) fronts = load_function("fast_non_dominated_sort", _type="python")(F) fronts = [np.sort(fronts[k]) for k in range(len(fronts))] _fronts = load_function("fast_non_dominated_sort", _type="cython")(F) _fronts = [np.sort(_fronts[k]) for k in range(len(_fronts))] self.assertEqual(len(fronts), len(_fronts)) for k in range(len(_fronts)): is_equal = _fronts[k] == fronts[k] self.assertTrue(np.all(is_equal))
def __init__(self, ref_dirs): self.ref_dirs = ref_dirs self.opt = None self.ideal_point = np.full(ref_dirs.shape[1], np.inf) self._decomposition = get_decomposition('asf') self._calc_perpendicular_distance = load_function( "calc_perpendicular_distance")
def _calc_pareto_front(self, ref_dirs=None, n_iterations=200, points_each_iteration=200, *args, **kwargs): pf = self.evaluate(self._calc_pareto_set_extremes(), return_values_of=["F"]) if ref_dirs is None: ref_dirs = get_ref_dirs(self.n_obj) for k in range(n_iterations): _pf = self.evaluate( self._calc_pareto_set_interior(points_each_iteration), return_values_of=["F"]) pf = np.row_stack([pf, _pf]) ideal, nadir = pf.min(axis=0), pf.max(axis=0) N = (pf - ideal) / (nadir - ideal) dist_matrix = load_function("calc_perpendicular_distance")( N, ref_dirs) closest = np.argmin(dist_matrix, axis=0) pf = pf[closest] pf = pf[np.lexsort(pf.T[::-1])] return pf
def notify(self, algorithm): F = algorithm.pop.get("F") python_fast_nds = load_function("fast_non_dominated_sort", _type="python")(F) cython_fast_nds = load_function("fast_non_dominated_sort", _type="cython")(F) python_efficient_fast_nds = load_function("efficient_non_dominated_sort", _type="python")(F) cython_efficient_fast_nds = load_function("efficient_non_dominated_sort", _type="cython")(F) python_tree_based_nds = load_function("tree_based_non_dominated_sort", _type="python")(F) assert_fronts_equal(python_fast_nds, cython_fast_nds) assert_fronts_equal(python_fast_nds, python_efficient_fast_nds) assert_fronts_equal(python_efficient_fast_nds, cython_efficient_fast_nds) assert_fronts_equal(python_fast_nds, python_tree_based_nds)
def test_efficient_non_dominated_sort(self): print("Testing ENS...") F = np.ones((1000, 3)) F[:, 1:] = np.random.random((1000, 2)) nds = load_function("fast_non_dominated_sort", _type="python")(F) python_fronts_seq = load_function("efficient_non_dominated_sort", _type="python")(F) cython_fronts_seq = load_function("efficient_non_dominated_sort", _type="cython")(F) assert_fronts_equal(nds, python_fronts_seq) assert_fronts_equal(nds, cython_fronts_seq) python_fronts_binary = load_function("efficient_non_dominated_sort", _type="python")(F, strategy="binary") cython_fronts_binary = load_function("efficient_non_dominated_sort", _type="cython")(F, strategy="binary") assert_fronts_equal(nds, python_fronts_binary) assert_fronts_equal(nds, cython_fronts_binary)
def associate_to_niches(F, niches, ideal_point, nadir_point, utopian_epsilon=0.0): utopian_point = ideal_point - utopian_epsilon denom = nadir_point - utopian_point denom[denom == 0] = 1e-12 # normalize by ideal point and intercepts N = (F - utopian_point) / denom dist_matrix = load_function("calc_perpendicular_distance")(N, niches) niche_of_individuals = np.argmin(dist_matrix, axis=1) dist_to_niche = dist_matrix[np.arange(F.shape[0]), niche_of_individuals] return niche_of_individuals, dist_to_niche, dist_matrix
def _do(self, problem, pop, *args, n_survive=None, **kwargs): assert problem.n_obj == 1, "This stochastic ranking implementation only works for single-objective problems." F, G = pop.get("F", "G") f = F[:, 0] if problem.n_constr == 0: I = f.argsort() else: phi = (np.maximum(0, G) ** 2).sum(axis=1) J = np.arange(len(phi)) I = load_function("stochastic_ranking")(f, phi, self.PR, J) return pop[I][:n_survive]
def do(self, F, return_rank=False, only_non_dominated_front=False, n_stop_if_ranked=None, **kwargs): F = F.astype(np.float) # if not set just set it to a very large values because the cython algorithms do not take None if n_stop_if_ranked is None: n_stop_if_ranked = int(1e8) func = load_function(self.method) # set the epsilon if it should be set if self.epsilon is not None: kwargs["epsilon"] = float(self.epsilon) fronts = func(F, **kwargs) # convert to numpy array for each front and filter by n_stop_if_ranked if desired _fronts = [] n_ranked = 0 for front in fronts: _fronts.append(np.array(front, dtype=np.int)) # increment the n_ranked solution counter n_ranked += len(front) # stop if more than this solutions are n_ranked if n_ranked >= n_stop_if_ranked: break fronts = _fronts if only_non_dominated_front: return fronts[0] if return_rank: rank = rank_from_fronts(fronts, F.shape[0]) return fronts, rank return fronts
def do(self, F, return_rank=False, only_non_dominated_front=False, n_stop_if_ranked=None): # if not set just set it to a very large values because the cython algorithms do not take None if n_stop_if_ranked is None: n_stop_if_ranked = int(1e8) if self.method == 'fast_non_dominated_sort': func = load_function("fast_non_dominated_sort") else: raise Exception("Unknown non-dominated sorting method: %s" % self.method) fronts = func(F, epsilon=self.epsilon) # convert to numpy array for each front and filter by n_stop_if_ranked if desired _fronts = [] n_ranked = 0 for front in fronts: _fronts.append(np.array(front, dtype=np.int)) # increment the n_ranked solution counter n_ranked += len(front) # stop if more than this solutions are n_ranked if n_ranked >= n_stop_if_ranked: break fronts = _fronts if only_non_dominated_front: return fronts[0] if return_rank: rank = rank_from_fronts(fronts, F.shape[0]) return fronts, rank return fronts
def _do(self, F, weights, **kwargs): d1, d2 = load_function("calc_distance_to_weights")(F, weights, self.utopian_point) return d1 + self.theta * d2
def test_fast_non_dominated_sorting(self): F = np.random.random((100, 2)) fronts = load_function("fast_non_dominated_sort", _type="python")(F) _fronts = load_function("fast_non_dominated_sort", _type="cython")(F) assert_fronts_equal(fronts, _fronts)
def __init__(self, ref_dirs): self.ref_dirs = ref_dirs self.opt = None self._decomposition = get_decomposition('tchebi') self._calc_perpendicular_distance = load_function( "calc_perpendicular_distance")
def _do(self, F, weights, **kwargs): _, d2 = load_function("calc_distance_to_weights")(F, weights) return d2