Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion include/cv/aggregator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class CVAggregator {

// gets the record of all cv results
LockPtr<std::map<int, IdentifiedTarget>> getCVRecord();

// Calculates the clustered center of each airdroptype in matched results,
// then assigns matched_results.matched_airdrop to the results
void calculateClusters();
void updateRecords(std::vector<IdentifiedTarget>& new_values);

private:
Expand Down
33 changes: 33 additions & 0 deletions src/cv/aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "utilities/lockptr.hpp"
#include "utilities/locks.hpp"
#include "utilities/logging.hpp"
#include "cv/clustering.hpp"

CVAggregator::CVAggregator(Pipeline&& p) : pipeline(std::move(p)) {
this->num_worker_threads = 0;
Expand Down Expand Up @@ -123,3 +124,35 @@ std::vector<AggregatedRun> CVAggregator::popAllRuns() {
this->results->runs.clear();
return out;
}

void CVAggregator::calculateClusters() {
LockPtr<std::map<int, IdentifiedTarget>> results = this->getCVRecord();
std::vector<std::vector<GPSCoord>> cluster_input;
for (const auto& pair : *results.data.get()) {
GPSProtoVec cords = pair.second.coordinates();
auto types = pair.second.target_type();
// add all types up to the current, in order to keep list format
for (int i = 0; i < cords.size(); i++) {
GPSCoord location = cords.at(i);
int type = types.at(i);
if (cluster_input.size() < type) {
for (int j = cluster_input.size(); j < type; j++) {
cluster_input.push_back(std::vector<GPSCoord>());
}
}
cluster_input[type].push_back(location);
}
}
Clustering clustering;
std::vector<GPSCoord> clusterCenters = clustering.FindClustersCenter(cluster_input);

LockPtr<MatchedResults> matched = this->getMatchedResults();
std::unordered_map<AirdropType, AirdropTarget> matched_clusters;
for (int i = 0; i < clusterCenters.size(); i++) {
AirdropTarget airdrop;
airdrop.set_index(static_cast<AirdropType>(i));
airdrop.mutable_coordinate()->CopyFrom(clusterCenters[i]);
matched_clusters.insert(std::pair(static_cast<AirdropType>(i), std::move(airdrop)));
}
matched.data->matched_airdrop = std::move(matched_clusters);
}
5 changes: 2 additions & 3 deletions src/ticks/cv_loiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "ticks/fly_search.hpp"
#include "ticks/ids.hpp"
#include "utilities/constants.hpp"
#include "cv/clustering.hpp"

CVLoiterTick::CVLoiterTick(std::shared_ptr<MissionState> state) : Tick(state, TickID::CVLoiter) {
this->status = CVLoiterTick::Status::None;
Expand Down Expand Up @@ -39,9 +40,7 @@ Tick* CVLoiterTick::tick() {
AirdropType::Water, AirdropType::Beacon,
};
*/

LockPtr<CVResults> results = state->getCV()->getResults();

state->getCV()->calculateClusters();
// for (const auto& bottle : ALL_BOTTLES) {
// // contains will never be false but whatever
// if (results.data->matches.contains(bottle)) {
Expand Down
7 changes: 5 additions & 2 deletions tests/integration/clustering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define POINT_DISTANCE 10
#define NUM_OUTLIERS 5
#define NUM_RUNS 5
//redeclaring method to not have fixed seed so the test is deterministic
//redeclaring method to not have fixed seed so the test is non-deterministic
unsigned int seed = time(NULL);
double random(double min, double max) {
return min + static_cast<double>(rand_r(&seed)) / RAND_MAX * (max - min);
Expand Down Expand Up @@ -86,11 +86,14 @@ int main()
scatter(x, y, std::vector<double>(), c);
hold(on);
auto plot = scatter(cluster_x, cluster_y);
::matplot::legend({"Cluster points", "Calculated cluster Center"});
plot->marker_style(line_spec::marker_style::diamond);
title("Clustering run " + std::to_string(run));
std::ostringstream stringStream;
stringStream << "run" << run << ".png";
std::string copyOfStr = stringStream.str();
// std::filesystem::remove(copyOfStr);
std::cout << "saved to /build/" << copyOfStr << std::endl;
//std::filesystem::remove(copyOfStr);
save(copyOfStr);
hold(off);

Expand Down