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

Change types and max values from 64 bit to 32 bit #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 11 additions & 11 deletions include/dkm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ This is an alternate initialization method based on the [kmeans++](https://en.wi
initialization algorithm.
*/
template <typename T, size_t N>
std::vector<std::array<T, N>> random_plusplus(const std::vector<std::array<T, N>>& data, uint32_t k, uint64_t seed) {
std::vector<std::array<T, N>> random_plusplus(const std::vector<std::array<T, N>>& data, uint32_t k, uint32_t seed) {
assert(k > 0);
assert(data.size() > 0);
using input_size_t = typename std::array<T, N>::size_type;
std::vector<std::array<T, N>> means;
// Using a very simple PRBS generator, parameters selected according to
// https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
std::linear_congruential_engine<uint64_t, 6364136223846793005, 1442695040888963407, UINT64_MAX> rand_engine(seed);
std::linear_congruential_engine<uint32_t, 6364136223846793005, 1442695040888963407, UINT32_MAX> rand_engine(seed);

// Select first mean at random from the set
{
Expand Down Expand Up @@ -208,7 +208,7 @@ class clustering_parameters {
_has_rand_seed(false), _rand_seed()
{}

void set_max_iteration(uint64_t max_iter)
void set_max_iteration(uint32_t max_iter)
{
_max_iter = max_iter;
_has_max_iter = true;
Expand All @@ -220,7 +220,7 @@ class clustering_parameters {
_has_min_delta = true;
}

void set_random_seed(uint64_t rand_seed)
void set_random_seed(uint32_t rand_seed)
{
_rand_seed = rand_seed;
_has_rand_seed = true;
Expand All @@ -231,18 +231,18 @@ class clustering_parameters {
bool has_random_seed() const { return _has_rand_seed; }

uint32_t get_k() const { return _k; };
uint64_t get_max_iteration() const { return _max_iter; }
uint32_t get_max_iteration() const { return _max_iter; }
T get_min_delta() const { return _min_delta; }
uint64_t get_random_seed() const { return _rand_seed; }
uint32_t get_random_seed() const { return _rand_seed; }

private:
uint32_t _k;
bool _has_max_iter;
uint64_t _max_iter;
uint32_t _max_iter;
bool _has_min_delta;
T _min_delta;
bool _has_rand_seed;
uint64_t _rand_seed;
uint32_t _rand_seed;
};

/*
Expand Down Expand Up @@ -275,14 +275,14 @@ std::tuple<std::vector<std::array<T, N>>, std::vector<uint32_t>> kmeans_lloyd(
assert(parameters.get_k() > 0); // k must be greater than zero
assert(data.size() >= parameters.get_k()); // there must be at least k data points
std::random_device rand_device;
uint64_t seed = parameters.has_random_seed() ? parameters.get_random_seed() : rand_device();
uint32_t seed = parameters.has_random_seed() ? parameters.get_random_seed() : rand_device();
std::vector<std::array<T, N>> means = details::random_plusplus(data, parameters.get_k(), seed);

std::vector<std::array<T, N>> old_means;
std::vector<std::array<T, N>> old_old_means;
std::vector<uint32_t> clusters;
// Calculate new means until convergence is reached or we hit the maximum iteration count
uint64_t count = 0;
uint32_t count = 0;
do {
clusters = details::calculate_clusters(data, means);
old_old_means = old_means;
Expand All @@ -304,7 +304,7 @@ Any code still using this signature should move to the version of this function
template <typename T, size_t N>
std::tuple<std::vector<std::array<T, N>>, std::vector<uint32_t>> kmeans_lloyd(
const std::vector<std::array<T, N>>& data, uint32_t k,
uint64_t max_iter = 0, T min_delta = -1.0) {
uint32_t max_iter = 0, T min_delta = -1.0) {
clustering_parameters<T> parameters(k);
if (max_iter != 0) {
parameters.set_max_iteration(max_iter);
Expand Down