<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Efficient ABAP Combination Code (HEAVY MATH) in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508254#M1654655</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;this problem is NP-complete, similar to [Subset sum problem|http://en.wikipedia.org/wiki/Subset_sum_problem].&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So first of all check once again, if You really want to write such algorithms in ABAP...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But if You want to write in ABAP...&lt;/P&gt;&lt;P&gt;If You check all combinations, You will reach time out quite fast &lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If You make some assumptions, like quantity in each container is quite small number (hundreds? thousends? ten thousends?),&lt;/P&gt;&lt;P&gt;then there is a chance &lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would try this algorithm:&lt;/P&gt;&lt;P&gt;1. Sort quantities in descending order (GT_QUANS)&lt;/P&gt;&lt;P&gt;2. Store requested quantity GV_REQ in table GT_VALUES.&lt;/P&gt;&lt;P&gt;Repeat:&lt;/P&gt;&lt;P&gt;3. Read and remove first value from GT_QUANS to GV_QUAN.&lt;/P&gt;&lt;P&gt;4. Subtract GV_REQ from each value from GT_VALUES:&lt;/P&gt;&lt;P&gt;   a) if result is &amp;gt; 0:&lt;/P&gt;&lt;P&gt;      - if result is in GT_QUANS than requested quantity can be chosen&lt;/P&gt;&lt;P&gt;      - otherwise store result in GT_TMP&lt;/P&gt;&lt;P&gt;5. Append whole results from GT_TMP to GT_VALUES&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;6. If GT_QUANS is empty, then requested quantity can not be chosen&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is an algorithm on a paper - rewrite it to ABAP, and do tests.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My example:&lt;/P&gt;&lt;P&gt;1. 20 16 10 8 6 3 2 check out if 37 can be chosen&lt;/P&gt;&lt;P&gt;2. Store 37&lt;/P&gt;&lt;P&gt;In loop:&lt;/P&gt;&lt;P&gt;for 20: store 17&lt;/P&gt;&lt;P&gt;for 16: store 21 1&lt;/P&gt;&lt;P&gt;for 10: store 27 7 11&lt;/P&gt;&lt;P&gt;for   8: store 29 9 13 19 2&lt;/P&gt;&lt;P&gt;Stop here - 2 is in GT_QUANS, requested quantity can be chosen.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;--&lt;/P&gt;&lt;P&gt;Przemysław&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 18 Jan 2012 15:28:03 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2012-01-18T15:28:03Z</dc:date>
    <item>
      <title>Efficient ABAP Combination Code (HEAVY MATH)</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508252#M1654653</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This will need strong MATH and ABAP skills....:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Given that I have a number of containers (N) each containing a positive quantity (Q) I need to prove&lt;/P&gt;&lt;P&gt;that a combination of these containers can or cannot be chosen to give a requested quantity (R).&lt;/P&gt;&lt;P&gt;(The true life example is some number of whole containers must be expediated and some will not).&lt;/P&gt;&lt;P&gt;Some containers MAY have the same quantity, but this is not required.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have code that assignes each position to a binary bit of an (N) position number and cycles through all &lt;/P&gt;&lt;P&gt;binary values (1, 10, 11, 100, .... etc.) adding each combination up.  This works fine up to 20 containers, but then the &lt;/P&gt;&lt;P&gt;geometric progression starts to make it too expensive in time to do this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does anyone have an efficient algorythm for this?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 14:22:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508252#M1654653</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-01-18T14:22:43Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient ABAP Combination Code (HEAVY MATH)</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508253#M1654654</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is more a logic question rather than an ABAP one, but you might investigate recursive techniques. They can be simler and run more quickly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 14:35:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508253#M1654654</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-01-18T14:35:08Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient ABAP Combination Code (HEAVY MATH)</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508254#M1654655</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;this problem is NP-complete, similar to [Subset sum problem|http://en.wikipedia.org/wiki/Subset_sum_problem].&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So first of all check once again, if You really want to write such algorithms in ABAP...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But if You want to write in ABAP...&lt;/P&gt;&lt;P&gt;If You check all combinations, You will reach time out quite fast &lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If You make some assumptions, like quantity in each container is quite small number (hundreds? thousends? ten thousends?),&lt;/P&gt;&lt;P&gt;then there is a chance &lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would try this algorithm:&lt;/P&gt;&lt;P&gt;1. Sort quantities in descending order (GT_QUANS)&lt;/P&gt;&lt;P&gt;2. Store requested quantity GV_REQ in table GT_VALUES.&lt;/P&gt;&lt;P&gt;Repeat:&lt;/P&gt;&lt;P&gt;3. Read and remove first value from GT_QUANS to GV_QUAN.&lt;/P&gt;&lt;P&gt;4. Subtract GV_REQ from each value from GT_VALUES:&lt;/P&gt;&lt;P&gt;   a) if result is &amp;gt; 0:&lt;/P&gt;&lt;P&gt;      - if result is in GT_QUANS than requested quantity can be chosen&lt;/P&gt;&lt;P&gt;      - otherwise store result in GT_TMP&lt;/P&gt;&lt;P&gt;5. Append whole results from GT_TMP to GT_VALUES&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;6. If GT_QUANS is empty, then requested quantity can not be chosen&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is an algorithm on a paper - rewrite it to ABAP, and do tests.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My example:&lt;/P&gt;&lt;P&gt;1. 20 16 10 8 6 3 2 check out if 37 can be chosen&lt;/P&gt;&lt;P&gt;2. Store 37&lt;/P&gt;&lt;P&gt;In loop:&lt;/P&gt;&lt;P&gt;for 20: store 17&lt;/P&gt;&lt;P&gt;for 16: store 21 1&lt;/P&gt;&lt;P&gt;for 10: store 27 7 11&lt;/P&gt;&lt;P&gt;for   8: store 29 9 13 19 2&lt;/P&gt;&lt;P&gt;Stop here - 2 is in GT_QUANS, requested quantity can be chosen.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;--&lt;/P&gt;&lt;P&gt;Przemysław&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 15:28:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508254#M1654655</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-01-18T15:28:03Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient ABAP Combination Code (HEAVY MATH)</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508255#M1654656</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The problem is, there are 2^n-n-1 unique combinations, where n is the number of containers (if I'm not totally mistaken). So with 20 containers you are already at 1048555 unique combinations. There must be a better approach to get an result, without pre-calculating all possible combinations.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 15:30:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508255#M1654656</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-01-18T15:30:06Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient ABAP Combination Code (HEAVY MATH)</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508256#M1654657</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for all comments and wonderfully fast.&lt;/P&gt;&lt;P&gt;For information, there will be a heavy weight to a single quantity in the containers, but not a requirement.&lt;/P&gt;&lt;P&gt;I will combine the recursion suggestion with the weighting of the highest occurring quantity and will&lt;/P&gt;&lt;P&gt;post the resulting code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have never asked a question before so will review the policies and award points as appropriate.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 15:42:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508256#M1654657</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-01-18T15:42:58Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient ABAP Combination Code (HEAVY MATH)</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508257#M1654658</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Resultant (and working code)....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Global data fields&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt; GTAB contains a list of container counts and quantities.&lt;/P&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt; For example 10 containers of 15 each will occupy 1 record&lt;/P&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt; in the table (cnt=10, qty=15).  Table should not contain 0 entries.&lt;/P&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt; (Loading is not covered hereu2026..)&lt;/P&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt; PMATCH is the input parameter for matching quantity.&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;data: begin of gtab occurs 0,&lt;/P&gt;&lt;P&gt;      cnt type i,&lt;/P&gt;&lt;P&gt;      qty type i,&lt;/P&gt;&lt;P&gt;      end of gtab.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;data: gstart type i,    "Total container quantities before current level.&lt;/P&gt;&lt;P&gt;      glevel type i,    "Level to Process&lt;/P&gt;&lt;P&gt;      glast  type i,    "Last Level to Process (Total Records in Table....)&lt;/P&gt;&lt;P&gt;      gmatch type i,    "Quantity to Match.&lt;/P&gt;&lt;P&gt;      gdone(1).         "Did we get a match?&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;start-of-selection.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;=== Sort by occurrances (most to bottom)&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;sort gtab by cnt qty.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;=== Set up for first level.&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;gstart = 0.&lt;/P&gt;&lt;P&gt;glevel = 1.&lt;/P&gt;&lt;P&gt;describe table gtab lines glast.&lt;/P&gt;&lt;P&gt;gmatch = pmatch.&lt;/P&gt;&lt;P&gt;clear gdone.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;perform process_level.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;amp;----&lt;/STRONG&gt;&lt;/P&gt;&lt;HR originaltext="----------------------------------------------------------------" /&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*&amp;amp;      Form  process_level&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;amp;----&lt;/STRONG&gt;&lt;/P&gt;&lt;HR originaltext="----------------------------------------------------------------" /&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt; Recursive call to see if a match can be made for combined quantity&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;STRONG&gt;----&lt;/STRONG&gt;&lt;/P&gt;&lt;HR originaltext="-----------------------------------------------------------------" /&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;FORM process_level .&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;data: lstart type i,    "Previous total (not counting this level)&lt;/P&gt;&lt;P&gt;      llevel type i.    "This level.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;data: lcnt type i,      "Number of counted containers (this level)&lt;/P&gt;&lt;P&gt;      lwrk type i.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;data: ltab like gtab.   "Local level&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt; Get Local (Level) Values&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;lstart = gstart.&lt;/P&gt;&lt;P&gt;llevel = glevel.&lt;/P&gt;&lt;P&gt;read table gtab into ltab index llevel.&lt;/P&gt;&lt;P&gt;write: / 'Processing level:', llevel.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Start with NO containers at this level. (prefer to lowest level first)&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;clear lcnt.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;do.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;  lwrk = lstart + ( lcnt * ltab-qty ).&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;--- See if calculation at this level is a match...&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;  if lwrk = gmatch.&lt;/P&gt;&lt;P&gt;    gdone = 'X'.&lt;/P&gt;&lt;P&gt;    write: / 'Match!'.&lt;/P&gt;&lt;P&gt;    exit.&lt;/P&gt;&lt;P&gt;  endif.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;--- Recurse to lower levels if needed.&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;  if llevel &amp;lt; glast.&lt;/P&gt;&lt;P&gt;    gstart = lwrk.            "Starting quantity for levels to here.&lt;/P&gt;&lt;P&gt;    glevel = llevel + 1.      "Next level.&lt;/P&gt;&lt;P&gt;    perform process_level.    "GO!&lt;/P&gt;&lt;P&gt;    if gdone = 'X'.           "Did lower level produce a match?&lt;/P&gt;&lt;P&gt;      exit.&lt;/P&gt;&lt;P&gt;    endif.&lt;/P&gt;&lt;P&gt;  endif.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;--- Next container at this level (if present)&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;  lcnt = lcnt + 1.&lt;/P&gt;&lt;P&gt;  if lcnt &amp;gt; ltab-cnt.&lt;/P&gt;&lt;P&gt;    exit.&lt;/P&gt;&lt;P&gt;  endif.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;enddo.&lt;/P&gt;&lt;P&gt;*&lt;/P&gt;&lt;P&gt;ENDFORM.                    " process_level&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 20:31:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/efficient-abap-combination-code-heavy-math/m-p/8508257#M1654658</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-01-18T20:31:34Z</dc:date>
    </item>
  </channel>
</rss>

