Allow classification package to be used without Boost IO Stream and Serialization

This commit is contained in:
Simon Giraudot 2020-04-07 11:02:16 +02:00
parent 9fd5dd1a43
commit 7cfe6df8e3
8 changed files with 51 additions and 2 deletions

View File

@ -38,10 +38,12 @@
#include <CGAL/tags.h>
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#endif
#ifdef BOOST_MSVC
# pragma warning(pop)
@ -98,6 +100,9 @@ public:
than the ones used by `other`, and in the same order.
*/
#if defined(DOXYGEN_RUNNING) || \
(defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && \
defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION))
Random_forest_classifier (const Random_forest_classifier& other,
const Feature_set& features)
: m_labels (other.m_labels), m_features (features), m_rfc (nullptr)
@ -106,6 +111,7 @@ public:
other.save_configuration(stream);
this->load_configuration(stream);
}
#endif
/// \cond SKIP_IN_MANUAL
~Random_forest_classifier ()
@ -280,6 +286,9 @@ public:
The output file is written in an GZIP container that is readable
by the `load_configuration()` method.
*/
#if defined(DOXYGEN_RUNNING) || \
(defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && \
defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION))
void save_configuration (std::ostream& output) const
{
boost::iostreams::filtering_ostream outs;
@ -288,6 +297,7 @@ public:
boost::archive::text_oarchive oas(outs);
oas << BOOST_SERIALIZATION_NVP(*m_rfc);
}
#endif
/*!
\brief Loads a configuration from the stream `input`.
@ -298,6 +308,9 @@ public:
the ones present when the file was generated using
`save_configuration()`.
*/
#if defined(DOXYGEN_RUNNING) || \
(defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && \
defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION))
void load_configuration (std::istream& input)
{
CGAL::internal::liblearning::RandomForest::ForestParams params;
@ -311,6 +324,7 @@ public:
boost::archive::text_iarchive ias(ins);
ias >> BOOST_SERIALIZATION_NVP(*m_rfc);
}
#endif
/// @}

View File

@ -40,7 +40,9 @@
#endif
#include <boost/random/uniform_01.hpp>
#include <boost/random/normal_distribution.hpp>
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
#include <boost/serialization/vector.hpp>
#endif
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
@ -93,6 +95,7 @@ struct ForestParams {
min_samples_per_node(5),
sample_reduction(0.368f)
{}
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
template <typename Archive>
void serialize(Archive& ar, unsigned /*version*/)
{
@ -105,6 +108,7 @@ struct ForestParams {
ar & BOOST_SERIALIZATION_NVP(min_samples_per_node);
ar & BOOST_SERIALIZATION_NVP(sample_reduction);
}
#endif
};
struct QuadraticSplitter {
@ -149,6 +153,7 @@ struct QuadraticSplitter {
data_points[i_sample] = std::make_pair(sample_fval, sample_class);
}
}
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
template <typename Archive>
void serialize(Archive& ar, unsigned /*version*/)
{
@ -156,6 +161,7 @@ struct QuadraticSplitter {
ar & BOOST_SERIALIZATION_NVP(w);
ar & BOOST_SERIALIZATION_NVP(threshold);
}
#endif
};
struct LinearSplitter {
@ -187,12 +193,14 @@ struct LinearSplitter {
data_points[i_sample] = std::make_pair(sample_fval, sample_class);
}
}
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
template <typename Archive>
void serialize(Archive& ar, unsigned /*version*/)
{
ar & BOOST_SERIALIZATION_NVP(w);
ar & BOOST_SERIALIZATION_NVP(threshold);
}
#endif
};
struct AxisAlignedSplitter {
@ -232,12 +240,14 @@ struct AxisAlignedSplitter {
data_points.push_back(std::make_pair(sample_fval, sample_class));
}
}
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
template <typename Archive>
void serialize(Archive& ar, unsigned /*version*/)
{
ar & BOOST_SERIALIZATION_NVP(feature);
ar & BOOST_SERIALIZATION_NVP(threshold);
}
#endif
};
struct AxisAlignedRandomSplitGenerator {

View File

@ -223,12 +223,14 @@ public:
return sum/trees.size();
}
#endif
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
template <typename Archive>
void serialize(Archive& ar, unsigned /* version */)
{
ar & BOOST_SERIALIZATION_NVP(params);
ar & BOOST_SERIALIZATION_NVP(trees);
}
#endif
void get_feature_usage (std::vector<std::size_t>& count) const
{

View File

@ -97,11 +97,13 @@ public:
return std::make_pair(best_thresh, float(best_loss));
}
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
template <typename Archive>
void serialize(Archive& ar, unsigned /* version */)
{
ar & boost::serialization::make_nvp("base", boost::serialization::base_object< Node< NodeGini<Splitter>, ForestParams, Splitter > >(*this));
}
#endif
};
}

View File

@ -21,8 +21,15 @@
#define CGAL_INTERNAL_LIBLEARNING_RANDOMFORESTS_NODE_H
#include "../dataview.h"
#include "common-libraries.hpp"
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
#include <boost/serialization/scoped_ptr.hpp>
#include <boost/serialization/vector.hpp>
#else
#include <boost/scoped_ptr.hpp>
#include <vector>
#endif
#if VERBOSE_NODE_LEARNING
#include <cstdio>
#endif
@ -228,6 +235,7 @@ public:
right->train(samples, labels, sample_idxes + offset_right, n_samples_right, split_generator, gen);
}
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
template <typename Archive>
void serialize(Archive& ar, unsigned /*version*/)
{
@ -243,6 +251,7 @@ public:
ar & BOOST_SERIALIZATION_NVP(right);
}
}
#endif
void get_feature_usage (std::vector<std::size_t>& count) const
{

View File

@ -18,7 +18,11 @@
#define CGAL_INTERNAL_LIBLEARNING_RANDOMFOREST_TREE_H
#include "../dataview.h"
#include "common-libraries.hpp"
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
#include <boost/serialization/scoped_ptr.hpp>
#else
#include <boost/scoped_ptr.hpp>
#endif
namespace CGAL { namespace internal {
@ -115,12 +119,16 @@ public:
return n_common/sqrt((n_common + n_1)*(n_common + n_2));
}
#endif
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
template <typename Archive>
void serialize(Archive& ar, unsigned /*version*/)
{
ar & BOOST_SERIALIZATION_NVP(params);
ar & BOOST_SERIALIZATION_NVP(root_node);
}
#endif
void get_feature_usage (std::vector<std::size_t>& count) const
{
root_node->get_feature_usage(count);

View File

@ -36,4 +36,6 @@ function(CGAL_target_use_Boost_IOStreams target)
endif()
endif()
target_compile_options( ${target} PUBLIC -DCGAL_LINKED_WITH_BOOST_IOSTREAMS)
endfunction()

View File

@ -11,4 +11,6 @@ function(CGAL_target_use_Boost_Serialization target)
target_link_libraries(${target} PUBLIC ${Boost_SERIALIZATION_LIBRARY})
endif()
target_compile_options( ${target} PUBLIC -DCGAL_LINKED_WITH_BOOST_SERIALIZATION)
endfunction()