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 eee66c5ee7
commit ed25fd2e04
8 changed files with 45 additions and 4 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)
@ -327,6 +329,9 @@ public:
[Serialization](https://www.boost.org/libs/serialization) and
[IO Streams](https://www.boost.org/libs/iostreams) (compiled with the GZIP dependency).
*/
#if defined(DOXYGEN_RUNNING) || \
(defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && \
defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION))
static void convert_deprecated_configuration_to_new_format (std::istream& input, std::ostream& output)
{
Label_set dummy_labels;
@ -335,10 +340,13 @@ public:
classifier.load_deprecated_configuration(input);
classifier.save_configuration(output);
}
#endif
/// @}
/// \cond SKIP_IN_MANUAL
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && \
defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
void load_deprecated_configuration (std::istream& input)
{
CGAL::internal::liblearning::RandomForest::ForestParams params;
@ -350,7 +358,7 @@ public:
boost::archive::text_iarchive ias(ins);
ias >> BOOST_SERIALIZATION_NVP(*m_rfc);
}
/// \endcond
#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
void write (std::ostream& os)
{
@ -173,6 +177,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*/)
{
@ -180,6 +185,7 @@ struct QuadraticSplitter {
ar & BOOST_SERIALIZATION_NVP(w);
ar & BOOST_SERIALIZATION_NVP(threshold);
}
#endif
};
struct LinearSplitter {
@ -211,12 +217,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 {
@ -256,12 +264,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
void write (std::ostream& os)
{
@ -274,7 +284,6 @@ struct AxisAlignedSplitter {
is.read((char*)(&feature), sizeof(int));
is.read((char*)(&threshold), sizeof(FeatureType));
}
};
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 write (std::ostream& os)
{

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 write (std::ostream& os)
{

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,15 @@ 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 write (std::ostream& os)
{

View File

@ -6,7 +6,7 @@ set(CGAL_target_use_Boost_IOStreams_included TRUE)
function(CGAL_target_use_Boost_IOStreams target)
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_ZLIB_HEADERS "boost/iostreams/filter/zlib.hpp")
find_package( Boost OPTIONAL_COMPONENTS bzip2 zlib)
@ -19,7 +19,7 @@ function(CGAL_target_use_Boost_IOStreams target)
else()
target_link_libraries(${target} PUBLIC ${Boost_IOSTREAMS_LIBRARY})
endif()
if( WIN32 )
if (Boost_ZLIB_FOUND AND Boost_BZIP2_FOUND)
target_link_libraries(${target} PUBLIC ${Boost_ZLIB_LIBRARY} ${Boost_BZIP2_LIBRARY})
@ -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()