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

Send back score of a duplicate report #572

Merged
merged 3 commits into from
Aug 25, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions subt_ign/src/GameLogicPlugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ class subt::GameLogicPluginPrivate
/// \brief Calculate the score of a new artifact request.
/// \param[in] _type The object type. See ArtifactType.
/// \param[in] _pose The object pose.
/// \return The score obtained for this object.
public: double ScoreArtifact(const subt::ArtifactType &_type,
const ignition::msgs::Pose &_pose);
/// \return A tuple where the first parameter is the score obtained for this
/// report, and the second parameter is true if the artifact report is a
/// duplicate and false otherwise.
public: std::tuple<double, bool> ScoreArtifact(
const subt::ArtifactType &_type,
const ignition::msgs::Pose &_pose);

/// \brief Create an ArtifactType from an integer.
//
Expand Down Expand Up @@ -385,8 +388,8 @@ class subt::GameLogicPluginPrivate
/// \brief Robot types for keeping track of unique robot platform types.
public: std::set<std::string> robotTypes;

/// \brief The unique artifact reports received.
public: std::vector<std::string> uniqueReports;
/// \brief The unique artifact reports received, and the score it received.
public: std::map<std::string, double> uniqueReports;

/// \brief Current state.
public: std::string state="init";
Expand Down Expand Up @@ -1198,10 +1201,14 @@ bool GameLogicPluginPrivate::OnNewArtifact(const subt::msgs::Artifact &_req,
else
{
std::lock_guard<std::mutex> lock(this->mutex);
auto scoreDiff = this->ScoreArtifact(artifactType, _req.pose());
auto [scoreDiff, duplicate] = this->ScoreArtifact(
artifactType, _req.pose());

_resp.set_score_change(scoreDiff);
_resp.set_report_status("scored");
this->totalScore += scoreDiff;

if (!duplicate)
this->totalScore += scoreDiff;

ignmsg << "Total score: " << this->totalScore << std::endl;
this->Log() << "new_total_score " << this->totalScore << std::endl;
Expand Down Expand Up @@ -1247,23 +1254,23 @@ bool GameLogicPluginPrivate::ArtifactFromInt(const uint32_t &_typeInt,
}

/////////////////////////////////////////////////
double GameLogicPluginPrivate::ScoreArtifact(const ArtifactType &_type,
const ignition::msgs::Pose &_pose)
std::tuple<double, bool> GameLogicPluginPrivate::ScoreArtifact(
const ArtifactType &_type, const ignition::msgs::Pose &_pose)
{
// Sanity check: Make sure that we have crossed the starting gate.
if (!this->started)
{
ignmsg << " The task hasn't started yet" << std::endl;
this->Log() << "task_not_started" << std::endl;
return 0.0;
return {0.0, false};
}

// Sanity check: Make sure that we still have artifacts.
if (this->foundArtifacts.size() >= this->artifactCount)
{
ignmsg << " No artifacts remaining" << std::endl;
this->Log() << "no_remaining_artifacts_of_specified_type" << std::endl;
return 0.0;
return {0.0, false};
}

// The teams are reporting the artifact poses relative to the fiducial located
Expand All @@ -1288,7 +1295,7 @@ double GameLogicPluginPrivate::ScoreArtifact(const ArtifactType &_type,
<< " reported_artifact_type: " << reportType << "\n";
this->LogEvent(stream.str());

return 0.0;
return {0.0, false};
}

// Pose converted into a string.
Expand All @@ -1297,12 +1304,12 @@ double GameLogicPluginPrivate::ScoreArtifact(const ArtifactType &_type,
std::to_string(_pose.position().z());

// Unique report Id: Type and pose combined into a string.
std::string uniqueReport = reportType + "_" + reportPose;
std::string uniqueReportStr = reportType + "_" + reportPose;

auto uniqueReport = this->uniqueReports.find(uniqueReportStr);

// Check whether we received the same report before.
if (std::find(this->uniqueReports.begin(),
this->uniqueReports.end(),
uniqueReport) != this->uniqueReports.end())
if (uniqueReport != this->uniqueReports.end())
{
ignmsg << "This report has been received before" << std::endl;
this->Log() << "This report has been received before" << std::endl;
Expand All @@ -1317,12 +1324,9 @@ double GameLogicPluginPrivate::ScoreArtifact(const ArtifactType &_type,
this->LogEvent(stream.str());

this->duplicateReportCount++;
return 0.0;
return {uniqueReport->second, true};
}

// This is a new unique report, let's save it.
this->uniqueReports.push_back(uniqueReport);

// This is a unique report.
this->reportCount++;

Expand Down Expand Up @@ -1391,6 +1395,9 @@ double GameLogicPluginPrivate::ScoreArtifact(const ArtifactType &_type,
}
}

// This is a new unique report, let's save it.
this->uniqueReports[uniqueReportStr] = score;

auto outDist = std::isinf(std::get<2>(minDistance)) ? -1 :
std::get<2>(minDistance);
std::ostringstream stream;
Expand All @@ -1413,7 +1420,7 @@ double GameLogicPluginPrivate::ScoreArtifact(const ArtifactType &_type,
ignmsg << " [Total]: " << score << std::endl;
this->Log() << "modified_score " << score << std::endl;

return score;
return {score, false};
}

/////////////////////////////////////////////////
Expand Down