mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-24 22:14:36 +02:00
Split Fuzzy.cpp/h
This commit is contained in:
parent
bd3d27c79b
commit
72b206347f
@ -10,7 +10,7 @@
|
||||
#include "StdInc.h"
|
||||
#include "AIUtility.h"
|
||||
#include "VCAI.h"
|
||||
#include "Fuzzy.h"
|
||||
#include "FuzzyHelper.h"
|
||||
|
||||
#include "../../lib/UnlockGuard.h"
|
||||
#include "../../lib/CConfigHandler.h"
|
||||
|
@ -15,7 +15,8 @@ set(VCAI_SRCS
|
||||
SectorMap.cpp
|
||||
BuildingManager.cpp
|
||||
MapObjectsEvaluator.cpp
|
||||
Fuzzy.cpp
|
||||
FuzzyEngines.cpp
|
||||
FuzzyHelper.cpp
|
||||
Goals.cpp
|
||||
main.cpp
|
||||
VCAI.cpp
|
||||
@ -31,7 +32,8 @@ set(VCAI_HEADERS
|
||||
SectorMap.h
|
||||
BuildingManager.h
|
||||
MapObjectsEvaluator.h
|
||||
Fuzzy.h
|
||||
FuzzyEngines.h
|
||||
FuzzyHelper.h
|
||||
Goals.h
|
||||
VCAI.h
|
||||
)
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,108 +1,74 @@
|
||||
/*
|
||||
* Fuzzy.h, part of VCMI engine
|
||||
*
|
||||
* Authors: listed in file AUTHORS in main folder
|
||||
*
|
||||
* License: GNU General Public License v2.0 or later
|
||||
* Full text of license available in license.txt file, in main folder
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
#include "fl/Headers.h"
|
||||
#include "Goals.h"
|
||||
|
||||
class VCAI;
|
||||
class CArmedInstance;
|
||||
class CBank;
|
||||
struct SectorMap;
|
||||
|
||||
class engineBase //subclasses create fuzzylite variables with "new" that are not freed - this is desired as fl::Engine wants to destroy these...
|
||||
{
|
||||
protected:
|
||||
fl::Engine engine;
|
||||
fl::RuleBlock rules;
|
||||
virtual void configure();
|
||||
void addRule(const std::string & txt);
|
||||
public:
|
||||
engineBase();
|
||||
};
|
||||
|
||||
class TacticalAdvantageEngine : public engineBase
|
||||
{
|
||||
public:
|
||||
TacticalAdvantageEngine();
|
||||
float getTacticalAdvantage(const CArmedInstance * we, const CArmedInstance * enemy); //returns factor how many times enemy is stronger than us
|
||||
private:
|
||||
fl::InputVariable * ourWalkers, *ourShooters, *ourFlyers, *enemyWalkers, *enemyShooters, *enemyFlyers;
|
||||
fl::InputVariable * ourSpeed, *enemySpeed;
|
||||
fl::InputVariable * bankPresent;
|
||||
fl::InputVariable * castleWalls;
|
||||
fl::OutputVariable * threat;
|
||||
};
|
||||
|
||||
class HeroMovementGoalEngineBase : public engineBase //in future - maybe derive from some (GoalEngineBase : public engineBase) class for handling non-movement goals with common utility for goal engines
|
||||
{
|
||||
public:
|
||||
HeroMovementGoalEngineBase();
|
||||
|
||||
virtual float evaluate(Goals::AbstractGoal & goal) = 0;
|
||||
|
||||
protected:
|
||||
void setSharedFuzzyVariables(Goals::AbstractGoal & goal);
|
||||
|
||||
fl::InputVariable * strengthRatio;
|
||||
fl::InputVariable * heroStrength;
|
||||
fl::InputVariable * turnDistance;
|
||||
fl::InputVariable * missionImportance;
|
||||
fl::OutputVariable * value;
|
||||
|
||||
private:
|
||||
float calculateTurnDistanceInputValue(const CGHeroInstance * h, int3 tile) const;
|
||||
};
|
||||
|
||||
class VisitTileEngine : public HeroMovementGoalEngineBase
|
||||
{
|
||||
public:
|
||||
VisitTileEngine();
|
||||
float evaluate(Goals::AbstractGoal & goal) override;
|
||||
};
|
||||
|
||||
class GetObjEngine : public HeroMovementGoalEngineBase
|
||||
{
|
||||
public:
|
||||
GetObjEngine();
|
||||
float evaluate(Goals::AbstractGoal & goal) override;
|
||||
protected:
|
||||
fl::InputVariable * objectValue;
|
||||
};
|
||||
|
||||
class FuzzyHelper
|
||||
{
|
||||
friend class VCAI;
|
||||
|
||||
public:
|
||||
TacticalAdvantageEngine tacticalAdvantageEngine;
|
||||
VisitTileEngine visitTileEngine;
|
||||
GetObjEngine getObjEngine;
|
||||
|
||||
float evaluate(Goals::Explore & g);
|
||||
float evaluate(Goals::RecruitHero & g);
|
||||
float evaluate(Goals::VisitTile & g);
|
||||
float evaluate(Goals::VisitObj & g);
|
||||
float evaluate(Goals::VisitHero & g);
|
||||
float evaluate(Goals::BuildThis & g);
|
||||
float evaluate(Goals::DigAtTile & g);
|
||||
float evaluate(Goals::CollectRes & g);
|
||||
float evaluate(Goals::Build & g);
|
||||
float evaluate(Goals::BuyArmy & g);
|
||||
float evaluate(Goals::GatherArmy & g);
|
||||
float evaluate(Goals::ClearWayTo & g);
|
||||
float evaluate(Goals::Invalid & g);
|
||||
float evaluate(Goals::AbstractGoal & g);
|
||||
void setPriority(Goals::TSubgoal & g);
|
||||
|
||||
ui64 estimateBankDanger(const CBank * bank); //TODO: move to another class?
|
||||
|
||||
Goals::TSubgoal chooseSolution(Goals::TGoalVec vec);
|
||||
//std::shared_ptr<AbstractGoal> chooseSolution (std::vector<std::shared_ptr<AbstractGoal>> & vec);
|
||||
};
|
||||
/*
|
||||
* FuzzyEngines.h, part of VCMI engine
|
||||
*
|
||||
* Authors: listed in file AUTHORS in main folder
|
||||
*
|
||||
* License: GNU General Public License v2.0 or later
|
||||
* Full text of license available in license.txt file, in main folder
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
#include "fl/Headers.h"
|
||||
#include "Goals.h"
|
||||
|
||||
class CArmedInstance;
|
||||
|
||||
class engineBase //subclasses create fuzzylite variables with "new" that are not freed - this is desired as fl::Engine wants to destroy these...
|
||||
{
|
||||
protected:
|
||||
fl::Engine engine;
|
||||
fl::RuleBlock rules;
|
||||
virtual void configure();
|
||||
void addRule(const std::string & txt);
|
||||
public:
|
||||
engineBase();
|
||||
};
|
||||
|
||||
class TacticalAdvantageEngine : public engineBase
|
||||
{
|
||||
public:
|
||||
TacticalAdvantageEngine();
|
||||
float getTacticalAdvantage(const CArmedInstance * we, const CArmedInstance * enemy); //returns factor how many times enemy is stronger than us
|
||||
private:
|
||||
fl::InputVariable * ourWalkers, *ourShooters, *ourFlyers, *enemyWalkers, *enemyShooters, *enemyFlyers;
|
||||
fl::InputVariable * ourSpeed, *enemySpeed;
|
||||
fl::InputVariable * bankPresent;
|
||||
fl::InputVariable * castleWalls;
|
||||
fl::OutputVariable * threat;
|
||||
};
|
||||
|
||||
class HeroMovementGoalEngineBase : public engineBase //in future - maybe derive from some (GoalEngineBase : public engineBase) class for handling non-movement goals with common utility for goal engines
|
||||
{
|
||||
public:
|
||||
HeroMovementGoalEngineBase();
|
||||
|
||||
virtual float evaluate(Goals::AbstractGoal & goal) = 0;
|
||||
|
||||
protected:
|
||||
void setSharedFuzzyVariables(Goals::AbstractGoal & goal);
|
||||
|
||||
fl::InputVariable * strengthRatio;
|
||||
fl::InputVariable * heroStrength;
|
||||
fl::InputVariable * turnDistance;
|
||||
fl::InputVariable * missionImportance;
|
||||
fl::OutputVariable * value;
|
||||
|
||||
private:
|
||||
float calculateTurnDistanceInputValue(const CGHeroInstance * h, int3 tile) const;
|
||||
};
|
||||
|
||||
class VisitTileEngine : public HeroMovementGoalEngineBase
|
||||
{
|
||||
public:
|
||||
VisitTileEngine();
|
||||
float evaluate(Goals::AbstractGoal & goal) override;
|
||||
};
|
||||
|
||||
class GetObjEngine : public HeroMovementGoalEngineBase
|
||||
{
|
||||
public:
|
||||
GetObjEngine();
|
||||
float evaluate(Goals::AbstractGoal & goal) override;
|
||||
protected:
|
||||
fl::InputVariable * objectValue;
|
||||
};
|
156
AI/VCAI/FuzzyHelper.cpp
Normal file
156
AI/VCAI/FuzzyHelper.cpp
Normal file
@ -0,0 +1,156 @@
|
||||
/*
|
||||
* FuzzyHelper.cpp, part of VCMI engine
|
||||
*
|
||||
* Authors: listed in file AUTHORS in main folder
|
||||
*
|
||||
* License: GNU General Public License v2.0 or later
|
||||
* Full text of license available in license.txt file, in main folder
|
||||
*
|
||||
*/
|
||||
#include "StdInc.h"
|
||||
#include "FuzzyHelper.h"
|
||||
|
||||
#include "../../lib/mapObjects/CommonConstructors.h"
|
||||
#include "VCAI.h"
|
||||
|
||||
FuzzyHelper * fh;
|
||||
|
||||
extern boost::thread_specific_ptr<VCAI> ai;
|
||||
|
||||
Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec)
|
||||
{
|
||||
if(vec.empty()) //no possibilities found
|
||||
return sptr(Goals::Invalid());
|
||||
|
||||
ai->cachedSectorMaps.clear();
|
||||
|
||||
//a trick to switch between heroes less often - calculatePaths is costly
|
||||
auto sortByHeroes = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
|
||||
{
|
||||
return lhs->hero.h < rhs->hero.h;
|
||||
};
|
||||
boost::sort(vec, sortByHeroes);
|
||||
|
||||
for(auto g : vec)
|
||||
{
|
||||
setPriority(g);
|
||||
}
|
||||
|
||||
auto compareGoals = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
|
||||
{
|
||||
return lhs->priority < rhs->priority;
|
||||
};
|
||||
return *boost::max_element(vec, compareGoals);
|
||||
}
|
||||
|
||||
ui64 FuzzyHelper::estimateBankDanger(const CBank * bank)
|
||||
{
|
||||
//this one is not fuzzy anymore, just calculate weighted average
|
||||
|
||||
auto objectInfo = VLC->objtypeh->getHandlerFor(bank->ID, bank->subID)->getObjectInfo(bank->appearance);
|
||||
|
||||
CBankInfo * bankInfo = dynamic_cast<CBankInfo *>(objectInfo.get());
|
||||
|
||||
ui64 totalStrength = 0;
|
||||
ui8 totalChance = 0;
|
||||
for(auto config : bankInfo->getPossibleGuards())
|
||||
{
|
||||
totalStrength += config.second.totalStrength * config.first;
|
||||
totalChance += config.first;
|
||||
}
|
||||
return totalStrength / std::max<ui8>(totalChance, 1); //avoid division by zero
|
||||
|
||||
}
|
||||
|
||||
float FuzzyHelper::evaluate(Goals::VisitTile & g)
|
||||
{
|
||||
return visitTileEngine.evaluate(g);
|
||||
}
|
||||
float FuzzyHelper::evaluate(Goals::VisitObj & g)
|
||||
{
|
||||
return getObjEngine.evaluate(g);
|
||||
}
|
||||
float FuzzyHelper::evaluate(Goals::VisitHero & g)
|
||||
{
|
||||
auto obj = ai->myCb->getObj(ObjectInstanceID(g.objid)); //we assume for now that these goals are similar
|
||||
if(!obj)
|
||||
return -100; //hero died in the meantime
|
||||
//TODO: consider direct copy (constructor?)
|
||||
g.setpriority(Goals::VisitTile(obj->visitablePos()).sethero(g.hero).setisAbstract(g.isAbstract).accept(this));
|
||||
return g.priority;
|
||||
}
|
||||
float FuzzyHelper::evaluate(Goals::GatherArmy & g)
|
||||
{
|
||||
//the more army we need, the more important goal
|
||||
//the more army we lack, the less important goal
|
||||
float army = g.hero->getArmyStrength();
|
||||
float ratio = g.value / std::max(g.value - army, 2000.0f); //2000 is about the value of hero recruited from tavern
|
||||
return 5 * (ratio / (ratio + 2)); //so 50% army gives 2.5, asymptotic 5
|
||||
}
|
||||
|
||||
float FuzzyHelper::evaluate(Goals::ClearWayTo & g)
|
||||
{
|
||||
if(!g.hero.h)
|
||||
throw cannotFulfillGoalException("ClearWayTo called without hero!");
|
||||
|
||||
int3 t = ai->getCachedSectorMap(g.hero)->firstTileToGet(g.hero, g.tile);
|
||||
|
||||
if(t.valid())
|
||||
{
|
||||
if(isSafeToVisit(g.hero, t))
|
||||
{
|
||||
g.setpriority(Goals::VisitTile(g.tile).sethero(g.hero).setisAbstract(g.isAbstract).accept(this));
|
||||
}
|
||||
else
|
||||
{
|
||||
g.setpriority (Goals::GatherArmy(evaluateDanger(t, g.hero.h)*SAFE_ATTACK_CONSTANT).
|
||||
sethero(g.hero).setisAbstract(true).accept(this));
|
||||
}
|
||||
return g.priority;
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
float FuzzyHelper::evaluate(Goals::BuildThis & g)
|
||||
{
|
||||
return g.priority; //TODO
|
||||
}
|
||||
float FuzzyHelper::evaluate(Goals::DigAtTile & g)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
float FuzzyHelper::evaluate(Goals::CollectRes & g)
|
||||
{
|
||||
return g.priority; //handled by ResourceManager
|
||||
}
|
||||
float FuzzyHelper::evaluate(Goals::Build & g)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
float FuzzyHelper::evaluate(Goals::BuyArmy & g)
|
||||
{
|
||||
return g.priority;
|
||||
}
|
||||
float FuzzyHelper::evaluate(Goals::Explore & g)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
float FuzzyHelper::evaluate(Goals::RecruitHero & g)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
float FuzzyHelper::evaluate(Goals::Invalid & g)
|
||||
{
|
||||
return -1e10;
|
||||
}
|
||||
float FuzzyHelper::evaluate(Goals::AbstractGoal & g)
|
||||
{
|
||||
logAi->warn("Cannot evaluate goal %s", g.name());
|
||||
return g.priority;
|
||||
}
|
||||
void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pattern
|
||||
{
|
||||
g->setpriority(g->accept(this)); //this enforces returned value is set
|
||||
}
|
42
AI/VCAI/FuzzyHelper.h
Normal file
42
AI/VCAI/FuzzyHelper.h
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* FuzzyHelper.h, part of VCMI engine
|
||||
*
|
||||
* Authors: listed in file AUTHORS in main folder
|
||||
*
|
||||
* License: GNU General Public License v2.0 or later
|
||||
* Full text of license available in license.txt file, in main folder
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
#include "FuzzyEngines.h"
|
||||
|
||||
class CBank;
|
||||
|
||||
class FuzzyHelper
|
||||
{
|
||||
public:
|
||||
TacticalAdvantageEngine tacticalAdvantageEngine;
|
||||
VisitTileEngine visitTileEngine;
|
||||
GetObjEngine getObjEngine;
|
||||
|
||||
float evaluate(Goals::Explore & g);
|
||||
float evaluate(Goals::RecruitHero & g);
|
||||
float evaluate(Goals::VisitTile & g);
|
||||
float evaluate(Goals::VisitObj & g);
|
||||
float evaluate(Goals::VisitHero & g);
|
||||
float evaluate(Goals::BuildThis & g);
|
||||
float evaluate(Goals::DigAtTile & g);
|
||||
float evaluate(Goals::CollectRes & g);
|
||||
float evaluate(Goals::Build & g);
|
||||
float evaluate(Goals::BuyArmy & g);
|
||||
float evaluate(Goals::GatherArmy & g);
|
||||
float evaluate(Goals::ClearWayTo & g);
|
||||
float evaluate(Goals::Invalid & g);
|
||||
float evaluate(Goals::AbstractGoal & g);
|
||||
void setPriority(Goals::TSubgoal & g);
|
||||
|
||||
ui64 estimateBankDanger(const CBank * bank); //TODO: move to another class?
|
||||
|
||||
Goals::TSubgoal chooseSolution(Goals::TGoalVec vec);
|
||||
//std::shared_ptr<AbstractGoal> chooseSolution (std::vector<std::shared_ptr<AbstractGoal>> & vec);
|
||||
};
|
@ -10,7 +10,7 @@
|
||||
#include "StdInc.h"
|
||||
#include "Goals.h"
|
||||
#include "VCAI.h"
|
||||
#include "Fuzzy.h"
|
||||
#include "FuzzyHelper.h"
|
||||
#include "ResourceManager.h"
|
||||
#include "BuildingManager.h"
|
||||
#include "../../lib/mapping/CMap.h" //for victory conditions
|
||||
|
@ -9,7 +9,7 @@
|
||||
*/
|
||||
#include "StdInc.h"
|
||||
#include "VCAI.h"
|
||||
#include "Fuzzy.h"
|
||||
#include "FuzzyHelper.h"
|
||||
#include "ResourceManager.h"
|
||||
#include "BuildingManager.h"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user