#include <ostream>
#include <random>
#include <sstream>
#include <string>
#include <vector>
double DistanceSquare(
const Point& b)
const {
return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y);
}
return Point { x + b.x, y + b.y };
}
Point operator / (
double s)
const {
return Point { x / s, y / s };
}
};
return os << '(' << p.x << ',' << p.y << ')';
}
struct ClosestCenter {
size_t cluster_id;
size_t count;
};
std::ostream&
operator << (std::ostream& os,
const ClosestCenter& cc) {
return os << '(' << cc.cluster_id
<< ':' << cc.point << ':' << cc.count << ')';
}
for (size_t iter = 0; iter < 10; ++iter)
{
std::vector<Point> local_centers = centers.
AllGather();
auto new_centers =
points
[local_centers](
const Point& p) {
double min_dist = p.DistanceSquare(local_centers[0]);
size_t cluster_id = 0;
for (size_t i = 1; i < local_centers.size(); ++i) {
double dist = p.DistanceSquare(local_centers[i]);
if (dist < min_dist)
min_dist = dist, cluster_id = i;
}
return ClosestCenter { cluster_id, p, 1 };
})
.ReduceByKey(
[](const ClosestCenter& cc) { return cc.cluster_id; },
[](const ClosestCenter& a, const ClosestCenter& b) {
return ClosestCenter {
a.cluster_id, a.point + b.point, a.count + b.count
};
})
.Map([](const ClosestCenter& cc) {
return cc.point / cc.count;
});
}
if (output) {
centers
})
.WriteLines(output);
}
else {
centers.
Print(
"final centers");
}
}
std::default_random_engine rng(std::random_device { } ());
std::uniform_real_distribution<double> dist(0.0, 1000.0);
auto points =
ctx, 100,
[&](const size_t&) {
return Point { dist(rng), dist(rng) };
});
return points.
Cache().Execute();
}
auto points =
.Map(
std::istringstream iss(input);
iss >> p.x >> p.y;
if (iss.peek() != EOF)
die(
"Could not parse point coordinates: " << input);
return p;
});
}
int main(
int argc,
char* argv[]) {
if (argc == 1)
else if (argc == 2)
else if (argc == 3)
else
std::cerr << "Usage: " << argv[0]
<< " [points] [output]" << std::endl;
});
}