#NEWLINE# def sample( self , n = 1 ) : #NEWLINE# #INDENT# if ( not ( type ( n ) is int ) ) : #NEWLINE# #INDENT# raise TypeError ( 'Number of samples must be integer' ) #NEWLINE# #UNINDENT# if ( selected_pos <= 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'Number of samples (%d) needs to be positive' , n ) #NEWLINE# #UNINDENT# samples = repmat ( self . mu , n , 1 ) #NEWLINE# for i in range ( n ) : #NEWLINE# #INDENT# num_active = sum ( self . mu ) #NEWLINE# num_changes = ( sum ( ( rand ( ( self . N - 1 ) ) < self . spread ) ) + 1 ) #NEWLINE# if ( num_changes > num_active ) : #NEWLINE# #INDENT# action = 0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( num_changes > ( self . dimension - num_active ) ) : #NEWLINE# #INDENT# action = 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# action = randint ( 0 , 3 ) #NEWLINE# #UNINDENT# #UNINDENT# if ( ( action is 0 ) or ( action is 1 ) ) : #NEWLINE# #INDENT# relevant_indices = where ( ( self . mu == action ) ) [ 0 ] #NEWLINE# selected = permutation ( arange ( len ( relevant_indices ) ) ) [ : num_changes ] #NEWLINE# changes = relevant_indices [ selected ] #NEWLINE# samples [ i ] [ changes ] = ( action == 0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action is 2 ) : #NEWLINE# #INDENT# pos_indices = where ( ( self . mu == 1 ) ) [ 0 ] #NEWLINE# neg_indices = where ( ( self . mu == 0 ) ) [ 0 ] #NEWLINE# selected_pos = permutation ( arange ( len ( pos_indices ) ) ) [ : num_changes ] #NEWLINE# selected_neg = permutation ( arange ( len ( neg_indices ) ) ) [ : num_changes ] #NEWLINE# changes_pos = pos_indices [ selected_pos ] #NEWLINE# changes_neg = neg_indices [ selected_neg ] #NEWLINE# samples [ i ] [ changes_pos ] = 0 #NEWLINE# samples [ i ] [ changes_neg ] = 1 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return Sample ( samples )