Skip to content

Commit 4d3d6c2

Browse files
authored
Merge pull request #2 from petergjoel/user_boost
updating mostly build-system and printing
2 parents 1394535 + 593d83f commit 4d3d6c2

File tree

7 files changed

+116
-16
lines changed

7 files changed

+116
-16
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
url = https://github.com/nlohmann/json.git
44
[submodule "third_party/ptrie"]
55
path = third_party/ptrie
6-
url = git@github.com:petergjoel/ptrie.git
6+
url = https://github.com/petergjoel/ptrie.git

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
cmake_minimum_required(VERSION 3.9)
1+
cmake_minimum_required(VERSION 3.12)
22

33
cmake_policy(SET CMP0048 NEW)
44
cmake_policy(SET CMP0069 NEW)
5+
cmake_policy(SET CMP0074 NEW)
56

67
set(CMAKE_CXX_STANDARD 17)
78
if (NOT CMAKE_BUILD_TYPE)
@@ -18,5 +19,9 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
1819
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
1920

2021
add_subdirectory(${CMAKE_SOURCE_DIR}/third_party/ptrie)
22+
# Unset Boost_ROOT here if set, not needed for JSON or source of strategy
23+
if(Boost_ROOT)
24+
unset(Boost_ROOT)
25+
endif()
2126
add_subdirectory(${CMAKE_SOURCE_DIR}/third_party/json)
2227
add_subdirectory(${CMAKE_SOURCE_DIR}/src/)

src/CMakeLists.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
cmake_minimum_required(VERSION 3.7)
1+
cmake_minimum_required(VERSION 3.12)
22
project(z2s C CXX)
33
set(CMAKE_CXX_STANDARD 17)
44
set(CMAKE_INCLUDE_CURRENT_DIR ON)
55

6-
find_package(Boost 1.66 COMPONENTS program_options REQUIRED)
7-
include_directories(${Boost_INCLUDE_DIR})
6+
option(LIBSTRATEGY_OnlyLibrary "Build only as library." OFF)
7+
8+
if(NOT LIBSTRATEGY_OnlyLibrary)
9+
find_package(Boost 1.66 COMPONENTS program_options REQUIRED)
10+
include_directories(${Boost_INCLUDE_DIR})
11+
endif()
812

913
add_library(strategy SHARED ${HEADER_FILES} libz2s.cpp ZonotopStrategy.cpp SimpleTree.cpp)
1014
target_include_directories (strategy PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
@@ -15,8 +19,10 @@ target_include_directories (strategyStatic PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
1519
target_link_libraries(strategyStatic PRIVATE nlohmann_json::nlohmann_json ptrie)
1620
set_target_properties(strategyStatic PROPERTIES OUTPUT_NAME strategy)
1721

18-
add_executable(z2s ${HEADER_FILES} main.cpp ZonotopStrategy.cpp SimpleTree.cpp)
19-
target_link_libraries(z2s PRIVATE stdc++fs nlohmann_json::nlohmann_json ptrie ${Boost_LIBRARIES})
22+
if(NOT LIBSTRATEGY_OnlyLibrary)
23+
add_executable(z2s ${HEADER_FILES} main.cpp ZonotopStrategy.cpp SimpleTree.cpp)
24+
target_link_libraries(z2s PRIVATE stdc++fs nlohmann_json::nlohmann_json ptrie ${Boost_LIBRARIES})
25+
endif()
2026

2127

2228
install(TARGETS strategy

src/SimpleTree.cpp

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,24 +1151,111 @@ std::ostream& SimpleTree::node_t::print(std::ostream& out, size_t tabs) const {
11511151

11521152
std::ostream& SimpleTree::print_c(std::ostream& stream, std::string name) const
11531153
{
1154+
if(_root == nullptr)
1155+
{
1156+
stream << "double " << name << "(unsigned int action, const double* disc, const double* vars)\n{\n";
1157+
stream << "\treturn 0 ; \n}\n";
1158+
return stream;
1159+
}
1160+
std::unordered_map<size_t,const node_t*> _idnode;
1161+
std::unordered_map<const node_t*,size_t> _nodeid;
1162+
_idnode[0] = _root.get();
1163+
_nodeid[_root.get()] = 0;
1164+
1165+
size_t lid = 0;
1166+
while(lid != _nodeid.size())
1167+
{
1168+
auto n = _idnode[lid];
1169+
if(!n->is_leaf())
1170+
{
1171+
auto ln = n->_low.get();
1172+
auto hn = n->_high.get();
1173+
if(_nodeid.count(ln) == 0)
1174+
{
1175+
auto id = _idnode.size();
1176+
_idnode[id] = ln;
1177+
_nodeid[ln] = id;
1178+
}
1179+
if(_nodeid.count(hn) == 0)
1180+
{
1181+
auto id = _idnode.size();
1182+
_idnode[id] = hn;
1183+
_nodeid[hn] = id;
1184+
}
1185+
}
1186+
++lid;
1187+
}
1188+
1189+
stream << "const int " << name << "_nodes[] = {";
1190+
for(size_t i = 0; i < lid; ++i)
1191+
{
1192+
if(i != 0) stream << ",";
1193+
auto n = _idnode[i];
1194+
if(n->is_leaf())
1195+
{
1196+
stream << "-1,-1,-1";
1197+
}
1198+
else
1199+
{
1200+
stream << _nodeid[n->_low.get()] << ",";
1201+
stream << _nodeid[n->_high.get()] << ",";
1202+
stream << n->_var;
1203+
}
1204+
}
1205+
stream << "};\n";
1206+
auto mm = _root->compute_min_max();
1207+
auto v = is_minimization() ? mm.second + 1 : mm.first -1;
1208+
stream << "const double " << name << "_values[] = {";
1209+
for(size_t i = 0; i < lid; ++i)
1210+
{
1211+
if(i != 0) stream << ",";
1212+
auto n = _idnode[i];
1213+
if(n->is_leaf())
1214+
{
1215+
if(!std::isinf(n->_cost) && !std::isnan(n->_cost))
1216+
stream << n->_cost;
1217+
else
1218+
stream << v;
1219+
}
1220+
else
1221+
{
1222+
stream << n->_limit;
1223+
}
1224+
}
1225+
stream << "};\n";
11541226
stream << "double " << name << "(unsigned int action, const double* disc, const double* vars)\n{\n";
1155-
stream << "\tconst double inf = INFINITY;\n";
11561227
//stream << "\t// Depth = " << _root->depth() << std::endl;
11571228
stream << "\t// Actions = " << _actions.size() << std::endl;
11581229
stream << "\t// Disc = " << _statevars.size() << std::endl;
11591230
stream << "\t// Cont = " << _pointvars.size() << std::endl;
1160-
1161-
std::unordered_set<const node_t*> printed;
1231+
stream << "\t// Nodes = " << lid << std::endl;
1232+
/*std::unordered_set<const node_t*> printed;
11621233
std::vector<const node_t*> toprint;
11631234
if(_root)
11641235
_root->print_c_nested(stream, _statevars.size(), 1, toprint, _root);
1165-
stream << "\treturn inf;\n";
1236+
auto mm = _root->compute_min_max();
1237+
auto v = is_minimization() ? mm.second + 1 : mm.first -1;
1238+
stream << "\treturn " << v << ";\n";
11661239
for(auto n : toprint)
11671240
{
11681241
n->print_c(stream, 0, printed, 1);
1169-
stream << "\treturn inf;\n";
1242+
stream << "\treturn " << v << ";\n";
11701243
}
1171-
stream << "\treturn inf;\n";
1244+
stream << "\treturn " << v << ";\n";
1245+
* */
1246+
stream << "\tint ins = 0;\n\twhile(true) {\n";
1247+
stream << "\t\tint l = " << name << "_nodes[ins*3]; int h = " << name << "_nodes[1+(ins*3)]; int v = " << name << "_nodes[2+(ins*3)];\n";
1248+
stream << "\t\tif(v == -1) return " << name << "_values[ins];\n";
1249+
stream << "\t\tdouble val = 0;\n";
1250+
stream << "\t\tif(v == " << _statevars.size() << ") val = action;\n";
1251+
stream << "\t\telse if(v > " << _statevars.size() << ") val = vars[v-" << (_statevars.size()+1) << "];\n";
1252+
stream << "\t\telse val = disc[v];\n";
1253+
stream << "\t\tif(val <= " << name << "_values[ins])\n";
1254+
stream << "\t\t\tins = l;\n";
1255+
stream << "\t\telse\n";
1256+
stream << "\t\t\tins = h;\n";
1257+
stream << "\t}\n";
1258+
stream << "\treturn " << v << ";\n";
11721259
stream << "}\n";
11731260
return stream;
11741261
}

src/ZonotopStrategy.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#ifndef ZONOTOPSTRATEGY_H
3030
#define ZONOTOPSTRATEGY_H
3131

32-
#include <ptrie_stable.h>
32+
#include <ptrie/ptrie_stable.h>
3333
#include <istream>
3434
#include <memory>
3535

@@ -39,7 +39,9 @@
3939
class ZonotopStrategy {
4040
public:
4141
ZonotopStrategy(const ZonotopStrategy& orig) = default;
42+
ZonotopStrategy(ZonotopStrategy&&) = default;
4243
virtual ~ZonotopStrategy() = default;
44+
ZonotopStrategy& operator=(ZonotopStrategy&&) = default;
4345
static ZonotopStrategy parse(std::istream&);
4446
int num_patterns() const;
4547
int max_pattern_length() const;

third_party/json

Submodule json updated 84 files

0 commit comments

Comments
 (0)