Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. %Operator definitions
  2. :- op(800, fx, if).
  3. :- op(700, yfx, then).
  4. :- op(200, yfx, and).
  5.  
  6. :- dynamic forecastDB/2.
  7.  
  8. % Rule 1
  9. if today(rain) then tomorrow(rain) : 0.5.
  10. if today(dry) then tomorrow(dry) : 0.5.
  11. if today(rain) and rainfall(low) then tomorrow(dry) : 0.6.
  12. if today(rain) and rainfall(low) and temperature(cold) then tomorrow(dry) : 0.7.
  13. if today(dry) and temperature(warm) then tomorrow(rain) : 0.65.
  14. if today(dry) and temperature(warm) and sky(overcast) then tomorrow(rain) : 0.55.
  15.  
  16. %fact(today(rain), 1.0).
  17. %fact(rainfall(low), 0.8).
  18. %fact(temperature(cold), 0.9).
  19. %fact(sky(overcast), 0.8).
  20.  
  21. is_true(Prämise, CF) :- fact(Prämise, CF).
  22.  
  23. min(Number1, Number2, Smaler) :-
  24. Number1 < Number2,
  25. Smaler is Number1.
  26. min(Number1, Number2, Smaler) :-
  27. Number1 > Number2,
  28. Smaler is Number2.
  29. min(Number1, Number2, Smaler) :-
  30. Number1 = Number2,
  31. Smaler is Number1.
  32.  
  33. composed_fact(Prämise, CF) :-
  34. is_true(Prämise, CF).
  35.  
  36. composed_fact(Prämise1 and Prämise2, CF) :-
  37. composed_fact(Prämise1, CFe1),
  38. composed_fact(Prämise2, CFe2),
  39. %is_true(Prämise1, CFe1),
  40. %is_true(Prämise2, CFe2).
  41. min(CFe1, CFe2, CF).
  42.  
  43. calculateTotalCF(CF1, CF2, CFt) :-
  44. CFt is CF1 + CF2*(1 - CF1).
  45.  
  46. new_forecast :-
  47. if X then Forecast : CFr,
  48. composed_fact(X, CFe),
  49. CF is CFe * CFr,
  50. lookup_forecast(Forecast, CF, N),
  51. assert(forecastDB(Forecast, N)).
  52.  
  53. lookup_forecast(Forecast, N, NewN) :-
  54. forecastDB(Forecast, OtherN),
  55. calculateTotalCF(N, OtherN, NewN),
  56. retract(forecastDB(Forecast, OtherN)).
  57.  
  58. lookup_forecast(Forecast, N, NewN) :-
  59. not(forecastDB(Forecast, _)),
  60. NewN is N.
  61.  
  62.  
  63.  
  64. % Main clause to process all rules and all cf-values
  65. process:-
  66. writeln('... processing rules ...'),
  67. retractall(forecastDB(_, _)),
  68. %new_forecast(X,Y),
  69. new_forecast,
  70. fail;true,
  71. print_DB.
  72.  
  73.  
  74. print_DB :-
  75. forecastDB(X, Y),
  76. write(X),write('\t: '),
  77. format('~4f',Y),
  78. writeln(''),fail;true.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement