Skip to content

Commit

Permalink
perf: optimize code 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
Joker2770 committed Aug 22, 2024
1 parent 4daae15 commit 6e29371
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/gomoku.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool Gomoku::set_rule(unsigned int rule_flag)

bool Gomoku::is_illegal(unsigned int x, unsigned int y)
{
return x > this->n - 1 || y > this->n - 1 || this->board[x][y] != 0;
return (x > this->n - 1 || y > this->n - 1 || this->board[x][y] != 0);
}

std::vector<int> Gomoku::get_legal_moves()
Expand Down
4 changes: 2 additions & 2 deletions src/onnx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ NeuralNetwork::~NeuralNetwork()
this->shared_session.reset();
}

std::future<NeuralNetwork::return_type> NeuralNetwork::commit(Gomoku *gomoku)
std::future<NeuralNetwork::return_type> NeuralNetwork::commit(const Gomoku *gomoku)
{
std::vector<float> state = transorm_gomoku_to_Tensor(gomoku);

Expand Down Expand Up @@ -282,7 +282,7 @@ std::vector<float> NeuralNetwork::transorm_board_to_Tensor(const board_type &boa
// return cat({ state0, state1, state2 }, 1);
}

std::vector<float> NeuralNetwork::transorm_gomoku_to_Tensor(Gomoku *gomoku)
std::vector<float> NeuralNetwork::transorm_gomoku_to_Tensor(const Gomoku *gomoku)
{
return NeuralNetwork::transorm_board_to_Tensor(gomoku->get_board(), gomoku->get_last_move(), gomoku->get_current_color());
}
Expand Down
4 changes: 2 additions & 2 deletions src/onnx.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ class NeuralNetwork
// void save_weights(std::string model_path);
~NeuralNetwork();

std::future<return_type> commit(Gomoku *gomoku); // commit task to queue
std::future<return_type> commit(const Gomoku *gomoku); // commit task to queue
// std::shared_ptr<torch::jit::script::Module> module; // torch module origin:private
static std::vector<float> transorm_gomoku_to_Tensor(Gomoku *gomoku);
static std::vector<float> transorm_gomoku_to_Tensor(const Gomoku *gomoku);
static std::vector<float> transorm_board_to_Tensor(const board_type &board, int last_move, int cur_player);

bool set_batch_size(unsigned int u_batch_size);
Expand Down
2 changes: 1 addition & 1 deletion src/play.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void SelfPlay::play(unsigned int saved_id)
while (game_state.first == 0)
{
// std::cout << "game id: " << saved_id << std::endl;
double temp = step < EXPLORE_STEP ? 1.0 : 1e-3;
double temp = (step < EXPLORE_STEP) ? 1.0 : 1e-3;
auto action_probs = mcts->get_action_probs(g.get(), temp);
// auto action_probs = m->get_action_probs(g.get(), 1);
// int best_action = m->get_best_action_from_prob(action_probs);
Expand Down
2 changes: 1 addition & 1 deletion src/train_and_eval/train_eval_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void play_for_eval(NeuralNetwork *a, NeuralNetwork *b, bool a_first, int *win_ta
// std::cout << episode << " th game!!" << std::endl;
while (game_state.first == 0)
{
int res = (step + a_first) % 2 ? ma.get_best_action(g.get()) : mb.get_best_action(g.get());
int res = ((step + a_first) % 2) ? ma.get_best_action(g.get()) : mb.get_best_action(g.get());
ma.update_with_move(res);
mb.update_with_move(res);
g->execute_move(res);
Expand Down

0 comments on commit 6e29371

Please sign in to comment.