1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-15 20:03:15 +02:00

use smart pointers for fl variables

This commit is contained in:
Dydzio
2018-08-06 19:20:36 +02:00
parent 12d750a767
commit 67439bdd36
2 changed files with 39 additions and 84 deletions

View File

@@ -129,20 +129,14 @@ TacticalAdvantageEngine::TacticalAdvantageEngine()
{ {
try try
{ {
ourShooters = new fl::InputVariable("OurShooters"); ourShooters = vstd::make_unique<fl::InputVariable>(fl::InputVariable("OurShooters"));
ourWalkers = new fl::InputVariable("OurWalkers"); ourWalkers = vstd::make_unique<fl::InputVariable>(fl::InputVariable("OurWalkers"));
ourFlyers = new fl::InputVariable("OurFlyers"); ourFlyers = vstd::make_unique<fl::InputVariable>(fl::InputVariable("OurFlyers"));
enemyShooters = new fl::InputVariable("EnemyShooters"); enemyShooters = vstd::make_unique<fl::InputVariable>(fl::InputVariable("EnemyShooters"));
enemyWalkers = new fl::InputVariable("EnemyWalkers"); enemyWalkers = vstd::make_unique<fl::InputVariable>(fl::InputVariable("EnemyWalkers"));
enemyFlyers = new fl::InputVariable("EnemyFlyers"); enemyFlyers = vstd::make_unique<fl::InputVariable>(fl::InputVariable("EnemyFlyers"));
//Tactical advantage calculation for(auto val : {ourShooters.get(), ourWalkers.get(), ourFlyers.get(), enemyShooters.get(), enemyWalkers.get(), enemyFlyers.get()})
std::vector<fl::InputVariable *> helper =
{
ourShooters, ourWalkers, ourFlyers, enemyShooters, enemyWalkers, enemyFlyers
};
for(auto val : helper)
{ {
engine.addInputVariable(val); engine.addInputVariable(val);
val->addTerm(new fl::Ramp("FEW", 0.6, 0.0)); val->addTerm(new fl::Ramp("FEW", 0.6, 0.0));
@@ -150,12 +144,10 @@ TacticalAdvantageEngine::TacticalAdvantageEngine()
val->setRange(0.0, 1.0); val->setRange(0.0, 1.0);
} }
ourSpeed = new fl::InputVariable("OurSpeed"); ourSpeed = vstd::make_unique<fl::InputVariable>(fl::InputVariable("OurSpeed"));
enemySpeed = new fl::InputVariable("EnemySpeed"); enemySpeed = vstd::make_unique<fl::InputVariable>(fl::InputVariable("EnemySpeed"));
helper = { ourSpeed, enemySpeed }; for(auto val : {ourSpeed.get(), enemySpeed.get()})
for(auto val : helper)
{ {
engine.addInputVariable(val); engine.addInputVariable(val);
val->addTerm(new fl::Ramp("LOW", 6.5, 3)); val->addTerm(new fl::Ramp("LOW", 6.5, 3));
@@ -164,8 +156,8 @@ TacticalAdvantageEngine::TacticalAdvantageEngine()
val->setRange(0, 25); val->setRange(0, 25);
} }
castleWalls = new fl::InputVariable("CastleWalls"); castleWalls = vstd::make_unique<fl::InputVariable>(fl::InputVariable("CastleWalls"));
engine.addInputVariable(castleWalls); engine.addInputVariable(castleWalls.get());
{ {
fl::Rectangle * none = new fl::Rectangle("NONE", CGTownInstance::NONE, CGTownInstance::NONE + (CGTownInstance::FORT - CGTownInstance::NONE) * 0.5f); fl::Rectangle * none = new fl::Rectangle("NONE", CGTownInstance::NONE, CGTownInstance::NONE + (CGTownInstance::FORT - CGTownInstance::NONE) * 0.5f);
castleWalls->addTerm(none); castleWalls->addTerm(none);
@@ -181,8 +173,8 @@ TacticalAdvantageEngine::TacticalAdvantageEngine()
} }
bankPresent = new fl::InputVariable("Bank"); bankPresent = vstd::make_unique<fl::InputVariable>(fl::InputVariable("Bank"));
engine.addInputVariable(bankPresent); engine.addInputVariable(bankPresent.get());
{ {
fl::Rectangle * termFalse = new fl::Rectangle("FALSE", 0.0, 0.5f); fl::Rectangle * termFalse = new fl::Rectangle("FALSE", 0.0, 0.5f);
bankPresent->addTerm(termFalse); bankPresent->addTerm(termFalse);
@@ -191,8 +183,8 @@ TacticalAdvantageEngine::TacticalAdvantageEngine()
bankPresent->setRange(0, 1); bankPresent->setRange(0, 1);
} }
threat = new fl::OutputVariable("Threat"); threat = vstd::make_unique<fl::OutputVariable>(fl::OutputVariable("Threat"));
engine.addOutputVariable(threat); engine.addOutputVariable(threat.get());
threat->addTerm(new fl::Ramp("LOW", 1, MIN_AI_STRENGHT)); threat->addTerm(new fl::Ramp("LOW", 1, MIN_AI_STRENGHT));
threat->addTerm(new fl::Triangle("MEDIUM", 0.8, 1.2)); threat->addTerm(new fl::Triangle("MEDIUM", 0.8, 1.2));
threat->addTerm(new fl::Ramp("HIGH", 1, 1.5)); threat->addTerm(new fl::Ramp("HIGH", 1, 1.5));
@@ -265,12 +257,13 @@ float TacticalAdvantageEngine::getTacticalAdvantage(const CArmedInstance * we, c
if(output < 0 || (output != output)) if(output < 0 || (output != output))
{ {
fl::InputVariable * tab[] = { bankPresent, castleWalls, ourWalkers, ourShooters, ourFlyers, ourSpeed, enemyWalkers, enemyShooters, enemyFlyers, enemySpeed }; fl::scalar tab[] = { bankPresent->getValue(), castleWalls->getValue(), ourWalkers->getValue(), ourShooters->getValue(), ourFlyers->getValue(), ourSpeed->getValue(),
enemyWalkers->getValue(), enemyShooters->getValue(), enemyFlyers->getValue(), enemySpeed->getValue() };
std::string names[] = { "bankPresent", "castleWalls", "ourWalkers", "ourShooters", "ourFlyers", "ourSpeed", "enemyWalkers", "enemyShooters", "enemyFlyers", "enemySpeed" }; std::string names[] = { "bankPresent", "castleWalls", "ourWalkers", "ourShooters", "ourFlyers", "ourSpeed", "enemyWalkers", "enemyShooters", "enemyFlyers", "enemySpeed" };
std::stringstream log("Warning! Fuzzy engine doesn't cover this set of parameters: "); std::stringstream log("Warning! Fuzzy engine doesn't cover this set of parameters: ");
for(int i = 0; i < boost::size(tab); i++) for(int i = 0; i < boost::size(tab); i++)
log << names[i] << ": " << tab[i]->getValue() << " "; log << names[i] << ": " << tab[i] << " ";
logAi->error(log.str()); logAi->error(log.str());
assert(false); assert(false);
} }
@@ -278,22 +271,6 @@ float TacticalAdvantageEngine::getTacticalAdvantage(const CArmedInstance * we, c
return output; return output;
} }
TacticalAdvantageEngine::~TacticalAdvantageEngine()
{
//TODO: smart pointers?
delete ourWalkers;
delete ourShooters;
delete ourFlyers;
delete enemyWalkers;
delete enemyShooters;
delete enemyFlyers;
delete ourSpeed;
delete enemySpeed;
delete bankPresent;
delete castleWalls;
delete threat;
}
//std::shared_ptr<AbstractGoal> chooseSolution (std::vector<std::shared_ptr<AbstractGoal>> & vec) //std::shared_ptr<AbstractGoal> chooseSolution (std::vector<std::shared_ptr<AbstractGoal>> & vec)
Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec) Goals::TSubgoal FuzzyHelper::chooseSolution(Goals::TGoalVec vec)
@@ -334,20 +311,19 @@ HeroMovementGoalEngineBase::HeroMovementGoalEngineBase()
{ {
try try
{ {
strengthRatio = new fl::InputVariable("strengthRatio"); //hero must be strong enough to defeat guards strengthRatio = vstd::make_unique<fl::InputVariable>(fl::InputVariable("strengthRatio")); //hero must be strong enough to defeat guards
heroStrength = new fl::InputVariable("heroStrength"); //we want to use weakest possible hero heroStrength = vstd::make_unique<fl::InputVariable>(fl::InputVariable("heroStrength")); //we want to use weakest possible hero
turnDistance = new fl::InputVariable("turnDistance"); //we want to use hero who is near turnDistance = vstd::make_unique<fl::InputVariable>(fl::InputVariable("turnDistance")); //we want to use hero who is near
missionImportance = new fl::InputVariable("lockedMissionImportance"); //we may want to preempt hero with low-priority mission missionImportance = vstd::make_unique<fl::InputVariable>(fl::InputVariable("lockedMissionImportance")); //we may want to preempt hero with low-priority mission
value = new fl::OutputVariable("Value"); value = vstd::make_unique<fl::OutputVariable>(fl::OutputVariable("Value"));
value->setMinimum(0); value->setMinimum(0);
value->setMaximum(5); value->setMaximum(5);
std::vector<fl::InputVariable *> helper = { strengthRatio, heroStrength, turnDistance, missionImportance }; for(auto val : { strengthRatio.get(), heroStrength.get(), turnDistance.get(), missionImportance.get() })
for(auto val : helper)
{ {
engine.addInputVariable(val); engine.addInputVariable(val);
} }
engine.addOutputVariable(value); engine.addOutputVariable(value.get());
strengthRatio->addTerm(new fl::Ramp("LOW", SAFE_ATTACK_CONSTANT, 0)); strengthRatio->addTerm(new fl::Ramp("LOW", SAFE_ATTACK_CONSTANT, 0));
strengthRatio->addTerm(new fl::Ramp("HIGH", SAFE_ATTACK_CONSTANT, SAFE_ATTACK_CONSTANT * 3)); strengthRatio->addTerm(new fl::Ramp("HIGH", SAFE_ATTACK_CONSTANT, SAFE_ATTACK_CONSTANT * 3));
@@ -427,14 +403,6 @@ void HeroMovementGoalEngineBase::setSharedFuzzyVariables(Goals::AbstractGoal & g
} }
} }
HeroMovementGoalEngineBase::~HeroMovementGoalEngineBase()
{
delete strengthRatio;
delete heroStrength;
delete turnDistance;
delete missionImportance;
}
float FuzzyHelper::evaluate(Goals::VisitTile & g) float FuzzyHelper::evaluate(Goals::VisitTile & g)
{ {
return visitTileEngine.evaluate(g); return visitTileEngine.evaluate(g);
@@ -524,9 +492,9 @@ GetObjEngine::GetObjEngine()
{ {
try try
{ {
objectValue = new fl::InputVariable("objectValue"); //value of that object type known by AI objectValue = vstd::make_unique<fl::InputVariable>(fl::InputVariable("objectValue")); //value of that object type known by AI
engine.addInputVariable(objectValue); engine.addInputVariable(objectValue.get());
//objectValue ranges are based on checking RMG priorities of some objects and trying to guess sane value ranges //objectValue ranges are based on checking RMG priorities of some objects and trying to guess sane value ranges
objectValue->addTerm(new fl::Ramp("LOW", 3000, 0)); //I have feeling that concave shape might work well instead of ramp for objectValue FL terms objectValue->addTerm(new fl::Ramp("LOW", 3000, 0)); //I have feeling that concave shape might work well instead of ramp for objectValue FL terms
@@ -553,11 +521,6 @@ GetObjEngine::GetObjEngine()
configure(); configure();
} }
GetObjEngine::~GetObjEngine()
{
delete objectValue;
}
float GetObjEngine::evaluate(Goals::AbstractGoal & goal) float GetObjEngine::evaluate(Goals::AbstractGoal & goal)
{ {
auto g = dynamic_cast<Goals::GetObj &>(goal); auto g = dynamic_cast<Goals::GetObj &>(goal);
@@ -603,10 +566,6 @@ VisitTileEngine::VisitTileEngine() //so far no VisitTile-specific variables that
configure(); configure();
} }
VisitTileEngine::~VisitTileEngine()
{
}
float VisitTileEngine::evaluate(Goals::AbstractGoal & goal) float VisitTileEngine::evaluate(Goals::AbstractGoal & goal)
{ {
auto g = dynamic_cast<Goals::VisitTile &>(goal); auto g = dynamic_cast<Goals::VisitTile &>(goal);

View File

@@ -33,13 +33,12 @@ public:
TacticalAdvantageEngine(); TacticalAdvantageEngine();
float getTacticalAdvantage(const CArmedInstance * we, const CArmedInstance * enemy); //returns factor how many times enemy is stronger than us float getTacticalAdvantage(const CArmedInstance * we, const CArmedInstance * enemy); //returns factor how many times enemy is stronger than us
~TacticalAdvantageEngine();
private: private:
fl::InputVariable * ourWalkers, *ourShooters, *ourFlyers, *enemyWalkers, *enemyShooters, *enemyFlyers; std::unique_ptr<fl::InputVariable> ourWalkers, ourShooters, ourFlyers, enemyWalkers, enemyShooters, enemyFlyers;
fl::InputVariable * ourSpeed, *enemySpeed; std::unique_ptr<fl::InputVariable> ourSpeed, enemySpeed;
fl::InputVariable * bankPresent; std::unique_ptr<fl::InputVariable> bankPresent;
fl::InputVariable * castleWalls; std::unique_ptr<fl::InputVariable> castleWalls;
fl::OutputVariable * threat; std::unique_ptr<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 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
@@ -48,16 +47,15 @@ public:
HeroMovementGoalEngineBase(); HeroMovementGoalEngineBase();
virtual float evaluate(Goals::AbstractGoal & goal) = 0; virtual float evaluate(Goals::AbstractGoal & goal) = 0;
virtual ~HeroMovementGoalEngineBase();
protected: protected:
void setSharedFuzzyVariables(Goals::AbstractGoal & goal); void setSharedFuzzyVariables(Goals::AbstractGoal & goal);
fl::InputVariable * strengthRatio; std::unique_ptr<fl::InputVariable> strengthRatio;
fl::InputVariable * heroStrength; std::unique_ptr<fl::InputVariable> heroStrength;
fl::InputVariable * turnDistance; std::unique_ptr<fl::InputVariable> turnDistance;
fl::InputVariable * missionImportance; std::unique_ptr<fl::InputVariable> missionImportance;
fl::OutputVariable * value; std::unique_ptr<fl::OutputVariable> value;
private: private:
float calculateTurnDistanceInputValue(const CGHeroInstance * h, int3 tile) const; float calculateTurnDistanceInputValue(const CGHeroInstance * h, int3 tile) const;
@@ -67,7 +65,6 @@ class VisitTileEngine : public HeroMovementGoalEngineBase
{ {
public: public:
VisitTileEngine(); VisitTileEngine();
~VisitTileEngine();
float evaluate(Goals::AbstractGoal & goal) override; float evaluate(Goals::AbstractGoal & goal) override;
}; };
@@ -75,10 +72,9 @@ class GetObjEngine : public HeroMovementGoalEngineBase
{ {
public: public:
GetObjEngine(); GetObjEngine();
~GetObjEngine();
float evaluate(Goals::AbstractGoal & goal) override; float evaluate(Goals::AbstractGoal & goal) override;
protected: protected:
fl::InputVariable * objectValue; std::unique_ptr<fl::InputVariable> objectValue;
}; };
class FuzzyHelper class FuzzyHelper