<s> __author__ = '<STR_LIT>' <EOL> import collections <EOL> from scipy import array , tile , sum <EOL> def crowding_distance ( individuals , fitnesses ) : <EOL> """<STR_LIT>""" <EOL> distances = collections . defaultdict ( lambda : <NUM_LIT:0> ) <EOL> individuals = list ( individuals ) <EOL> n_obj = len ( fitnesses [ individuals [ <NUM_LIT:0> ] ] ) <EOL> for i in range ( n_obj ) : <EOL> individuals . sort ( key = lambda x : fitnesses [ x ] [ i ] ) <EOL> normalization = float ( fitnesses [ individuals [ <NUM_LIT:0> ] ] [ i ] - fitnesses [ individuals [ - <NUM_LIT:1> ] ] [ i ] ) <EOL> distances [ individuals [ <NUM_LIT:0> ] ] = <NUM_LIT> <EOL> distances [ individuals [ - <NUM_LIT:1> ] ] = <NUM_LIT> <EOL> tripled = list ( zip ( individuals , individuals [ <NUM_LIT:1> : - <NUM_LIT:1> ] , individuals [ <NUM_LIT:2> : ] ) ) <EOL> for pre , ind , post in tripled : <EOL> distances [ ind ] += ( fitnesses [ pre ] [ i ] - fitnesses [ post ] [ i ] ) / normalization <EOL> return distances <EOL> def _non_dominated_front_old ( iterable , key = lambda x : x , allowequality = True ) : <EOL> """<STR_LIT>""" <EOL> items = list ( iterable ) <EOL> keys = dict ( ( i , key ( i ) ) for i in items ) <EOL> dim = len ( list ( keys . values ( ) ) [ <NUM_LIT:0> ] ) <EOL> if any ( dim != len ( k ) for k in list ( keys . values ( ) ) ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> dominations = collections . defaultdict ( lambda : [ ] ) <EOL> for i in items : <EOL> for j in items : <EOL> if allowequality : <EOL> if all ( keys [ i ] [ k ] < keys [ j ] [ k ] for k in range ( dim ) ) : <EOL> dominations [ i ] . append ( j ) <EOL> else : <EOL> if all ( keys [ i ] [ k ] <= keys [ j ] [ k ] for k in range ( dim ) ) : <EOL> dominations [ i ] . append ( j ) <EOL> dominates = lambda i , j : j in dominations [ i ] <EOL> res = set ( ) <EOL> items = set ( items ) <EOL> for i in items : <EOL> res . add ( i ) <EOL> for j in list ( res ) : <EOL> if i is j : <EOL> continue <EOL> if dominates ( j , i ) : <EOL> res . remove ( i ) <EOL> break <EOL> elif dominates ( i , j ) : <EOL> res . remove ( j ) <EOL> return res <EOL> def _non_dominated_front_fast ( iterable , key = lambda x : x , allowequality = True ) : <EOL> """<STR_LIT>""" <EOL> items = list ( iterable ) <EOL> keys = dict ( ( i , key ( i ) ) for i in items ) <EOL> dim = len ( list ( keys . values ( ) ) [ <NUM_LIT:0> ] ) <EOL> dominations = { } <EOL> for i in items : <EOL> for j in items : <EOL> good = True <EOL> if allowequality : <EOL> for k in range ( dim ) : <EOL> if keys [ i ] [ k ] >= keys [ j ] [ k ] : <EOL> good = False <EOL> break <EOL> else : <EOL> for k in range ( dim ) : <EOL> if keys [ i ] [ k ] > keys [ j ] [ k ] : <EOL> good = False <EOL> break <EOL> if good : <EOL> dominations [ ( i , j ) ] = None <EOL> res = set ( ) <EOL> items = set ( items ) <EOL> for i in items : <EOL> res . add ( i ) <EOL> for j in list ( res ) : <EOL> if i is j : <EOL> continue <EOL> if ( j , i ) in dominations : <EOL> res . remove ( i ) <EOL> break <EOL> elif ( i , j ) in dominations : <EOL> res . remove ( j ) <EOL> return res <EOL> def _non_dominated_front_merge ( iterable , key = lambda x : x , allowequality = True ) : <EOL> items = list ( iterable ) <EOL> l = len ( items ) <EOL> if l > <NUM_LIT:20> : <EOL> part1 = list ( _non_dominated_front_merge ( items [ : l // <NUM_LIT:2> ] , key , allowequality ) ) <EOL> part2 = list ( _non_dominated_front_merge ( items [ l // <NUM_LIT:2> : ] , key , allowequality ) ) <EOL> if len ( part1 ) >= l // <NUM_LIT:3> or len ( part2 ) >= l // <NUM_LIT:3> : <EOL> return _non_dominated_front_fast ( part1 + part2 , key , allowequality ) <EOL> else : <EOL> return _non_dominated_front_merge ( part1 + part2 , key , allowequality ) <EOL> else : <EOL> return _non_dominated_front_fast ( items , key , allowequality ) <EOL> def _non_dominated_front_arr ( iterable , key = lambda x : x , allowequality = True ) : <EOL> """<STR_LIT>""" <EOL> items = list ( iterable ) <EOL> fits = list ( map ( key , items ) ) <EOL> l = len ( items ) <EOL> x = array ( fits ) <EOL> a = tile ( x , ( l , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> b = a . transpose ( ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ) ) <EOL> if allowequality : <EOL> ndom = sum ( a <= b , axis = <NUM_LIT:2> ) <EOL> else : <EOL> ndom = sum ( a < b , axis = <NUM_LIT:2> ) <EOL> ndom = array ( ndom , dtype = bool ) <EOL> res = set ( ) <EOL> for ii in range ( l ) : <EOL> res . add ( ii ) <EOL> for ij in list ( res ) : <EOL> if ii == ij : <EOL> continue <EOL> if not ndom [ ij , ii ] : <EOL> res . remove ( ii ) <EOL> break <EOL> elif not ndom [ ii , ij ] : <EOL> res . remove ( ij ) <EOL> return set ( [ items [ i ] for i in res ] ) <EOL> def _non_dominated_front_merge_arr ( iterable , key = lambda x : x , allowequality = True ) : <EOL> items = list ( iterable ) <EOL> l = len ( items ) <EOL> if l > <NUM_LIT:100> : <EOL> part1 = list ( _non_dominated_front_merge_arr ( items [ : l / <NUM_LIT:2> ] , key , allowequality ) ) <EOL> part2 = list ( _non_dominated_front_merge_arr ( items [ l / <NUM_LIT:2> : ] , key , allowequality ) ) <EOL> if len ( part1 ) >= l / <NUM_LIT:3> or len ( part2 ) >= l / <NUM_LIT:3> : <EOL> return _non_dominated_front_arr ( part1 + part2 , key , allowequality ) <EOL> else : <EOL> return _non_dominated_front_merge_arr ( part1 + part2 , key , allowequality ) <EOL> else : <EOL> return _non_dominated_front_arr ( items , key , allowequality ) <EOL> non_dominated_front = _non_dominated_front_merge_arr <EOL> def non_dominated_sort ( iterable , key = lambda x : x , allowequality = True ) : <EOL> """<STR_LIT>""" <EOL> items = set ( iterable ) <EOL> fronts = [ ] <EOL> while items : <EOL> front = non_dominated_front ( items , key , allowequality ) <EOL> items -= front <EOL> fronts . append ( front ) <EOL> return fronts <EOL> '''<STR_LIT>''' <EOL> def _const_non_dominated_front_merge_arr ( iterable , key = lambda x : x , allowequality = True ) : <EOL> items = list ( iterable ) <EOL> l = len ( items ) <EOL> if l > <NUM_LIT:100> : <EOL> part1 = list ( _const_non_dominated_front_merge_arr ( items [ : l // <NUM_LIT:2> ] , key , allowequality ) ) <EOL> part2 = list ( _const_non_dominated_front_merge_arr ( items [ l // <NUM_LIT:2> : ] , key , allowequality ) ) <EOL> if len ( part1 ) >= l // <NUM_LIT:3> or len ( part2 ) >= l // <NUM_LIT:3> : <EOL> return _const_non_dominated_front_arr ( part1 + part2 , key , allowequality ) <EOL> else : <EOL> return _const_non_dominated_front_merge_arr ( part1 + part2 , key , allowequality ) <EOL> else : <EOL> return _const_non_dominated_front_arr ( items , key , allowequality ) <EOL> def _const_non_dominated_front_arr ( iterable , key = lambda x : x , allowequality = True ) : <EOL> """<STR_LIT>""" <EOL> items = list ( iterable ) <EOL> fits = list ( map ( key , items ) ) <EOL> x = array ( [ fits [ i ] [ <NUM_LIT:0> ] for i in range ( len ( fits ) ) ] ) <EOL> v = array ( [ fits [ i ] [ <NUM_LIT:1> ] for i in range ( len ( fits ) ) ] ) <EOL> c = array ( [ fits [ i ] [ <NUM_LIT:2> ] for i in range ( len ( fits ) ) ] ) <EOL> l = len ( items ) <EOL> a = tile ( x , ( l , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> b = a . transpose ( ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ) ) <EOL> if allowequality : <EOL> ndom = sum ( a <= b , axis = <NUM_LIT:2> ) <EOL> else : <EOL> ndom = sum ( a < b , axis = <NUM_LIT:2> ) <EOL> ndom = array ( ndom , dtype = bool ) <EOL> res = set ( ) <EOL> for ii in range ( l ) : <EOL> res . add ( ii ) <EOL> for ij in list ( res ) : <EOL> if ii == ij : <EOL> continue <EOL> if not ndom [ ij , ii ] and v [ ij ] and v [ ii ] : <EOL> res . remove ( ii ) <EOL> break <EOL> elif not ndom [ ii , ij ] and v [ ij ] and v [ ii ] : <EOL> res . remove ( ij ) <EOL> elif v [ ij ] and not v [ ii ] : <EOL> res . remove ( ii ) <EOL> break <EOL> elif v [ ii ] and not v [ ij ] : <EOL> res . remove ( ij ) <EOL> elif not v [ ii ] and not v [ ij ] : <EOL> cii = abs ( sum ( c [ ii ] ) ) <EOL> cij = abs ( sum ( c [ ij ] ) ) <EOL> if cii < cij : <EOL> res . remove ( ij ) <EOL> else : <EOL> res . remove ( ii ) <EOL> break <EOL> return set ( [ items [ i ] for i in res ] ) <EOL> const_non_dominated_front = _const_non_dominated_front_merge_arr <EOL> def const_non_dominated_sort ( iterable , key = lambda x : x , allowequality = True ) : <EOL> """<STR_LIT>""" <EOL> items = set ( iterable ) <EOL> fronts = [ ] <EOL> while items : <EOL> front = const_non_dominated_front ( items , key , allowequality ) <EOL> items -= front <EOL> fronts . append ( front ) <EOL> return fronts <EOL> def const_crowding_distance ( individuals , fitnesses ) : <EOL> """<STR_LIT>""" <EOL> distances = collections . defaultdict ( lambda : <NUM_LIT:0> ) <EOL> individuals = list ( individuals ) <EOL> n_obj = len ( fitnesses [ individuals [ <NUM_LIT:0> ] ] [ <NUM_LIT:0> ] ) <EOL> for i in range ( n_obj ) : <EOL> individuals . sort ( key = lambda x : fitnesses [ x ] [ <NUM_LIT:0> ] [ i ] ) <EOL> normalization = float ( fitnesses [ individuals [ <NUM_LIT:0> ] ] [ <NUM_LIT:0> ] [ i ] - fitnesses [ individuals [ - <NUM_LIT:1> ] ] [ <NUM_LIT:0> ] [ i ] ) <EOL> distances [ individuals [ <NUM_LIT:0> ] ] = <NUM_LIT> <EOL> distances [ individuals [ - <NUM_LIT:1> ] ] = <NUM_LIT> <EOL> tripled = list ( zip ( individuals , individuals [ <NUM_LIT:1> : - <NUM_LIT:1> ] , individuals [ <NUM_LIT:2> : ] ) ) <EOL> for pre , ind , post in tripled : <EOL> distances [ ind ] += ( fitnesses [ pre ] [ <NUM_LIT:0> ] [ i ] - fitnesses [ post ] [ <NUM_LIT:0> ] [ i ] ) / normalization <EOL> return distances <EOL> def const_number_of_feasible_pop ( iterable , key = lambda x : x , allowequality = True ) : <EOL> """<STR_LIT>""" <EOL> items = list ( iterable ) <EOL> fits = list ( map ( key , items ) ) <EOL> v = list ( [ fits [ i ] [ <NUM_LIT:1> ] for i in range ( len ( fits ) ) ] ) <EOL> n = v . count ( True ) <EOL> return n </s>
