Learning better divided into functions
This commit is contained in:
parent
24a4d45640
commit
b2a572efb6
1 changed files with 74 additions and 39 deletions
113
solver.hpp
113
solver.hpp
|
@ -9,11 +9,8 @@ struct Game {
|
||||||
int N, M;
|
int N, M;
|
||||||
// Table of NxM saying which position can hold which color
|
// Table of NxM saying which position can hold which color
|
||||||
vector<vector<bool>> possible;
|
vector<vector<bool>> possible;
|
||||||
// Searched sequence
|
|
||||||
vector<int> sequence;
|
|
||||||
Game(int p_N, int p_M) : N(p_N), M(p_M) {
|
Game(int p_N, int p_M) : N(p_N), M(p_M) {
|
||||||
possible = vector<vector<bool>>(N, vector<bool>(M, 1));
|
possible = vector<vector<bool>>(N, vector<bool>(M, 1));
|
||||||
sequence = vector<int>(N, -1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Learning functions
|
// Learning functions
|
||||||
|
@ -29,7 +26,22 @@ struct Game {
|
||||||
for(int n = 0; n < N; n++)
|
for(int n = 0; n < N; n++)
|
||||||
possible[n][col] = 0;
|
possible[n][col] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Checking
|
||||||
|
int final_color(int n) {
|
||||||
|
int final_col, count = 0;
|
||||||
|
for(int col = 0; col < M; col++)
|
||||||
|
if(possible[n][col]) {
|
||||||
|
final_col = col;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count == 1)
|
||||||
|
return final_col;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print known information
|
||||||
void print() {
|
void print() {
|
||||||
cout << " ";
|
cout << " ";
|
||||||
for(int col = 0; col < M; col++)
|
for(int col = 0; col < M; col++)
|
||||||
|
@ -44,57 +56,80 @@ struct Game {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// For remembering guesses with their responses
|
||||||
|
struct Historic_guess {
|
||||||
|
vector<int> guess, response;
|
||||||
|
Historic_guess(vector<int> p_guess, vector<int> p_response) {
|
||||||
|
guess = p_guess;
|
||||||
|
response = p_response;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class Solver {
|
class Solver {
|
||||||
Game known;
|
Game known;
|
||||||
vector<vector<int>> history = {};
|
vector<Historic_guess> history = {};
|
||||||
|
vector<int> sequence;
|
||||||
public:
|
public:
|
||||||
Solver(int p_N, int p_M) : known({p_N, p_M}) {}
|
Solver(int p_N, int p_M) : known({p_N, p_M}) {
|
||||||
|
sequence = vector<int>(p_N, -1);
|
||||||
|
}
|
||||||
vector<int> guess() {
|
vector<int> guess() {
|
||||||
// TODO
|
// TODO
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
void learn_back(vector<int> historic_guess) {
|
|
||||||
|
// Specific reactions
|
||||||
|
void if_not_here_then_nowhere(vector<int> guess) {
|
||||||
|
// Get all positions of these colors
|
||||||
|
auto positions_of_colors = vector<vector<int>>(known.M, vector<int>(0));
|
||||||
|
for(int n = 0; n < known.N; n++)
|
||||||
|
positions_of_colors[guess[n]].push_back(n);
|
||||||
|
|
||||||
|
// If color can't be anywhere here, it can't be in the sequence
|
||||||
|
for(int col = 0; col < known.M; col++) {
|
||||||
|
int possible_count = 0;
|
||||||
|
for(int n : positions_of_colors[col])
|
||||||
|
if(known.possible[n][col])
|
||||||
|
possible_count++;
|
||||||
|
if(possible_count == 0 && positions_of_colors[col].size() > 0)
|
||||||
|
known.empty_color(col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void not_here(vector<int> guess) {
|
||||||
|
for(int n = 0; n < known.N; n++)
|
||||||
|
known.cannot_be(n, guess[n]);
|
||||||
|
}
|
||||||
|
void empty(vector<int> guess) {
|
||||||
|
for(int col : guess)
|
||||||
|
known.empty_color(col);
|
||||||
|
}
|
||||||
|
|
||||||
|
void learn_back(Historic_guess hist) {
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
void learn(vector<int> guess, vector<int> response) {
|
void learn(vector<int> guess, vector<int> response) {
|
||||||
// Reduce color positions
|
// None of these colors are there
|
||||||
if(response[0] == 0 && response[1] == 0) { // None of these colors are there
|
if(response[0] == 0 && response[1] == 0)
|
||||||
for(int col : guess)
|
empty(guess);
|
||||||
known.empty_color(col);
|
|
||||||
}
|
|
||||||
else if(response[1] == 0) { // None at the right spot
|
|
||||||
// Not here
|
|
||||||
for(int n = 0; n < known.N; n++)
|
|
||||||
known.cannot_be(n, guess[n]);
|
|
||||||
}
|
|
||||||
else if(response[0] == 0) { // At least only on the right spot
|
|
||||||
// Get all positions of these colors
|
|
||||||
auto positions_of_colors = vector<vector<int>>(known.M, vector<int>(0));
|
|
||||||
for(int n = 0; n < known.N; n++)
|
|
||||||
positions_of_colors[guess[n]].push_back(n);
|
|
||||||
|
|
||||||
// If color can't be anywhere here, it can't be in the sequence
|
// None at the right spot
|
||||||
for(int col = 0; col < known.M; col++) {
|
else if(response[1] == 0)
|
||||||
int possible_count = 0;
|
not_here(guess);
|
||||||
for(int n : positions_of_colors[col])
|
|
||||||
if(known.possible[n][col])
|
// At least only on the right spot
|
||||||
possible_count++;
|
else if(response[0] == 0)
|
||||||
if(possible_count == 0 && positions_of_colors[col].size() > 0)
|
if_not_here_then_nowhere(guess);
|
||||||
known.empty_color(col);
|
|
||||||
}
|
// TODO nonzero / nonzero
|
||||||
}
|
else
|
||||||
else {
|
{}
|
||||||
// TODO nonzero / nonzero
|
|
||||||
}
|
|
||||||
|
|
||||||
// Learn from previous guesses
|
// Learn from previous guesses
|
||||||
for(vector<int> past_guess : history)
|
for(auto hist : history)
|
||||||
learn_back(past_guess);
|
learn_back(hist);
|
||||||
|
|
||||||
// Write to history
|
// Write to history
|
||||||
for(int r : response)
|
history.push_back({guess, response});
|
||||||
guess.push_back(r);
|
|
||||||
history.push_back(guess);
|
|
||||||
}
|
}
|
||||||
void print() {
|
void print() {
|
||||||
known.print();
|
known.print();
|
||||||
|
|
Loading…
Reference in a new issue