Skip to content

Commit

Permalink
Merge pull request #42 from CLSFramework/fix-performance-bug
Browse files Browse the repository at this point in the history
Fix performance bug and server side decision making
  • Loading branch information
naderzare authored Nov 11, 2024
2 parents 408cc53 + 131506b commit 1f1b7dd
Show file tree
Hide file tree
Showing 30 changed files with 1,555 additions and 320 deletions.
426 changes: 413 additions & 13 deletions idl/grpc/service.proto

Large diffs are not rendered by default.

63 changes: 60 additions & 3 deletions idl/thrift/soccer_service.thrift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// version 1.6
// version 1.7

namespace cpp soccer
namespace py soccer
Expand Down Expand Up @@ -761,6 +761,58 @@ struct HeliosGoalieKick {}

struct HeliosShoot {}

struct OpponentEffector {
1: list<double> negetive_effect_by_distance,
2: bool negetive_effect_by_distance_based_on_first_layer,
3: list<double> negetive_effect_by_reach_steps,
4: bool negetive_effect_by_reach_steps_based_on_first_layer
}

struct ActionTypeEffector {
1: double direct_pass,
2: double lead_pass,
3: double through_pass,
4: double short_dribble,
5: double long_dribble,
6: double cross,
7: double hold
}

struct TeammateEffector {
1: map<i32, double> coefficients,
2: bool apply_based_on_first_layer
}

struct PlannerEvaluationEffector {
1: optional OpponentEffector opponent_effector,
2: optional ActionTypeEffector action_type_effector,
3: optional TeammateEffector teammate_effector
}

struct HeliosFieldEvaluator {
1: double x_coefficient,
2: double ball_dist_to_goal_coefficient,
3: double effective_max_ball_dist_to_goal
}

struct MatrixFieldEvaluatorY {
1: list<double> evals
}

struct MatrixFieldEvaluator {
1: list<MatrixFieldEvaluatorY> evals
}

struct PlannerFieldEvaluator {
1: optional HeliosFieldEvaluator helios_field_evaluator,
2: optional MatrixFieldEvaluator matrix_field_evaluator
}

struct PlannerEvaluation {
1: PlannerEvaluationEffector effectors,
2: PlannerFieldEvaluator field_evaluators
}

struct HeliosOffensivePlanner {
1: bool direct_pass,
2: bool lead_pass,
Expand All @@ -771,7 +823,10 @@ struct HeliosOffensivePlanner {
7: bool simple_pass,
8: bool simple_dribble,
9: bool simple_shoot
10: bool server_side_decision
10: bool server_side_decision,
11: i32 max_depth,
12: i32 max_nodes,
13: PlannerEvaluation evaluation
}

struct HeliosBasicOffensive {}
Expand Down Expand Up @@ -861,7 +916,9 @@ struct PlayerActions {
1: list<PlayerAction> actions,
2: bool ignore_preprocess,
3: bool ignore_doforcekick,
4: bool ignore_doHeardPassRecieve
4: bool ignore_doHeardPassRecieve,
5: bool ignore_doIntention,
6: bool ignore_shootInPreprocess
}

struct ChangePlayerType {
Expand Down
231 changes: 195 additions & 36 deletions src/grpc-client/grpc_client_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,65 @@ void GrpcClientPlayer::init(rcsc::SoccerAgent *agent,
sample_communication = Communication::Ptr(new SampleCommunication());
}

void GrpcClientPlayer::updateChainByDefault(const rcsc::WorldModel &wm)
{
FieldEvaluator::ConstPtr field_evaluator = FieldEvaluator::ConstPtr(new SampleFieldEvaluator);
CompositeActionGenerator *g = new CompositeActionGenerator();

g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_StrictCheckPass(), 1));
g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_Cross(), 1));
g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_ShortDribble(), 1));
g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_SelfPass(), 1));

g->addGenerator(new ActGen_RangeActionChainLengthFilter(new ActGen_Shoot(),
2, ActGen_RangeActionChainLengthFilter::MAX));
ActionGenerator::ConstPtr action_generator = ActionGenerator::ConstPtr(g);
ActionChainHolder::instance().setFieldEvaluator(field_evaluator);
ActionChainHolder::instance().setActionGenerator(action_generator);
ActionChainHolder::instance().update(wm);
}

void GrpcClientPlayer::updateChainByPlannerAction(const rcsc::WorldModel &wm, const protos::PlayerAction &action)
{
CompositeActionGenerator *g = new CompositeActionGenerator();
if (action.helios_offensive_planner().max_depth() > 0)
g->max_depth = action.helios_offensive_planner().max_depth();
if (action.helios_offensive_planner().max_nodes() > 0)
g->max_nodes = action.helios_offensive_planner().max_nodes();

FieldEvaluator::Ptr field_evaluator = FieldEvaluator::Ptr(new SampleFieldEvaluator);
if (action.helios_offensive_planner().has_evaluation())
field_evaluator->set_grpc_evalution_method(action.helios_offensive_planner().evaluation());

if (action.helios_offensive_planner().lead_pass()
|| action.helios_offensive_planner().direct_pass() || action.helios_offensive_planner().through_pass())
g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_StrictCheckPass(), 1));
if (action.helios_offensive_planner().cross())
g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_Cross(), 1));
if (action.helios_offensive_planner().simple_pass())
g->addGenerator(new ActGen_RangeActionChainLengthFilter(new ActGen_DirectPass(),
2, ActGen_RangeActionChainLengthFilter::MAX));
if (action.helios_offensive_planner().short_dribble())
g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_ShortDribble(), 1));
if (action.helios_offensive_planner().long_dribble())
g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_SelfPass(), 1));
if (action.helios_offensive_planner().simple_dribble())
g->addGenerator(new ActGen_RangeActionChainLengthFilter(new ActGen_SimpleDribble(),
2, ActGen_RangeActionChainLengthFilter::MAX));
if (action.helios_offensive_planner().simple_shoot())
g->addGenerator(new ActGen_RangeActionChainLengthFilter(new ActGen_Shoot(),
2, ActGen_RangeActionChainLengthFilter::MAX));
if (g->M_generators.empty())
{
return;
}
ActionGenerator::ConstPtr action_generator = ActionGenerator::ConstPtr(g);

ActionChainHolder::instance().setFieldEvaluator(field_evaluator);
ActionChainHolder::instance().setActionGenerator(action_generator);
ActionChainHolder::instance().update(wm);
}

void GrpcClientPlayer::getActions()
{
auto agent = M_agent;
Expand All @@ -117,12 +176,21 @@ void GrpcClientPlayer::getActions()
state.set_allocated_register_response(response);
protos::PlayerActions actions;
ClientContext context;
// Set the deadline to 1 second from now
auto deadline = std::chrono::system_clock::now() + std::chrono::seconds(1);
context.set_deadline(deadline);

Status status = M_stub_->GetPlayerActions(&context, state, &actions);

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

if (status.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED) {
// The call timed out
std::cerr << "rpcerror-timeout" << std::endl;
}
return;
}

Expand All @@ -135,6 +203,28 @@ void GrpcClientPlayer::getActions()
return;
}
}
const rcsc::WorldModel & wm = agent->world();

if ( !actions.ignore_shootinpreprocess() )
{
if ( wm.gameMode().type() != rcsc::GameMode::IndFreeKick_
&& wm.time().stopped() == 0
&& wm.self().isKickable()
&& Bhv_StrictCheckShoot().execute( agent ) )
{
// reset intention
agent->setIntention( static_cast< rcsc::SoccerIntention * >( 0 ) );
return;
}
}

if ( !actions.ignore_dointention() )
{
if ( agent->doIntention() )
{
return;
}
}

if (do_forceKick && !actions.ignore_doforcekick())
{
Expand All @@ -145,7 +235,7 @@ void GrpcClientPlayer::getActions()
return;
}
}

if (do_heardPassReceive && !actions.ignore_doheardpassrecieve())
{
if (doHeardPassReceive(agent))
Expand All @@ -155,6 +245,102 @@ void GrpcClientPlayer::getActions()
return;
}
}

// if (agent->world().gameMode().type() == rcsc::GameMode::PlayOn)
// {
// if (agent->world().self().goalie())
// {
// protos::PlayerAction action;
// action.set_allocated_helios_goalie(new protos::HeliosGoalie());
// actions.add_actions()->CopyFrom(action);
// }
// else if (agent->world().self().isKickable())
// {
// const auto &wm = agent->world();
// protos::PlayerAction action;

// auto planner = new protos::HeliosOffensivePlanner();
// planner->set_direct_pass(true);
// planner->set_lead_pass(true);
// planner->set_through_pass(true);
// planner->set_short_dribble(true);
// planner->set_long_dribble(true);
// planner->set_cross(true);
// planner->set_simple_pass(false);
// planner->set_simple_dribble(false);
// planner->set_simple_shoot(true);

// action.set_allocated_helios_offensive_planner(planner);
// actions.add_actions()->CopyFrom(action);
// }
// else
// {
// protos::PlayerAction action;
// auto move = new protos::HeliosBasicMove();
// action.set_allocated_helios_basic_move(move);
// actions.add_actions()->CopyFrom(action);
// }
// }
// else
// {
// protos::PlayerAction action;
// auto set_play = new protos::HeliosSetPlay();
// action.set_allocated_helios_set_play(set_play);
// actions.add_actions()->CopyFrom(action);
// }

int planner_action_index = -1;
for (int i = 0; i < actions.actions_size(); i++)
{
auto action = actions.actions(i);
if (action.action_case() == PlayerAction::kHeliosOffensivePlanner)
{
planner_action_index = i;
break;
}
}

if (planner_action_index != -1)
{
updateChainByPlannerAction(wm, actions.actions(planner_action_index));
}
else
{
updateChainByDefault(wm);
}

// if (agent->world().gameMode().type() == rcsc::GameMode::PlayOn)
// {
// if (agent->world().self().goalie())
// {
// RoleGoalie().execute(agent);
// return;
// }
// else if (agent->world().self().isKickable())
// {
// const auto &wm = agent->world();
// if (Bhv_PlannedAction().execute(agent))
// {
// agent->debugClient().addMessage("PlannedAction");
// }
// else
// {
// Body_HoldBall().execute(agent);
// agent->setNeckAction(new Neck_ScanField());
// }
// }
// else
// {
// Bhv_BasicMove().execute(agent);
// return;
// }
// }
// else
// {
// Bhv_SetPlay().execute(agent);
// return;
// }


for (int i = 0; i < actions.actions_size(); i++)
{
Expand Down Expand Up @@ -543,38 +729,6 @@ void GrpcClientPlayer::getActions()
}
}
else if (action.action_case() == PlayerAction::kHeliosOffensivePlanner) {
FieldEvaluator::ConstPtr field_evaluator = FieldEvaluator::ConstPtr(new SampleFieldEvaluator);
CompositeActionGenerator *g = new CompositeActionGenerator();

if (action.helios_offensive_planner().lead_pass()
|| action.helios_offensive_planner().direct_pass() || action.helios_offensive_planner().through_pass())
g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_StrictCheckPass(), 1));
if (action.helios_offensive_planner().cross())
g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_Cross(), 1));
if (action.helios_offensive_planner().simple_pass())
g->addGenerator(new ActGen_RangeActionChainLengthFilter(new ActGen_DirectPass(),
2, ActGen_RangeActionChainLengthFilter::MAX));
if (action.helios_offensive_planner().short_dribble())
g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_ShortDribble(), 1));
if (action.helios_offensive_planner().long_dribble())
g->addGenerator(new ActGen_MaxActionChainLengthFilter(new ActGen_SelfPass(), 1));
if (action.helios_offensive_planner().simple_dribble())
g->addGenerator(new ActGen_RangeActionChainLengthFilter(new ActGen_SimpleDribble(),
2, ActGen_RangeActionChainLengthFilter::MAX));
if (action.helios_offensive_planner().simple_shoot())
g->addGenerator(new ActGen_RangeActionChainLengthFilter(new ActGen_Shoot(),
2, ActGen_RangeActionChainLengthFilter::MAX));
if (g->M_generators.empty())
{
Body_HoldBall().execute(agent);
agent->setNeckAction(new Neck_ScanField());
continue;
}
ActionGenerator::ConstPtr action_generator = ActionGenerator::ConstPtr(g);
ActionChainHolder::instance().setFieldEvaluator(field_evaluator);
ActionChainHolder::instance().setActionGenerator(action_generator);
ActionChainHolder::instance().update(agent->world());

if (action.helios_offensive_planner().server_side_decision())
{
if (GetBestPlannerAction())
Expand All @@ -588,7 +742,11 @@ void GrpcClientPlayer::getActions()
{
agent->debugClient().addMessage("PlannedAction");
}

else
{
Body_HoldBall().execute(agent);
agent->setNeckAction(new Neck_ScanField());
}
}

}
Expand Down Expand Up @@ -630,14 +788,15 @@ bool GrpcClientPlayer::GetBestPlannerAction()
<< std::endl;
return false;
}
ActionChainHolder::instance().updateBestChain(best_action.index());

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()))
if (Bhv_PlannedAction().execute(agent))
{
#ifdef DEBUG_CLIENT_PLAYER
std::cout << "PlannedAction" << std::endl;
Expand Down
2 changes: 2 additions & 0 deletions src/grpc-client/grpc_client_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class GrpcClientPlayer : public GrpcClient, public RpcPlayerClient
bool use_same_grpc_port = true,
bool add_20_to_grpc_port_if_right_side = false) override;

void updateChainByDefault(const rcsc::WorldModel &wm);
void updateChainByPlannerAction(const rcsc::WorldModel &wm, const protos::PlayerAction &action);
void getActions();
bool GetBestPlannerAction();
void convertResultPairToRpcActionStatePair( google::protobuf::Map<int32_t, protos::RpcActionState> * map);
Expand Down
Loading

0 comments on commit 1f1b7dd

Please sign in to comment.