# include # include # include # include # include # include using namespace std; # include "knapsack_rational.hpp" int main ( ); void knapsack_rational_test ( ); void knapsack_reorder_test ( ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // knapsack_rational_test() tests knapsack_rational(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 15 November 2024 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "knapsack_rational_test():\n"; cout << " C++ version\n"; cout << " Test knapsack_rational().\n"; knapsack_reorder_test ( ); knapsack_rational_test ( ); // // Terminate. // cout << "\n"; cout << "knapsack_rational_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void knapsack_rational_test ( ) //****************************************************************************80 // // 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]; cout << "\n"; cout << "knapsack_rational_test():\n"; cout << " knapsack_rational() solves the rational knapsack problem.\n"; cout << "\n"; cout << " Object, Profit, Mass, Profit Density\n"; cout << "\n"; for ( i = 0; i < n; i++ ) { cout << " " << setw(4) << i + 1 << " " << setw(7) << p[i] << " " << setw(7) << w[i] << " " << setw(7) << p[i] / w[i] << "\n"; } knapsack_reorder ( n, p, w ); cout << "\n"; cout << " After reordering by Profit Density:\n"; cout << "\n"; cout << " Object, Profit, Mass, Profit Density\n"; cout << "\n"; for ( i = 0; i < n; i++ ) { cout << " " << setw(4) << i + 1 << " " << setw(7) << p[i] << " " << setw(7) << w[i] << " " << setw(7) << p[i] / w[i] << "\n"; } cout << "\n"; cout << " Total mass restriction is " << mass_limit << "\n"; knapsack_rational ( n, mass_limit, p, w, x, mass, profit ); cout << "\n"; cout << " Object, Density, Choice, Profit, Mass\n"; cout << "\n"; for ( i = 0; i < n; i++ ) { cout << " " << setw(4) << i + 1 << " " << setw(7) << p[i] / w[i] << " " << setw(7) << x[i] * p[i] << " " << setw(7) << x[i] * w[i] << "\n"; } cout << "\n"; cout << " Total: " << profit << " " << mass << "\n"; return; # undef N } //****************************************************************************80 void knapsack_reorder_test ( ) //****************************************************************************80 // // 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 }; cout << "\n"; cout << "knapsack_reorder_test():\n"; cout << " knapsack_reorder() reorders the knapsack data.\n"; cout << "\n"; cout << " Object, Profit, Mass, Profit Density\n"; cout << "\n"; for ( i = 0; i < n; i++ ) { cout << " " << setw(6) << i << " " << setw(7) << p[i] << " " << setw(7) << w[i] << " " << setw(7) << p[i] / w[i] << "\n"; } knapsack_reorder ( n, p, w ); cout << "\n"; cout << " After reordering by Profit Density:\n"; cout << "\n"; cout << " Object, Profit, Mass, Profit Density\n"; cout << "\n"; for ( i = 0; i < n; i++ ) { cout << " " << setw(6) << i << " " << setw(7) << p[i] << " " << setw(7) << w[i] << " " << setw(7) << p[i] / w[i] << "\n"; } return; } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // 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: // // 08 July 2009 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }