-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnhancedCostCalculator.cpp
More file actions
235 lines (173 loc) · 7.44 KB
/
EnhancedCostCalculator.cpp
File metadata and controls
235 lines (173 loc) · 7.44 KB
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
//
// Enhanced Cost Calculator Implementation
//
#include "EnhancedCostCalculator.h"
#include <algorithm>
#if defined(OPTIMIZE_MODE)
#include "SimpleParameterOptimizer.h"
double EnhancedCostCalculator::calculateGCost(const Genome &genome, int action, double preGCost) {
// Base cost is turn number (maintains depth-first preference)
double gCost = preGCost + CostParams::turnHeignt;
// Add fine-grained action costs to break ties
gCost += getActionCost(action);
if (genome.AllyPlayer.PoisonEnable == true && action == BattleEmulator::SPECIAL_ANTIDOTE) {
gCost -= 0.1;
}
return gCost;
}
double EnhancedCostCalculator::calculateHCost(const Genome &genome, double enemyMaxHp, double playerMaxHp) {
if (genome.EnemyPlayer.hp <= 0) {
return 0.0; // Goal reached
}
double hCost = 0.0;
// Primary heuristic: enemy HP ratio (scaled down for better granularity)
hCost = (genome.EnemyPlayer.hp / enemyMaxHp) * CostParams::enemyHpWeight;
// Player HP consideration (more granular than original)
double playerHpRatio = genome.AllyPlayer.hp / playerMaxHp;
hCost += (1.0 - playerHpRatio) * CostParams::playerHpWeight;
// MP consideration (resource management)
hCost += calculateResourceCost(genome) * CostParams::resourceWeight;
// Status effect penalties/bonuses
hCost += calculateStatusEffectCost(genome) * CostParams::StatusEffectWeight;
return hCost;
}
double EnhancedCostCalculator::getActionCost(int action) {
switch (action) {
case BattleEmulator::ATTACK_ALLY:
return CostParams::AttackPenalty;
case BattleEmulator::DRAGON_SLASH:
return CostParams::dragonSlashPenalty;
case BattleEmulator::HEAL:
return CostParams::healPenalty;
case BattleEmulator::MEDICINAL_HERBS:
return CostParams::itemHealPenalty;
case BattleEmulator::DEFENCE:
return CostParams::defensePenalty;
case BattleEmulator::FLEE_ALLY:
return CostParams::fleePenalty;
case BattleEmulator::CRACK_ALLY:
return CostParams::buffPenalty;
case BattleEmulator::ACROBATIC_STAR:
return CostParams::specialPenalty;
case BattleEmulator::SPECIAL_ANTIDOTE:
return CostParams::antidotePenalty;
case BattleEmulator::SPECIAL_MEDICINE:
return CostParams::itemHealPenalty;
case BattleEmulator::WOOSH_ALLY:
return 0.3;
default:
return 0.1; // Default moderate penalty
}
}
double EnhancedCostCalculator::calculateStatusEffectCost(const Genome &genome) {
double statusCost = 0.0;
// Negative status effects (penalties)
if (genome.AllyPlayer.paralysis) statusCost += CostParams::paralysisWeight;
if (genome.AllyPlayer.sleeping) statusCost += CostParams::sleepWeight;
if (genome.AllyPlayer.PoisonEnable) statusCost += CostParams::poisonWeight;
// Positive status effects (bonuses - negative cost)
statusCost -= genome.AllyPlayer.BuffLevel * 0.1;
statusCost -= genome.AllyPlayer.AtkBuffLevel * 0.1;
statusCost -= genome.AllyPlayer.TensionLevel * 0.05;
// Special abilities
if (genome.AllyPlayer.acrobaticStar) statusCost -= CostParams::SpHeight;
if (genome.AllyPlayer.specialCharge) statusCost -= CostParams::ActHeight;
return statusCost;
}
double EnhancedCostCalculator::calculateResourceCost(const Genome &genome) {
double resourceCost = 0.0;
// MP consideration
if (genome.AllyPlayer.maxMp > 0) {
double mpRatio = static_cast<double>(genome.AllyPlayer.mp) / genome.AllyPlayer.maxMp;
resourceCost += (1.0 - mpRatio) * 0.5; // Penalty for low MP
}
// Item count considerations (rough estimates)
if (genome.AllyPlayer.SpecialMedicineCount <= 1 && genome.AllyPlayer.SpecialAntidoteCount <= 1) {
resourceCost += 0.2; // Penalty for low healing items
}
return resourceCost;
}
#else
double EnhancedCostCalculator::calculateGCost(const Genome &genome, int action, double preGCost) {
// Base cost is turn number (maintains depth-first preference)
double gCost = preGCost + 2.0;
// Add fine-grained action costs to break ties
gCost += getActionCost(action);
if (genome.AllyPlayer.PoisonEnable == true && action == BattleEmulator::SPECIAL_ANTIDOTE) {
gCost -= 0.1;
}
return gCost;
}
double EnhancedCostCalculator::calculateHCost(const Genome &genome, double enemyMaxHp, double playerMaxHp) {
if (genome.EnemyPlayer.hp <= 0) {
return 0.0; // Goal reached
}
double hCost = 0.0;
// Primary heuristic: enemy HP ratio (scaled down for better granularity)
hCost = (genome.EnemyPlayer.hp / enemyMaxHp) * 31.0;
// Player HP consideration (more granular than original)
double playerHpRatio = genome.AllyPlayer.hp / playerMaxHp;
hCost += (1.0 - playerHpRatio) * 2.0;
// MP consideration (resource management)
hCost += calculateResourceCost(genome);
// Status effect penalties/bonuses
hCost += calculateStatusEffectCost(genome);
return hCost;
}
double EnhancedCostCalculator::getActionCost(int action) {
switch (action) {
case BattleEmulator::ATTACK_ALLY:
return 0.01;
case BattleEmulator::DRAGON_SLASH:
return 0.01; // Offensive actions have no penalty
case BattleEmulator::HEAL:
return 0.01; // Slight penalty for healing
case BattleEmulator::MEDICINAL_HERBS:
return 0.03; // Less penalty for item healing
case BattleEmulator::DEFENCE:
return 0.05; // Higher penalty for defensive actions
case BattleEmulator::FLEE_ALLY:
return 0.1; // High penalty for fleeing
case BattleEmulator::CRACK_ALLY:
return 0.05; // Small penalty for buff spells
case BattleEmulator::ACROBATIC_STAR:
return 0.0001; // Small penalty for special abilities
case BattleEmulator::SPECIAL_ANTIDOTE:
return 0.001000;
case BattleEmulator::SPECIAL_MEDICINE:
return 0.015;
case BattleEmulator::WOOSH_ALLY:
return 0.1;
default:
return 0.1; // Default moderate penalty
}
}
double EnhancedCostCalculator::calculateStatusEffectCost(const Genome &genome) {
double statusCost = 0.0;
// Negative status effects (penalties)
if (genome.AllyPlayer.paralysis) statusCost += 1.0;
if (genome.AllyPlayer.sleeping) statusCost += 1.5;
if (genome.AllyPlayer.PoisonEnable) statusCost += 0.5;
// Positive status effects (bonuses - negative cost)
statusCost -= genome.AllyPlayer.BuffLevel * 0.1;
statusCost -= genome.AllyPlayer.AtkBuffLevel * 0.1;
statusCost -= genome.AllyPlayer.TensionLevel * 0.05;
// Special abilities
if (genome.AllyPlayer.acrobaticStar) statusCost -= 0.2;
if (genome.AllyPlayer.specialCharge) statusCost -= 0.2;
return statusCost;
}
double EnhancedCostCalculator::calculateResourceCost(const Genome &genome) {
double resourceCost = 0.0;
// MP consideration
if (genome.AllyPlayer.maxMp > 0) {
double mpRatio = static_cast<double>(genome.AllyPlayer.mp) / genome.AllyPlayer.maxMp;
resourceCost += (1.0 - mpRatio) * 0.5; // Penalty for low MP
}
// Item count considerations (rough estimates)
if (genome.AllyPlayer.SpecialMedicineCount <= 1 && genome.AllyPlayer.SpecialAntidoteCount <= 1) {
resourceCost += 0.2; // Penalty for low healing items
}
return resourceCost;
}
#endif