Prolog - aggregate : extract other variable than the minimum - prolog

I would like to know if there is a way to extract the X Value when I'm doing this :
aggregate_all(min(V), simulate(P, Color, V, X), Value)
The simulate predicate is used with P and Color as inputs and V and X as outputs.
For now, this works well to get the min value of V, but what I actually want is to get the value of X when V is at its minimum.
Is there a way to do that ? Any idea about how should I proceed ?

aggregate library supports a 'Witness' on min/max scalar aggregates: then this should work
aggregate_all(min(V,X), simulate(P, Color, V, X), min(Value,X))

Related

How can I remove "for loop iteration" by using torch tensor operator?

I want to get rid of "for loop iteration" by using Pytorch functions in my code. But the formula is complicated and I can't find a clue. Can the "for loop iteration" in the below replaced with the Torch operation?
B=10
L=20
H=5
mat_A=torch.randn(B,L,L,H)
mat_B=torch.randn(L,B,B,H)
tmp_B=torch.zeros_like(mat_B)
for x in range(L):
for y in range(B):
for z in range(B):
tmp_B[:,y,z,:]+=mat_B[x,y,z,:]*mat_A[z,x,:,:]
This looks like a good setup for applying torch.einsum. However, we first need to explicit the : placeholders by defining each individual accumulation term.
In order to do so, consider the shape of your intermediate tensor results. The first, mat_B[x,y,z] is shaped (H,), while the second mat_A[z,x,] is shaped (L, H).
In pseudo-code your initial operation is as follows:
for x, y, z, l, h in LxBxBxLxH:
tmp_B[:,y,z,:] += mat_B[x,y,z,:]*mat_A[z,x,:,:]
Knowing this, we can reformulate your initial loop in pseudo-code as:
for x, y, z, l, h in LxBxBxLxH:
tmp_B[l,y,z,h] += mat_B[x,y,z,h]*mat_A[z,x,l,h]
Therefore, we can apply torch.einsum by using the same notation as above:
>>> torch.einsum('xyzh,zxlh->lyzh', mat_B, mat_A)

Is there a way to create a type 3 grammar for this language?

I'm trying to find a grammar of the highest type possible for this language:
L={0^2n 1^(n-1)|n>=1}
I only managed to do this:
S->00X
X->00X1|λ
Which is not type 3. I can't seem to figure out how to get it to type 3 (if that's even possible).
You can't do it, because L is not a regular language.
Assume that L is regular. Let w = 0^(2p)1^(p-1) for some integer p>=1, so that |w| > p. Further, consider the strings x, y, and z such that w = xyz with |xy| <= p, which means both x and y are sequences of 0s (since p < 2p). By the pumping lemma, any string of the form xy^nz is also in L, but that means we can increase the number of 0s without increasing the number of 1s found in z. Thus, our assumption that L is regular must be false.

How to set mutually exclusive probabilities in Problog?

A person X can either be inpatient or outpatient.
Given the fact location(X,outpatient) how can Problog infer that the probability of location(X,inpatient) is 0?
For example I want a side effect of:
person(1).
location(1,inpatient).
dependent(1,opioids).
receive(1,clonidine).
query(detoxification(1,opioids,success)).
to be an inference that location(1,outpatient) has zero probability.
If I write location(X,outpatient);location(X,inpatient)., all queries return both with a probability of 1.
If I write P::location(X,outpatient);(1-P)::location(X,inpatient). that gives an error because I haven't specified a value for P. If I specify a value for P, that value is never updated (as expected because Prolog treats variables as algebraic variables and I haven't told Problog to update P.
If I write location(X,outpatient) :- \+ location(X,inpatient). I create a negative cycle, which I have to if I am to specify the inverse goal.
One solution:
P::property(X,location,inpatient);(1-P)::property(X,location, outpatient) :-
inpatient(X),
P is 1.
P::property(X,location,outpatient);(1-P)::property(X,location, inpatient) :-
outpatient(X),
P is 1.
P::inpatient(X);(1-P)::outpatient(X) :-
property(X,location,inpatient),
P is 1.
P::outpatient(X);(1-P)::inpatient(X) :-
property(X,location,outpatient),
P is 1.
This binds inpatient/1 to property/3 for the property of location with value inpatient.

Use lutyuv to calculate u component from y component

In FFMpeg I use as filter
lutyuv="y=0.000018712*val*val+0.0027334*val+1.3141:u=gammaval(1.09)"
This works well. Now I want to calculate u value as a function of calculated y, something like u=1.1*previous_calculated_y*val. How can I access previously calculated y value in formula for u? Or how can I access y information in formula for u?
Example: If y value was zero I would like to get for u = 1.3141*val.
I googled and found nothing.
lutyuv="y=0.000018712*val*val+0.0027334*val+1.3141:u=gammaval(1.09)"

Comparing the value of a variable prolog

Is it possible to compare a variable to an atom?
Is it possible to compare the contents of a variable with an atom as shown below?
Y = [[cirlce], nextTo [square]].
nth0(1, Y, Op),Op == nextTo, doSomething(circle, square).
This can easily be tested as below:
Y = [[cirlce], nextTo, [square]],
nth0(1, Y, nextTo), doSomething(circle, square).
Matching "nextTo" with the element found at index 1 by nth0/3 and will yield true

Resources