-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBoo.cpp
More file actions
415 lines (388 loc) · 13.3 KB
/
Boo.cpp
File metadata and controls
415 lines (388 loc) · 13.3 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
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
#include "Boo.h"
#include "BOBuilder.h"
#include "UnitTypes.h"
#include <iostream>
#include <sstream>
namespace suboo {
int setBest(BuildOrder & best, BuildOrder & candidate, const std::string & name) {
int besttotal = best.getFinal().getTimeStamp();
int bestmin = best.getFinal().getMinerals();
int bestwait = 0;
for (auto & bi : best.getItems()) {
bestwait += bi.totalWait();
}
if (timeBO(candidate)) {
auto total = candidate.getFinal().getTimeStamp();
auto bomin = candidate.getFinal().getMinerals();
int totalwait = 0;
for (auto & bi : candidate.getItems()) {
totalwait += bi.totalWait();
}
if (total < besttotal || (total <= besttotal && (totalwait < bestwait || totalwait == bestwait && (bomin > 1 + bestmin || ::abs(bomin - bestmin) < 1)))) {
auto deltat = total - besttotal;
auto deltamin = bomin - bestmin;
auto deltawait = totalwait - bestwait;
auto delta = deltat > 0 ? deltat : deltamin > 0 ? deltamin : deltawait;
best = candidate;
// std::cout << name << " feasible in " << total;
// std::cout << " mins=" << bomin;
// std::cout << " totalwait=" << totalwait;
// std::cout << " (new best)" << std::endl;
return delta;
}
}
return 0;
}
std::pair<int, BuildOrder> findBest(const BuildOrder & base, std::vector<BuildOrder> & candidates, std::vector<std::string> candDesc) {
int best = base.getFinal().getTimeStamp();
int bestmin = base.getFinal().getMinerals();
int bestwait = 0;
for (auto & bi : base.getItems()) {
bestwait += bi.totalWait();
}
int basewait = bestwait;
int bestindex = -1;
int index = 0;
for (auto & bo : candidates) {
if (timeBO(bo)) {
auto total = bo.getFinal().getTimeStamp();
auto bomin = bo.getFinal().getMinerals();
int totalwait = 0;
for (auto & bi : bo.getItems()) {
totalwait += bi.totalWait();
}
// std::cout << candDesc[index] << " feasible in " << total;
// std::cout << " mins=" << bomin;
// std::cout << " totalwait=" << totalwait;
if (total < best || (total <= best && (totalwait < bestwait || totalwait == bestwait && (bomin > 1 + bestmin || ::abs(bomin - bestmin) < 1 )))) {
bestindex = index;
bestmin = bomin;
best = total;
bestwait = totalwait;
// std::cout << " (new best)";
}
// std::cout << std::endl;
}
else {
// std::cout << candDesc[index] << " unfeasible." << std::endl;
}
index++;
}
auto delta = (base.getFinal().getTimeStamp() - best);
auto deltamin = bestmin - base.getFinal().getMinerals();
auto deltawait = bestwait - basewait;
return { delta > 0 ? delta : deltamin > 0 ? deltamin : deltawait, bestindex == -1 ? BuildOrder() : candidates[bestindex] };
}
std::pair<int, BuildOrder> Randomizer::improve(const BuildOrder & base, int depth) {
BuildOrder best = base;
for (int i = 0; i < 1000; i++) {
auto copy = best;
auto & deq = copy.getItems();
std::random_shuffle(deq.begin(), deq.end());
BOBuilder::enforcePrereqBySwap(copy);
if (timeBO(copy)) {
auto timefin = copy.getFinal().getTimeStamp();
auto timebest = best.getFinal().getTimeStamp();
if (timefin < timebest || timefin == timebest && (best.getFinal().getMinerals() - copy.getFinal().getMinerals()) >0) {
best = copy;
}
}
}
auto delta = (base.getFinal().getTimeStamp() - best.getFinal().getTimeStamp());
auto deltamin = (base.getFinal().getMinerals() - best.getFinal().getMinerals());
return { delta > 0 ? delta : deltamin , best };
}
std::pair<int, BuildOrder> LeftShifter::improve(const BuildOrder & base, int depth)
{
auto & tech = TechTree::getTechTree();
std::vector<BuildOrder> candidates;
std::vector<std::string > candindexes;
candidates.reserve(20);
// find any adjacent events with same timing
auto & items = base.getItems();
auto best = base;
GameState current = tech.getInitial();
for (int i = 0, e = items.size(); i < e; i++) {
auto & bi = items[i];
int skip = 0;
for (int j = i + 1; j < e; j++) {
auto & bj = items[j];
if (! (bj == bi)) {
bool biprecedesbj = true;
if (bi.getAction() == BUILD && bj.getAction() == BUILD) {
auto & ui = tech.getUnitById(bi.getTarget());
auto & uj = tech.getUnitById(bj.getTarget());
if (uj.prereq != ui.type && (uj.builder != ui.type || uj.builder == UnitId::PROTOSS_PROBE || current.hasFinishedUnit(uj.builder) )) {
biprecedesbj = false;
}
}
if (bj.getAction() == TRANSFER_VESPENE && bi.getAction() == BUILD && bi.getTarget() == UnitId::PROTOSS_ASSIMILATOR) {
}
else {
if (bi.getAction() == BUILD && bj.getAction() != BUILD) {
biprecedesbj = false;
}
}
if (bj.getTime() == bi.getTime() || !biprecedesbj) {
BuildOrder bo = base;
//std::stringstream sstr;
//sstr << "swapping (" << i << " :"; bo.getItems()[i].print(sstr);
//sstr << ") and (" << j << " :"; bo.getItems()[j].print(sstr);
//sstr << ")" ;
bo.swapItems(i, j);
if (!BOBuilder::enforcePrereqBySwap(bo)) {
std::cout << "problem with swap prereq rule " << std::endl;
}
candidates.push_back(bo);
//candindexes.push_back(sstr.str());
}
if (!(j < e - 1 && items[j + 1] == items[j])) {
break;
}
}
else {
//skip++;
}
}
if (bi.getAction() == BUILD) {
current.addUnit(bi.getTarget());
}
i += skip;
}
return findBest(base, candidates,candindexes);
}
std::pair<int, BuildOrder> AddVespeneGatherer::improve(const BuildOrder & base, int depth)
{
int nexi = base.getFinal().countUnit(UnitId::PROTOSS_NEXUS);
int ass = base.getFinal().countUnit(UnitId::PROTOSS_ASSIMILATOR);
if (2 *nexi > ass) {
BuildOrder candidate = base;
candidate.addItemFront(TRANSFER_VESPENE);
candidate.addItemFront(UnitId::PROTOSS_ASSIMILATOR);
if (timeBO(candidate)) {
int delta = base.getFinal().getTimeStamp() - candidate.getFinal().getTimeStamp() ;
if (delta > 0) {
return {delta, candidate };
}
else {
if (depth >0) {
candidate = BOBuilder::improveBO(candidate,std::min(depth-1,1));
if (timeBO(candidate)) {
//auto best = base;
//if (setBest(base,candidate))
std::vector<BuildOrder> candidates;
candidates.push_back(candidate);
return findBest(base, candidates , { "Add Gas forceful" });
}
}
}
}
}
return std::pair<int, BuildOrder>(0,BuildOrder());
}
std::pair<int, BuildOrder> AddMineralGatherer::improve(const BuildOrder & base, int depth)
{
std::vector<BuildOrder> candidates;
candidates.reserve(base.getItems().size());
std::vector<std::string> candnames;
bool needpylon = false;
if (base.getFinal().getAvailableSupply() == 0) {
needpylon = true;
}
auto & tech = TechTree::getTechTree();
int available = tech.getInitial().getAvailableSupply();
if ( base.getFinal().probesToSaturation() > 0) {
for (int i = 0, e = base.getItems().size(); i < e; i++) {
if (available >= 1) {
BuildOrder candidate = base;
candidate.insertItem(UnitId::PROTOSS_PROBE, i);
if (needpylon) {
if (i <= 3) {
candidate.insertItem(UnitId::PROTOSS_PYLON, 0);
}
else {
candidate.insertItem(UnitId::PROTOSS_PYLON, i - 3);
}
}
if (!BOBuilder::enforcePrereqBySwap(candidate)) {
std::cout << "problem with swap prereq rule " << std::endl;
}
candidates.push_back(candidate);
//candnames.push_back("Add Probe at index " + std::to_string(i));
// find the next pylons and bring them forward one
for (int j = i + 2; j < e; j++) {
auto & act = candidate.getItems()[j];
if (act.getAction() == BUILD && act.getTarget() == UnitId::PROTOSS_PYLON) {
candidate.swapItems(j, j - 1);
candidates.push_back(candidate);
//candnames.push_back("Add Probe at index " + std::to_string(i) + " and shift pylon at index " + std::to_string(j));
}
}
}
auto & bi = base.getItems()[i];
available += tech.getUnitById(bi.getTarget()).food_provided;
}
}
return findBest(base,candidates,candnames);
}
std::pair<int, BuildOrder> AddProduction::improve(const BuildOrder & base, int depth)
{
auto & tech = TechTree::getTechTree();
std::unordered_map<UnitId, int> used;
for (auto & bi : base.getItems()) {
if (bi.getAction() == BUILD) {
auto & u = tech.getUnitById(bi.getTarget());
if (u.builder != UnitId::INVALID && !sc2util::IsWorkerType(u.builder)) {
auto it = used.find(u.builder);
if (it == used.end()) {
used[u.builder] = 1;
}
else {
it->second++;
}
}
}
}
std::vector<BuildOrder> candidates;
candidates.reserve(base.getItems().size());
std::vector<std::string> candnames;
for (auto & pair : used) {
if (pair.second > base.getFinal().countUnit(pair.first)) {
auto & builder = tech.getUnitById(pair.first);
// try to stutter
int index = 0;
bool ok = false;
for (auto & bi : base.getItems()) {
if (ok || bi.getAction() == BUILD && bi.getTarget() == pair.first) {
auto candidate = base;
candidate.insertItem(pair.first, index);
candidates.push_back(candidate);
//candnames.push_back("Add production " + builder.name + " at step " + std::to_string(index));
ok = true;
}
index++;
}
}
}
return findBest(base, candidates, candnames);
}
std::pair<int, BuildOrder> NoWaitShifter::improve(const BuildOrder & base, int depth)
{
auto & tech = TechTree::getTechTree();
std::vector<BuildOrder> candidates;
std::vector<std::string > candindexes;
candidates.reserve(20);
// find any event with zero wait : left shift it
auto & items = base.getItems();
GameState current = tech.getInitial();
for (int i = 1, e = items.size(); i < e; i++) {
auto & bi = items[i];
if (bi == items[i - 1]) {
continue;
}
if (bi.getAction() == TRANSFER_VESPENE && items[i - 1].getTarget() == UnitId::PROTOSS_ASSIMILATOR) {
// causes build to be broken
continue;
}
if (bi.totalWait() == 0) {
BuildOrder bo = base;
//std::stringstream sstr;
//sstr << "swapping (" << i-1 << " :"; bo.getItems()[i-1].print(sstr);
//sstr << ") and (" << i << " :"; bo.getItems()[i].print(sstr);
//sstr << ")";
bo.swapItems(i, i-1);
candidates.push_back(bo);
//candindexes.push_back(sstr.str());
if (bo.getItems()[i].totalWait() == 0) {
for (int j = i - 2; j >= 0; j--) {
// chains of nowait, try swapping the blocking thing with any of the non blocked events
if (bo.getItems()[j].totalWait() == 0) {
continue;
}
else {
BuildOrder bo = base;
//std::stringstream sstr;
//sstr << "swapping (" << j << " :"; bo.getItems()[j].print(sstr);
//sstr << ") and (" << i << " :"; bo.getItems()[i].print(sstr);
//sstr << ")";
bo.swapItems(i, j);
candidates.push_back(bo);
//candindexes.push_back(sstr.str());
break;
}
}
}
}
}
return findBest(base, candidates, candindexes);
}
std::pair<int, BuildOrder> AddMineralGathererStack::improve(const BuildOrder & base, int depth)
{
if (base.getFinal().probesToSaturation() > 0) {
auto copy = base;
int i = 0, tobuild = base.getFinal().probesToSaturation();
for (; i < tobuild && i < 3; i++) {
copy.addItemFront(UnitId::PROTOSS_PROBE);
}
copy = BOBuilder::enforcePrereq(copy);
if (timeBO(copy)) {
if (depth >0)
copy = BOBuilder::improveBO(copy, std::min(depth-1,1));
std::vector<BuildOrder> candidates;
candidates.push_back(copy);
return findBest(base, candidates, { "optimizedSaturation" });
}
}
return std::pair<int, BuildOrder>(0, BuildOrder());
}
std::pair<int, BuildOrder> AddProductionForceful::improve(const BuildOrder & base,int depth)
{
auto & tech = TechTree::getTechTree();
std::map<UnitId, int> waitFor;
for (auto & bi : base.getItems()) {
if (bi.getAction() == BUILD) {
if (bi.timeFree > 0) {
auto & unit = tech.getUnitById(bi.getTarget());
if (unit.builder != UnitId::INVALID) {
waitFor[unit.builder] += bi.timeFree;
}
}
}
}
std::vector<std::pair<UnitId, int> > pairs(waitFor.begin(), waitFor.end());
std::sort(pairs.begin(), pairs.end(), [](auto & a, auto & b) { return a.second > b.second; });
std::vector<BuildOrder> candidates;
std::vector<std::string > candindexes;
for (auto & pair : pairs) {
if (pair.second >= 30) {
auto copy = base;
copy.addItem(pair.first);
copy = BOBuilder::enforcePrereq(copy);
if (timeBO(copy)) {
if (depth >0)
copy = BOBuilder::improveBO(copy,std::min(depth-1,3));
candidates.push_back(copy);
//candindexes.push_back("Forcefully add production :" + tech.getUnitById(pair.first).name);
}
}
}
return findBest(base, candidates, candindexes);
}
std::pair<int, BuildOrder> RemoveExtra::improve(const BuildOrder & base, int depth)
{
std::vector<BuildOrder> candidates;
std::vector<std::string > candindexes;
int foodPer = TechTree::getTechTree().getUnitById(UnitId::PROTOSS_PYLON).food_provided;
if (base.getFinal().getAvailableSupply() > foodPer) {
auto bo = base;
// find last pylon
for (auto it = bo.getItems().begin() + (bo.getItems().size() - 1), e = bo.getItems().begin(); it != e; --it) {
if (it->getAction() == BUILD && it->getTarget() == UnitId::PROTOSS_PYLON) {
bo.getItems().erase(it);
break;
}
}
}
return std::pair<int, BuildOrder>();
}
}