diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 76e0be122..043fd7299 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -31,5 +31,8 @@ target_link_libraries(rand_example ignition-math${IGN_MATH_VER}::ignition-math${ add_executable(graph_example graph_example.cc) target_link_libraries(graph_example ignition-math${IGN_MATH_VER}::ignition-math${IGN_MATH_VER}) +add_executable(kmeans kmeans.cc) +target_link_libraries(kmeans ignition-math${IGN_MATH_VER}::ignition-math${IGN_MATH_VER}) + add_executable(color_example color_example.cc) target_link_libraries(color_example ignition-math${IGN_MATH_VER}::ignition-math${IGN_MATH_VER}) diff --git a/examples/kmeans.cc b/examples/kmeans.cc new file mode 100644 index 000000000..6f02d2091 --- /dev/null +++ b/examples/kmeans.cc @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2021 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +#include +#include + +#include + +int main(int argc, char **argv) +{ + // Create some observations. + std::vector obs; + obs.push_back(ignition::math::Vector3d(1.0, 1.0, 0.0)); + obs.push_back(ignition::math::Vector3d(1.1, 1.0, 0.0)); + obs.push_back(ignition::math::Vector3d(1.2, 1.0, 0.0)); + obs.push_back(ignition::math::Vector3d(1.3, 1.0, 0.0)); + obs.push_back(ignition::math::Vector3d(1.4, 1.0, 0.0)); + obs.push_back(ignition::math::Vector3d(5.0, 1.0, 0.0)); + obs.push_back(ignition::math::Vector3d(5.1, 1.0, 0.0)); + obs.push_back(ignition::math::Vector3d(5.2, 1.0, 0.0)); + obs.push_back(ignition::math::Vector3d(5.3, 1.0, 0.0)); + obs.push_back(ignition::math::Vector3d(5.4, 1.0, 0.0)); + + // Initialize Kmeans with two partitions. + ignition::math::Kmeans kmeans(obs); + + std::vector obsCopy; + obsCopy = kmeans.Observations(); + + for (auto &elem : obsCopy) + elem += ignition::math::Vector3d(0.1, 0.2, 0.0); + + // append more observations + kmeans.AppendObservations(obsCopy); + + // cluster + std::vector centroids; + std::vector labels; + auto result = kmeans.Cluster(2, centroids, labels); + + // Check that there are two centroids. + std::cout << "Is there a solution ? " << result << std::endl; + std::cout << "There are " << centroids.size() << " centroids" << std::endl; + std::cout << "labels: ["; + for (auto &label: labels) + { + std::cout << label << ", "; + } + std::cout << "]" << '\n'; +} diff --git a/examples/kmeans.py b/examples/kmeans.py new file mode 100644 index 000000000..a4507d94f --- /dev/null +++ b/examples/kmeans.py @@ -0,0 +1,51 @@ +# Copyright (C) 2021 Open Source Robotics Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License") +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from ignition.math import Kmeans, Vector3d + +# Create some observations. +obs = list([]) +obs.append(Vector3d(1.0, 1.0, 0.0)) +obs.append(Vector3d(1.1, 1.0, 0.0)) +obs.append(Vector3d(1.2, 1.0, 0.0)) +obs.append(Vector3d(1.3, 1.0, 0.0)) +obs.append(Vector3d(1.4, 1.0, 0.0)) +obs.append(Vector3d(5.0, 1.0, 0.0)) +obs.append(Vector3d(5.1, 1.0, 0.0)) +obs.append(Vector3d(5.2, 1.0, 0.0)) +obs.append(Vector3d(5.3, 1.0, 0.0)) +obs.append(Vector3d(5.4, 1.0, 0.0)) + +# Initialize Kmeans with two partitions. +kmeans = Kmeans(obs) + +# copy original Vector to add more data +obs_copy = list(kmeans.observations()).copy() +obs2 = list([]) + +for idx, a in enumerate(obs_copy): + obs_copy[idx] = a + Vector3d(0.1, 0.2, 0.0) + obs2.append(obs_copy[idx]) + +# Append more observations +kmeans.append_observations(obs2) + +# Calling cluster +result, centroids, labels = kmeans.cluster(2) + +# Check that there are two centroids. +print("Is there a solution ? {}".format(result)); +print("There are {} centroids".format(len(centroids))) +print("labels:", labels)