# include # include # include # include # include # include "knapsack_rational.h" int main ( ); void knapsack_rational_test ( ); void knapsack_reorder_test ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: knapsack_rational_test() tests knapsack_rational(). Licensing: This code is distributed under the MIT license. Modified: 16 November 2024 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "knapsack_rational_test():\n" ); printf ( " C version\n" ); printf ( " Test knapsack_rational().\n" ); knapsack_rational_test ( ); knapsack_reorder_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "knapsack_rational_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void knapsack_rational_test ( ) /******************************************************************************/ /* Purpose: knapsack_rational_test() tests knapsack_rational(). Licensing: This code is distributed under the MIT license. Modified: 28 July 2011 Author: John Burkardt */ { # define N 5 int i; double mass; double mass_limit = 26.0; int n = N; double p[N] = { 24.0, 13.0, 23.0, 15.0, 16.0 }; double profit; double w[N] = { 12.0, 7.0, 11.0, 8.0, 9.0 }; double x[N]; printf ( "\n" ); printf ( "knapsack_rational_test():\n" ); printf ( " knapsack_rational() solves the rational knapsack problem.\n" ); printf ( "\n" ); printf ( " Object, Profit, Mass, Profit Density\n" ); printf ( "\n" ); for ( i = 0; i < n; i++ ) { printf ( " %4d %7g %7g %7g\n", i + 1, p[i], w[i], p[i] / w[i] ); } knapsack_reorder ( n, p, w ); printf ( "\n" ); printf ( " After reordering by Profit Density:\n" ); printf ( "\n" ); printf ( " Object, Profit, Mass, Profit Density\n" ); printf ( "\n" ); for ( i = 0; i < n; i++ ) { printf ( " %4d %7g %7g %7g\n", i + 1, p[i], w[i], p[i] / w[i] ); } printf ( "\n" ); printf ( " Total mass restriction is %g\n", mass_limit ); knapsack_rational ( n, mass_limit, p, w, x, &mass, &profit ); printf ( "\n" ); printf ( " Object, Density, Choice, Profit, Mass\n" ); printf ( "\n" ); for ( i = 0; i < n; i++ ) { printf ( " %4d %7g %7g %7g\n", i + 1, p[i] / w[i], x[i] * p[i], x[i] * w[i] ); } printf ( "\n" ); printf ( " Total: %g %g\n", profit, mass ); return; # undef N } /******************************************************************************/ void knapsack_reorder_test ( ) /******************************************************************************/ /* Purpose: knapsack_reorder_test() tests knapsack_reorder(). Licensing: This code is distributed under the MIT license. Modified: 27 July 2011 Author: John Burkardt */ { int i; int n = 5; double p[5] = { 24.0, 13.0, 23.0, 15.0, 16.0 }; double w[5] = { 12.0, 7.0, 11.0, 8.0, 9.0 }; printf ( "\n" ); printf ( "knapsack_reorder_test():\n" ); printf ( " knapsack_reorder() reorders the knapsack data.\n" ); printf ( "\n" ); printf ( " Object, Profit, Mass, Profit Density\n" ); printf ( "\n" ); for ( i = 0; i < n; i++ ) { printf ( " %6d %7g %7g %7g\n", i, p[i], w[i], p[i] / w[i] ); } knapsack_reorder ( n, p, w ); printf ( "\n" ); printf ( " After reordering by Profit Density:\n" ); printf ( "\n" ); printf ( " Object, Profit, Mass, Profit Density\n" ); printf ( "\n" ); for ( i = 0; i < n; i++ ) { printf ( " %6d %7g %7g %7g\n", i, p[i], w[i], p[i] / w[i] ); } return; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 31 May 2001 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 24 September 2003 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); fprintf ( stdout, "%s\n", time_buffer ); return; # undef TIME_SIZE }