Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Combinations by using internal Table

Former Member
0 Likes
1,752

Hi Friends,

I got a requirement to perform a combinations which i posted in this forum earlier.

Now i ll describe in-depth about the exact requirement.

My itab is like this

Name Cu FE Si AG

x1 10 5 4 2

x2 4 7 4 3

..

..

...

X(n) 2 4 5 2

-


Target 50 30 20 10 ...(its not in itab.. user will give this target...)

-


Now there are n number of X...(say n=56)

User will specify that they wanna use only 3 comcentrates out of 5

which gives nCr = 5C3 = 5!/ 3! x 2! ==== 10 combinations.

so it will be..

x1, x2, x3

x1,x2,x4

x1,x2,x5........ it goes on like this....

now it forms .... 10x1 + 4x2 + x3 = 50 (target).......(Cu)

5x1 + 7x2 + x3 = 30....................(Fe)

4x1 + 4x2 + x3 = 20....................(Si)

..

now i need to solve this equation and get x1, x2, x3 values....

Is ther anyway to acheive this from begining????

Thanks n Regards

Aravindh Mani

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,301

Hi Aravindh,

Based on your question, this is a linear equations problem. The solution would be to use determinants and find an optimal solution (you may have to refer to some mathematical website to find out how determinants are solved).

For the combinations, a possible (and a little tedious) solution is as follows:

1) Maintain a binary variable (viz for 5 entries in table, variable length will be 5) and initialize it as '00000'.

2) Continuously check if there are exactly three '1's in the variable. If not, keep incrementing.

This way, the variable will take values: 00000, 00001, 00010, 00011, 00100, 00101, 00110, 00111 <- condition met

3) If condition (2) is met, fetch the corresponding lines from the internal table. eg: if 00111, fetch lines 3, 4, 5; if 01011, fetch lines 2, 4, 5; etc etc...

As I said, this is quite a tedious solution; if I can think of something simpler, I'll surely get back to you! Hope this helped!!

Do revert if you need a pseudo-code or anything else from my side!!

Cheers,

Shailesh

Always provide feedback for helpful answers!!

8 REPLIES 8
Read only

Former Member
0 Likes
1,302

Hi Aravindh,

Based on your question, this is a linear equations problem. The solution would be to use determinants and find an optimal solution (you may have to refer to some mathematical website to find out how determinants are solved).

For the combinations, a possible (and a little tedious) solution is as follows:

1) Maintain a binary variable (viz for 5 entries in table, variable length will be 5) and initialize it as '00000'.

2) Continuously check if there are exactly three '1's in the variable. If not, keep incrementing.

This way, the variable will take values: 00000, 00001, 00010, 00011, 00100, 00101, 00110, 00111 <- condition met

3) If condition (2) is met, fetch the corresponding lines from the internal table. eg: if 00111, fetch lines 3, 4, 5; if 01011, fetch lines 2, 4, 5; etc etc...

As I said, this is quite a tedious solution; if I can think of something simpler, I'll surely get back to you! Hope this helped!!

Do revert if you need a pseudo-code or anything else from my side!!

Cheers,

Shailesh

Always provide feedback for helpful answers!!

Read only

Former Member
0 Likes
1,301

Dear Shailesh,

Thanks for the quick response.

I can understand the way to find combimations...

But point (1). Maintain a binary variable (viz for 5 entries in table, variable length will be 5) and initialize it as '00000'.

This i couldnt understand.. How to maintain binary varialble... for 5 rows and 5 columns.....??

(2) Continuously check if there are exactly three '1's in the variable. If not, keep incrementing.

This way, the variable will take values: 00000, 00001, 00010, 00011, 00100, 00101, 00110, 00111 <- condition met

with wat variable this condition will check..

Also taking the variable from itab and making them as a single variable to show combinatoins is ok..

But wat i want is this combinations must be used to find the solution for my linear equation.

i got some code from SDN to find permutations in which i made it to find combinations.. but i can show some se of variables as combinations and not able to further calculations

check this..(permutations)

Thanks

Aravindh Mani

Edited by: Aravindh Mani on Aug 12, 2009 11:49 AM

Read only

0 Likes
1,301

Hi Aravindh,

I'm giving a pseudo-code here to explain a solution; do revert if you have any queries:

1) * First find table length (no. of rows) and find the value 2 ^ length (in our case 2 ^ 5 = 32)

length = 32.

2) * Now, create a subroutine to store the binary equivalent of each number from 1 to 32

  • The following code will populate bintab as "00000, 00001, 00010, 00011, 00100, etc until 11111"

  • If you want, just skip to step 3 knowing that bintab will have the above values

i = 1.

while i<=32.

dividend = i.

while dividend / 2 >= 1

<Find (idividend / 2) in integer (say quotient), and dividend mod 2 (say remainder)>

< move quotient to dividend, append remainder to a character type variable>

  • For example, when i = dividend = 6:

  • First pass, quotient = 3, remainder = 0, string = 0, new dividend = 3.

  • Second pass, quotient = 1, remainder = 1, string = 10, new dividend = 1

  • Third pass, quotient = 0, remainder = 1, string = 110, new dividend = 0 and STOP (while condition met)

endwhile.

<provide leading zeros to string, if neccesary, to make 5 characters. In above example, string = '00110'>

append string to bintab.

<end of subroutine>

3) * Filter the bintab.

loop at bintab.

< count the number of '1's in each entry, if count not equal to 3, delete the entry>

endloop.

4) * Now read internal table lines based on bintab

loop at bintab.

<read each character of bintab. If a character is '1', read the corresponding entry in your master linear equations table>

  • For example, if bintab = '10101', read the 1st row, 3rd row and 5th row. This was the whole point of using binary.

< apply algorithm for solving linear equations here, using data from the above rows >

endloop.

Phew! Hope this is a feasible solution; all the best!!

Cheers,

Shailesh

Always provide feedback for helpful answers!

Read only

former_member209217
Active Contributor
0 Likes
1,301

Need to look for a logic which solves linear equations.

I have not come across this type of requrement.But it seems to be a nice one.

I will post if i find any solution for this.

Read only

Former Member
0 Likes
1,301

Dear Shailesh,

Ur Logic worked well...

Now i can able to find the combinations by using binary method..

Also i m looking for some standard and simple method to solve complex linear equation,

I m trying some logic, meanwhile if u got any other logic then plz post here..

Thanks again Shailesh

Regards

Aravindh

Edited by: Aravindh Mani on Aug 14, 2009 6:23 AM

Edited by: Aravindh Mani on Aug 14, 2009 6:24 AM

Read only

0 Likes
1,301

Hi Aravindh,

    • Edit **

Actually, since your lines are variable (ie., sometimes there will be 5 lines, sometimes 6, maybe even 10), it is better to make the field length of 'bintab' dynamic.

So if you have 5 lines, bintab will be 00111, 01011, etc... for 6 lines, bintab will be 001111, 010111, etc.... and so on.

Coming back to your question, your lines are:

x1 5 1 2 1

x2 4 2 9 8

x3 2 4 2 1

x4 6 3 1 8

Assuming user selects '3' as input (three rows to be selected), your bintab will be 0111, 1011, 1101, 1110.

Use the following pseudo-code:

< first create a dynamic table 'linear' with columns equal to user-input "rows" (in our example "3")>

loop at bintab.

initialise c=0.

while c<length (here length = 4)

<check bintab+c(1) = '1'>

<if yes, append that line to new internal table, say 'equations'>

c = c + 1.

  • In the first pass, bintab = 0111. Here, the WHILE loop will populate

  • 'equations' table with lines 2, 3, 4.

  • Thus equations table will be:

  • 4 2 9 8

  • 2 4 2 1

  • 6 3 1 8

endwhile.

  • Now use 'equations' table to form the linear euqations.

  • Unfortunately, transposing will be a problem, so we can use a workaround:

loop at equations.

  • in the first pass, create three rows in 'linear' as follows:

linear-x1 = equations-var1.

append linear.

clear: linear.

linear-x1 = equations-var2.

append linear.

clear: linear.

linear-x1 = equations-var3.

append linear.

clear: linear.

  • Therefore linear becomes

*x1 | x2 | x3

*----


*4 &nbsp;| 0 | 0

*2 &nbsp;| 0 | 0

*9 &nbsp;| 0 | 0

  • From second pass onwards, modify entries in linear as:

linear-x2 = equations-var1.

modify linear transporting x2.

clear: linear.

linear-x2 = equations-var2.

modify linear transporting x2.

clear: linear.

and so on....

  • Thus linear will become:

  • x1 | x2 | x3

*----


  • 4 &nbsp;| 2 | 6

  • 2 &nbsp;| 4 | 3

  • 9 &nbsp;| 2 | 1

endloop.

<Now apply algorithm to solve the three equations in linear.>

endloop.

There are quite a few areas which you have to make dynamic in the above code.

I would suggest you keep everything static first, say 3 rows, 3 equations

Then solve statically. You can make your variables and tables dynamic later on, one at a time.

Let me know if this answers your question...

*Edit*

Cheers,

Shailesh.

Always provide feedback for helpful answers!!

Read only

Former Member
0 Likes
1,301

Thanks Shailesh,

I ll look into this link...

Also i got a prblm..

My values table is like this..

x1 5 1 2 1

x2 4 2 9 8

x3 2 4 2 1

x4 6 3 1 8

no i have 11100 as current lin in bintab...

1, 2, 3 position...

so

x1 x2 x3 x4 x5

5 4 2 0 0 -


how to acheive this.. Converting rows to columns with restriction and sometimes it will be like

0 2 0 1 1

Thanks

Aravindh

Read only

0 Likes
1,301

Hi,

Just i have come across this FM RS_DME_SC_LINEAR_EQUATION .Check whether it will be useful to u r not.

I have checked the usage analysis for this FM.I didn't find any reports or programs using this.

Regards,

lakshman.