Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop chain serverside #8

Merged
merged 13 commits into from
Sep 13, 2024
58 changes: 58 additions & 0 deletions idl/grpc/service.proto
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// version 0

syntax = "proto3";

package protos;
Expand Down Expand Up @@ -758,6 +760,7 @@ message HeliosChainAction {
bool simple_pass = 7;
bool simple_dribble = 8;
bool simple_shoot = 9;
bool server_side_decision = 10;
}

message HeliosBasicOffensive {}
Expand Down Expand Up @@ -836,6 +839,7 @@ message PlayerAction {
HeliosSetPlay helios_set_play = 62;
HeliosPenalty helios_penalty = 63;
HeliosCommunicaion helios_communication = 64;

}
}

Expand Down Expand Up @@ -1202,6 +1206,59 @@ message PlayerType {
float player_speed_max = 34;
}

enum RpcActionCategory {
AC_Hold = 0;
AC_Dribble = 1;
AC_Pass = 2;
AC_Shoot = 3;
AC_Clear = 4;
AC_Move = 5;
AC_NoAction = 6;
}
message RpcCooperativeAction {
RpcActionCategory category = 1;
int32 index = 2;
int32 sender_unum = 3;
int32 target_unum = 4;
RpcVector2D target_point = 5;
double first_ball_speed = 6;
double first_turn_moment = 7;
double first_dash_power = 8;
double first_dash_angle_relative = 9;
int32 duration_step = 10;
int32 kick_count = 11;
int32 turn_count = 12;
int32 dash_count = 13;
bool final_action = 14;
string description = 15;
int32 parent_index = 16;
}

message RpcPredictState {
int32 spend_time = 1;
int32 ball_holder_unum = 2;
RpcVector2D ball_position = 3;
RpcVector2D ball_velocity = 4;
double our_defense_line_x = 5;
double our_offense_line_x = 6;
}

message RpcActionState {
RpcCooperativeAction action = 1;
RpcPredictState predict_state = 2;
double evaluation = 3;
}

message BestPlannerActionRequest {
RegisterResponse register_response = 1;
map<int32, RpcActionState> pairs = 2;
State state = 3;
}

message BestPlannerActionResponse {
int32 index = 1;
}

message Empty {
}

Expand All @@ -1215,4 +1272,5 @@ service Game {
rpc SendPlayerType(PlayerType) returns (Empty) {} //should be PlayerTypes
rpc Register(RegisterRequest) returns (RegisterResponse) {}
rpc SendByeCommand(RegisterResponse) returns (Empty) {}
rpc GetBestPlannerAction(BestPlannerActionRequest) returns (BestPlannerActionResponse) {}
}
60 changes: 59 additions & 1 deletion idl/thrift/soccer_service.thrift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// version 0

namespace cpp soccer
namespace py soccer

Expand Down Expand Up @@ -724,6 +726,7 @@ struct HeliosChainAction {
7: bool simple_pass,
8: bool simple_dribble,
9: bool simple_shoot
10: bool server_side_decision
}

struct HeliosBasicOffensive {}
Expand Down Expand Up @@ -1162,6 +1165,60 @@ struct PlayerType {
34: double player_speed_max,
}

enum RpcActionCategory {
AC_Hold = 0;
AC_Dribble = 1;
AC_Pass = 2;
AC_Shoot = 3;
AC_Clear = 4;
AC_Move = 5;
AC_NoAction = 6;
}

struct RpcCooperativeAction {
1: RpcActionCategory category;
2: i32 index;
3: i32 sender_unum;
4: i32 target_unum;
5: RpcVector2D target_point;
6: double first_ball_speed;
7: double first_turn_moment;
8: double first_dash_power;
9: double first_dash_angle_relative;
10: i32 duration_step;
11: i32 kick_count;
12: i32 turn_count;
13: i32 dash_count;
14: bool final_action;
15: string description;
16: i32 parent_index;
}

struct RpcPredictState {
1: i32 spend_time;
2: i32 ball_holder_unum;
3: RpcVector2D ball_position;
4: RpcVector2D ball_velocity;
5: double our_defense_line_x;
6: double our_offense_line_x;
}

struct RpcActionState {
1: RpcCooperativeAction action;
2: RpcPredictState predict_state;
3: double evaluation;
}

struct BestPlannerActionRequest {
1: RegisterResponse register_response,
2: map<i32, RpcActionState> pairs;
3: State state;
}

struct BestPlannerActionResponse {
1: i32 index;
}

struct Empty {}

service Game {
Expand All @@ -1173,6 +1230,7 @@ service Game {
Empty SendPlayerParams(1: PlayerParam player_param),
Empty SendPlayerType(1: PlayerType player_type),
RegisterResponse Register(1: RegisterRequest request),
Empty SendByeCommand(1: RegisterResponse register_response)
Empty SendByeCommand(1: RegisterResponse register_response),
BestPlannerActionResponse GetBestPlannerAction(1: BestPlannerActionRequest best_planner_action_request)
}

148 changes: 143 additions & 5 deletions src/grpc-client/grpc_client_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::chrono::milliseconds;

#define DEBUG
// #define DEBUG_CLIENT_PLAYER

#ifdef DEBUG
#ifdef DEBUG_CLIENT_PLAYER
#define LOG(x) std::cout << x << std::endl
#define LOGV(x) std::cout << #x << ": " << x << std::endl
#else
Expand Down Expand Up @@ -629,12 +629,24 @@ void GrpcClientPlayer::getActions()
ActionChainHolder::instance().setFieldEvaluator(field_evaluator);
ActionChainHolder::instance().setActionGenerator(action_generator);
ActionChainHolder::instance().update(agent->world());
if (Bhv_PlannedAction().execute(agent))

if (action.helios_chain_action().server_side_decision())
{
agent->debugClient().addMessage("PlannedAction");
break;
if (GetBestPlannerAction())
{
agent->debugClient().addMessage("GetBestPlannerAction");
break;
}
}
else
{
if (Bhv_PlannedAction().execute(agent))
{
agent->debugClient().addMessage("PlannedAction");
break;
}

}
Body_HoldBall().execute(agent);
agent->setNeckAction(new Neck_ScanField());
break;
Expand All @@ -650,6 +662,132 @@ void GrpcClientPlayer::getActions()
}
}

bool GrpcClientPlayer::GetBestPlannerAction()
{
protos::BestPlannerActionRequest *action_state_pairs = new protos::BestPlannerActionRequest();
protos::RegisterResponse* response = new protos::RegisterResponse(*M_register_response);
action_state_pairs->set_allocated_register_response(response);
State state = generateState();
action_state_pairs->set_allocated_state(&state);
#ifdef DEBUG_CLIENT_PLAYER
std::cout << "GetBestActionStatePair:" << "c" << M_agent->world().time().cycle() << std::endl;
std::cout << "results size:" << ActionChainHolder::instance().graph().getAllResults().size() << std::endl;
#endif
for (auto & index_resultPair : ActionChainHolder::instance().graph().getAllResults())
{
try
{

auto & result_pair = index_resultPair.second;
auto action_ptr = result_pair.first->actionPtr();
auto state_ptr = result_pair.first->statePtr();
int unique_index = action_ptr->uniqueIndex();
int parent_index = action_ptr->parentIndex();
auto eval = result_pair.second;
#ifdef DEBUG_CLIENT_PLAYER
std::cout<<"index:"<<index_resultPair.first<<" "<<unique_index<<" "<<parent_index<<" "<<eval<<std::endl;
#endif
auto map = action_state_pairs->mutable_pairs();
auto rpc_action_state_pair = protos::RpcActionState();
auto rpc_cooperative_action = new protos::RpcCooperativeAction();
auto rpc_predict_state = new protos::RpcPredictState();
auto category = protos::RpcActionCategory::AC_Hold;

switch (action_ptr->category())
{
case CooperativeAction::Hold:
category = protos::RpcActionCategory::AC_Hold;
break;
case CooperativeAction::Dribble:
category = protos::RpcActionCategory::AC_Dribble;
break;
case CooperativeAction::Pass:
category = protos::RpcActionCategory::AC_Pass;
break;
case CooperativeAction::Shoot:
category = protos::RpcActionCategory::AC_Shoot;
break;
case CooperativeAction::Clear:
category = protos::RpcActionCategory::AC_Clear;
break;
case CooperativeAction::Move:
category = protos::RpcActionCategory::AC_Move;
break;
case CooperativeAction::NoAction:
category = protos::RpcActionCategory::AC_NoAction;
break;
default:
break;
}
rpc_cooperative_action->set_category(category);
rpc_cooperative_action->set_index(unique_index);
rpc_cooperative_action->set_sender_unum(action_ptr->playerUnum());
rpc_cooperative_action->set_target_unum(action_ptr->targetPlayerUnum());
rpc_cooperative_action->set_allocated_target_point(StateGenerator::convertVector2D(action_ptr->targetPoint()));
rpc_cooperative_action->set_first_ball_speed(action_ptr->firstBallSpeed());
rpc_cooperative_action->set_first_turn_moment(action_ptr->firstTurnMoment());
rpc_cooperative_action->set_first_dash_power(action_ptr->firstDashPower());
rpc_cooperative_action->set_first_dash_angle_relative(action_ptr->firstDashAngle().degree());
rpc_cooperative_action->set_duration_step(action_ptr->durationStep());
rpc_cooperative_action->set_kick_count(action_ptr->kickCount());
rpc_cooperative_action->set_turn_count(action_ptr->turnCount());
rpc_cooperative_action->set_dash_count(action_ptr->dashCount());
rpc_cooperative_action->set_final_action(action_ptr->isFinalAction());
rpc_cooperative_action->set_description(action_ptr->description());
rpc_cooperative_action->set_parent_index(parent_index);

rpc_predict_state->set_spend_time(state_ptr->spendTime());
rpc_predict_state->set_ball_holder_unum(state_ptr->ballHolderUnum());
rpc_predict_state->set_allocated_ball_position(StateGenerator::convertVector2D(state_ptr->ball().pos()));
rpc_predict_state->set_allocated_ball_velocity(StateGenerator::convertVector2D(state_ptr->ball().vel()));
rpc_predict_state->set_our_defense_line_x(state_ptr->ourDefenseLineX());
rpc_predict_state->set_our_offense_line_x(state_ptr->ourOffensePlayerLineX());

rpc_action_state_pair.set_allocated_action(rpc_cooperative_action);
rpc_action_state_pair.set_allocated_predict_state(rpc_predict_state);
rpc_action_state_pair.set_evaluation(eval);

(*map)[unique_index] = rpc_action_state_pair;
}
catch (const std::exception &e)
{
std::cout << e.what() << '\n';
}
}

#ifdef DEBUG_CLIENT_PLAYER
std::cout << "map size:" << action_state_pairs->pairs_size() << std::endl;
#endif

protos::BestPlannerActionResponse best_action;
ClientContext context;
Status status = M_stub_->GetBestPlannerAction(&context, *action_state_pairs, &best_action);

if (!status.ok())
{
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return false;
}

auto agent = M_agent;

#ifdef DEBUG_CLIENT_PLAYER
std::cout << "best action index:" << best_action.index() << std::endl;
#endif

if (Bhv_PlannedAction().execute(agent, best_action.index()))
{
#ifdef DEBUG_CLIENT_PLAYER
std::cout << "PlannedAction" << std::endl;
#endif
agent->debugClient().addMessage("PlannedAction");
return true;
}

return false;
}

void GrpcClientPlayer::addSayMessage(protos::Say sayMessage) const
{
auto agent = M_agent;
Expand Down
21 changes: 12 additions & 9 deletions src/grpc-client/grpc_client_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
#include "player/sample_communication.h"
#include "rpc-client/rpc-player-client.h"

class GrpcClientPlayer : public GrpcClient, public RpcPlayerClient {
rcsc::PlayerAgent * M_agent;
class GrpcClientPlayer : public GrpcClient, public RpcPlayerClient
{
rcsc::PlayerAgent *M_agent;
Communication::Ptr sample_communication;
public:

public:
GrpcClientPlayer();

void init(rcsc::SoccerAgent * agent,
std::string target="localhost",
int port=50051,
bool use_same_grpc_port=true,
bool add_20_to_grpc_port_if_right_side=false) override;
void init(rcsc::SoccerAgent *agent,
std::string target = "localhost",
int port = 50051,
bool use_same_grpc_port = true,
bool add_20_to_grpc_port_if_right_side = false) override;

void getActions();
bool GetBestPlannerAction();
void addSayMessage(protos::Say sayMessage) const;
State generateState() const;
void addHomePosition(protos::WorldModel * world_model) const;
void addHomePosition(protos::WorldModel *world_model) const;
};
Loading