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

View File

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

View File

@ -223,12 +223,14 @@ public:
return sum/trees.size(); return sum/trees.size();
} }
#endif #endif
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
template <typename Archive> template <typename Archive>
void serialize(Archive& ar, unsigned /* version */) void serialize(Archive& ar, unsigned /* version */)
{ {
ar & BOOST_SERIALIZATION_NVP(params); ar & BOOST_SERIALIZATION_NVP(params);
ar & BOOST_SERIALIZATION_NVP(trees); ar & BOOST_SERIALIZATION_NVP(trees);
} }
#endif
void get_feature_usage (std::vector<std::size_t>& count) const 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)); 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> template <typename Archive>
void serialize(Archive& ar, unsigned /* version */) void serialize(Archive& ar, unsigned /* version */)
{ {
ar & boost::serialization::make_nvp("base", boost::serialization::base_object< Node< NodeGini<Splitter>, ForestParams, Splitter > >(*this)); 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 #define CGAL_INTERNAL_LIBLEARNING_RANDOMFORESTS_NODE_H
#include "../dataview.h" #include "../dataview.h"
#include "common-libraries.hpp" #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/scoped_ptr.hpp>
#include <boost/serialization/vector.hpp> #include <boost/serialization/vector.hpp>
#else
#include <boost/scoped_ptr.hpp>
#include <vector>
#endif
#if VERBOSE_NODE_LEARNING #if VERBOSE_NODE_LEARNING
#include <cstdio> #include <cstdio>
#endif #endif
@ -228,6 +235,7 @@ public:
right->train(samples, labels, sample_idxes + offset_right, n_samples_right, split_generator, gen); 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> template <typename Archive>
void serialize(Archive& ar, unsigned /*version*/) void serialize(Archive& ar, unsigned /*version*/)
{ {
@ -243,6 +251,7 @@ public:
ar & BOOST_SERIALIZATION_NVP(right); ar & BOOST_SERIALIZATION_NVP(right);
} }
} }
#endif
void get_feature_usage (std::vector<std::size_t>& count) const void get_feature_usage (std::vector<std::size_t>& count) const
{ {

View File

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

View File

@ -6,7 +6,7 @@ set(CGAL_target_use_Boost_IOStreams_included TRUE)
function(CGAL_target_use_Boost_IOStreams target) function(CGAL_target_use_Boost_IOStreams target)
if( WIN32 ) if( WIN32 )
# to avoid a warning with old cmake # to avoid a warning with old cmake
set(_Boost_BZIP2_HEADERS "boost/iostreams/filter/bzip2.hpp") set(_Boost_BZIP2_HEADERS "boost/iostreams/filter/bzip2.hpp")
set(_Boost_ZLIB_HEADERS "boost/iostreams/filter/zlib.hpp") set(_Boost_ZLIB_HEADERS "boost/iostreams/filter/zlib.hpp")
find_package( Boost OPTIONAL_COMPONENTS bzip2 zlib) find_package( Boost OPTIONAL_COMPONENTS bzip2 zlib)
@ -19,7 +19,7 @@ function(CGAL_target_use_Boost_IOStreams target)
else() else()
target_link_libraries(${target} PUBLIC ${Boost_IOSTREAMS_LIBRARY}) target_link_libraries(${target} PUBLIC ${Boost_IOSTREAMS_LIBRARY})
endif() endif()
if( WIN32 ) if( WIN32 )
if (Boost_ZLIB_FOUND AND Boost_BZIP2_FOUND) if (Boost_ZLIB_FOUND AND Boost_BZIP2_FOUND)
target_link_libraries(${target} PUBLIC ${Boost_ZLIB_LIBRARY} ${Boost_BZIP2_LIBRARY}) target_link_libraries(${target} PUBLIC ${Boost_ZLIB_LIBRARY} ${Boost_BZIP2_LIBRARY})
@ -36,4 +36,6 @@ function(CGAL_target_use_Boost_IOStreams target)
endif() endif()
endif() endif()
target_compile_options( ${target} PUBLIC -DCGAL_LINKED_WITH_BOOST_IOSTREAMS)
endfunction() endfunction()

View File

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