function optimal ( trial_num, n, top )

%*****************************************************************************80
%
%% optimal() simulates the dating problem.
%
%  Discussion:
%
%    A dating club offers N potential partners.
%    It turns out that any of TOP of these partners would be acceptable.
%    The dater gets 1 date with each partner, but immediately after the
%    date, must either marry that partner, or move to the next date.
%    The dater plans to date a sample of the partners without a marriage
%    offer, and then marry the next partner who is better than all
%    the sample dates. 
%    As the sample size is varied, what are the chances of happiness?
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    27 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Digital Dice: Computational Solutions to Practical Probability Problems,
%    Princeton, 2008,
%    ISBN: 978-0-691-15821-1.
%
%  Input:
%
%    integer trial_num: the number of trials.
%
%    integer N, the size of the dating pool.
%
%    integer TOP, the number of acceptable choices.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'optimal():\n' );
  fprintf ( 1, '  A dating club offers %d potential partners.\n', n );
  fprintf ( 1, '  It turns out that any of %d of these partners would be acceptable.\n', top );
  fprintf ( 1, '  The dater gets 1 date with each partner, but immediately after the\n' );
  fprintf ( 1, '  date, must either marry that partner, or move to the next date.\n' );
  fprintf ( 1, '  The dater plans to date a sample of the partners without a marriage\n' );
  fprintf ( 1, '  offer, and then marry the next partner who is better than all\n' );
  fprintf ( 1, '  the sample dates. \n' );
  fprintf ( 1, '  As the sample size is varied, what are the chances of happiness?\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Use graphics to display result.\n' );

  results = zeros ( 1, ceil ( 4 * n / 5 ) );

  for sample_size = 1 : ceil ( 4 * n / 5 )

    check = zeros ( 1, sample_size );
    success = 0;

    for loop = 1 : trial_num

      order = randperm ( n );
      for j = 1 : sample_size
        check(j) = order(j);
      end
      best_in_sample = min(check);
      exit = 0;
      k = sample_size + 1;
      while ( exit == 0 )
        if ( order(k) < best_in_sample )
          select = order(k);
          exit = 1;
        else
          k = k + 1;
        end
        if ( k == n + 1 )
          select = order(n);
          exit = 1;
        end
      end

      for k = 1 : top
        if ( select == k )
          success = success + 1;
        end
      end

    end

    results(sample_size) = success;

  end

  results = results / trial_num;
%
%  Graphics.
%
  clf ( );
  bar ( results );
  grid ( 'on' );
  xlabel ( 'Sample lot size' );
  ylabel ( 'Probability of happiness' );
  title ( 'The optimal dating problem.' );
  filename = 'optimal.png';
  print ( '-dpng', filename )
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
