<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> import random <EOL> import numpy <EOL> try : <EOL> numpy . ndarray <EOL> except AttributeError : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> from . bitstrings import BitStringBase <EOL> class BitString ( BitStringBase ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def random ( cls , length , bit_prob = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( length , int ) and length >= <NUM_LIT:0> <EOL> assert isinstance ( bit_prob , ( int , float ) ) and <NUM_LIT:0> <= bit_prob <= <NUM_LIT:1> <EOL> bits = numpy . random . choice ( <EOL> [ False , True ] , <EOL> size = ( length , ) , <EOL> p = [ <NUM_LIT:1> - bit_prob , bit_prob ] <EOL> ) <EOL> bits . flags . writeable = False <EOL> return cls ( bits ) <EOL> @ classmethod <EOL> def crossover_template ( cls , length , points = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( length , int ) and length >= <NUM_LIT:0> <EOL> assert isinstance ( points , int ) and points >= <NUM_LIT:0> <EOL> points = random . sample ( range ( length + <NUM_LIT:1> ) , points ) <EOL> points . sort ( ) <EOL> points . append ( length ) <EOL> previous = <NUM_LIT:0> <EOL> include_range = bool ( random . randrange ( <NUM_LIT:2> ) ) <EOL> pieces = [ ] <EOL> for point in points : <EOL> if point > previous : <EOL> fill = ( numpy . ones if include_range else numpy . zeros ) <EOL> pieces . append ( fill ( point - previous , dtype = bool ) ) <EOL> include_range = not include_range <EOL> previous = point <EOL> bits = numpy . concatenate ( pieces ) <EOL> bits . flags . writeable = False <EOL> return cls ( bits ) <EOL> def __init__ ( self , bits , length = None ) : <EOL> if isinstance ( bits , numpy . ndarray ) : <EOL> if bits . dtype == numpy . bool : <EOL> if bits . flags . writeable : <EOL> bits = bits . copy ( ) <EOL> bits . writeable = False <EOL> else : <EOL> bits = numpy . array ( bits , bool ) <EOL> bits . flags . writeable = False <EOL> hash_value = None <EOL> elif isinstance ( bits , int ) : <EOL> if length is None : <EOL> length = bits . bit_length ( ) <EOL> else : <EOL> assert length >= bits . bit_length ( ) <EOL> if bits < <NUM_LIT:0> : <EOL> bits &= ( <NUM_LIT:1> << length ) - <NUM_LIT:1> <EOL> bit_values = [ ] <EOL> for _ in range ( length ) : <EOL> bit_values . append ( bits % <NUM_LIT:2> ) <EOL> bits >>= <NUM_LIT:1> <EOL> bit_values . reverse ( ) <EOL> bits = numpy . array ( bit_values , bool ) <EOL> bits . flags . writeable = False <EOL> hash_value = None <EOL> elif isinstance ( bits , BitString ) : <EOL> bits , hash_value = bits . _bits , bits . _hash <EOL> elif isinstance ( bits , str ) : <EOL> bit_list = [ ] <EOL> for char in bits : <EOL> if char == '<STR_LIT:1>' : <EOL> bit_list . append ( True ) <EOL> elif char == '<STR_LIT:0>' : <EOL> bit_list . append ( False ) <EOL> elif char == '<STR_LIT:#>' : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + repr ( char ) ) <EOL> bits = numpy . array ( bit_list , bool ) <EOL> bits . flags . writeable = False <EOL> hash_value = None <EOL> else : <EOL> bits = numpy . array ( bits , bool ) <EOL> bits . flags . writeable = False <EOL> hash_value = None <EOL> if length is not None and len ( bits ) != length : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> super ( ) . __init__ ( bits , hash_value ) <EOL> def any ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _bits . any ( ) <EOL> def count ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( numpy . count_nonzero ( self . _bits ) ) <EOL> def __int__ ( self ) : <EOL> """<STR_LIT>""" <EOL> value = <NUM_LIT:0> <EOL> for bit in self . _bits : <EOL> value <<= <NUM_LIT:1> <EOL> value += int ( bit ) <EOL> return value <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . _bits ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return iter ( self . _bits ) <EOL> def __getitem__ ( self , index ) : <EOL> """<STR_LIT>""" <EOL> result = self . _bits [ index ] <EOL> if isinstance ( index , slice ) : <EOL> result . flags . writeable = False <EOL> return BitString ( result ) <EOL> return result <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _hash is None : <EOL> self . _hash = hash ( int ( self ) ) ^ hash ( len ( self ) ) <EOL> return self . _hash <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( other , BitString ) : <EOL> return NotImplemented <EOL> return numpy . array_equal ( self . _bits , other . _bits ) <EOL> def __and__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , int ) : <EOL> other = BitString ( other , len ( self . _bits ) ) <EOL> elif not isinstance ( other , BitString ) : <EOL> other = BitString ( other ) <EOL> bits = numpy . bitwise_and ( self . _bits , other . _bits ) <EOL> bits . flags . writeable = False <EOL> return type ( self ) ( bits ) <EOL> def __or__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , int ) : <EOL> other = BitString ( other , len ( self . _bits ) ) <EOL> elif not isinstance ( other , BitString ) : <EOL> other = BitString ( other ) <EOL> bits = numpy . bitwise_or ( self . _bits , other . _bits ) <EOL> bits . flags . writeable = False <EOL> return type ( self ) ( bits ) <EOL> def __xor__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , int ) : <EOL> other = BitString ( other , len ( self . _bits ) ) <EOL> elif not isinstance ( other , BitString ) : <EOL> other = BitString ( other ) <EOL> bits = numpy . bitwise_xor ( self . _bits , other . _bits ) <EOL> bits . flags . writeable = False <EOL> return type ( self ) ( bits ) <EOL> def __invert__ ( self ) : <EOL> """<STR_LIT>""" <EOL> bits = numpy . bitwise_not ( self . _bits ) <EOL> bits . flags . writeable = False <EOL> return type ( self ) ( bits ) <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , int ) : <EOL> other = BitString ( other , len ( self . _bits ) ) <EOL> elif not isinstance ( other , BitString ) : <EOL> other = BitString ( other ) <EOL> bits = numpy . concatenate ( ( self . _bits , other . _bits ) ) <EOL> bits . flags . writeable = False <EOL> return type ( self ) ( bits ) </s>
