#! /usr/bin/env python3 # def best_of_the_bunch_simulation ( n, k ): #*****************************************************************************80 # ## best_of_the_bunch_simulation() tries to select the best of N options. # # Discussion: # # The options are presented in some permuted sequence. # The user can take the option and quit. # Otherwise, that option is rejected and the next is presented. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 07 July 2022 # # Author: # # John Burkardt # # Input: # # integer n: the number of options. # # integer k: the number of options to examine and reject. # k = 0: accept very first option. # k = n-1: accept the very last option. # # Output: # # integer winnings: the value of the option chosen. # # integer max_seen: the maximum value seen. # # integer max_value: the maximum option value. # from numpy.random import default_rng import numpy as np rng = default_rng ( ) value = np.arange ( 1, n + 1 ) value = rng.permutation ( value ) max_value = np.max ( value ) if ( k == 0 ): max_seen = - np.Inf else: max_seen = np.max ( value[0:k] ) for i in range ( k, n ): if ( ( max_seen < value[i] ) or ( i == n - 1 ) ): winnings = value[i] break return winnings, max_seen, max_value, value def best_of_the_bunch_simulation_test ( ): #*****************************************************************************80 # ## best_of_the_bunch_simulation_test() tests best_of_the_bunch_simulation(). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 08 July 2022 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'best_of_the_bunch_simulation_test()' ) print ( ' There are N objects, of varying value.' ) print ( ' You can examine each object, in order, once.' ) print ( ' If you accept the object, you are done.' ) print ( ' Otherwise, you can view the next object.' ) print ( ' Your goal is to maximize your winnings.' ) n = 10 k = 0 tries = 10 print ( '' ) print ( ' Try k =', k ) print ( '' ) total = 0 for i in range ( 0, tries ): winnings, max_seen, max_value, value = best_of_the_bunch_simulation ( n, k ) print ( '[', value, '], choice = ', winnings ) total = total + winnings print ( ' Average winnings = ', total / tries ) n = 10 k = n - 1 tries = 10 print ( '' ) print ( ' Try k =', k ) print ( '' ) total = 0 for i in range ( 0, tries ): winnings, max_seen, max_value, value = best_of_the_bunch_simulation ( n, k ) print ( '[', value, '], choice = ', winnings ) total = total + winnings print ( ' Average winnings = ', total / tries ) n = 10 k = 3 tries = 10 print ( '' ) print ( ' Try k =', k ) print ( '' ) total = 0 for i in range ( 0, tries ): winnings, max_seen, max_value, value = best_of_the_bunch_simulation ( n, k ) print ( '[', value, '], choice = ', winnings ) total = total + winnings print ( ' Average winnings = ', total / tries ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) best_of_the_bunch_simulation_test ( ) timestamp ( )