aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Harley2019-04-10 14:20:39 +0100
committerTom Harley2019-04-10 14:20:39 +0100
commit7bb83894eb74e66a79d5184333d5ff8db6ece091 (patch)
tree2d015178cd273b77db9c6c9ca0128de0c0c1c5ff
parentEnable reading deal seeds from a file (diff)
downloadmetasol-7bb83894eb74e66a79d5184333d5ff8db6ece091.tar.gz
metasol-7bb83894eb74e66a79d5184333d5ff8db6ece091.zip

Add voters to vote when vote is split

-rw-r--r--centre.cpp29
-rw-r--r--debug.h2
-rw-r--r--metasol.cpp129
-rw-r--r--metasol.hpp29
4 files changed, 133 insertions, 56 deletions
diff --git a/centre.cpp b/centre.cpp
index ac5fab7..a14451d 100644
--- a/centre.cpp
+++ b/centre.cpp
@@ -568,7 +568,11 @@ ms_settings get_settings(user_data *d) {
s.reserved_move_count = 0u;
s.max_concurrent_threads = 24u;
s.max_concurrent_games = 5u;
- s.vote_count = 10u;
+
+ s.initial_vote_count = 10u;
+ s.vote_increase_step = 100u;
+ s.max_vote_count = 1000u;
+ s.agree_ratio = 80u;
s.user_data = (void *) d;
@@ -814,6 +818,29 @@ void json_settings(ms_settings *s, user_data *d, const char *filename) {
arp("run_node_limit", NUMBER_TYPE, &d->run_timeout);
arp("run-node-limit", NUMBER_TYPE, &d->run_timeout);
+ arp("initial vote count", NUMBER_TYPE, &s->initial_vote_count);
+ arp("initial_vote_count", NUMBER_TYPE, &s->initial_vote_count);
+ arp("initial-vote-count", NUMBER_TYPE, &s->initial_vote_count);
+
+ arp("max vote count", NUMBER_TYPE, &s->max_vote_count);
+ arp("max_vote_count", NUMBER_TYPE, &s->max_vote_count);
+ arp("max-vote-count", NUMBER_TYPE, &s->max_vote_count);
+
+ arp("vote increase step", NUMBER_TYPE, &s->vote_increase_step);
+ arp("vote_increase_step", NUMBER_TYPE, &s->vote_increase_step);
+ arp("vote-increase-step", NUMBER_TYPE, &s->vote_increase_step);
+
+ arp("vote increase magnitude", NUMBER_TYPE,
+ &s->vote_increase_magnitude);
+ arp("vote_increase_magnitude", NUMBER_TYPE,
+ &s->vote_increase_magnitude);
+ arp("vote-increase-magnitude", NUMBER_TYPE,
+ &s->vote_increase_magnitude);
+
+ arp("agree ratio", NUMBER_TYPE, &s->agree_ratio);
+ arp("agree_ratio", NUMBER_TYPE, &s->agree_ratio);
+ arp("agree-ratio", NUMBER_TYPE, &s->agree_ratio);
+
} default: {
assert(false);
}
diff --git a/debug.h b/debug.h
index 96447b2..2f1cbc0 100644
--- a/debug.h
+++ b/debug.h
@@ -9,7 +9,7 @@
# define debug(...)
#endif
-static inline char *move_str_buf(ms_move *m) {
+static inline char *move_str_buf(const ms_move *m) {
static const int buf_size = 30;
static char buf[buf_size];
static char *buf_p = &buf[0];
diff --git a/metasol.cpp b/metasol.cpp
index daac9ad..9a6ff9f 100644
--- a/metasol.cpp
+++ b/metasol.cpp
@@ -355,9 +355,13 @@ ms_settings fetch_default_settings() {
s.user_data = NULL;
s.max_concurrent_threads = 1u;
s.max_concurrent_games = 1u;
- s.vote_count = 100u;
- s.forever = false;
+ s.initial_vote_count = 10u;
+ s.max_vote_count = 1000u;
+ s.vote_increase_step = 0u;
+ s.vote_increase_magnitude = 10u;
+ s.agree_ratio = 0;
s.seed = 0u;
+ s.forever = false;
return s;
}
@@ -707,20 +711,25 @@ void move_decided(ms_game_state *gs, ms_rules *r, ms_move *move, vote *votes,
* the only voters that don't need to recalculate their votes, the
* other voters are reset.
*/
+ unsigned reset_count = 0u;
for (unsigned i = 0; i < vote_count; ++i) {
if (votes[i].result == SOLUTION_FOUND) {
if (equal_moves(move, &votes[i].moves.back())) {
votes[i].moves.pop_back();
} else {
- debug("voter %d is being reset\n", i);
+ reset_count++;
votes[i].result = CANCELLED;
}
}
}
+ if (reset_count) {
+ debug("%u voters reset\n", reset_count);
+ }
+
}
}
-bool find_majority_move(vote *v, unsigned vote_count, ms_move *m) {
+bool find_majority_move(vote *v, unsigned vote_count, float r, ms_move *m) {
std::map<ms_move, unsigned, votecmp> votes;
for (unsigned i = 0; i < vote_count; ++i) {
@@ -731,18 +740,19 @@ bool find_majority_move(vote *v, unsigned vote_count, ms_move *m) {
if (t == votes.end()) {
votes[move] = 1;
} else {
- unsigned x = t->second + 1;
-
- if (x > vote_count / 2) {
- *m = t->first;
- return true;
- } else {
- votes[move] = x;
- }
+ votes[move] = t->second + 1;
}
}
}
+ for (auto &vote : votes) {
+ float vr = ((float) vote.second) / ((float) vote_count);
+ if (vr > 0.5f) {
+ *m = vote.first;
+ return vr >= r;
+ }
+ }
+
return false;
}
@@ -750,9 +760,9 @@ typedef std::vector<std::future<void>> jobs;
void run_new_voters(ms_game_state *gs, ms_rules *r, ms_settings *s,
ctpl::thread_pool *thpool, ms_game_state *ss, vote *v, thread_info *ti,
- jobs *fs, unsigned n) {
+ jobs *fs, unsigned vote_count, unsigned prev_vote_count) {
- for (unsigned i = 0; i < n; ++i) {
+ for (unsigned i = prev_vote_count; i < vote_count; ++i) {
/*
* Move sequences that still represent the game need not be overwritten.
@@ -772,11 +782,17 @@ void run_new_voters(ms_game_state *gs, ms_rules *r, ms_settings *s,
}
}
-bool find_modal_move(vote *v, unsigned n, ms_move *m) {
+bool find_satisfying_modal_move(vote *v, unsigned n, float r, bool ignore_ratio,
+ ms_move *m) {
+ /*
+ * Group all votes by value.
+ */
std::map<ms_move, unsigned, votecmp> votes;
+ unsigned success_count = 0u;
for (unsigned i = 0; i < n; ++i) {
if (v[i].result == SOLUTION_FOUND) {
+ success_count++;
assert(!v[i].moves.empty());
ms_move move = v[i].moves.back();
@@ -790,10 +806,14 @@ bool find_modal_move(vote *v, unsigned n, ms_move *m) {
}
}
+ /*
+ * Find most-occurring vote.
+ */
struct { ms_move m; unsigned c; } max;
max.c = 0;
for (auto &vote : votes) {
+ debug("%s: %u votes\n", move_str_buf(&vote.first), vote.second);
if (vote.second > max.c) {
max.m = vote.first;
max.c = vote.second;
@@ -802,6 +822,8 @@ bool find_modal_move(vote *v, unsigned n, ms_move *m) {
if (max.c == 0) {
return false;
+ } else if (((float) max.c) / ((float) success_count) < r && !ignore_ratio) {
+ return false;
} else {
*m = max.m;
return true;
@@ -848,9 +870,6 @@ solve_status run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
ctpl::thread_pool *thpool, vote *votes, thread_info *t_infos,
ms_game_state *shuffled_states) {
- unsigned vote_count = s->vote_count;
- ms_move m;
-
/*
* If there are no face-down cards, then any voter run should tell us if the
* deal is solvable. This function is only entered if a voter found a
@@ -861,50 +880,56 @@ solve_status run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
return SOLVED;
}
- /*
- * Set up the thread_info structs and add a job to the threadpool for each
- * one.
- */
- jobs fs;
- run_new_voters(gs, r, s, thpool, shuffled_states, votes, t_infos, &fs,
- vote_count);
+ unsigned vote_count = s->initial_vote_count;
+ unsigned previous_vote_count = 0u;
+ float agree_frac = s->agree_ratio / 100.0f;
+ ms_move m;
/*
- * Wait on all of the voters to complete.
+ * Do this until a move is made
*/
- for (auto &f : fs) {
- assert(f.valid());
- f.wait();
- }
+ while (true) {
- /*
- * Print information about each voter.
- */
- debug( "%-15s %-10s %3s\n", "state", "move", "moves left");
- for (unsigned i = 0; i < s->vote_count; ++i) {
- if (votes[i].moves.empty()) {
- debug("%-15s\n", finished_state_str(votes[i].result));
+ /*
+ * Set up the thread_info structs and add a job to the threadpool for
+ * each one.
+ */
+ jobs fs;
+ run_new_voters(gs, r, s, thpool, shuffled_states, votes, t_infos, &fs,
+ vote_count, previous_vote_count);
+
+ /*
+ * Wait on all of the voters to complete.
+ */
+ for (auto &f : fs) {
+ assert(f.valid());
+ f.wait();
+ }
+
+ /*
+ * Find the move with the most votes, if it passes the required ratio.
+ */
+ if (find_satisfying_modal_move(votes, vote_count, agree_frac,
+ vote_count == s->max_vote_count, &m)) {
+ debug("modal move found: %s\n", move_str_buf(&m));
+ move_decided(gs, r, &m, votes, vote_count);
+ break;
} else {
- ms_move m = votes[i].moves.back();
- debug("%-15s %-10s %3lu\n", finished_state_str(votes[i].result),
- move_str_buf(&m),
- votes[i].moves.size());
+ previous_vote_count = vote_count;
+ vote_count += s->vote_increase_step;
+ vote_count *= s->vote_increase_magnitude;
+ vote_count = std::min(vote_count, s->max_vote_count);
+ debug("modal move not found; using %u voters\n", vote_count);
}
- }
- /*
- * Find the move with the most votes.
- */
- if (find_modal_move(votes, vote_count, &m)) {
- move_decided(gs, r, &m, votes, vote_count);
}
/*
* If a majority of voters already agree on a move, running other voters is
* a waste of time.
*/
- while (find_majority_move(votes, vote_count, &m)) {
- debug("majority move found\n");
+ while (find_majority_move(votes, vote_count, agree_frac, &m)) {
+ debug("majority move found: %s\n", move_str_buf(&m));
move_decided(gs, r, &m, votes, vote_count);
}
@@ -914,13 +939,13 @@ solve_status run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
int ms_run(ms_game_state *gs, ms_rules *r, ms_settings *s,
ctpl::thread_pool *tp) {
- std::vector<vote> votes(s->vote_count);
+ std::vector<vote> votes(s->max_vote_count);
for (vote &v : votes) {
v.result = CANCELLED;
}
- std::vector<thread_info> t_infos(s->vote_count);
- std::vector<ms_game_state> states(s->vote_count);
+ std::vector<thread_info> t_infos(s->max_vote_count);
+ std::vector<ms_game_state> states(s->max_vote_count);
solve_status x;
do {
diff --git a/metasol.hpp b/metasol.hpp
index 573e741..c2b31ce 100644
--- a/metasol.hpp
+++ b/metasol.hpp
@@ -759,9 +759,34 @@ typedef struct {
unsigned max_concurrent_games;
/**
- * How many votes are used to determine the next move.
+ * How many voters are initially used to determine the next move.
*/
- unsigned vote_count;
+ unsigned initial_vote_count;
+
+ /**
+ * The number of voters to add to the voting pool each time voters are
+ * added.
+ */
+ unsigned vote_increase_step;
+
+ /**
+ * The magnitude with which to increase the number of voters being used each
+ * time voters are added.
+ *
+ * Applied after @ref vote_increase_step.
+ */
+ unsigned vote_increase_magnitude;
+
+ /**
+ * The maximum number of voters to use when determining the next move.
+ */
+ unsigned max_vote_count;
+
+ /**
+ * The ratio of votes required for a satisfying vote. This is honoured
+ * unless the max voter count has already been reached.
+ */
+ unsigned agree_ratio;
/**
* The seed used when shuffling face-down cards around.