mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-24 22:14:36 +02:00
Create fuzzy logic map object evaluation fundamentals
This commit is contained in:
parent
27541a72f5
commit
df00ced204
@ -19,6 +19,7 @@
|
||||
#include "../../lib/VCMI_Lib.h"
|
||||
#include "../../CCallback.h"
|
||||
#include "VCAI.h"
|
||||
#include "MapObjectsEvaluator.h"
|
||||
|
||||
#define MIN_AI_STRENGHT (0.5f) //lower when combat AI gets smarter
|
||||
#define UNGUARDED_OBJECT (100.0f) //we consider unguarded objects 100 times weaker than us
|
||||
@ -97,6 +98,8 @@ FuzzyHelper::FuzzyHelper()
|
||||
ta.configure();
|
||||
initVisitTile();
|
||||
vt.configure();
|
||||
initWanderTarget();
|
||||
wanderTarget.configure();
|
||||
}
|
||||
|
||||
|
||||
@ -286,6 +289,38 @@ float FuzzyHelper::getTacticalAdvantage(const CArmedInstance * we, const CArmedI
|
||||
return output;
|
||||
}
|
||||
|
||||
float FuzzyHelper::getWanderTargetObjectValue(const CGHeroInstance & h, const ObjectIdRef & obj)
|
||||
{
|
||||
float distFromObject = calculateTurnDistanceInputValue(&h, obj->pos);
|
||||
boost::optional<int> objValueKnownByAI = MapObjectsEvaluator::getInstance().getObjectValue(obj->ID, obj->subID);
|
||||
int objValue = 0;
|
||||
|
||||
if(objValueKnownByAI.is_initialized())
|
||||
{
|
||||
objValue = objValueKnownByAI.value();
|
||||
}
|
||||
else
|
||||
{
|
||||
MapObjectsEvaluator::getInstance().addObjectData(obj->ID, obj->subID, 0);
|
||||
logGlobal->warn("AI met object type it doesn't know - ID: " + std::to_string(obj->ID) + ", subID: " + std::to_string(obj->subID) + " - adding to database with value " + std::to_string(objValue));
|
||||
}
|
||||
|
||||
float output = -1.0f;
|
||||
try
|
||||
{
|
||||
wanderTarget.distance->setValue(distFromObject);
|
||||
wanderTarget.objectValue->setValue(objValue);
|
||||
wanderTarget.engine.process();
|
||||
output = wanderTarget.visitGain->getValue();
|
||||
}
|
||||
catch (fl::Exception & fe)
|
||||
{
|
||||
logAi->error("evaluate getWanderTargetObjectValue: %s", fe.getWhat());
|
||||
}
|
||||
assert(output >= 0.0f);
|
||||
return output;
|
||||
}
|
||||
|
||||
FuzzyHelper::TacticalAdvantage::~TacticalAdvantage()
|
||||
{
|
||||
//TODO: smart pointers?
|
||||
@ -427,6 +462,55 @@ void FuzzyHelper::initVisitTile()
|
||||
}
|
||||
}
|
||||
|
||||
void FuzzyHelper::initWanderTarget()
|
||||
{
|
||||
try
|
||||
{
|
||||
wanderTarget.distance = new fl::InputVariable("distance"); //distance on map from object
|
||||
wanderTarget.objectValue = new fl::InputVariable("objectValue"); //value of that object type known by AI
|
||||
wanderTarget.visitGain = new fl::OutputVariable("visitGain");
|
||||
wanderTarget.visitGain->setMinimum(0);
|
||||
wanderTarget.visitGain->setMaximum(10);
|
||||
|
||||
wanderTarget.engine.addInputVariable(wanderTarget.distance);
|
||||
wanderTarget.engine.addInputVariable(wanderTarget.objectValue);
|
||||
wanderTarget.engine.addOutputVariable(wanderTarget.visitGain);
|
||||
|
||||
//for now distance variable same as in as VisitTile
|
||||
wanderTarget.distance->addTerm(new fl::Ramp("SHORT", 0.5, 0));
|
||||
wanderTarget.distance->addTerm(new fl::Triangle("MEDIUM", 0.1, 0.8));
|
||||
wanderTarget.distance->addTerm(new fl::Ramp("LONG", 0.5, 3));
|
||||
wanderTarget.distance->setRange(0, 3.0);
|
||||
|
||||
//objectValue ranges are based on checking RMG priorities of some objects and trying to guess sane value ranges
|
||||
wanderTarget.objectValue->addTerm(new fl::Ramp("LOW", 3000, 0)); //I have feeling that concave shape might work well instead of ramp for objectValue FL terms
|
||||
wanderTarget.objectValue->addTerm(new fl::Triangle("MEDIUM", 2500, 6000));
|
||||
wanderTarget.objectValue->addTerm(new fl::Ramp("HIGH", 5000, 20000));
|
||||
wanderTarget.objectValue->setRange(0, 20000); //relic artifact value is border value by design, even better things are scaled down.
|
||||
|
||||
wanderTarget.visitGain->addTerm(new fl::Ramp("LOW", 5, 0));
|
||||
wanderTarget.visitGain->addTerm(new fl::Triangle("MEDIUM", 4, 6));
|
||||
wanderTarget.visitGain->addTerm(new fl::Ramp("LOW", 5, 10));
|
||||
wanderTarget.visitGain->setRange(0, 10);
|
||||
|
||||
wanderTarget.addRule("if distance is LONG and objectValue is HIGH then visitGain is MEDIUM");
|
||||
wanderTarget.addRule("if distance is MEDIUM and objectValue is HIGH then visitGain is somewhat HIGH");
|
||||
wanderTarget.addRule("if distance is SHORT and objectValue is HIGH then visitGain is HIGH");
|
||||
|
||||
wanderTarget.addRule("if distance is LONG and objectValue is MEDIUM then visitGain is somewhat LOW");
|
||||
wanderTarget.addRule("if distance is MEDIUM and objectValue is MEDIUM then visitGain is MEDIUM");
|
||||
wanderTarget.addRule("if distance is SHORT and objectValue is MEDIUM then visitGain is somewhat HIGH");
|
||||
|
||||
wanderTarget.addRule("if distance is LONG and objectValue is LOW then visitGain is very LOW");
|
||||
wanderTarget.addRule("if distance is MEDIUM and objectValue is LOW then visitGain is LOW");
|
||||
wanderTarget.addRule("if distance is SHORT and objectValue is LOW then visitGain is MEDIUM");
|
||||
}
|
||||
catch(fl::Exception & fe)
|
||||
{
|
||||
logAi->error("FindWanderTarget: %s", fe.getWhat());
|
||||
}
|
||||
}
|
||||
|
||||
float FuzzyHelper::evaluate(Goals::VisitTile & g)
|
||||
{
|
||||
//we assume that hero is already set and we want to choose most suitable one for the mission
|
||||
@ -546,3 +630,10 @@ void FuzzyHelper::setPriority(Goals::TSubgoal & g)
|
||||
{
|
||||
g->setpriority(g->accept(this)); //this enforces returned value is set
|
||||
}
|
||||
|
||||
FuzzyHelper::EvalWanderTargetObject::~EvalWanderTargetObject()
|
||||
{
|
||||
delete distance;
|
||||
delete objectValue;
|
||||
delete visitGain;
|
||||
}
|
||||
|
@ -55,6 +55,14 @@ class FuzzyHelper
|
||||
~EvalVisitTile();
|
||||
} vt;
|
||||
|
||||
class EvalWanderTargetObject : public engineBase //designed for use with VCAI::wander()
|
||||
{
|
||||
public:
|
||||
fl::InputVariable * distance;
|
||||
fl::InputVariable * objectValue;
|
||||
fl::OutputVariable * visitGain;
|
||||
~EvalWanderTargetObject();
|
||||
} wanderTarget;
|
||||
|
||||
private:
|
||||
float calculateTurnDistanceInputValue(const CGHeroInstance * h, int3 tile) const;
|
||||
@ -66,6 +74,7 @@ public:
|
||||
FuzzyHelper();
|
||||
void initTacticalAdvantage();
|
||||
void initVisitTile();
|
||||
void initWanderTarget();
|
||||
|
||||
float evaluate(Goals::Explore & g);
|
||||
float evaluate(Goals::RecruitHero & g);
|
||||
@ -83,6 +92,7 @@ public:
|
||||
|
||||
ui64 estimateBankDanger(const CBank * bank);
|
||||
float getTacticalAdvantage(const CArmedInstance * we, const CArmedInstance * enemy); //returns factor how many times enemy is stronger than us
|
||||
float getWanderTargetObjectValue(const CGHeroInstance & h, const ObjectIdRef & obj);
|
||||
|
||||
Goals::TSubgoal chooseSolution(Goals::TGoalVec vec);
|
||||
//std::shared_ptr<AbstractGoal> chooseSolution (std::vector<std::shared_ptr<AbstractGoal>> & vec);
|
||||
|
@ -19,7 +19,7 @@ MapObjectsEvaluator::MapObjectsEvaluator()
|
||||
for(auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
|
||||
{
|
||||
auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
|
||||
if(!handler->isStaticObject() && handler->getRMGInfo().value)
|
||||
if(!handler->isStaticObject())
|
||||
{
|
||||
objectDatabase[CompoundMapObjectID(primaryID, secondaryID)] = handler->getRMGInfo().value;
|
||||
}
|
||||
|
@ -1540,7 +1540,12 @@ void VCAI::wander(HeroPtr h)
|
||||
|
||||
if(dests.size()) //performance improvement
|
||||
{
|
||||
const ObjectIdRef & dest = *boost::min_element(dests, CDistanceSorter(h.get())); //find next closest one
|
||||
auto fuzzyLogicSorter = [h](const ObjectIdRef & l, const ObjectIdRef & r) -> bool
|
||||
{
|
||||
return fh->getWanderTargetObjectValue( *h.get(), l) < fh->getWanderTargetObjectValue(*h.get(), r);
|
||||
};
|
||||
|
||||
const ObjectIdRef & dest = *boost::max_element(dests, fuzzyLogicSorter); //find best object to visit based on fuzzy logic evaluation
|
||||
|
||||
//wander should not cause heroes to be reserved - they are always considered free
|
||||
logAi->debug("Of all %d destinations, object oid=%d seems nice", dests.size(), dest.id.getNum());
|
||||
|
Loading…
Reference in New Issue
Block a user