Source code for surrogate.sorting.sorNDHelperB

# Copyright 2016 Quan Pan
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
# Author: Quan Pan <>
# License: Apache License, Version 2.0
# Create: 2016-12-02

from operator import itemgetter

from .utils import isDominated, sweepB, splitB

[docs]def sorNDHelperB(best, worst, obj, front): """Assign front numbers to the solutions in H according to the solutions in L. The solutions in L are assumed to have correct front numbers and the solutions in H are not compared with each other, as this is supposed to happen after sorNDHelperB is called.""" key = itemgetter(obj) if len(worst) == 0 or len(best) == 0: # One of the lists is empty: nothing to do return elif len(best) == 1 or len(worst) == 1: # One of the lists has one individual: compare directly for hi in worst: for li in best: if isDominated(hi[:obj + 1], li[:obj + 1]) or hi[:obj + 1] == li[:obj + 1]: front[hi] = max(front[hi], front[li] + 1) elif obj == 1: sweepB(best, worst, front) elif key(min(best, key=key)) >= key(max(worst, key=key)): # All individuals from L dominate H for objective M: # Also supports the case where every individuals in L and H # has the same value for the current objective # Skip to objective M-1 sorNDHelperB(best, worst, obj - 1, front) elif key(max(best, key=key)) >= key(min(worst, key=key)): best1, best2, worst1, worst2 = splitB(best, worst, obj) sorNDHelperB(best1, worst1, obj, front) sorNDHelperB(best1, worst2, obj - 1, front) sorNDHelperB(best2, worst2, obj, front)