function knapsack_random_test01 ( )

%*****************************************************************************80
%
%% knapsack_random_test01() tests knapsack_random().
%
%  Discussion:
%
%    In the knapsack problem, a knapsack of capacity K is given,
%    as well as N items, with the I-th item of value V(I) and weight W(I).
%
%    A selection is "feasible" if the total weight is no greater than K.
%
%    A selection is "optimal" if it is feasible, and the total value of
%    the selected items is not exceeded for any other feasible selection
%
%    This code simply chooses a selection at random, determines if it is
%    feasible, and if so, reports the total value of the selection.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 November 2024
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'knapsack_random_test01():\n' );
  fprintf ( 1, '  knapsack_random() randomly selects a subset of n items\n' );
  fprintf ( 1, '  and considers it as a solution to a knapsack problem.\n' );
  fprintf ( 1, '  Maximize profit without exceeding weight limit.\n' );

  n = 5;
  v = [  24;  13;  23;  15;  16 ];
  w = [  12;   7;  11;   8;   9 ];
  k = 26;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of items is %d\n', n );
  fprintf ( 1, '  Value array:' );
  disp ( v );
  fprintf ( 1, '  Weight array:' );
  disp ( w );
  fprintf ( 1, '  Weight limit is %d\n', k );

  for test = 0 : 10

    c_test = knapsack_random ( n );
    v_test = ( c_test' * v );
    w_test = ( c_test' * w );
    if ( 0.0 < w_test )
      r_test = v_test / w_test;
    else
      r_test = 0.0;
    end

    fprintf ( 1, '\n' );
    fprintf ( 1, '  Selected items:\n' );
    disp ( c_test );
    if ( k < w_test )
      fprintf ( 1, '  Weight %d exceeds weight limit %d\n', w_test, k );
    else
      fprintf ( 1, '  Weight %d\n', w_test );
      fprintf ( 1, '  Value  %d\n', v_test );
      fprintf ( 1, '  Ratio  %f\n', r_test );
    end

  end

  return
end

