ExpertSystem.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "../vcmi/global.h"
  2. #include "../vcmi/CCallback.h"
  3. #include "../vcmi/lib/HeroBonus.h"
  4. #include <boost/bind.hpp>
  5. #include <vector>
  6. /*
  7. * ExpertSystem.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. struct Bonus;
  16. template <typename fact> class AIholder;
  17. template <typename input, typename output> class Rule;
  18. typedef Rule<Bonus, Bonus> BRule;
  19. typedef boost::function<bool(int, si32)> TLogic;
  20. bool greaterThan (int prop, si32 val);
  21. enum conditionType {LESS_THAN, EQUAL, GREATER_THAN, UNEQUAL, PRESENT};
  22. template <typename ruleType, typename fact> class ExpertSystemShell
  23. {
  24. enum runType {ANY_GOAL, TRESHOLD, FULL}; //Treshold - stop when received decision has high AI value
  25. private:
  26. ICallback* m_cb;
  27. protected:
  28. std::vector<ruleType> knowledge;
  29. std::vector<ruleType*> rulesToErase;
  30. std::vector<ruleType*> rulesToAdd;
  31. std::vector<fact*> factsToErase;
  32. std::vector<fact*> factsToAdd;
  33. ui16 goalCounter; //count / evaluate achieved goals for runType
  34. public:
  35. ExpertSystemShell(){goalCounter = 0;};
  36. std::list<fact> facts; //facts are AIholders with actual game objects, for eg. "my bonus is worthless"
  37. template <typename t1> void getKnowledge(const t1 &a1){};
  38. void getNextRule(){};
  39. void addRule(ruleType &rule){rulesToAdd.insert(rule);};
  40. void removeRule(ruleType &rule){rulesToErase.insert(rule);};
  41. template <typename t2> void returnGoals(const t2 &a2){};
  42. template <typename cond> void DataDrivenReasoning(runType type);
  43. };
  44. template <typename ruleType, typename fact> class Blackboard : public ExpertSystemShell <ruleType, fact>
  45. //handle Bonus info coming from different sections of the game
  46. {
  47. public:
  48. Blackboard(){this->goalCounter = 0;}; //template magic, do not touch!
  49. std::vector<ExpertSystemShell*> experts; //modules responsible for different tasks
  50. };
  51. template <typename input> class condition
  52. {//compares selected object parameter with value using functor. universal (?) logic handler
  53. public:
  54. input object; //what the fact is, or what it's like (CSelector)
  55. si32 value;
  56. ui8 parameter;
  57. TLogic functor; //value of selected parameter, condition value
  58. condition(){object = NULL; value = 0; parameter = 0; functor = greaterThan;};
  59. bool matchesFact(input &fact){return false;};
  60. };
  61. template <typename input, typename conType> class Rule
  62. {
  63. friend class ExpertSystemShell <input, conType>;
  64. public:
  65. bool fired; //if conditions of rule were met and it produces some output
  66. ui8 conditionCounter;
  67. protected:
  68. std::vector<std::pair<conType, input*>> cons; //conditions and matching facts
  69. input decision;
  70. virtual void canBeFired(); //if this data makes any sense for rule - type check
  71. virtual bool checkCondition(); //if condition is true or false
  72. virtual bool checkCondition(std::vector<input*> &feed);
  73. virtual void fireRule(); //use paired conditions and facts by default
  74. virtual void fireRule(ExpertSystemShell<input, conType> &system);
  75. virtual void fireRule(std::vector<input*> &feed);
  76. virtual void refreshRule();
  77. virtual void refreshRule(std::vector<conType> &conditionSet); //in case conditions were erased
  78. public:
  79. Rule(){fired = false; conditionCounter = 0; decision = NULL;};
  80. template <typename givenInput> bool matchesInput() //if condition and data match type
  81. {return dynamic_cast<input*>(&givenInput);};
  82. };
  83. template <typename input, typename output> class Goal : public Rule <input, output>
  84. {
  85. protected:
  86. boost::function<void(output)> decision(); //do something with AI eventually
  87. public:
  88. void fireRule(){};
  89. };
  90. template <typename input, typename output> class Weight : public Rule <input, output>
  91. {
  92. public:
  93. float multiplier; //multiply input by value and return to output
  94. void fireTule(){};
  95. };
  96. template <typename input> class AIholder
  97. { //stores certain condition and its temporal value
  98. public:
  99. si32 aiValue;
  100. input *object;
  101. AIholder(){object = NULL; aiValue = 0;}
  102. AIholder(input &o){object = o; aiValue = 0;}
  103. AIholder(input &o, si32 val){object = 0; aiValue = val;}
  104. };
  105. class BonusSystemExpert : public ExpertSystemShell <BRule, Bonus>
  106. { //TODO: less templates?
  107. enum effectType {POSITIVE=1, NEGATIVE=2, EXCLUDING=4, ENEMY=8, ALLY=16}; //what is the influence of bonus and for who
  108. };
  109. class BonusCondition : public condition<CSelector> //used to test rule
  110. {
  111. public:
  112. enum Parameter
  113. {
  114. type, subtype, val, duration, source, id, valType, additionalInfo, effectRange, limiter //?
  115. };
  116. bool matchesFact(Bonus &fact);
  117. };
  118. class BonusHolder : public AIholder<Bonus>
  119. {
  120. public:
  121. BonusHolder(Bonus &bonus){object = &bonus; aiValue = bonus.val;}
  122. BonusHolder(Bonus &bonus, si32 val){object = &bonus; aiValue = val;}
  123. };
  124. class BonusRule : public Rule <BonusHolder, BonusCondition>
  125. {
  126. protected:
  127. void fireRule();
  128. };
  129. inline bool greaterThan (int prop, si32 val) //does it make any sense to keep functors inline?
  130. {
  131. if ((si32)prop > val)
  132. return true;
  133. return false;
  134. }
  135. inline bool lessThan (int prop, si32 val)
  136. {
  137. if ((si32)prop < val)
  138. return true;
  139. return false;
  140. }
  141. inline bool eqal (int prop, si32 val)
  142. {
  143. if ((si32)prop == val)
  144. return true;
  145. return false;
  146. }
  147. inline bool unequal (int prop, si32 val)
  148. {
  149. if ((si32)prop != val)
  150. return true;
  151. return false;
  152. }
  153. inline bool present (int prop, si32 val=0)
  154. //inline bool present (int prop) //TODO: can we use function with less arguments?
  155. {
  156. return(prop); //unfixable warning :(
  157. }
  158. class LogicConjunction
  159. {
  160. const TLogic first, second; //TODO: universal argument list of functions?
  161. public:
  162. LogicConjunction(const TLogic First, const TLogic Second)
  163. :first(First), second(Second)
  164. {
  165. }
  166. bool operator()(int prop, si32 val) const
  167. {
  168. return first(prop,val) && second(prop,val);
  169. }
  170. };
  171. TLogic operator&&(const TLogic &first, const TLogic &second);
  172. class LogicAlternative
  173. {
  174. const TLogic first, second;
  175. public:
  176. LogicAlternative(const TLogic First, const TLogic Second)
  177. :first(First), second(Second)
  178. {
  179. }
  180. bool operator()(int prop, si32 val) const
  181. {
  182. return first(prop,val) || second(prop,val);
  183. }
  184. };
  185. TLogic operator||(const TLogic &first, const TLogic &second);
  186. class KnowledgeHandler///I'd opt for one omniscent knowledge manager, so no templates here
  187. {
  188. public:
  189. std::list<BonusRule> knowledge; //permanent storage of rules
  190. void parseKnowledge(std::string &filename){};
  191. void addKnowledge(ExpertSystemShell<BRule,Bonus> &expert);
  192. void addFacts(ExpertSystemShell<BRule,Bonus> &expert);
  193. };