Hello i'm trying to learn more about clingo, have this terms: v(1,2).v(2,1).v(3,4).v(4,3), means that first element cannot be in the same row of the second element, the second cannot be in the same of the first, ecc.. would like to write some rules to find a matrix 2x2 in which element (I,J,N) are placed according to that limits. thanks in advance
v(1,2).v(2,1).v(3,4).v(4,3)
rows(1..2).
col(1..2).
1{m(I,J,N) : v(N)}1 :- rows(I), col(J).
1{m(I,J,N) : rows(I), col(J)}1 :- v(N).
...code...
output
[1,1,1][1,2,4][2,1,2][2,2,3]
[1,1,4][1,2,1][2,1,2][2,2,3]
[1,1,1][1,2,4][2,1,3][2,2,2]
[1,1,4][1,2,1][2,1,3][2,2,2]
The first rule
1{m(I,J,N) : v(N)}1 :- rows(I), col(J).
puts into each location of the matrix one v(N) but you do not have v(N) defined, you have v(N,M) defined.
The second rule
1{m(I,J,N) : rows(I), col(J)}1 :- v(N).
Puts each v(N) in exactly one row and column.
I suggest you replace v(X) by w(X) and define
w(N) :- v(N,_).
w(N) :- v(_,N).
That means you get all possible values form the v(X,Y) into w.
Related
I have the following input in Clingo:
val(a,2,3).
val(a,4,5).
val(b,0,6).
val(b,1,2).
the required is to have two predicates representing the first and second occurrence of each letter where the first one is the minimum number of the third index of val (case 1: only based on min of 3rd index), in case they are the same, then based on minimum number of the second index (case 2: both conditions).
Note that the maximum number of occurrences for each letter is 2.
The expected results is having two predicates with these values (only for this case, as sometimes the input is different):
first(a,2,3), first(b,1,2), second(a,4,5), second(b,0,6)
I tried the following code for case 1:
val(X,Y,W) :- val(X,Y,W).
first(X,Y,Z) :- val(X,Y,Z), not val(X,Y',Z'), Z'>Z,Y'!=Y.
second(X,Y,Z) :- val(X,Y,Z), not first(X,_,_).
but an error messages showed saying that Z' and Y' are unsafe.
Yes, Z' and Y' are unsafe because they do not appear in any positive predicate.
Moreover:
val(X,Y,W) :- val(X,Y,W). is a tautology and, therefore, it is useless;
it is not clear to me why you are imposing that Y'!=Y;
not first(X,_,_) is wrong, since it is checking that there doesn't exist ANY predicate first with variable X in the first argument (but this should always happen).
If I've correctly understood your requirements, this should be the correct implementation:
first(X,Y,Z) :- val(X,Y,Z), val(X,Y',Z'), Z'>Z . % case 1
first(X,Y,Z) :- val(X,Y,Z), val(X,Y',Z'), Z'=Z, Y'>Y . % case 2
second(X,Y,Z) :- val(X,Y,Z), not first(X,Y,Z) .
I'm new in prolog and I'm trying to write the predicate encode(L,L1) which counts the duplicates of elements in L ,for example:
encode([4,4,4,3,3],L).
L=[3,4,2,3].
This is what I have written:
encode(L,L1) :- encode(L,1,L1).
encode([],_,[]).
encode([H],N,[N,H]).
encode([H,H|T],N1,[N,H|T1]) :- M is N1+1, encode([H|T],M,[N,H|T1]).
encode([H,Y|T],N,[N,H|T1]) :- H\=Y, encode([Y|T],T1).
The above predicate is not reversible. It only works if the first parameter is provided.
How can I write encode reversible?
For example:
encode(L,[3,4,2,3]).
L = [4,4,4,3,3].
I think your algorithm has a redundant counter in it. A little simplified would be:
encoded([], []).
encoded([X], [1,X]).
encoded([X,Y|T], [1,X|R]) :-
dif(X, Y),
encoded([Y|T], R).
encoded([X,X|T], [N,X|R]) :-
N #> 1,
N #= N1 + 1,
encoded([X|T], [N1,X|R]).
Note in the last clause we need to ensure that N is greater than 1 as well.
I have the following code in DRL file
rule "MyExample"
when
$eentity : ExampleEntity()
$sentity : SecondEntity( secondField == $eentity.getMainField())
then
System.out.println(true);
end
This is works but I need to convert it to spreadsheet table. I tried so much variants but no one works. How can I write this rule in decision table?
Use a single condition column:
CONDITION
ExampleEntity($mf: mainField) SecondEntity
secondField == $mf /*param*/
Combine Ex with Sec on equal field values
x
The /*param*/ and the x is a hack to get the condition being generated for the row.
In Matlab, in order to change the value of a block I do
set_param('model/V','Amplitude','100')
and the value of V is 100. But if I do
for i=1:10
set_param('model/V','Amplitude','P(i)')
...
end
The it stores the value of V as P(i). But in order to access the i-th element of the 20-by-1 P matrix, I need to refer to it by P(i). What is my error?
Change the value to string using:-
set_param('model/V','Amplitude',num2str(P(i)) );
Also it will set the value of 'model/V' to P(20) i.e. last one.
You might want to loop through the current blocks too
Something like : (just example)
set_param(['model/V' num2str(i)],'Amplitude',num2str(P(i)) );
for model/V1, model/V2,...model/V20 .
I want to remove a (*) asterisk from my matrix and write out that matrix to a text file and the remaining elements will be concatenated to each other without a space or any kind of delimiters. I wrote this code
for b = 1 : length(out7num_r7_nt_back)
if ~(out7num_r7_nt_back(b) == '*')
out7num_r7_back(b) = '';
end
end
disp(out7num_r7_nt_back);
dlmwrite('my_data.txt',out7num_r7_nt_back, '');
I got this error message:
??? Index of element to remove exceeds matrix dimensions.
You can use a vectorized boolean index, replacing the loop as follows:
out7num_r7_nt_back = out7num_r7_nt_back(out7num_r7_nt_back(b) ~= '*');
That should be much faster as well.
Value of upper bound of for loop (length(out7num_r7_nt_back)) gets evaluated only once!
Say you have '*ab' in a variable. Loop will count to 3 (length of variable). Inside the loop when program erases '*', you'll get 'ab' which is of length 2. Since loop is iterating to 3, program will try to access out7num_r7_nt_back(3) which is out of bounds.
You could remove characters while going backwards:
...
for b = length(out7num_r7_nt_back) : -1 : 1
...
But you should prefer vectorized approach because it's faster and cleaner to write.