1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00

I hope that's final shape of universal Expert System, now it's time to add some functionality.

This commit is contained in:
DjWarmonger 2010-11-13 19:25:21 +00:00
parent e2f4a60c4b
commit 67baa3d8e4
2 changed files with 178 additions and 23 deletions

View File

@ -12,15 +12,113 @@
* *
*/ */
template <typename ruleType, typename facts> void ExpertSystemShell<ruleType, facts>::DataDrivenReasoning(runType type) template <typename ruleType, typename facts> template <typename cond> void ExpertSystemShell<ruleType, facts>::DataDrivenReasoning(runType type)
{ {
std::set<ruleType>::iterator ir;
std::list<fact>::iterator iF;
std::set<std::pair<cond, input*>>::iterator ic;
bool factWasAdded = false; //carry it over inner loop
switch (type) switch (type)
{ {
case ANY_GOAL: //first produced decision ends reasoning case ANY_GOAL: //first produced decision ends reasoning
{ {
for (std::list<typename facts>::iterator it = factList.begin(); it != factList.end(); it++) int goalCounter = 0;
{}; while(!goalCounter) //we reach goal or can't modify knowledge anymore
{
for (ir = knowledge.begin(); ir != knowledge.end(); ir++)
{
for (iF = factList.begin(); iF != factList.end(); iF++)
{
for (ic = ir->conditions.begin(); ic != ir->conditions.end(), ic++)
{
if (ic->first.object.matchesFact(iF->object)) //condition matches held object
{
(ic->second = *iF);
++(ir->conditionCounter);
}
}
if (ir->conditions.size() >= ir->conditionCounter());
{
ir->fireRule(); //all conditions matched
ir->conditionCounter = 0; //do it here or in rule itself?
if (dynamic_cast<&Goal>(*ir))
++goalCounter;
}
}
matchedConditions = 0;
//modify set until something happens (hopefully!)
for (iF = factsToErase.begin(); iF != factsToErase.end(); iF++)
factList.erase(knowledge.find(*iF));
factsToErase.clear(); //TODO: what if fact is remembered by rule, yet already erased?
for (iF = factsToAdd.begin(); iF != factsToAdd.end(); iF++)
factList.insert(*iF);
if (factsToAdd.size())
{
factsToAdd.clear();
factWasAdded = true;
}
}
for (ir = rulesToErase.begin(); ir != rulesToErase.end(); ir++)
knowledge.erase(knowledge.find(*ir));
rulesToErase.clear();
for (ir = rulesToAdd.begin(); ir != rulesToAdd.end(); ir++)
knowledge.insert(*ir);
if (!(factWasAdded || rulesToAdd.size())) //we can't do anything more
break;
rulesToAdd.clear();
};
} }
break; break;
} }
} }
void BonusRule::fireRule()
{
for (std::set<std::pair<BonusCondition, BonusHolder*>>::iterator it = cons.begin(); it != cons.end(); it++)
{
switch (it->first.parameter)
{ //compare fact with condition
case BonusCondition::type:
if (!it->first.functor(it->second->object->type, it->first.value)) return;
break;
case BonusCondition::subtype: //probably suprfluous, Selector already handles that
if (!it->first.functor(it->second->object->subtype, it->first.value)) return;
break;
case BonusCondition::val:
if (!it->first.functor(it->second->object->val, it->first.value)) return;
break;
case BonusCondition::duration:
if (!it->first.functor(it->second->object->duration, it->first.value)) return;
break;
case BonusCondition::source:
if (!it->first.functor(it->second->object->source, it->first.value)) return;
break;
case BonusCondition::id:
if (!it->first.functor(it->second->object->id, it->first.value)) return;
break;
case BonusCondition::valType:
if (!it->first.functor(it->second->object->valType, it->first.value)) return;
break;
case BonusCondition::additionalInfo:
if (!it->first.functor(it->second->object->additionalInfo, it->first.value)) return;
break;
case BonusCondition::effectRange:
if (!it->first.functor(it->second->object->effectRange, it->first.value)) return;
break;
default: //ignore or accept?
break;
};
}
//TODO: add new fact or modify existing one
}
//template <typename input, typename conType> void Rule<input, conType>::refreshRule(std::set<conType> &conditionSet)
//{
// cons.clear();
// for (std::set<conType>::iterator it = conditionSet.begin(); it != conditionSet.end(); it++)
// cons.insert(std::make_pair<conType,input*>(*it, NULL)); //pointer to condition and null fact
//}
bool BonusCondition::matchesFact(Bonus &fact)
{
if (object(fact)) //Bonus(fact) matches local Selector(object)
return true;
return false;
}

View File

@ -14,54 +14,72 @@
* *
*/ */
struct Bonus; struct Bonus;
template <typename fact> class AIholder;
template <typename input, typename output> class Rule; template <typename input, typename output> class Rule;
typedef Rule<Bonus, Bonus> BRule; typedef Rule<Bonus, Bonus> BRule;
bool greaterThan (int prop, si32 val);
template <typename ruleType, typename facts> class ExpertSystemShell enum conditionType {LESS_THAN, EQUAL, GREATER_THAN, UNEQUAL, PRESENT};
template <typename ruleType, typename fact> class ExpertSystemShell
{ {
enum runType {ANY_GOAL, TRESHOLD, FULL}; enum runType {ANY_GOAL, TRESHOLD, FULL};
private: private:
ICallback* m_cb; ICallback* m_cb;
protected: protected:
std::set<ruleType> knowledge; std::set<ruleType> knowledge;
std::set<ruleType*> rulesToErase;
std::set<ruleType*> rulesToAdd;
std::set<fact*> factsToErase;
std::set<fact*> factsToAdd;
ui16 goalCounter; //count / evaluate achieved goals for runType ui16 goalCounter; //count / evaluate achieved goals for runType
public: public:
ExpertSystemShell(){goalCounter = 0;}; ExpertSystemShell(){goalCounter = 0;};
std::list<facts> factList; std::list<fact> facts; //facts are AIholders with actual game objects, for eg. "my bonus is worthless"
template <typename t1> void getKnowledge(const t1 &a1){}; template <typename t1> void getKnowledge(const t1 &a1){};
template <typename ruleType> void getNextRule(){}; void getNextRule(){};
void addRule(ruleType &rule){rulesToAdd.insert(rule);};
void removeRule(ruleType &rule){rulesToErase.insert(rule);};
template <typename t2> void returnGoals(const t2 &a2){}; template <typename t2> void returnGoals(const t2 &a2){};
virtual void DataDrivenReasoning(runType type); template <typename cond> void DataDrivenReasoning(runType type);
}; };
template <typename ruleType, typename facts> class Blackboard : public ExpertSystemShell <ruleType, facts> template <typename ruleType, typename fact> class Blackboard : public ExpertSystemShell <ruleType, fact>
//handle Bonus info coming from different sections of the game //handle Bonus info coming from different sections of the game
{ {
public: public:
Blackboard(){this->goalCounter = 0;}; Blackboard(){this->goalCounter = 0;}; //template magic, do not touch!
std::vector<ExpertSystemShell*> experts; //modules responsible for different tasks std::vector<ExpertSystemShell*> experts; //modules responsible for different tasks
}; };
template <typename input> class condition template <typename input> class condition
{ {//determines selected object parameter with value using functor
enum conditionType {LESS_THAN, EQUAL, GREATER_THAN, UNEQUAL, PRESENT};
public: public:
input object; input object; //what the fact is, or what it's like (CSelector)
ui16 value; si32 value;
conditionType conType; ui8 parameter;
boost::function<bool(int,si32)> functor; //value of selected parameter, condition value
condition(){object = NULL; value = 0; parameter = 0; functor = greaterThan;};
bool matchesFact(input &fact){return false;};
}; };
template <typename input, typename output> class Rule template <typename input, typename conType> class Rule
{ {
friend class ExpertSystemShell <input, output>; friend class ExpertSystemShell <input, conType>;
public: public:
bool fired; //if conditions of rule were met and it produces some output bool fired; //if conditions of rule were met and it produces some output
ui8 conditionCounter;
protected: protected:
std::set<input*> conditions; std::set<std::pair<conType, input*>> cons; //conditions and matching facts
output decision; input decision;
virtual void canBeFired(); //if this data makes any sense for rule virtual void canBeFired(); //if this data makes any sense for rule
virtual void fireRule(); virtual void fireRule(std::set<input*> &feed);
virtual void fireRule(); //use paired conditions and facts by default
virtual void refreshRule(std::set<conType> &conditionSet); //in case conditions were erased
public: public:
Rule(){fired = false; conditionCounter = 0; decision = NULL;};
template <typename givenInput> bool matchesInput() //if condition and data match type template <typename givenInput> bool matchesInput() //if condition and data match type
{return dynamic_cast<input*>(&givenInput);}; {return dynamic_cast<input*>(&givenInput);};
}; };
@ -77,11 +95,50 @@ public:
template <typename input, typename output> class Weight : public Rule <input, output> template <typename input, typename output> class Weight : public Rule <input, output>
{ {
public: public:
float value; //multiply input by value and return to output float multiplier; //multiply input by value and return to output
void fireTule(){}; void fireTule(){};
}; };
template <typename input> class AIholder
{ //stores certain condition and its temporal value
public:
si32 aiValue;
input *object;
AIholder(){object = NULL; aiValue = 0;}
AIholder(input &o){object = o; aiValue = 0;}
AIholder(input &o, si32 val){object = 0; aiValue = val;}
};
class BonusSystemExpert : public ExpertSystemShell <BRule, Bonus> class BonusSystemExpert : public ExpertSystemShell <BRule, Bonus>
{ //TODO: less templates? { //TODO: less templates?
enum effectType {POSITIVE=1, NEGATIVE=2, EXCLUDING=4, ENEMY=8, ALLY=16}; //what is the influencce of bonus and for who enum effectType {POSITIVE=1, NEGATIVE=2, EXCLUDING=4, ENEMY=8, ALLY=16}; //what is the influence of bonus and for who
}; };
class BonusCondition : public condition<CSelector> //used to test rule
{
public:
enum Parameter
{
type, subtype, val, duration, source, id, valType, additionalInfo, effectRange, limiter //?
};
bool matchesFact(Bonus &fact);
};
class BonusHolder : public AIholder<Bonus>
{
public:
BonusHolder(Bonus &bonus){object = &bonus; aiValue = bonus.val;}
BonusHolder(Bonus &bonus, si32 val){object = &bonus; aiValue = val;}
};
class BonusRule : public Rule <BonusHolder, BonusCondition>
{
protected:
void fireRule();
};
bool greaterThan (int prop, si32 val)
{
if ((si32)prop > val)
return true;
return false;
}