-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework11.cpp
677 lines (632 loc) · 16.5 KB
/
homework11.cpp
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_map>
#include <map>
#include <string>
#include <assert.h>
#include <set>
#include <algorithm>
#include <random>
#include <chrono>
#include <time.h>
using namespace std;
typedef int PosF2[2];
typedef int Pos;
#define ZQY_DEBUG_PRINT 1
#define GET_ROW(pos,size) ((pos)/(size))
#define GET_COL(pos,size) (pos%size)
#define GET_IDX(row,col,size) ((row)*(size)+(col))
#define EMPTY '0'
#define LIZARD '1'
#define TREE '2'
typedef vector<char> T_NursaryLayout;
struct Cell
{
char state; // '0' = empty, '1' = lizard; '2' = tree;
int attackCount;
};
// A node denoting the decision tree node
// set<Pos> denotes the unique identifier of the set
// Affected Positions denotes the positions affected by the current map
struct LizardPlacement
{
set<Pos> lizardPos;
set<Pos> affectedPositions; // This should be lazily evaluated
set<Pos> availablePositions; // This should be needed to generate next set of item
set<Pos> availableRows;
set<Pos> availableCols;
bool evaluated;
};
bool comparePlacementOpt(LizardPlacement& A, LizardPlacement& B) { return min(A.availableCols.size(), A.availableRows.size()) < min(B.availableCols.size(), B.availableRows.size()); }
bool comparePlacement(LizardPlacement& A, LizardPlacement& B) { return A.availablePositions.size() < B.availablePositions.size(); }
class ZooKeeper
{
public:
void parseInput();
void generateOutput();
void evaluateNursery(string mode = "DFS");
void printLayout(T_NursaryLayout& layout, int size);
private:
// Place the lizard on Position
bool placeLizard(Pos pos);
// Remove the lizard from Position
bool removeLizard(Pos pos);
bool canKillEachOther(Pos Pos0, Pos Pos1, vector<Pos> trees);
void SAInit();
int SACalculateAttackCount();
void SAUpdate();
double SARandomProbability();
inline bool isSameRow(Pos Pos0, Pos Pos1, int size)
{
return (floor(Pos0 / size) == floor(Pos1 / size));
}
inline bool isSameCol(Pos Pos0, Pos Pos1, int size)
{
return Pos0 % size == Pos1 % size;
}
inline bool isSameDiag(Pos Pos0, Pos Pos1, int size)
{
return abs(GET_ROW(Pos0,size) - GET_ROW(Pos1,size)) == abs(GET_COL(Pos0,size) - GET_COL(Pos1,size));
}
void evaluateAvailablePositions(LizardPlacement& placement);
// Prebake affected positions based on layouts
void computeAffectedPositions();
public:
string m_Instruction;
int m_NurserySize;
int m_NumLizards;
T_NursaryLayout m_NurseryLayout;
T_NursaryLayout m_FinalLayout;
bool m_foundResult;
T_NursaryLayout m_WorkingLayout;
set<Pos> m_SALizardPositions;
int m_SATemperature;
int m_SAIterationCount;
vector<Pos> m_Trees; // a vector to store the set of trees present in the nursery
unordered_map<Pos, vector<Pos>> m_precomputedAffectedPositions;
};
int main()
{
ZooKeeper myKeeper;
myKeeper.parseInput();
myKeeper.evaluateNursery(myKeeper.m_Instruction);
myKeeper.generateOutput();
return 0;
}
void ZooKeeper::parseInput()
{
std::ifstream inFile("input.txt");
inFile >> m_Instruction;
inFile >> m_NurserySize;
inFile >> m_NumLizards;
string temp;
while (inFile >> temp)
{
assert(temp.size() == m_NurserySize);
for(int i = 0; i < m_NurserySize; i++)
{
// original input
m_NurseryLayout.push_back(temp[i]);//storing the input in the mNurseryLayout
m_FinalLayout.push_back(temp[i]);
// working map
Cell cell;//generate a state node for this tree
cell.state = temp[i];
m_WorkingLayout.emplace_back(temp[i]);//emplace and not pushback as we dont need a memory copy
// tree cache
if (temp[i] == TREE)
{
m_Trees.push_back(m_NurseryLayout.size()-1);
}
}
}
}
void ZooKeeper::generateOutput()
{
std::ofstream outFile;
outFile.open("output.txt");
if (m_foundResult)
{
outFile << "OK" << endl;
for (int i = 0; i < m_NurserySize; i++)
{
for (int j = 0; j < m_NurserySize; j++)
{
outFile << m_FinalLayout[GET_IDX(i,j,m_NurserySize)];
}
outFile << endl;
}
}
else
{
outFile << "FAIL" << endl;
}
}
void ZooKeeper::evaluateNursery(string mode)
{
computeAffectedPositions();
if (mode == "DFS")
{
// Initialized the stack for dfs
stack<LizardPlacement> dfsStack;
LizardPlacement node;
node.evaluated = false;
evaluateAvailablePositions(node);
dfsStack.emplace(node);
// Find the next available spot in the next row
int lizardPlaced = 0;
while (lizardPlaced < m_NumLizards && !dfsStack.empty())//try to convert to for loop
{
LizardPlacement currentPlacement = dfsStack.top();
dfsStack.pop();
if(!currentPlacement.availablePositions.empty()) // search depth
{
vector<LizardPlacement> tempList;
for (Pos p : currentPlacement.availablePositions)
{
LizardPlacement newNode;
newNode.lizardPos = currentPlacement.lizardPos; // Is this a copy constructor?
newNode.lizardPos.insert(p);
newNode.evaluated = false;
evaluateAvailablePositions(newNode);
tempList.emplace_back(newNode);
}
std::sort(tempList.begin(), tempList.end(), comparePlacement);
for (auto n : tempList)//try to use alternative of auto
{
dfsStack.push(n);//
}
lizardPlaced = dfsStack.top().lizardPos.size();
}
else // pop stack
{
lizardPlaced = currentPlacement.lizardPos.size();
}
}
if(lizardPlaced == m_NumLizards)//solution found
{
assert(lizardPlaced == dfsStack.top().lizardPos.size());
// write out put to working copy
for (Pos p : dfsStack.top().lizardPos)
{
assert(m_FinalLayout[p] == EMPTY);
m_FinalLayout[p] = LIZARD;
}
m_foundResult = true;//flag a result found
}
else
{
assert(dfsStack.empty());
m_foundResult = false;
}
}
else if (mode == "BFS")
{
//#TODO: BFS is super slow now, there need to be a mechanism to accelerate the methods
//#QUSTION: is BFS searching all possible combinations in nth Lizard layer a good way?
queue<LizardPlacement> bfsQueue;
LizardPlacement node;
node.evaluated = false;
evaluateAvailablePositions(node);
bfsQueue.emplace(node);
int lizardPlaced = 0;
while (lizardPlaced < m_NumLizards && !bfsQueue.empty())
{
LizardPlacement currentPlacement = bfsQueue.front();
bfsQueue.pop();
if (!currentPlacement.availablePositions.empty())
{
for(Pos p : currentPlacement.availablePositions)
{
LizardPlacement newNode;
newNode.lizardPos = currentPlacement.lizardPos;
newNode.lizardPos.insert(p);
newNode.evaluated = false;
evaluateAvailablePositions(newNode);
bfsQueue.emplace(newNode);
}
vector<LizardPlacement> tempList;
std::sort(tempList.begin(), tempList.end(), comparePlacement);
for (auto n : tempList)
{
bfsQueue.push(n);
}
lizardPlaced = currentPlacement.lizardPos.size()+1;
}
else
{
lizardPlaced = currentPlacement.lizardPos.size();
}
}
if (lizardPlaced == m_NumLizards)//solution found
{
assert(lizardPlaced == bfsQueue.front().lizardPos.size());
for (Pos p : bfsQueue.front().lizardPos)
{
assert(m_FinalLayout[p] == EMPTY);
m_FinalLayout[p] = LIZARD;
}
m_foundResult = true;
}
else
{
assert(bfsQueue.empty());
m_foundResult = false;
}
}
else if (mode == "SA")
{
// #TODO: figure out deltaE and T for SA in this implementation
// Set up the map with random
SAInit();
while (m_SATemperature > 0)
{
SAUpdate();
m_SAIterationCount++;
#if ZQY_DEBUG_PRINT
// cout << "Current temperature = " << m_SATemperature << endl;
// cout << "Current iteration count = " << m_SAIterationCount << endl;
#endif
}
if (m_SATemperature == 0)
{
m_foundResult = true;
m_FinalLayout = std::move(m_WorkingLayout);
}
}
else
{
// should not reach this point at all
assert(false);
}
}
void ZooKeeper::printLayout(T_NursaryLayout& layout, int size)
{
assert(layout.size() == size*size);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout << layout[GET_IDX(i, j, size)];
}
cout << endl;
}
}
bool ZooKeeper::placeLizard(Pos pos)
{
return true;
}
bool ZooKeeper::removeLizard(Pos pos)
{
return true;
}
bool ZooKeeper::canKillEachOther(Pos Pos0, Pos Pos1, vector<Pos> trees)
{
if (isSameRow(Pos0,Pos1,m_NurserySize)) // Same row
{
for (Pos treePos : trees)
{
if (isSameRow(Pos0, treePos, m_NurserySize) && (Pos0 - treePos) * (Pos1 - treePos) < 0) // Tree in between
{
return true;
}
}
}
else if(isSameCol(Pos0,Pos1,m_NurserySize)) // Same col
{
for (Pos treePos : trees)
{
if (isSameCol(Pos0, treePos, m_NurserySize) && (Pos0 - treePos) * (Pos1 - treePos) < 0)
{
return true;
}
}
}
else if (isSameDiag(Pos0,Pos1,m_NurserySize)) // Same diag
{
for (Pos treePos : trees)
{
if (isSameDiag(Pos0, treePos, m_NurserySize) && isSameDiag(Pos1, treePos, m_NurserySize) && (Pos0 - treePos) * (Pos1 - treePos) < 0)
{
return true;
}
}
}
else
{
return false;
}
return false;
}
void ZooKeeper::SAInit()
{
// This function places the lizards randomly inside the working space
// Then calculates the current T
for (int i = 0; i < m_NumLizards; i++)
{
srand(i);
int row = i % m_NurserySize;
int col = rand() % m_NurserySize;
int idx = GET_IDX(row, col, m_NurserySize);
while (m_WorkingLayout[idx] != EMPTY)
{
idx = (idx + 1) % (m_NurserySize*m_NurserySize);
}
m_WorkingLayout[idx] = LIZARD;
m_SALizardPositions.insert(idx);
}
// calculate the conflict score T every turn
m_SATemperature = SACalculateAttackCount();
m_SAIterationCount = 0;
#if ZQY_DEBUG_PRINT
// printLayout(m_WorkingLayout, m_NurserySize);
#endif
}
int ZooKeeper::SACalculateAttackCount()
{
int count = 0;
for (auto pos : m_SALizardPositions)
{
for (auto attackedPos : m_precomputedAffectedPositions[pos])
{
if (m_SALizardPositions.find(attackedPos) != m_SALizardPositions.end())
{
count++;
}
}
}
#if ZQY_DEBUG_PRINT
// cout << "Attack count: " << count << endl;
#endif
return count;
}
void ZooKeeper::SAUpdate()
{
// pure randomized selection of movement converges slowly.
// Add a selection for the most promising move?
// What heuristics is needed?
auto itLizard(m_SALizardPositions.begin());
double advancement = SARandomProbability() * m_SALizardPositions.size();
advance(itLizard, floor(advancement));
int randEmptyPos = floor(SARandomProbability() * (m_NurserySize*m_NurserySize));
int curPos = *itLizard;
while (m_WorkingLayout[randEmptyPos] != EMPTY)
{
randEmptyPos = (randEmptyPos+1) % (m_NurserySize*m_NurserySize);
}
int curAttackCount = 0;
int newAttackCount = 0;
for (auto pos : m_precomputedAffectedPositions[curPos])
{
if (m_SALizardPositions.find(pos) != m_SALizardPositions.end() && pos != curPos)
{
curAttackCount+=2;
}
}
for (auto pos : m_precomputedAffectedPositions[randEmptyPos])
{
if (m_SALizardPositions.find(pos) != m_SALizardPositions.end() && pos != curPos)
{
newAttackCount+=2;
}
}
srand(time(nullptr));
#if ZQY_DEBUG_PRINT
//cout << "Current Attack Count is" << curAttackCount << " at " << curPos << endl;
//cout << "Current Attack Count is" << newAttackCount << " at " << randEmptyPos << endl;
#endif
if (newAttackCount < curAttackCount)
{
// accept
m_SATemperature = m_SATemperature - curAttackCount + newAttackCount;
m_WorkingLayout[randEmptyPos] = LIZARD;
m_WorkingLayout[curPos] = EMPTY;
m_SALizardPositions.insert(randEmptyPos);
m_SALizardPositions.erase(curPos);
#if ZQY_DEBUG_PRINT
// cout << "Accepted move from " << curPos << " to " << randEmptyPos << endl;
#endif
}
else
{
double pAccept = exp(((double)curAttackCount - (double)newAttackCount) / (double)m_SATemperature);
if (SARandomProbability() < pAccept)
{
// accept with probability
m_SATemperature = m_SATemperature - curAttackCount + newAttackCount;
m_WorkingLayout[randEmptyPos] = LIZARD;
m_WorkingLayout[*itLizard] = EMPTY;
m_SALizardPositions.insert(randEmptyPos);
m_SALizardPositions.erase(*itLizard);
#if ZQY_DEBUG_PRINT
// cout << "Accepted move from " << curPos << " to " << randEmptyPos << "with probability" << pAccept << endl;
#endif
}
else
{
#if ZQY_DEBUG_PRINT
// cout << "Rejected move from " << curPos << " to " << randEmptyPos << "with probability" << pAccept << endl;
#endif
}
}
#if ZQY_DEBUG_PRINT
printLayout(m_WorkingLayout, m_NurserySize);
#endif
}
double ZooKeeper::SARandomProbability()//check and see what all these methods mean
{
mt19937_64 rng;
uint64_t timeSeed = chrono::high_resolution_clock::now().time_since_epoch().count();
seed_seq ss{ uint32_t(timeSeed & 0xffffffff),uint32_t(timeSeed >> 32) };
rng.seed(ss);
std::uniform_real_distribution<double> unif(0, 1);
double res = unif(rng);
#if ZQY_DEBUG_PRINT
//cout << "Random number generated = " << res << endl;
#endif
return res;
}
void ZooKeeper::evaluateAvailablePositions(LizardPlacement& placement)
{
if (!placement.evaluated)
{
for (auto p : placement.lizardPos)
{
vector<Pos>& affectedPos = m_precomputedAffectedPositions.find(p)->second;
for (auto pp : affectedPos)
{
placement.affectedPositions.insert(pp);
}
}
for (int i = 0; i < m_NurserySize*m_NurserySize; i++)
{
if (m_NurseryLayout[i] == EMPTY
&& placement.affectedPositions.find(i) == placement.affectedPositions.end()
&& placement.lizardPos.find(i) == placement.lizardPos.end())
{
placement.availablePositions.insert(i);
}
}
for (Pos p : placement.availablePositions)
{
placement.availableCols.insert(GET_COL(p, m_NurserySize));
placement.availableRows.insert(GET_ROW(p, m_NurserySize));
}
placement.evaluated = true;
}
}
void ZooKeeper::computeAffectedPositions()
{
// Question: what's the cost and benefit of writing this? If trees are very few, there is no point, just check if trees block between the two
for (int i = 0; i < m_NurserySize * m_NurserySize; i++)
{
vector<Pos> attackingRange;
// Mark Row cells attacked by from i
{
int col = GET_COL(i,m_NurserySize);
int row = GET_ROW(i, m_NurserySize);
int minTree = row * m_NurserySize - 1;
int maxTree = (row + 1) * m_NurserySize;
for (int j = row * m_NurserySize; j < (row+1)* m_NurserySize; j++)
{
//#TODO: can be optimized and shorter
if (j < i && m_NurseryLayout[j] == TREE && minTree < j)
{
minTree = j;
}
else if (j > i && m_NurseryLayout[j] == TREE && maxTree > j)
{
maxTree = j;
}
}
for (int j = minTree + 1; j < maxTree; j++)
{
if(j!=i)
{
attackingRange.push_back(j);
}
}
}
// Mark Col cells attacked by from i
{
int row = GET_ROW(i, m_NurserySize);
int col = GET_COL(i, m_NurserySize);
int minTree = -m_NurserySize + col;
int maxTree = m_NurseryLayout.size();
// Test all the trees in this col
for (int j = col; j < m_NurserySize * m_NurserySize + col; j += m_NurserySize)
{
//#TODO: can be optimized and shorter
if (j < i && m_NurseryLayout[j] == TREE && minTree < j)
{
minTree = j;
}
else if (j > i && m_NurseryLayout[j] == TREE && maxTree > j)
{
maxTree = j;
}
}
// store attacking rows from pos i
for (int j = minTree + m_NurserySize; j < maxTree; j += m_NurserySize)
{
if(j!=i)
{
attackingRange.push_back(j);
}
}
}
// Mark \ diagnal
{
int row = GET_ROW(i, m_NurserySize)-1;
int col = GET_COL(i, m_NurserySize)-1;
while (row >= 0 && col >= 0)
{
int idx = GET_IDX(row, col, m_NurserySize);
if (m_NurseryLayout[idx] != TREE)
{
attackingRange.push_back(idx);
row--;
col--;
}
else
{
break;
}
}
row = GET_ROW(i, m_NurserySize) + 1;
col = GET_COL(i, m_NurserySize) + 1;
while (row < m_NurserySize && col < m_NurserySize)
{
int idx = GET_IDX(row, col, m_NurserySize);
if (m_NurseryLayout[idx] != TREE)
{
attackingRange.push_back(idx);
row++;
col++;
}
else
{
break;
}
}
}
// Mark / diagnal
{
int row = GET_ROW(i, m_NurserySize) - 1;
int col = GET_COL(i, m_NurserySize) + 1;
while (row >= 0 && row < m_NurserySize && col >= 0 && col < m_NurserySize)
{
int idx = GET_IDX(row, col, m_NurserySize);
if (m_NurseryLayout[idx] != TREE)
{
attackingRange.push_back(idx);
row--;
col++;
}
else
{
break;
}
}
row = GET_ROW(i, m_NurserySize) + 1;
col = GET_COL(i, m_NurserySize) - 1;
while (row >= 0 && row < m_NurserySize && col >= 0 && col < m_NurserySize)
{
int idx = GET_IDX(row, col, m_NurserySize);
if (m_NurseryLayout[idx] != TREE)
{
attackingRange.push_back(idx);
row++;
col--;
}
else
{
break;
}
}
}
m_precomputedAffectedPositions.emplace(std::pair<Pos, vector<Pos>>(i,attackingRange));
}
}