program main !*****************************************************************************80 ! !! knapsack_random_test() tests knapsack_random(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 29 November 2015 ! ! Author: ! ! John Burkardt ! implicit none call timestamp ( ) write ( *, '(a)' ) '' write ( *, '(a)' ) 'knapsack_random_test():' write ( *, '(a)' ) ' Fortran90 version' write ( *, '(a)' ) ' Test knapsack_random()' call subset_random_test ( ) call knapsack_random_test01 ( ) ! ! Terminate. ! write ( *, '(a)' ) '' write ( *, '(a)' ) 'knapsack_random_test():' write ( *, '(a)' ) ' Normal end of execution.' stop end subroutine i4vec_print ( n, a, title ) !*****************************************************************************80 ! !! i4vec_print() prints an I4VEC. ! ! Discussion: ! ! An I4VEC is a vector of I4's. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 02 May 2010 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer N, the number of components of the vector. ! ! integer A(N), the vector to be printed. ! ! character ( len = * ) TITLE, a title. ! implicit none integer n integer a(n) integer i character ( len = * ) title if ( 0 < len_trim ( title ) ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) end if write ( *, '(a)' ) ' ' do i = 1, n write ( *, '(2x,i8,a,2x,i12)' ) i, ':', a(i) end do return end subroutine 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: ! ! 29 November 2024 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: n = 5 integer c_test(n) integer, parameter :: k = 26 integer test real r_test integer, save, dimension ( n ) :: v = (/ & 24, 13, 23, 15, 16 /) integer v_test integer, save, dimension ( n ) :: w = (/ & 12, 7, 11, 8, 9 /) integer w_test write ( *, '(a)' ) '' write ( *, '(a)' ) 'knapsack_random_test01():' write ( *, '(a)' ) ' knapsack_random() randomly selects a subset of n items' write ( *, '(a)' ) ' and considers it as a solution to a knapsack problem.' write ( *, '(a)' ) ' Maximize profit without exceeding weight limit.' write ( *, '(a)' ) '' write ( *, '(a,i2)' ) ' Number of items is ', n call i4vec_print ( n, v, ' Value array:' ) call i4vec_print ( n, w, ' Weight array:' ) write ( *, '(a,i7)' ) ' Weight limit is ', k do test = 1, 10 call knapsack_random ( n, c_test ) v_test = dot_product ( c_test, v ) w_test = dot_product ( c_test, w ) if ( 0.0 < w_test ) then r_test = real ( v_test ) / real ( w_test ) else r_test = 0.0 end if write ( *, '(a)' ) '' call i4vec_print ( n, c_test, ' Selected items:' ) if ( k < w_test ) then write ( *, '(a,i4,a,i4)' ) ' Weight ', w_test, ' exceeds weight limit', k else write ( *, '(a,i6)' ) ' Weight ', w_test write ( *, '(a,i6)' ) ' Value ', v_test write ( *, '(a,g10.3)' ) ' Ratio ', r_test end if end do return end subroutine subset_next_test ( ) !*****************************************************************************80 ! !! subset_next_test() tests subset_next(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 25 November 2024 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: n = 5 integer i integer j integer s(n) write ( *, '(a)' ) '' write ( *, '(a)' ) 'subset_next_test():' write ( *, '(a)' ) ' Test subset_next()' write ( *, '(a)' ) '' write ( *, '(a,i2)' ) ' Generate in order subsets of size ', n write ( *, '(a)' ) '' s(1:n) = 0 i = -1 do i = i + 1 write ( *, '(a,i2,a)', advance = 'no' ) ' ', i, ':' do j = 1, n write ( *, '(2x,i1)', advance = 'no' ) s(j) end do write ( *, '(a)' ) '' call subset_next ( n, s ) if ( sum ( s ) == 0 ) then exit end if end do return end subroutine subset_random_test ( ) !*****************************************************************************80 ! !! subset_random_test() tests subset_random(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 29 November 2024 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: n = 5 integer i integer index integer s(n) integer test write ( *, '(a)' ) '' write ( *, '(a)' ) 'subset_random_test():' write ( *, '(a)' ) ' Test subset_random()' write ( *, '(a)' ) '' write ( *, '(a,i2)' ) ' Subsets will be of size n = ', n write ( *, '(a)' ) '' do test = 1, 10 call subset_random ( n, s ) call subset_to_rank ( n, s, index ) write ( *, '(2x,i2,a)', advance = 'no' ) index, ':' do i = 1, n write ( *, '(2x,i1)', advance = 'no' ) s(i) end do write ( *, '(a)' ) '' end do return end subroutine timestamp ( ) !*****************************************************************************80 ! !! timestamp() prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 15 August 2021 ! ! Author: ! ! John Burkardt ! implicit none character ( len = 8 ) ampm integer d integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s integer values(8) integer y call date_and_time ( values = values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(i2.2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end