File: Fuzzy.cpp

package info (click to toggle)
vcmi 0.99%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 10,264 kB
  • ctags: 16,826
  • sloc: cpp: 121,945; objc: 248; sh: 193; makefile: 28; python: 13; ansic: 9
file content (533 lines) | stat: -rw-r--r-- 17,571 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include "StdInc.h"
#include "Fuzzy.h"
#include <limits>

#include "../../lib/mapObjects/MapObjects.h"
#include "../../lib/mapObjects/CommonConstructors.h"
#include "../../lib/CCreatureHandler.h"
#include "../../lib/CPathfinder.h"
#include "../../lib/CGameStateFwd.h"
#include "../../lib/VCMI_Lib.h"
#include "../../CCallback.h"
#include "VCAI.h"

/*
 * Fuzzy.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
 *
*/

#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

struct BankConfig;
class IObjectInfo;
class CBankInfo;
class Engine;
class InputVariable;
class CGTownInstance;

//using namespace Goals;

FuzzyHelper *fh;

extern boost::thread_specific_ptr<CCallback> cb;
extern boost::thread_specific_ptr<VCAI> ai;

engineBase::engineBase()
{
	engine.addRuleBlock(&rules);
}

void engineBase::configure()
{
	engine.configure("Minimum", "Maximum", "Minimum", "AlgebraicSum", "Centroid");
	logAi->info(engine.toString());
}

void engineBase::addRule(const std::string &txt)
{
	rules.addRule(fl::Rule::parse(txt, &engine));
}

struct armyStructure
{
	float walkers, shooters, flyers;
	ui32 maxSpeed;
};

armyStructure evaluateArmyStructure (const CArmedInstance * army)
{
	ui64 totalStrenght = army->getArmyStrength();
	double walkersStrenght = 0;
	double flyersStrenght = 0;
	double shootersStrenght = 0;
	ui32 maxSpeed = 0;

	for(auto s : army->Slots())
	{
		bool walker = true;
		if (s.second->type->hasBonusOfType(Bonus::SHOOTER))
		{
			shootersStrenght += s.second->getPower();
			walker = false;
		}
		if (s.second->type->hasBonusOfType(Bonus::FLYING))
		{
			flyersStrenght += s.second->getPower();
			walker = false;
		}
		if (walker)
			walkersStrenght += s.second->getPower();

		vstd::amax(maxSpeed, s.second->type->valOfBonuses(Bonus::STACKS_SPEED));
	}
	armyStructure as;
	as.walkers = walkersStrenght / totalStrenght;
	as.shooters = shootersStrenght / totalStrenght;
	as.flyers = flyersStrenght / totalStrenght;
	as.maxSpeed = maxSpeed;
	assert(as.walkers || as.flyers || as.shooters);
	return as;
}

FuzzyHelper::FuzzyHelper()
{
	initTacticalAdvantage();
	ta.configure();
	initVisitTile();
	vt.configure();
}


void FuzzyHelper::initTacticalAdvantage()
{
	try
	{

		ta.ourShooters = new fl::InputVariable("OurShooters");
		ta.ourWalkers = new fl::InputVariable("OurWalkers");
		ta.ourFlyers = new fl::InputVariable("OurFlyers");
		ta.enemyShooters = new fl::InputVariable("EnemyShooters");
		ta.enemyWalkers = new fl::InputVariable("EnemyWalkers");
		ta.enemyFlyers = new fl::InputVariable("EnemyFlyers");

		//Tactical advantage calculation
		std::vector<fl::InputVariable*> helper =
		{
			ta.ourShooters, ta.ourWalkers, ta.ourFlyers, ta.enemyShooters, ta.enemyWalkers, ta.enemyFlyers
		};

		for (auto val : helper)
		{
			ta.engine.addInputVariable(val);
			val->addTerm(new fl::Ramp("FEW", 0.6, 0.0));
			val->addTerm(new fl::Ramp("MANY", 0.4, 1));
			val->setRange(0.0, 1.0);
		}

		ta.ourSpeed = new fl::InputVariable("OurSpeed");
		ta.enemySpeed = new fl::InputVariable("EnemySpeed");

		helper = {ta.ourSpeed, ta.enemySpeed};

		for (auto val : helper)
		{
			ta.engine.addInputVariable(val);
			val->addTerm(new fl::Ramp("LOW", 6.5, 3));
			val->addTerm(new fl::Triangle("MEDIUM", 5.5, 10.5));
			val->addTerm(new fl::Ramp("HIGH", 8.5, 16));
			val->setRange(0, 25);
		}

		ta.castleWalls = new fl::InputVariable("CastleWalls");
		ta.engine.addInputVariable(ta.castleWalls);
		{
			fl::Rectangle* none = new fl::Rectangle("NONE", CGTownInstance::NONE, CGTownInstance::NONE + (CGTownInstance::FORT - CGTownInstance::NONE) * 0.5f);
			ta.castleWalls->addTerm(none);

			fl::Trapezoid* medium = new fl::Trapezoid("MEDIUM", (CGTownInstance::FORT - CGTownInstance::NONE) * 0.5f, CGTownInstance::FORT,
				CGTownInstance::CITADEL, CGTownInstance::CITADEL + (CGTownInstance::CASTLE - CGTownInstance::CITADEL) * 0.5f);
			ta.castleWalls->addTerm(medium);

			fl::Ramp* high = new fl::Ramp("HIGH", CGTownInstance::CITADEL - 0.1, CGTownInstance::CASTLE);
			ta.castleWalls->addTerm(high);

			ta.castleWalls->setRange(CGTownInstance::NONE, CGTownInstance::CASTLE);
		}



		ta.bankPresent = new fl::InputVariable("Bank");
		ta.engine.addInputVariable(ta.bankPresent);
		{
			fl::Rectangle* termFalse = new fl::Rectangle("FALSE", 0.0, 0.5f);
			ta.bankPresent->addTerm(termFalse);
			fl::Rectangle* termTrue = new fl::Rectangle("TRUE", 0.5f, 1);
			ta.bankPresent->addTerm(termTrue);
			ta.bankPresent->setRange(0, 1);
		}

		ta.threat = new fl::OutputVariable("Threat");
		ta.engine.addOutputVariable(ta.threat);
		ta.threat->addTerm(new fl::Ramp("LOW", 1, MIN_AI_STRENGHT));
		ta.threat->addTerm(new fl::Triangle("MEDIUM", 0.8, 1.2));
		ta.threat->addTerm(new fl::Ramp("HIGH", 1, 1.5));
		ta.threat->setRange(MIN_AI_STRENGHT, 1.5);

		ta.addRule("if OurShooters is MANY and EnemySpeed is LOW then Threat is LOW");
		ta.addRule("if OurShooters is MANY and EnemyShooters is FEW then Threat is LOW");
		ta.addRule("if OurSpeed is LOW and EnemyShooters is MANY then Threat is HIGH");
		ta.addRule("if OurSpeed is HIGH and EnemyShooters is MANY then Threat is LOW");

		ta.addRule("if OurWalkers is FEW and EnemyShooters is MANY then Threat is somewhat LOW");
		ta.addRule("if OurShooters is MANY and EnemySpeed is HIGH then Threat is somewhat HIGH");
		//just to cover all cases
		ta.addRule("if OurShooters is FEW and EnemySpeed is HIGH then Threat is MEDIUM");
		ta.addRule("if EnemySpeed is MEDIUM then Threat is MEDIUM");
		ta.addRule("if EnemySpeed is LOW and OurShooters is FEW then Threat is MEDIUM");

		ta.addRule("if Bank is TRUE and OurShooters is MANY then Threat is somewhat HIGH");
		ta.addRule("if Bank is TRUE and EnemyShooters is MANY then Threat is LOW");

		ta.addRule("if CastleWalls is HIGH and OurWalkers is MANY then Threat is very HIGH");
		ta.addRule("if CastleWalls is HIGH and OurFlyers is MANY and OurShooters is MANY then Threat is MEDIUM");
		ta.addRule("if CastleWalls is MEDIUM and OurShooters is MANY and EnemyWalkers is MANY then Threat is LOW");

	}
	catch (fl::Exception & pe)
	{
		logAi->error("initTacticalAdvantage: %s", pe.getWhat());
	}
}

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 / totalChance;

}

float FuzzyHelper::getTacticalAdvantage (const CArmedInstance *we, const CArmedInstance *enemy)
{
	float output = 1;
	try
	{
		armyStructure ourStructure = evaluateArmyStructure(we);
		armyStructure enemyStructure = evaluateArmyStructure(enemy);

		ta.ourWalkers->setInputValue(ourStructure.walkers);
		ta.ourShooters->setInputValue(ourStructure.shooters);
		ta.ourFlyers->setInputValue(ourStructure.flyers);
		ta.ourSpeed->setInputValue(ourStructure.maxSpeed);

		ta.enemyWalkers->setInputValue(enemyStructure.walkers);
		ta.enemyShooters->setInputValue(enemyStructure.shooters);
		ta.enemyFlyers->setInputValue(enemyStructure.flyers);
		ta.enemySpeed->setInputValue(enemyStructure.maxSpeed);

		bool bank = dynamic_cast<const CBank*> (enemy);
		if (bank)
			ta.bankPresent->setInputValue(1);
		else
			ta.bankPresent->setInputValue(0);

		const CGTownInstance * fort = dynamic_cast<const CGTownInstance*> (enemy);
		if (fort)
		{
			ta.castleWalls->setInputValue(fort->fortLevel());
		}
		else
			ta.castleWalls->setInputValue(0);

		//engine.process(TACTICAL_ADVANTAGE);//TODO: Process only Tactical_Advantage
		ta.engine.process();
		output = ta.threat->getOutputValue();
	}
	catch (fl::Exception & fe)
	{
		logAi->error("getTacticalAdvantage: %s ",fe.getWhat());
	}

	if (output < 0 || (output != output))
	{
		fl::InputVariable* tab[] = {ta.bankPresent, ta.castleWalls, ta.ourWalkers, ta.ourShooters, ta.ourFlyers, ta.ourSpeed, ta.enemyWalkers, ta.enemyShooters, ta.enemyFlyers, ta.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: ");

		for (int i = 0; i < boost::size(tab); i++)
			log << names[i] << ": " << tab[i]->getInputValue() << " ";
		logAi->error(log.str());
		assert(false);
	}

	return output;
}

FuzzyHelper::TacticalAdvantage::~TacticalAdvantage()
{
	//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)

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;
	};
	boost::sort (vec, compareGoals);

	return vec.back();
}

float FuzzyHelper::evaluate (Goals::Explore & g)
{
	return 1;
}
float FuzzyHelper::evaluate (Goals::RecruitHero & g)
{
	return 1; //just try to recruit hero as one of options
}
FuzzyHelper::EvalVisitTile::~EvalVisitTile()
{
	delete strengthRatio;
	delete heroStrength;
	delete turnDistance;
	delete missionImportance;
}

void FuzzyHelper::initVisitTile()
{
	try
	{
		vt.strengthRatio = new fl::InputVariable("strengthRatio"); //hero must be strong enough to defeat guards
		vt.heroStrength = new fl::InputVariable("heroStrength"); //we want to use weakest possible hero
		vt.turnDistance = new fl::InputVariable("turnDistance"); //we want to use hero who is near
		vt.missionImportance = new fl::InputVariable("lockedMissionImportance"); //we may want to preempt hero with low-priority mission
		vt.value = new fl::OutputVariable("Value");
		vt.value->setMinimum(0);
		vt.value->setMaximum(5);

		std::vector<fl::InputVariable*> helper = {vt.strengthRatio, vt.heroStrength, vt.turnDistance, vt.missionImportance};
		for (auto val : helper)
		{
			vt.engine.addInputVariable(val);
		}
		vt.engine.addOutputVariable(vt.value);

		vt.strengthRatio->addTerm(new fl::Ramp("LOW", SAFE_ATTACK_CONSTANT, 0));
		vt.strengthRatio->addTerm(new fl::Ramp("HIGH", SAFE_ATTACK_CONSTANT, SAFE_ATTACK_CONSTANT * 3));
		vt.strengthRatio->setRange(0, SAFE_ATTACK_CONSTANT * 3 );

		//strength compared to our main hero
		vt.heroStrength->addTerm(new fl::Ramp("LOW", 0.2, 0));
		vt.heroStrength->addTerm(new fl::Triangle("MEDIUM", 0.2, 0.8));
		vt.heroStrength->addTerm(new fl::Ramp("HIGH", 0.5, 1));
		vt.heroStrength->setRange(0.0, 1.0);

		vt.turnDistance->addTerm(new fl::Ramp("SMALL", 0.5, 0));
		vt.turnDistance->addTerm(new fl::Triangle("MEDIUM", 0.1, 0.8));
		vt.turnDistance->addTerm(new fl::Ramp("LONG", 0.5, 3));
		vt.turnDistance->setRange(0.0, 3.0);

		vt.missionImportance->addTerm(new fl::Ramp("LOW", 2.5, 0));
		vt.missionImportance->addTerm(new fl::Triangle("MEDIUM", 2, 3));
		vt.missionImportance->addTerm(new fl::Ramp("HIGH", 2.5, 5));
		vt.missionImportance->setRange(0.0, 5.0);

		//an issue: in 99% cases this outputs center of mass (2.5) regardless of actual input :/
		 //should be same as "mission Importance" to keep consistency
		vt.value->addTerm(new fl::Ramp("LOW", 2.5, 0));
		vt.value->addTerm(new fl::Triangle("MEDIUM", 2, 3)); //can't be center of mass :/
		vt.value->addTerm(new fl::Ramp("HIGH", 2.5, 5));
		vt.value->setRange(0.0,5.0);

		//use unarmed scouts if possible
		vt.addRule("if strengthRatio is HIGH and heroStrength is LOW then Value is very HIGH");
		//we may want to use secondary hero(es) rather than main hero
		vt.addRule("if strengthRatio is HIGH and heroStrength is MEDIUM then Value is somewhat HIGH");
		vt.addRule("if strengthRatio is HIGH and heroStrength is HIGH then Value is somewhat LOW");
		//don't assign targets to heroes who are too weak, but prefer targets of our main hero (in case we need to gather army)
		vt.addRule("if strengthRatio is LOW and heroStrength is LOW then Value is very LOW");
		//attempt to arm secondary heroes is not stupid
		vt.addRule("if strengthRatio is LOW and heroStrength is MEDIUM then Value is somewhat HIGH");
		vt.addRule("if strengthRatio is LOW and heroStrength is HIGH then Value is LOW");

		//do not cancel important goals
		vt.addRule("if lockedMissionImportance is HIGH then Value is very LOW");
		vt.addRule("if lockedMissionImportance is MEDIUM then Value is somewhat LOW");
		vt.addRule("if lockedMissionImportance is LOW then Value is HIGH");
		//pick nearby objects if it's easy, avoid long walks
		vt.addRule("if turnDistance is SMALL then Value is HIGH");
		vt.addRule("if turnDistance is MEDIUM then Value is MEDIUM");
		vt.addRule("if turnDistance is LONG then Value is LOW");
	}
	catch (fl::Exception & fe)
	{
		logAi->error("visitTile: %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
	if (!g.hero)
		return 0;

	//assert(cb->isInTheMap(g.tile));
	float turns = 0;
	float distance = CPathfinderHelper::getMovementCost(g.hero.h, g.tile);
	if (!distance) //we stand on that tile
		turns = 0;
	else
	{
		if (distance < g.hero->movement) //we can move there within one turn
			turns = (fl::scalar)distance / g.hero->movement;
		else
			turns = 1 + (fl::scalar)(distance - g.hero->movement) / g.hero->maxMovePoints(true); //bool on land?
	}

	float missionImportance = 0;
	if (vstd::contains(ai->lockedHeroes, g.hero))
		missionImportance = ai->lockedHeroes[g.hero]->priority;

	float strengthRatio = 10.0f; //we are much stronger than enemy
	ui64 danger = evaluateDanger (g.tile, g.hero.h);
	if (danger)
		strengthRatio = (fl::scalar)g.hero.h->getTotalStrength() / danger;

	try
	{
		vt.strengthRatio->setInputValue(strengthRatio);
		vt.heroStrength->setInputValue((fl::scalar)g.hero->getTotalStrength() / ai->primaryHero()->getTotalStrength());
		vt.turnDistance->setInputValue(turns);
		vt.missionImportance->setInputValue(missionImportance);

		vt.engine.process();
		//engine.process(VISIT_TILE); //TODO: Process only Visit_Tile
		g.priority = vt.value->getOutputValue();
	}
	catch (fl::Exception & fe)
	{
		logAi->error("evaluate VisitTile: %s",fe.getWhat());
	}
	assert (g.priority >= 0);
	return g.priority;
}
float FuzzyHelper::evaluate (Goals::VisitHero & g)
{
	auto obj = cb->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();
	return g.value / std::max(g.value - army, 1000.0f);
}

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 1;
}
float FuzzyHelper::evaluate (Goals::DigAtTile & g)
{
	return 0;
}
float FuzzyHelper::evaluate (Goals::CollectRes & g)
{
	return 0;
}
float FuzzyHelper::evaluate (Goals::Build & g)
{
	return 0;
}
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)
{
	g->setpriority(g->accept(this)); //this enforces returned value is set
}