{"id":116,"date":"2023-11-28T06:52:15","date_gmt":"2023-11-28T06:52:15","guid":{"rendered":"https:\/\/sapabap.dwimansolution.com\/?p=116"},"modified":"2023-11-28T06:52:15","modified_gmt":"2023-11-28T06:52:15","slug":"sap-abap-knapsack","status":"publish","type":"post","link":"https:\/\/sapabap.dwimansolution.com\/index.php\/2023\/11\/28\/sap-abap-knapsack\/","title":{"rendered":"SAP ABAP &#8211; Knapsack"},"content":{"rendered":"\n<p>What is this knapsack? The point is we use the knapsack algorithm to get <strong>optimal results<\/strong>.<\/p>\n\n\n\n<p><strong>Example Case<\/strong>: we have 3 items that we should bring by our cart to sell. But our cart is only can bring max 50kg, and that means the cart is not capable of bringing all the items. Which items we should bring to get the maximum profit?<\/p>\n\n\n\n<p><br>item 1 : weight = 10kg ; profit = 60<br>item 2 : weight = 20kg ; profit = 100<br>item 3 : weight = 30kg ; profit = 120<br>we would like to search max profit with max weight = 50<br><strong>expected: <\/strong>the result should be items 2 &amp; 3 chosen with total weight = 50 and total profit = 220<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>How we get that with ABAP. Here I share my example code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>REPORT zknapsack.\r\n\r\nTYPES: BEGIN OF ty_item,\r\n         profit TYPE i,\r\n         weight TYPE i,\r\n       END OF ty_item,\r\n       tt_item TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY.\r\n\r\nTYPES: BEGIN OF ty_sum,\r\n         sum_w TYPE i,\r\n         sum_p TYPE i,\r\n         items TYPE tt_item,\r\n       END OF ty_sum.\r\n\r\nCLASS lcl_knapsack DEFINITION.\r\n\r\n  PUBLIC SECTION.\r\n    CLASS-METHODS:\r\n      get_optimal\r\n        IMPORTING\r\n                  im_w_max      TYPE i\r\n                  imt_item      TYPE tt_item\r\n                  im_n          TYPE i\r\n        RETURNING VALUE(rs_sum) TYPE ty_sum.\r\n\r\n  PRIVATE SECTION.\r\n    CLASS-METHODS: get_max\r\n      IMPORTING\r\n                im_data1      TYPE ty_sum\r\n                im_data2      TYPE ty_sum\r\n      RETURNING VALUE(rs_sum) TYPE ty_sum.\r\n\r\n\r\n\r\nENDCLASS.\r\n\r\n\r\nCLASS lcl_knapsack IMPLEMENTATION.\r\n  METHOD get_optimal.\r\n    IF im_n = 0 OR im_w_max &lt;= 0.\r\n      RETURN.\r\n    ENDIF.\r\n\r\n    IF imt_item&#91; im_n ]-weight > im_w_max.\r\n      rs_sum = get_optimal( im_w_max = im_w_max imt_item = imt_item im_n = im_n - 1 ).\r\n    ELSE.\r\n      DATA(l_data) = get_optimal( im_w_max = im_w_max - imt_item&#91; im_n ]-weight\r\n                                   imt_item = imt_item\r\n                                   im_n = im_n - 1 ).\r\n      l_data-sum_w = l_data-sum_w + imt_item&#91; im_n ]-weight.\r\n      l_data-sum_p = l_data-sum_p + imt_item&#91; im_n ]-profit.\r\n      APPEND imt_item&#91; im_n ] TO l_data-items.\r\n\r\n      rs_sum = get_max( im_data1 = l_data\r\n                        im_data2 = get_optimal( im_w_max = im_w_max imt_item = imt_item im_n = im_n - 1 ) ).\r\n    ENDIF.\r\n  ENDMETHOD. \" get_optimal\r\n\r\n  METHOD get_max.\r\n    IF im_data1-sum_p &lt; im_data2-sum_p.\r\n      rs_sum = im_data2.\r\n    ELSE.\r\n      rs_sum = im_data1.\r\n    ENDIF.\r\n  ENDMETHOD. \" get_max\r\n\r\nENDCLASS.\r\n\r\nSTART-OF-SELECTION.\r\n* ------------------------------------------------------------------------------------------\r\n  \" CASE:\r\n  \" item 1 : weight = 10 ; profit = 60\r\n  \" item 2 : weight = 20 ; profit = 100\r\n  \" item 3 : weight = 30 ; profit = 120\r\n  \" we would like to search max profit with max weight = 50\r\n  \" the result should be items 2 &amp; 3 chosen with total weight = 50 and total profit = 220\r\n* ------------------------------------------------------------------------------------------\r\n\r\n  DATA:\r\n    lt_item  TYPE tt_item,\r\n    lv_n     TYPE i,\r\n    lv_w_max TYPE i.\r\n\r\n  lt_item = VALUE #( ( weight = 10 profit = 60 )\r\n                     ( weight = 20 profit = 100 )\r\n                     ( weight = 30 profit = 120 ) ).\r\n  lv_n = lines( lt_item ).\r\n  lv_w_max = 50.\r\n\r\n  DATA(ls_result) = lcl_knapsack=>get_optimal(\r\n                      EXPORTING\r\n                        im_w_max = lv_w_max\r\n                        imt_item = lt_item\r\n                        im_n     = lv_n\r\n                    ).\r\n\r\n  WRITE:\/ ls_result-sum_p, ls_result-sum_w.<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Don&#8217;t only copy and paste on your code but please understand the concept. I hope you can do this better. Thank you.:)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>references:<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/www.geeksforgeeks.org\/0-1-knapsack-problem-dp-10\/\" target=\"_blank\">https:\/\/www.geeksforgeeks.org\/0-1-knapsack-problem-dp-10\/<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is this knapsack? The point is we use the knapsack algorithm to get optimal results. Example Case: we have 3 items that we should bring by our cart to sell. But our cart is only can bring max 50kg, and that means the cart is not capable of bringing all the items. Which items&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/posts\/116"}],"collection":[{"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":3,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":119,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/posts\/116\/revisions\/119"}],"wp:attachment":[{"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}