Skip to content

Commit

Permalink
Merge pull request #6 from DEIS-Tools/new_libs
Browse files Browse the repository at this point in the history
updated ptries and json, using zip-download instead of git
  • Loading branch information
petergjoel authored Feb 11, 2020
2 parents 65998f0 + a463138 commit e4237fc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 45 deletions.
32 changes: 18 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(EXTERNAL_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/external)

ExternalProject_add(ptrie-ext
GIT_REPOSITORY https://github.com/petergjoel/ptrie
GIT_TAG 1.0.3
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DPTRIE_BuildTests=OFF
)

ExternalProject_add(json-ext
GIT_REPOSITORY https://github.com/nlohmann/json
GIT_TAG v3.7.0
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DJSON_BuildTests=OFF
)

include_directories(${EXTERNAL_INSTALL_LOCATION}/include)
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)
option(STRATEGY_GetDependencies "Fetch external dependencies from web." ON)

if (STRATEGY_GetDependencies)
ExternalProject_add(ptrie-ext
URL https://github.com/petergjoel/ptrie/archive/v1.1.0.zip
URL_HASH SHA512=092a8f50ca21d1199b19a10c4e0273c93a717a9f6491998a16bf21d21d37e6537ffd8a06ac41a2b623241da6036546d44b754567441944565e2a16646378cf29
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release
)

ExternalProject_add(json-ext
URL https://github.com/nlohmann/json/archive/v3.7.3.zip
URL_HASH SHA512=b47a07de9a071cce645a173d084df5dd31f7669154fc00f6c99e0506474d30e8376acaee1d3c79a50def4f25a36042951bfa4fca9a704687e59c368d05053158
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DJSON_BuildTests=OFF
)

include_directories(${EXTERNAL_INSTALL_LOCATION}/include)
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)
endif (STRATEGY_GetDependencies)

# Unset Boost_ROOT here if set, not needed for JSON or source of strategy
if(Boost_ROOT)
Expand Down
4 changes: 1 addition & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ endif()
add_library(strategy SHARED ${HEADER_FILES} libz2s.cpp ZonotopStrategy.cpp SimpleTree.cpp)
add_dependencies(strategy ptrie-ext json-ext)
target_include_directories (strategy PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(strategy PRIVATE ptrie)

add_library(strategyStatic STATIC ${HEADER_FILES} libz2s.cpp ZonotopStrategy.cpp SimpleTree.cpp)
add_dependencies(strategyStatic ptrie-ext json-ext)
target_include_directories (strategyStatic PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(strategyStatic PRIVATE ptrie)
set_target_properties(strategyStatic PROPERTIES OUTPUT_NAME strategy)

if(NOT LIBSTRATEGY_OnlyLibrary)
add_executable(z2s ${HEADER_FILES} main.cpp ZonotopStrategy.cpp SimpleTree.cpp)
add_dependencies(z2s ptrie-ext json-ext)
target_link_libraries(z2s PRIVATE stdc++fs ptrie ${Boost_LIBRARIES})
target_link_libraries(z2s PRIVATE stdc++fs ${Boost_LIBRARIES})
endif()


Expand Down
39 changes: 15 additions & 24 deletions src/SimpleTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ SimpleTree SimpleTree::parse(std::istream& input, bool simplify, bool subsumptio
if(subsumption) tree._root->subsumption_reduction(minim, tree);
if(simplify)
{
ptrie::map<std::shared_ptr<node_t>> nodemap;
nodemap_t nodemap;
if(tree._root)
tree._root = tree._root->simplify(true, nodemap, tree);
if(tree._root)
Expand Down Expand Up @@ -871,7 +871,7 @@ double SimpleTree::node_t::value(const double* disc, const double* cont, uint32_



std::shared_ptr<SimpleTree::node_t> SimpleTree::node_t::simplify(bool make_dd, ptrie::map<std::shared_ptr<node_t>>& nodemap, SimpleTree& parent) {
std::shared_ptr<SimpleTree::node_t> SimpleTree::node_t::simplify(bool make_dd, nodemap_t& nodemap, SimpleTree& parent) {
if(_low) _low = _low->simplify(make_dd, nodemap, parent);
if(_high) _high = _high->simplify(make_dd, nodemap, parent);
if(_low)
Expand Down Expand Up @@ -957,41 +957,32 @@ std::shared_ptr<SimpleTree::node_t> SimpleTree::node_t::simplify(bool make_dd, p
}
if(make_dd)
{
auto sig = signature();
auto r = nodemap.insert(sig.first.get(), sig.second);
auto& ptr = nodemap.get_data(r.second);
auto& ptr = nodemap[*this];
if(ptr == nullptr)
ptr = nodemap.get_data(r.second) = shared_from_this();
ptr = shared_from_this();
return ptr;
}
return shared_from_this();
}

std::pair<std::unique_ptr<unsigned char[]>, size_t> SimpleTree::node_t::signature() const {
std::pair<std::unique_ptr<unsigned char[]>, size_t> res;
res.second = sizeof(uint32_t)*1+sizeof(double)+sizeof(size_t)*2;
res.first = std::make_unique<unsigned char[]>(res.second);
memset(res.first.get(), 0, res.second);
unsigned char* next = res.first.get();
if(is_leaf())
SimpleTree::signature_t::signature_t(const SimpleTree::node_t& node)
{
if(node.is_leaf())
{
((double*)next)[0] = _cost;
res.second = sizeof(double);
_limit = node._limit;
_low = node._low.get();
_high = node._high.get();
_var = node._var;
}
else
{
((uint32_t*)next)[0] = _var;
next += sizeof(uint32_t);
((double*)next)[0] = _limit;
next += sizeof(double);
((size_t*)next)[0] = (size_t)_low.get();
((size_t*)next)[1] = (size_t)_high.get();
_var = 0;
_low = nullptr;
_high = nullptr;
_limit = node._cost;
}
return res;
}



bool SimpleTree::node_t::is_leaf() const {
return _low == nullptr && _high == nullptr;
}
Expand Down
44 changes: 40 additions & 4 deletions src/SimpleTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,23 @@ class SimpleTree {
std::ostream& print_c(std::ostream& stream, std::string name) const;
double value(const double* disc, const double* cont, uint32_t action) const;
bool is_minimization() const { return _is_minimization; }
const std::vector<std::string> &actions() const { return _actions; }
const std::vector<std::string> &actions() const;
const std::vector<std::string> &discrete_features() const { return _statevars; }
const std::vector<std::string> &continous_features() const { return _pointvars; }
private:

using json = nlohmann::json;
struct node_t;
struct signature_t {
uint32_t _var;
double _limit;
node_t* _low;
node_t* _high;
signature_t(const SimpleTree::node_t&);
} __attribute__((packed));
friend class ptrie::byte_iterator<signature_t>;

using nodemap_t = ptrie::map<signature_t,std::shared_ptr<node_t>>;
SimpleTree() = default;

static std::vector<double> parse_key(const std::string& key);
Expand All @@ -64,7 +76,7 @@ class SimpleTree {
void insert(std::vector<double>& key, json& tree, size_t action, SimpleTree& parent, size_t prefix, bool minimize, double accuracy, std::vector<double>& exactness);
std::ostream& print(std::ostream& out, size_t tabs = 0) const;
bool is_leaf() const;
std::shared_ptr<node_t> simplify(bool make_dd, ptrie::map<std::shared_ptr<node_t>>& nodemap, SimpleTree& parent);
std::shared_ptr<node_t> simplify(bool make_dd, nodemap_t& nodemap, SimpleTree& parent);
void subsumption_reduction(bool minimization, SimpleTree& parent);
void action_nodes(std::vector<std::shared_ptr<node_t>>& nodes, uint32_t low, uint32_t high, uint32_t varid);
std::pair<double,double> compute_min_max();
Expand Down Expand Up @@ -101,16 +113,40 @@ class SimpleTree {
return _low < other._low;
return _high < other._high;
}
std::pair<std::unique_ptr<unsigned char[]>, size_t> signature() const;
};

std::vector<std::string> _actions;
std::vector<std::string> _statevars;
std::vector<std::string> _pointvars;
std::shared_ptr<node_t> _root;
bool _is_minimization = true;

};

namespace ptrie{
template<>
struct byte_iterator<SimpleTree::signature_t> {
static uchar& access(SimpleTree::signature_t* data, size_t id)
{
return ((uchar*)data)[id];
}

static const uchar& const_access(const SimpleTree::signature_t* data, size_t id)
{
return ((const uchar*)data)[id];
}

static constexpr size_t element_size()
{
return sizeof(size_t)*2+sizeof(double)+sizeof(uint32_t);
}

static constexpr bool continious()
{
return true;
}

// add read_blob, write_blob
};
}
#endif /* SIMPLETREE_H */

0 comments on commit e4237fc

Please sign in to comment.