Here come a little ABAP challenge:
For an ABAP projet, i must build from an internal table with 2 columns (example1) another table containing all combinations possibles (example2).
"X" columns represent the parameter. "Y" represent the parameter value.
example1:
X(param)
Y(value)
A a1
A a2
A a3
B b1
B b2
C c1
C c2
In the result table (example2):
We must get all combinations with a numeric id (on 3 columns).
The new "z" column represent the combination id. For each combination there is a number of lines equal to the number of dictinct parameters(in our case 3 line for A,B and C).
"x" column still represent the parameter and "y" column the associated value.
example2:
z(combi num)
x(param)
y(value)
1 A a1
1 B b1
1 C c1
2 A a1
2 B b1
2 C c2
3 A a1
3 B b2
3 C c1
4 A a1
4 B b2
4 C c2
etc...
etc...
etc...
12 A a3
12 B b2
12 C c2
Another remark is that the number of parameters and the number of values per parameters is not fixed (the initial internal table can evolve a lot and so the combinations possibles).
We maybe need recursion but i'm not sure of it.
Here is a non-recursive way to do it, you might have to rewrite the parts that use the new 740 syntax. The idea is pretty simple, first transform the data into an internal table with one entry per parameter containing a table with the possible values, the LOOP loop. From there it is a simple matter of going through all the combinations and adding these to another internal table, the WHILE loop.
REPORT z_algorithm.
TYPES: ty_param TYPE char1,
ty_value TYPE char2,
BEGIN OF ty_struct,
x TYPE ty_param,
y TYPE ty_value,
END OF ty_struct,
BEGIN OF ty_combi,
z TYPE i,
s TYPE ty_struct,
END OF ty_combi.
TYPES: BEGIN OF ty_param_struct,
x TYPE ty_param,
ys TYPE STANDARD TABLE OF ty_value WITH DEFAULT KEY,
ix TYPE i,
END OF ty_param_struct.
DATA: tab TYPE STANDARD TABLE OF ty_struct,
params TYPE STANDARD TABLE OF ty_param_struct,
done TYPE abap_bool VALUE abap_false,
z TYPE i VALUE 0,
overflow TYPE abap_bool VALUE abap_false,
combis TYPE STANDARD TABLE OF ty_combi.
START-OF-SELECTION.
APPEND VALUE: #( x = 'A' y = 'a1' ) TO tab,
#( x = 'A' y = 'a2' ) TO tab,
#( x = 'A' y = 'a3' ) TO tab,
#( x = 'B' y = 'b1' ) TO tab,
#( x = 'B' y = 'b2' ) TO tab,
#( x = 'C' y = 'c1' ) TO tab,
#( x = 'C' y = 'c2' ) TO tab.
LOOP AT tab ASSIGNING FIELD-SYMBOL(<tab>).
READ TABLE params WITH KEY x = <tab>-x ASSIGNING FIELD-SYMBOL(<param>).
IF sy-subrc NE 0.
APPEND INITIAL LINE TO params ASSIGNING <param>.
<param>-x = <tab>-x.
<param>-ix = 1.
ENDIF.
APPEND <tab>-y TO <param>-ys.
ENDLOOP.
WHILE done EQ abap_false.
ADD 1 TO z.
overflow = abap_true.
done = abap_true.
LOOP AT params ASSIGNING <param>.
READ TABLE <param>-ys INDEX <param>-ix ASSIGNING FIELD-SYMBOL(<y>).
APPEND VALUE #( z = z s-x = <param>-x s-y = <y> ) TO combis.
IF overflow EQ abap_true.
ADD 1 TO <param>-ix.
ENDIF.
IF <param>-ix GT lines( <param>-ys ).
overflow = abap_true.
<param>-ix = 1.
ELSE.
overflow = abap_false.
done = abap_false.
ENDIF.
ENDLOOP.
ENDWHILE.
Related
I have been modelling some directive graphs in prolog. It consists of nodes and edges and how they are connected. Every node has a certain value of surfers (page rank).
e.g.
e(a,c).
e(b,a).
e(b,h).
:-dynamic s/2.
s(a,1).
s(b,1).
s(c,1).
I have written an interpreter. This interpreter calculates new values for each node.
The problem is how to assign these new values to the database.
I have been doing it with retractall(s(,)) and asserting new values to every single node with e.g. assertz(s(a,sum0),
assertz(s(b,sum1), ....
This has been working, but is it possible to assert new values without asserting each particular node in the graph a value, so that the interpreter is completely independent from the graph?
I have been trying to generate a list with all nodes in a graph and indicating the nodes of a graph in this list.
sum_nodes_0(X,Y):-
list_nodes(_,L),
amount_nodes(N),
nth1(I,L,X),I=<N,sum_node(X,Y).
reset(X,S):-
list_nodes(_,L),
s(X,S),
sum_node(X,Y),
retractall(s(_,_)),
forall(member(X,L),assertz(s(X,Y))).
sum_nodes_0 displays me the new values like.
X = a,
Y = 1.0 ;
X = b,
Y = 1 ;
X = c,
Y = 2 ;
X = d,
Y = 0.5 ;
X = e,
Y = 1.5 ;
X = f,
Y = 0.5 ;
X = g,
Y = 1 ;
X = h,
Y = 0.5.
These values I want to be assigned by reset(X,S). But reset just resets one value not all. e.g.
X=h; Y=0
I have tried foreach, forall and member predicates. But it didn't work as intended.
I hope that someone has an idea.
I have a measure in Tableau that has values a,b,c and a field of countries that each have varying values of a,b,c.
I want to make a sort that allows you to select either a,b, or c, and then ranks the countries accordingly by their value of a,b,or c.
I have tried to do a parameter to calculated field, but that doesn't work because that method is for sorting between measures, not values of measures.
This can be accomplished with a parameter and calculated field--what have you tried that didn't work?
[sort parameter]
A string value with allowable values of a, b, or c
If a, b, and c are each their own measure:
[sort field]
IF [sort parameter] = 'a' then
[a]
ELSEIF [sort parameter] = 'b' then
[b]
ELSE
[c]
END
If values of a, b, and c are in one measure (perhaps called [value]) and a separate column exists to distinguish (perhaps called [type]):
[sort field]
IF [sort parameter] = 'a' then
SUM(IIF([type] = 'a',[value],NULL)
ELSEIF [sort parameter] = 'b' then
SUM(IIF([type] = 'b',[value],NULL)
ELSE
SUM(IIF([type] = 'c',[value],NULL)
END
Sort your visualization by [sort field].
Hi how can I make the "FILTER" function to work if I have multiple parameters separates by comma.
FACTION HERO
A X
B Y
A,B Z
Now if I do a FILTER(B:B; A:A="A") the output will only be X. But Since Z is also in Faction A the output should be X and Z. How can I take care of this?
Wanted output Actual output
X X
Z
Two ways are:
=filter(B:B, regexmatch(A:A, "A"))
=QUERY( A:B , "Select B Where A contains 'A' " )
When I read the book -- Artificial Intelligence (a modern approach), I came across the following sentence describing the method to convert a n-ary Constraint Search Problem to a binary one:
Another way to convert an n-ary CSP to a binary one is the dual graph
transformation: create a new graph in which there will be one variable
for each constraint in the original graph, and one binary constraint
for each pair of constraints in the original graph that share
variables. For example, if the original graph has variables {X, Y, Z}
and constraints ⟨(X, Y, Z), C1⟩ and ⟨(X, Y ), C2⟩ then the dual graph
would have variables {C1, C2} with the binary constraint ⟨(X, Y ), R1
⟩, where (X, Y ) are the shared variables and R1 is a new relation
that defines the constraint between the shared variables, as specified
by the original C1 and C2.
I don't quite get the example provided in the book, can anybody help to explain it in another way and may better provide a concrete example? thanks :D
Let's say your problem has the following constraints:
C1, which involves x, y and z:
x + y = z
C2, which involves x and y:
x < y
with the following domains:
x :: [1,2,3]
y :: [1,2,3]
z :: [1,2,3]
The author says that you need to create 2 more variables, one for each constraint. They are defined as follows:
c1 = < x, y, z >
c2 = < x, y >
The domains of c1 and c2 are defined so that they don't violate C1 and C2, i.e.:
c1 :: [ <1,2,3>, <2,1,3>, <1,1,2>]
c2 :: [<1,2>, <2,3>, <1,3>]
c1 and c2 will be the nodes of the dual graph, but first you need to define a constraint between them, i.e. R1:
R1: "the 1st and the 2nd element of c1 (x and y) must be equal to the 1st and the 2nd element of c2 respectively" (actually you could split it in two simpler constraints)
Let's say if I have a row with 3+ attributes A, B, C and other Attrs
row1: A=1, B=5, C=10, Other Attrs
row2: A=1, B=5, C=10, Other Attrs
row3: A=2, B=5, C=10, Other Attrs
row4: A=2, B=5, C=10, Other Attrs
row5: A=3, B=6, C=11, Other Attrs
row6: A=3, B=6, C=11, Other Attrs
So if I want to assign each row a group ID by combination of A, B, C, row1, row2 have same group id x, row 3 row 4 have group id y, row 5 row 6 have group id z.
A simple solution is:
group id = A * S + B * S^2 + C * S^3 (S is the max integer number)
But my problem is I need an algorithm to back out each of A, B, C value from group id
for example, I need to be able to back out A=1, B=5, C=10 from group id x solely
First, this would be enough:
group id = A + B * S + C * S^2;
To get back A, B, and C:
A = id % S;
B = (id / S) % S;
C = (id / S^2);
Where / is integer division and % is modulo.
Based on your example a much simpler approach would be:
a * 10^6 + b*10^3 + c
which would give you
001005010
001005010
002005010
002005010
003006011
003006011
and the inverse of this function is very simple. If your numbers are possibly longer than 3 digits, adjust accordingly.