forked from andor9/tyrant_optimize
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathalgorithms.cpp
1234 lines (1115 loc) · 44.5 KB
/
algorithms.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <vector>
#include "algorithms_util.h"
using namespace tuo;
inline bool try_improve_deck(Deck* d1, unsigned from_slot, unsigned to_slot, const Card* card_candidate,
const Card*& best_commander, const Card*& best_alpha_dominion, std::vector<const Card*>& best_cards,
FinalResults<long double>& best_score, unsigned& best_gap, std::string& best_deck,
std::unordered_map<std::string, EvaluatedResults>& evaluated_decks, EvaluatedResults& zero_results,
unsigned long& skipped_simulations, Process& proc, bool print
#ifndef NQUEST
, Quest & quest
#endif
)
{
unsigned deck_cost(0);
std::vector<std::pair<signed, const Card *>> cards_out, cards_in;
std::mt19937& re = proc.threads_data[0]->re;
// setup best deck
d1->commander = best_commander;
d1->alpha_dominion = best_alpha_dominion;
d1->cards = best_cards;
// try to adjust the deck
if (!adjust_deck(d1, from_slot, to_slot, card_candidate, fund, re, deck_cost, cards_out, cards_in))
{ return false; }
// check gap
unsigned new_gap = check_requirement(d1, requirement
#ifndef NQUEST
, quest
#endif
);
if ((new_gap > 0) && (new_gap >= best_gap))
{ return false; }
// check previous simulations
auto && cur_deck = d1->hash();
auto && emplace_rv = evaluated_decks.insert({cur_deck, zero_results});
auto & prev_results = emplace_rv.first->second;
if (!emplace_rv.second)
{
skipped_simulations += prev_results.second;
}
// Evaluate new deck
auto compare_results = proc.compare(best_score.n_sims, prev_results, best_score);
auto current_score = compute_score(compare_results, proc.factors);
// Is it better ?
if (new_gap < best_gap || current_score.points > best_score.points + min_increment_of_score)
{
// Then update best score/slot, print stuff
if(print)std::cout << "Deck improved: " << d1->hash() << ": " << card_slot_id_names(cards_out) << " -> " << card_slot_id_names(cards_in) << ": ";
best_gap = new_gap;
best_score = current_score;
best_deck = cur_deck;
best_commander = d1->commander;
best_alpha_dominion = d1->alpha_dominion;
best_cards = d1->cards;
if(print)print_score_info(compare_results, proc.factors);
if(print)print_deck_inline(deck_cost, best_score, d1);
return true;
}
return false;
}
//------------------------------------------------------------------------------
/*
* Calc value of current set deck in d1 (proc.your_decks[0])
*/
inline FinalResults<long double> fitness(Deck* d1,
FinalResults<long double>& best_score,
std::unordered_map<std::string, EvaluatedResults>& evaluated_decks, EvaluatedResults& zero_results,
unsigned long& skipped_simulations, Process& proc, bool compare = false)
{
// check previous simulations
auto && cur_deck = d1->hash();
//std::cout << "Deck hash: " << d1->hash() << " with ";
auto && emplace_rv = evaluated_decks.insert({cur_deck, zero_results});
auto & prev_results = emplace_rv.first->second;
if (!emplace_rv.second)
{
skipped_simulations += prev_results.second;
}
// Evaluate new deck
if (compare) {
auto compare_results= proc.compare(best_score.n_sims, prev_results,best_score);
auto current_score = compute_score(compare_results, proc.factors);
return current_score;
}
else
{
auto compare_results= proc.evaluate(best_score.n_sims, prev_results);
auto current_score = compute_score(compare_results, proc.factors);
//best_score = current_score;
//auto best_deck = d1->clone();
//print_score_info(compare_results, proc.factors);
//print_deck_inline(get_deck_cost(best_deck), best_score, best_deck);
return current_score;
}
}
//------------------------------------------------------------------------------
Deck* filter_best_deck(std::vector<Deck*> your_decks, Deck* d1,
FinalResults<long double>& best_score,
std::unordered_map<std::string, EvaluatedResults>& evaluated_decks, EvaluatedResults& zero_results,
unsigned long& skipped_simulations, Process& proc) {
Deck * cur_return = your_decks[0];
FinalResults<long double> cur_score;
for(unsigned i=1;i < your_decks.size();++i) //start with 1 since first always is simmed already
{
copy_deck(your_decks[i],d1);
cur_score = fitness(d1, best_score, evaluated_decks, zero_results, skipped_simulations, proc,true);
if(cur_score.points > best_score.points)
{
cur_return = your_decks[i];
best_score = cur_score;
std::cout << "Deck improved: " << d1->hash() <<":";
print_deck_inline(get_deck_cost(d1), best_score,d1);
}
}
return cur_return;
}
//------------------------------------------------------------------------------
DeckResults hill_climbing(unsigned num_min_iterations, unsigned num_iterations, std::vector<Deck*> your_decks , Process& proc, Requirement & requirement
#ifndef NQUEST
, Quest & quest
#endif
)
{
Deck * d1 = proc.your_decks[0];
EvaluatedResults zero_results = { EvaluatedResults::first_type(proc.enemy_decks.size()), 0 };
std::string best_deck = d1->hash();
std::unordered_map<std::string, EvaluatedResults> evaluated_decks{{best_deck, zero_results}};
EvaluatedResults& results = proc.evaluate(num_min_iterations, evaluated_decks.begin()->second);
print_score_info(results, proc.factors);
FinalResults<long double> best_score = compute_score(results, proc.factors);
unsigned long skipped_simulations = 0;
// use the best deck from all passed decks
copy_deck(filter_best_deck(your_decks, d1, best_score, evaluated_decks, zero_results, skipped_simulations, proc),d1);
// update freezed_cards
freezed_cards = std::min<unsigned>(opt_freezed_cards, d1->cards.size());
const Card* best_commander = d1->commander;
const Card* best_alpha_dominion = d1->alpha_dominion;
std::vector<const Card*> best_cards = d1->cards;
unsigned deck_cost = get_deck_cost(d1);
fund = std::max(fund, deck_cost);
print_deck_inline(deck_cost, best_score, d1,true);
std::mt19937& re = proc.threads_data[0]->re;
unsigned best_gap = check_requirement(d1, requirement
#ifndef NQUEST
, quest
#endif
);
bool is_random = (d1->strategy == DeckStrategy::random) || (d1->strategy == DeckStrategy::flexible);
bool deck_has_been_improved = true;
std::vector<const Card*> commander_candidates;
std::vector<const Card*> alpha_dominion_candidates;
std::vector<const Card*> card_candidates;
auto mixed_candidates = get_candidate_lists(proc);
commander_candidates = mixed_candidates.at(0);
alpha_dominion_candidates = mixed_candidates.at(1);
card_candidates = mixed_candidates.at(2);
// add current alpha dominion to candidates if necessary
// or setup first candidate into the deck if no alpha dominion defined
if (use_owned_dominions)
{
if (best_alpha_dominion)
{
if (!std::count(alpha_dominion_candidates.begin(), alpha_dominion_candidates.end(), best_alpha_dominion))
{
alpha_dominion_candidates.emplace_back(best_alpha_dominion);
}
}
else if (!alpha_dominion_candidates.empty())
{
best_alpha_dominion = d1->alpha_dominion = alpha_dominion_candidates[0];
}
if (debug_print > 0)
{
for (const Card* dom_card : alpha_dominion_candidates)
{
std::cout << " ** next Alpha Dominion candidate: " << dom_card->m_name
<< " ($: " << alpha_dominion_cost(dom_card) << ")" << std::endl;
}
}
if (!best_alpha_dominion && owned_alpha_dominion)
{
best_alpha_dominion = owned_alpha_dominion;
std::cout << "Setting up owned Alpha Dominion into a deck: " << best_alpha_dominion->m_name << std::endl;
}
}
//std::reverse(card_candidates.begin(), card_candidates.end());
// << main climbing loop >>
for (unsigned from_slot(freezed_cards), dead_slot(freezed_cards); ;
from_slot = std::max(freezed_cards, (from_slot + 1) % std::min<unsigned>(max_deck_len, best_cards.size() + 1)))
{
if(is_timeout_reached()){ break;}
if (deck_has_been_improved)
{
dead_slot = from_slot;
deck_has_been_improved = false;
}
else if (from_slot == dead_slot || best_score.points - target_score > -1e-9)
{
if (best_score.n_sims >= num_iterations || best_gap > 0)
{ break; } // exit main climbing loop
auto & prev_results = evaluated_decks[best_deck];
skipped_simulations += prev_results.second;
// Re-evaluate the best deck
d1->commander = best_commander;
d1->alpha_dominion = best_alpha_dominion;
d1->cards = best_cards;
auto evaluate_result = proc.evaluate(std::min(prev_results.second * iterations_multiplier, num_iterations), prev_results);
best_score = compute_score(evaluate_result, proc.factors);
print_score_info(evaluate_result, proc.factors);
dead_slot = from_slot;
}
if (best_score.points - target_score > -1e-9)
{ continue; }
// commander
if (requirement.num_cards.count(best_commander) == 0)
{
// << commander candidate loop >>
for (const Card* commander_candidate: commander_candidates)
{
if (best_score.points - target_score > -1e-9)
{ break; }
if (commander_candidate == best_commander)
{ continue; }
deck_has_been_improved |= try_improve_deck(d1, -1, -1, commander_candidate,
best_commander, best_alpha_dominion, best_cards, best_score, best_gap, best_deck,
evaluated_decks, zero_results, skipped_simulations, proc,true
#ifndef NQUEST
, quest
#endif
);
}
// Now that all commanders are evaluated, take the best one
d1->commander = best_commander;
d1->alpha_dominion = best_alpha_dominion;
d1->cards = best_cards;
}
// alpha dominion
if (use_owned_dominions && !alpha_dominion_candidates.empty())
{
// << alpha dominion candidate loop >>
for (const Card* alpha_dominion_candidate: alpha_dominion_candidates)
{
if (best_score.points - target_score > -1e-9)
{ break; }
if (alpha_dominion_candidate == best_alpha_dominion)
{ continue; }
deck_has_been_improved |= try_improve_deck(d1, -1, -1, alpha_dominion_candidate,
best_commander, best_alpha_dominion, best_cards, best_score, best_gap, best_deck,
evaluated_decks, zero_results, skipped_simulations, proc,true
#ifndef NQUEST
, quest
#endif
);
}
// Now that all alpha dominions are evaluated, take the best one
d1->commander = best_commander;
d1->alpha_dominion = best_alpha_dominion;
d1->cards = best_cards;
}
// shuffle candidates
std::shuffle(card_candidates.begin(), card_candidates.end(), re);
// << card candidate loop >>
//for (const Card* card_candidate: card_candidates)
for (auto it = card_candidates.begin(); it != card_candidates.end();++it)
{
const Card* card_candidate = *it;
for (unsigned to_slot(is_random ? from_slot : card_candidate ? freezed_cards : (best_cards.size() - 1));
to_slot < (is_random ? (from_slot + 1) : (best_cards.size() + (from_slot < best_cards.size() ? 0 : 1)));
++ to_slot)
{
if (card_candidate ?
(from_slot < best_cards.size() && (from_slot == to_slot && card_candidate == best_cards[to_slot])) // 2 Omega -> 2 Omega
:
(from_slot == best_cards.size())) // void -> void
{ continue; }
deck_has_been_improved |= try_improve_deck(d1, from_slot, to_slot, card_candidate,
best_commander, best_alpha_dominion, best_cards, best_score, best_gap, best_deck,
evaluated_decks, zero_results, skipped_simulations, proc,true
#ifndef NQUEST
, quest
#endif
);
}
if (best_score.points - target_score > -1e-9)
{ break; }
}
}
d1->commander = best_commander;
d1->alpha_dominion = best_alpha_dominion;
d1->cards = best_cards;
unsigned simulations = 0;
for (auto evaluation: evaluated_decks)
{ simulations += evaluation.second.second; }
std::cout << "Evaluated " << evaluated_decks.size() << " decks (" << simulations << " + " << skipped_simulations << " simulations)." << std::endl;
print_sim_card_values(d1,proc,num_iterations);
std::cout << "Optimized Deck: ";
print_deck_inline(get_deck_cost(d1), best_score, d1,true);
print_upgraded_cards(d1);
return std::make_pair(d1->clone(),best_score);
}
inline long double acceptanceProbability(long double old_score, long double new_score, long double temperature)
{
if(new_score > old_score)
{
return 1;
}
//100/max_score normalizes the acceptance probability with the used mode/score-range
//1000 is factor to set the temperature lower (can be changed indirect by setting begin-temperature and its decrease)
return exp(((new_score-old_score)/temperature*1000*100)/max_possible_score[(size_t)optimization_mode]);
}
DeckResults simulated_annealing(unsigned num_min_iterations, unsigned num_iterations, std::vector<Deck*> your_decks, Process& proc, Requirement & requirement
#ifndef NQUEST
, Quest & quest
#endif
)
{
Deck* cur_deck = proc.your_decks[0];
EvaluatedResults zero_results = { EvaluatedResults::first_type(proc.enemy_decks.size()), 0 };
//std::string best_deck = d1->hash();
std::unordered_map<std::string, EvaluatedResults> evaluated_decks{{cur_deck->hash(), zero_results}};
EvaluatedResults& results = proc.evaluate(num_min_iterations, evaluated_decks.begin()->second);
print_score_info(results, proc.factors);
FinalResults<long double> best_score = compute_score(results, proc.factors);
//const Card* best_commander = d1->commander;
//const Card* best_alpha_dominion = cur_deck->alpha_dominion;
//std::vector<const Card*> best_cards = cur_deck->cards;
unsigned long skipped_simulations = 0;
// use the best deck from all passed decks
copy_deck(filter_best_deck(your_decks, cur_deck, best_score, evaluated_decks, zero_results, skipped_simulations, proc),cur_deck);
// update freezed_cards
freezed_cards = std::min<unsigned>(opt_freezed_cards, cur_deck->cards.size());
unsigned deck_cost = get_deck_cost(cur_deck);
fund = std::max(fund, deck_cost);
print_deck_inline(deck_cost, best_score, cur_deck,true);
std::mt19937& re = proc.threads_data[0]->re;
unsigned cur_gap = check_requirement(cur_deck, requirement
#ifndef NQUEST
, quest
#endif
);
bool is_random = (cur_deck->strategy == DeckStrategy::random) || (cur_deck->strategy == DeckStrategy::flexible);
std::vector<const Card*> all_candidates;
auto mixed_candidates = get_candidate_lists(proc);
all_candidates.reserve(mixed_candidates.at(0).size()+mixed_candidates.at(1).size()+mixed_candidates.at(2).size());
all_candidates.insert(all_candidates.end(), std::make_move_iterator(mixed_candidates.at(0).begin()),std::make_move_iterator(mixed_candidates.at(0).end()));
all_candidates.insert(all_candidates.end(), std::make_move_iterator(mixed_candidates.at(1).begin()),std::make_move_iterator(mixed_candidates.at(1).end()));
all_candidates.insert(all_candidates.end(), std::make_move_iterator(mixed_candidates.at(2).begin()),std::make_move_iterator(mixed_candidates.at(2).end()));
//clear
mixed_candidates.at(0).clear(); mixed_candidates.at(0).shrink_to_fit();
mixed_candidates.at(1).clear(); mixed_candidates.at(1).shrink_to_fit();
mixed_candidates.at(2).clear(); mixed_candidates.at(2).shrink_to_fit();
mixed_candidates.clear();mixed_candidates.shrink_to_fit();
// add current alpha dominion to candidates if necessary
// or setup first candidate into the deck if no alpha dominion defined
if (use_owned_dominions)
{
if (cur_deck->alpha_dominion)
{
if (!std::count(all_candidates.begin(), all_candidates.end(), cur_deck->alpha_dominion))
{
all_candidates.emplace_back(cur_deck->alpha_dominion);
}
}
if (!cur_deck->alpha_dominion && owned_alpha_dominion)
{
cur_deck->alpha_dominion = owned_alpha_dominion;
std::cout << "Setting up owned Alpha Dominion into a deck: " << cur_deck->alpha_dominion->m_name << std::endl;
}
}
Deck* prev_deck = cur_deck->clone();
Deck* best_deck = cur_deck->clone();
FinalResults<long double> prev_score = best_score;
FinalResults<long double> cur_score = best_score;
unsigned best_gap = cur_gap;
deck_cost = 0;
unsigned from_slot(freezed_cards);
unsigned from_slot_tmp(freezed_cards);
unsigned to_slot(1);
if(debug_print >0)std::cout << "Starting Anneal" << std::endl;
while(temperature > 1 && !(best_score.points - target_score > -1e-9 || is_timeout_reached()))
{
cur_deck->commander = prev_deck->commander;
cur_deck->alpha_dominion = prev_deck->alpha_dominion;
cur_deck->cards = prev_deck->cards;
from_slot = std::max(freezed_cards, (from_slot+1) % std::min<unsigned>(max_deck_len, cur_deck->cards.size() +1));
const Card* candidate = all_candidates.at(std::uniform_int_distribution<unsigned>(0,all_candidates.size()-1)(re));
if((!candidate || (candidate->m_category == CardCategory::normal && candidate->m_type != CardType::commander && candidate->m_category != CardCategory::dominion_alpha)))
{
to_slot = std::uniform_int_distribution<unsigned>(is_random ? from_slot : candidate ? freezed_cards : (cur_deck->cards.size() -1),(is_random ? (from_slot+1) : (cur_deck->cards.size() + ( from_slot < cur_deck->cards.size() ? 0 : 1)))-1)(re);
if(candidate ?
(from_slot < cur_deck->cards.size() && (from_slot == to_slot && candidate == cur_deck->cards[to_slot]))
:
(from_slot == cur_deck->cards.size()))
{
continue;
}
from_slot_tmp = from_slot;
}
else if(candidate->m_type == CardType::commander && requirement.num_cards.count(cur_deck->commander) == 0)
{
cur_deck->commander = candidate;
from_slot_tmp = -1;
to_slot = -1;
}
else if(candidate->m_category == CardCategory::dominion_alpha && use_owned_dominions)
{
cur_deck->alpha_dominion = candidate;
from_slot_tmp = -1;
to_slot = -1;
}
else{
continue;
}
std::vector<std::pair<signed, const Card * >> cards_out, cards_in;
if (!adjust_deck(cur_deck, from_slot_tmp, to_slot, candidate, fund, re, deck_cost, cards_out, cards_in))
{ continue;}
cur_gap = check_requirement(cur_deck, requirement
#ifndef NQUEST
, quest
#endif
);
if ((cur_gap > 0) && (cur_gap >= best_gap))
{ continue; }
//same deck skip
if(cur_deck->hash().compare(prev_deck->hash())==0)continue;
cur_score = fitness(cur_deck, best_score, evaluated_decks, zero_results, skipped_simulations, proc);
if(acceptanceProbability(prev_score.points, cur_score.points , temperature) > std::uniform_real_distribution<double>(0,1)(re))
{
if(cur_score.points > best_score.points)
{
best_score = cur_score;
best_deck = cur_deck->clone();
best_gap = cur_gap;
std::cout << "Deck improved: " << best_deck->hash() << ": (temp=" << temperature << ") :";
print_deck_inline(get_deck_cost(best_deck), best_score, best_deck);
}
if(debug_print>0)std::cout << "UPDATED DECK: " << cur_deck->hash() << ": (temp=" << temperature << ") :";
if(debug_print>0)print_deck_inline(get_deck_cost(cur_deck), cur_score, cur_deck);
prev_score = cur_score;
prev_deck = cur_deck->clone();
}
temperature *=1-coolingRate;
}
unsigned simulations = 0;
for (auto evaluation: evaluated_decks)
{ simulations += evaluation.second.second; }
std::cout << "Evaluated " << evaluated_decks.size() << " decks (" << simulations << " + " << skipped_simulations << " simulations)." << std::endl;
print_sim_card_values(best_deck,proc,num_iterations);
std::cout << "Optimized Deck: ";
print_deck_inline(get_deck_cost(best_deck), best_score, best_deck,true);
print_upgraded_cards(best_deck);
return std::make_pair(best_deck->clone(),best_score);
}
void crossover(Deck* src1,Deck* src2, Deck* cur_deck, std::mt19937& re,unsigned best_gap,std::unordered_map<std::string, EvaluatedResults>& evaluated_decks
#ifndef NQUEST
, Quest & quest
#endif
)
{
cur_deck->commander = std::uniform_int_distribution<unsigned>(0, 1)(re)?src1->commander:src2->commander;
cur_deck->alpha_dominion = std::uniform_int_distribution<unsigned>(0, 1)(re)?src1->alpha_dominion:src2->alpha_dominion;
bool finished = false;
unsigned itr = 0;
while(!finished && itr < 100) //todo opt
{
itr++;
cur_deck->cards.clear();
for(unsigned it =0; it < std::max(src1->cards.size(),src2->cards.size());it++)
{
if(src1->cards.size() <=it)
{cur_deck->cards.push_back(src2->cards[it]);}
else if(src2->cards.size() <=it)
{cur_deck->cards.push_back(src1->cards[it]);}
else
{
cur_deck->cards.push_back(std::uniform_int_distribution<unsigned>(0, 1)(re)?src1->cards[it]:src2->cards[it]);
}
}
if(!valid_deck(cur_deck)) {continue;} //repeat
if(evaluated_decks.count(cur_deck->hash())){continue;} // deck already simmed
unsigned cur_gap = check_requirement(cur_deck, requirement
#ifndef NQUEST
, quest
#endif
);
if ((cur_gap > 0) && (cur_gap >= best_gap))
{ continue; }
finished = true; // exit while
}
if(!finished) copy_deck(std::uniform_int_distribution<unsigned>(0, 1)(re)?src1:src2,cur_deck);
}
void mutate(Deck* src, Deck* cur_deck, std::vector<const Card*> all_candidates, std::mt19937& re,unsigned best_gap,std::unordered_map<std::string, EvaluatedResults>& evaluated_decks
#ifndef NQUEST
, Quest & quest
#endif
)
{
copy_deck(src,cur_deck);
bool is_random = (cur_deck->strategy == DeckStrategy::random) || (cur_deck->strategy == DeckStrategy::flexible); //should be same for all decks from input!?
unsigned deck_cost = 0;
unsigned from_slot(freezed_cards);
unsigned from_slot_tmp(freezed_cards);
unsigned to_slot(1);
bool finished = false;
unsigned itr=0;
while(!finished && itr < 100) // todo opt
{
itr++;
copy_deck(src,cur_deck);
from_slot = std::uniform_int_distribution<unsigned>(1,std::min<unsigned>(max_deck_len-1, cur_deck->cards.size()))(re);
const Card* candidate = all_candidates.at(std::uniform_int_distribution<unsigned>(0,all_candidates.size()-1)(re));
if((!candidate || (candidate->m_category == CardCategory::normal && candidate->m_type != CardType::commander && candidate->m_category != CardCategory::dominion_alpha)))
{
to_slot = std::uniform_int_distribution<unsigned>(is_random ? from_slot : candidate ? freezed_cards : (cur_deck->cards.size() -1),(is_random ? (from_slot+1) : (cur_deck->cards.size() + ( from_slot < cur_deck->cards.size() ? 0 : 1)))-1)(re);
if(candidate ?
(from_slot < cur_deck->cards.size() && (from_slot == to_slot && candidate == cur_deck->cards[to_slot]))
:
(from_slot == cur_deck->cards.size()))
{
continue;
}
from_slot_tmp = from_slot;
}
else if(candidate->m_type == CardType::commander && requirement.num_cards.count(cur_deck->commander) == 0)
{
cur_deck->commander = candidate;
from_slot_tmp = -1;
to_slot = -1;
}
else if(candidate->m_category == CardCategory::dominion_alpha && use_owned_dominions)
{
cur_deck->alpha_dominion = candidate;
from_slot_tmp = -1;
to_slot = -1;
}
else{
continue;
}
std::vector<std::pair<signed, const Card * >> cards_out, cards_in;
if (!adjust_deck(cur_deck, from_slot_tmp, to_slot, candidate, fund, re, deck_cost, cards_out, cards_in))
{ continue;}
if(evaluated_decks.count(cur_deck->hash()))
{continue;} // deck already simmed
if(!valid_deck(cur_deck)) {continue;} //repeat
unsigned cur_gap = check_requirement(cur_deck, requirement
#ifndef NQUEST
, quest
#endif
);
if ((cur_gap > 0) && (cur_gap >= best_gap))
{ continue; }
finished = true; // exit while
}
if(!finished) copy_deck(src,cur_deck);
}
DeckResults genetic_algorithm(unsigned num_min_iterations, unsigned num_iterations, std::vector<Deck*> your_decks, Process& proc, Requirement & requirement
#ifndef NQUEST
, Quest & quest
#endif
)
{
//std::cerr << "START GENETIC" << std::endl;
if(pool_size==0){
if(your_decks.size()>min_pool_size) { //
pool_size = your_decks.size();
}
else {
pool_size = min_pool_size;
}
}
unsigned pool_cross = pool_size*opt_pool_cross/(opt_pool_mutate+opt_pool_cross+opt_pool_keep);
unsigned pool_keep = pool_size*opt_pool_keep/(opt_pool_mutate+opt_pool_cross+opt_pool_keep);
unsigned pool_mutate = pool_size-pool_cross-pool_keep;
//your_decks.size();
std::vector<std::pair<Deck*,FinalResults<long double>>> pool;
Deck* cur_deck = proc.your_decks[0];
EvaluatedResults zero_results = { EvaluatedResults::first_type(proc.enemy_decks.size()), 0 };
std::unordered_map<std::string, EvaluatedResults> evaluated_decks{{cur_deck->hash(), zero_results}};
EvaluatedResults& results = proc.evaluate(num_min_iterations, evaluated_decks.begin()->second);
print_score_info(results, proc.factors);
FinalResults<long double> best_score = compute_score(results, proc.factors); //init sim, todo remove
unsigned deck_cost = get_deck_cost(cur_deck);
fund = std::max(fund, deck_cost);
print_deck_inline(deck_cost, best_score, cur_deck,true);
std::mt19937& re = proc.threads_data[0]->re;
unsigned cur_gap = check_requirement(cur_deck, requirement
#ifndef NQUEST
, quest
#endif
);
unsigned long skipped_simulations = 0;
std::vector<const Card*> all_candidates;
auto mixed_candidates = get_candidate_lists(proc);
all_candidates.reserve(mixed_candidates.at(0).size()+mixed_candidates.at(1).size()+mixed_candidates.at(2).size());
all_candidates.insert(all_candidates.end(), std::make_move_iterator(mixed_candidates.at(0).begin()),std::make_move_iterator(mixed_candidates.at(0).end()));
all_candidates.insert(all_candidates.end(), std::make_move_iterator(mixed_candidates.at(1).begin()),std::make_move_iterator(mixed_candidates.at(1).end()));
all_candidates.insert(all_candidates.end(), std::make_move_iterator(mixed_candidates.at(2).begin()),std::make_move_iterator(mixed_candidates.at(2).end()));
//clear
mixed_candidates.at(0).clear(); mixed_candidates.at(0).shrink_to_fit();
mixed_candidates.at(1).clear(); mixed_candidates.at(1).shrink_to_fit();
mixed_candidates.at(2).clear(); mixed_candidates.at(2).shrink_to_fit();
mixed_candidates.clear();mixed_candidates.shrink_to_fit();
// add current alpha dominion to candidates if necessary
// or setup first candidate into the deck if no alpha dominion defined
for( auto tmp_deck : your_decks) // check all decks for owned dominions
{
if (use_owned_dominions)
{
if (tmp_deck->alpha_dominion)
{
if (!std::count(all_candidates.begin(), all_candidates.end(), tmp_deck->alpha_dominion))
{
all_candidates.emplace_back(tmp_deck->alpha_dominion);
}
}
if (!tmp_deck->alpha_dominion && owned_alpha_dominion)
{
tmp_deck->alpha_dominion = owned_alpha_dominion;
std::cout << "Setting up owned Alpha Dominion into a deck: " << tmp_deck->alpha_dominion->m_name << std::endl;
}
}
}
Deck* best_deck = cur_deck->clone();
FinalResults<long double> cur_score = best_score;
unsigned best_gap = cur_gap;
//fill pool
if(your_decks.size()>pool_size) your_decks.resize(pool_size); //TODO this drops potential better overflowing decks?!
for ( unsigned it =your_decks.size(); it < pool_size;it++)
{
unsigned j = std::uniform_int_distribution<unsigned>(0,your_decks.size()-1)(re);
unsigned i = std::uniform_int_distribution<unsigned>(0,your_decks.size()-1)(re);
Deck* nxt = your_decks[j]->clone();
if(std::uniform_int_distribution<unsigned>(0,1)(re))
{
mutate(your_decks[i],nxt,all_candidates,re,best_gap,evaluated_decks
#ifndef NQUEST
, quest
#endif
);
}
else
{
crossover(your_decks[i],your_decks[j],nxt,re,best_gap,evaluated_decks
#ifndef NQUEST
, quest
#endif
);
}
your_decks.push_back(nxt);
}
//sim pool
for( auto i_deck :your_decks)
{
copy_deck(i_deck,cur_deck);
cur_score = fitness(cur_deck, best_score, evaluated_decks, zero_results, skipped_simulations, proc,true);
pool.push_back(std::make_pair(i_deck,cur_score));
if(cur_score.points > best_score.points)
{
best_score = cur_score;
best_deck = cur_deck->clone();
best_gap = check_requirement(cur_deck, requirement
#ifndef NQUEST
, quest
#endif
);
std::cout << "Deck improved: " << best_deck->hash() <<":";
print_deck_inline(get_deck_cost(best_deck), best_score, best_deck);
}
}
for( unsigned gen= 0; gen< generations && !is_timeout_reached() ;gen++ )
{
std::cout << "GENERATION: " << gen << std::endl;
//sort
auto sort = [](std::pair<Deck*,FinalResults<long double>> l,std::pair<Deck*,FinalResults<long double>> r) {return l.second.points > r.second.points;};
std::sort(pool.begin(),pool.end(),sort);
//breed
//cross
for ( unsigned it = 0; it < pool_cross;it++)
{
unsigned i = std::uniform_int_distribution<unsigned>(0,pool_keep-1)(re);
unsigned j = std::uniform_int_distribution<unsigned>(pool_keep,pool_size-pool_mutate)(re);
//unsigned k= std::uniform_int_distribution<unsigned>(0,pool_size-pool_mutate)(re);
unsigned k = -1;
while (k >= pool_size-pool_mutate)
k=std::geometric_distribution<unsigned>(0.2)(re); //prefer crossover with strong decks
crossover(pool[i].first,pool[k].first,pool[j].first,re,best_gap, evaluated_decks
#ifndef NQUEST
, quest
#endif
);
//crossover(pool[it+pool_size/4*2].first,pool[it+pool_size/4*3].first,pool[it+pool_size/4*3].first,re,best_gap, evaluated_decks);
//crossover(pool[it].first,pool[(it+pool_size/8)%(pool_size/4)].first,pool[it+pool_size/4*2].first,re,best_gap, evaluated_decks);
//mutate(pool[it].first,pool[it+pool_size/4*3].first,all_candidates,re,best_gap, evaluated_decks);
}
//mutate pool_keep to replace lowest scores
for ( unsigned it = pool_size-pool_mutate; it < pool_size;it++)
{
unsigned i = std::uniform_int_distribution<unsigned>(0,pool_keep-1)(re);
//unsigned j = std::uniform_int_distribution<unsigned>(pool_keep,pool_size-1)(re);
mutate(pool[i].first,pool[it].first,all_candidates,re,best_gap, evaluated_decks
#ifndef NQUEST
, quest
#endif
);
}
//mutate duplicates
for ( unsigned it = 0; it < pool_size;it++)
{
for (unsigned i = it+1; i < pool_size;i++)
{
if(pool[it].first->alpha_dominion && pool[i].first->alpha_dominion && pool[it].first->hash().substr(8)==pool[i].first->hash().substr(8)) //ignore commander + dominion
{
mutate(pool[i].first->clone(),pool[i].first,all_candidates,re,best_gap, evaluated_decks
#ifndef NQUEST
, quest
#endif
);
FinalResults<long double> nil{0, 0, 0, 0, 0, 0, 1};
pool[i].second = nil; //lowest score approx Null
}
}
}
//calc fitness
for (unsigned it = pool_keep; it < pool_size; it++)
{
copy_deck(pool[it].first,cur_deck);
cur_score = fitness(cur_deck, best_score, evaluated_decks, zero_results, skipped_simulations, proc,true);
pool[it].second = cur_score;
if(cur_score.points > best_score.points)
{
best_score = cur_score;
best_deck = cur_deck->clone();
best_gap = check_requirement(cur_deck, requirement
#ifndef NQUEST
, quest
#endif
);
if(it < pool_size-pool_mutate)
{
if (debug_print >= 0)
std::cout << "Crossover: " <<std::endl;
std::cout << "Deck improved: " << best_deck->hash() <<":";
}
else
{
if (debug_print >= 0)
std::cout << "Mutation: " <<std::endl;
std::cout << "Deck improved: " << best_deck->hash() <<":";
}
print_deck_inline(get_deck_cost(best_deck), best_score, best_deck);
}
}
#ifndef NDEBUG
if (debug_print >= 0)
{
std::cout << "---------------POOL---------------" << std::endl;
for (unsigned it =0; it < pool.size();++it)
{
if(it==0)std::cout << "---------------KEEP---------------" << std::endl;
if(it==pool_keep)std::cout << "---------------CROSS--------------" << std::endl;
if(it==pool_keep+pool_cross)std::cout << "---------------MUTATE-------------" << std::endl;
auto a = pool[it];
print_deck_inline(get_deck_cost(a.first),a.second,a.first);
}
std::cout << "---------------PEND---------------" << std::endl;
}
#endif
}
unsigned simulations = 0;
for (auto evaluation: evaluated_decks)
{ simulations += evaluation.second.second; }
std::cout << "Evaluated " << evaluated_decks.size() << " decks (" << simulations << " + " << skipped_simulations << " simulations)." << std::endl;
print_sim_card_values(best_deck,proc,num_iterations);
std::cout << "Optimized Deck: ";
print_deck_inline(get_deck_cost(best_deck), best_score, best_deck,true);
print_upgraded_cards(best_deck);
return std::make_pair(best_deck->clone(),best_score);
}
unsigned factorial(unsigned n)
{
unsigned retval = 1;
for (int i = n; i > 1; --i)
retval *= i;
return retval;
}
void recursion(unsigned num_iterations, std::vector<unsigned> used, unsigned pool, std::vector<const Card*> forts,Process&proc, FinalResults<long double>& best_score,std::vector<const Card*> & best_forts,std::unordered_map<std::string,EvaluatedResults> & evaluated_decks, EvaluatedResults& zero_results, unsigned long& skipped_simulations)
{
if(used.size()==pool)
{
for(auto your_deck : proc.your_decks)
{
your_deck->fortress_cards.clear();
for(unsigned i = 0; i < pool;++i)
{
your_deck->fortress_cards.emplace_back(forts[used[i]]);
}
}
//sim
std::stringstream ios;
encode_deck_ext_b64(ios,proc.your_decks[0]->fortress_cards);
auto hash = ios.str();
auto && emplace_rv = evaluated_decks.insert({hash,zero_results});
auto & prev_results = emplace_rv.first->second;
if(!emplace_rv.second)
{
skipped_simulations += prev_results.second;
}
auto compare_results= proc.evaluate(num_iterations, prev_results);
auto current_score = compute_score(compare_results, proc.factors);
if(current_score.points > best_score.points+min_increment_of_score) {
best_score = current_score;
std::vector<const Card*> copy_forts(proc.your_decks[0]->fortress_cards);
best_forts = copy_forts;
std::cout << "Forts improved: " << hash << " : ";
print_score_info(compare_results, proc.factors);
print_score_inline(best_score);
std::cout << ": ";
print_cards_inline(best_forts);
}
used.clear();
used.shrink_to_fit();
}
else
{
for(unsigned i =0;i < forts.size();++i)
{
if(std::find(used.begin(),used.end(),i)==used.end()) //not contained
{
std::vector<unsigned> tmp_used (used);
tmp_used.emplace_back(i);
recursion(num_iterations,tmp_used,pool,forts,proc,best_score,best_forts,evaluated_decks,zero_results,skipped_simulations);
}
}
}
}
DeckResults forts_climbing(unsigned num_iterations, Process& proc) {
EvaluatedResults zero_results = { EvaluatedResults::first_type(proc.enemy_decks.size()*proc.your_decks.size()), 0 };
unsigned pool = std::get<0>(proc.your_decks[0]->variable_forts[0]);
std::vector<const Card*> forts(std::get<2>(proc.your_decks[0]->variable_forts[0]));
for(unsigned i =0; i < proc.your_decks.size();++i)
{
proc.your_decks[i]->variable_forts.clear();
}
std::vector<unsigned> used;
used.reserve(pool);
std::vector<const Card*> best_forts{pool};
FinalResults<long double> best_score{0, 0, 0, 0, 0, 0, 0};
unsigned long skipped_simulations{0};
std::unordered_map<std::string,EvaluatedResults> evaluated_decks{{"",zero_results}};
recursion(num_iterations,used,pool,forts, proc,best_score,best_forts,evaluated_decks,zero_results,skipped_simulations);
unsigned simulations = 0;
for (auto evaluation: evaluated_decks)
{ simulations += evaluation.second.second; }
std::cout << "Evaluated " << evaluated_decks.size() << " decks (" << simulations << " + " << skipped_simulations << " simulations)." << std::endl;
std::cout << "Optimized Deck: ";
print_cards_inline(best_forts);
Deck* tmp = proc.your_decks[0]->clone();
tmp->commander = nullptr;
tmp->alpha_dominion = nullptr;
tmp->cards = best_forts; // return forts
return std::make_pair(tmp,best_score);
}
inline bool contains(std::multimap<FinalResults<long double>, Deck*, std::greater<FinalResults<long double>>> best,Deck* d)
{
for(auto it = best.begin();it!=best.end();it++)
{
if(it->second->hash().compare(d->hash())==0) return true;
}
return false;
}
inline void check_and_update(std::multimap<FinalResults<long double>, Deck*, std::greater<FinalResults<long double>>>& best,Deck* cur_deck,bool & deck_has_been_improved, FinalResults<long double>& best_score )
{
auto tmp_result = (best.size()<pool_size)?0.:(std::next(best.begin(),pool_size))->first.points;
//std::cout << "IMP CMD" << std::endl;
if(best_score.points > tmp_result && !contains(best,cur_deck))
{
deck_has_been_improved = true;
if(best_score > best.begin()->first){
std::cout << "Deck improved: " << cur_deck->hash() <<":" << std::endl;
print_deck_inline(get_deck_cost(cur_deck), best_score, cur_deck);
}
best.insert(std::make_pair(best_score,cur_deck->clone()));
if(best.size()==pool_size+1)best.erase(std::prev(best.end(),1));
}
}
DeckResults beam_climb(unsigned num_min_iterations, unsigned num_iterations, std::vector<Deck*> your_decks, Process& proc, Requirement & requirement
#ifndef NQUEST
, Quest & quest
#endif
)
{
if(pool_size==0){
pool_size = min_beam_size;
}
//your_decks.size();
//std::vector<std::pair<Deck*,FinalResults<long double>>> pool;
//auto sort = [](FinalResults<long double> l,FinalResults<long double> r) {return l.points > r.points;};
std::multimap<FinalResults<long double>, Deck*, std::greater<FinalResults<long double>>> best;
Deck* cur_deck = proc.your_decks[0];
EvaluatedResults zero_results = { EvaluatedResults::first_type(proc.enemy_decks.size()), 0 };
std::unordered_map<std::string, EvaluatedResults> evaluated_decks{{cur_deck->hash(), zero_results}};