prolog, a conditional like form
well i'm having a hard time working with prolog (gprolog). My problem is this, I've the next predicate:
prob(factA,factB,X,first):- X=<50.
prob(factA,factB,X,second):- X>50,X=<80.
prob(factA,factB,X,none):- X>80,X=<100.
that is factA have 50% of probability of occur, factB a 30%, and lastly 20% of n开发者_C百科on of the facts occur. Also i've others predicate where change the factB but not factA, also change the X intervals.
Now i have the functor:
result(X,Y,N):- random(1,100,I),
prob(X,Y,I,N).
this predicate tells what is N for X an Y in prob. This predicate handle the case:
result(factA,factB,N).
but now i have to handle:
result(factB,factA,N).
and it will return a valid result, any idea?, i did something like this:
result(X,Y,N):- Y = factA,
random(1,100,I),
prob(Y,X,I,N).
J is 100-I,
prob(Y,X,J,N).
but this doesn't manage well the 'none' result.
Somebody has any idea?
Thanks!
Looking at my comment above: if first
is 50% (and with some optimisation if you "know" random value will be between 1 and 100):
prob(X,first):- X=<50, !.
prob(X,second):- X=<80, !.
prob(X,none).
I guess this is not what you meant since it does not depend on facts at all, so they're not needed as arguments.
Thus, assuming factA
is 50%, a bit more complex but basically the same idea as yours:
fact_prob(factA, 50).
fact_prob(factB, 30).
random_fact(X, _, I, R) :-
fact_prob(X, XP),
I <= XP, !,
R = first.
random_fact(X, Y, I, R) :-
fact_prob(X, XP),
fact_prob(Y, YYP),
XYP is XP + YP,
I <= XYP, !,
R = second.
random_fact(_, _, _, none).
Disclaimer: I never worked with gprolog, you might need to adjust details to your dialect.
精彩评论