From 51eae063e1134964b9b8e15a8b43c181a286c481 Mon Sep 17 00:00:00 2001 From: Twan van Laarhoven Date: Tue, 19 Nov 2019 22:48:00 +0100 Subject: [PATCH] Budget for optimizer --- src/Makefile | 3 +++ src/simulation.hpp | 27 +++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Makefile b/src/Makefile index f2105b5..8bf4db3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -11,6 +11,9 @@ SOURCES = $(LIB_SOURCES) repl.cpp hsbg: $(SOURCES) *.hpp $(GXX) $(GXX_FLAGS) $(SOURCES) -O -o $@ +debug: $(SOURCES) *.hpp + $(GXX) $(GXX_FLAGS) $(SOURCES) -g -o hsbg + test: $(SOURCES) *.hpp $(GXX) $(GXX_FLAGS) $(LIB_SOURCES) test.cpp -g -o $@ diff --git a/src/simulation.hpp b/src/simulation.hpp index 08aa9cf..7826876 100644 --- a/src/simulation.hpp +++ b/src/simulation.hpp @@ -123,28 +123,35 @@ struct OptimizeMinionOrder { double best_score; int n; - OptimizeMinionOrder(Board const& board, Board const& enemy, int runs = DEFAULT_NUM_RUNS) { + OptimizeMinionOrder(Board const& board, Board const& enemy, int budget = DEFAULT_NUM_RUNS) { n = board.size(); + // number of permutations + int nperm = 1; + for (int i=1; i<=n; ++i) nperm *= i; + int runs = max(10, min(budget, budget * 50 / nperm)); + int full_runs = budget; // current situation std::array order; for (int i=0; i best_score) { + if (score > best_score) { best_score = score; best_order = order; } } while (std::next_permutation(order.begin(), order.begin() + n)); + // re-check with full number of runs, also to avoid multiple-testing bias + if (runs < full_runs && best_score > current_score) { + Battle battle(board, enemy); + permute_minions(battle.board[0], board.minions, best_order.data(), n); + best_score = simulate_optimization_score(battle, runs); + } } };