Skip to content

Commit

Permalink
[sivia] added SIVIA algorithms (Ctc,Sep) + binding
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRohou committed Apr 11, 2022
1 parent c77aea3 commit f37e819
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 10 deletions.
43 changes: 43 additions & 0 deletions python/src/core/sivia/codac_py_sivia.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* \file
* Sep Python binding
* Originated from the former pyIbex library (Benoît Desrochers)
* ----------------------------------------------------------------------------
* \date 2020
* \author Benoît Desrochers, Simon Rohou
* \copyright Copyright 2021 Codac Team
* \license This program is distributed under the terms of
* the GNU Lesser General Public License (LGPL).
*/

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/operators.h>
#include <pybind11/functional.h>
#include "codac_type_caster.h"

#include "../contractors/static/codac_py_Ctc.h"
#include "../separators/codac_py_Sep.h"

#include <codac_sivia.h>

using namespace std;
using namespace codac;
namespace py = pybind11;
using namespace pybind11::literals;


void export_unsupported_geometry(py::module& m, py::class_<Ctc,pyCtc>& ctc, py::class_<ibex::Sep,pySep>& sep)
{
m.def("SIVIA", [](const IntervalVector& x, Ctc& ctc, float precision, const SetColorMap& color_map)
{
SIVIA(x, &ctc, precision, color_map);
},
"x"_a.noconvert(), "ctc"_a.noconvert(), "precision"_a.noconvert(), "color_map"_a.noconvert() = DEFAULT_SET_COLOR_MAP);

m.def("SIVIA", [](const IntervalVector& x, ibex::Sep& sep, float precision, const SetColorMap& color_map)
{
SIVIA(x, &sep, precision, color_map);
},
"x"_a.noconvert(), "sep"_a.noconvert(), "precision"_a.noconvert(), "color_map"_a.noconvert() = DEFAULT_SET_COLOR_MAP);
}
6 changes: 3 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ add_subdirectory(3rd) # 3rd party (todo: make a clean dependency)

set(CODAC_PKG_CONFIG_FILE ${CMAKE_CURRENT_BINARY_DIR}/codac.pc)

set(CODAC_PKG_CONFIG_CFLAGS "-I\${includedir}/ibex -I\${includedir}/codac -I\${includedir}/codac-rob -I\${includedir}/codac-unsupported")
set(CODAC_PKG_CONFIG_LIBS "-L\${libdir} -lcodac -lcodac-rob -lcodac-unsupported")
set(CODAC_PKG_CONFIG_CFLAGS "-I\${includedir}/ibex -I\${includedir}/codac -I\${includedir}/codac-rob -I\${includedir}/codac-unsupported -I\${includedir}/codac")
set(CODAC_PKG_CONFIG_LIBS "-L\${libdir} -lcodac -lcodac-rob -lcodac-unsupported -lcodac")

if(WITH_CAPD)
set(CODAC_PKG_CONFIG_CFLAGS "${CODAC_PKG_CONFIG_CFLAGS} -I\${includedir}/codac-capd")
Expand Down Expand Up @@ -65,7 +65,7 @@ find_library(CODAC_UNSUPPORTED_LIBRARY NAMES codac-unsupported
PATH_SUFFIXES lib)
set(CODAC_VERSION ${PROJECT_VERSION})
set(CODAC_LIBRARIES \${CODAC_LIBRARY} \${CODAC_ROB_LIBRARY} \${CODAC_UNSUPPORTED_LIBRARY})
set(CODAC_LIBRARIES \${CODAC_LIBRARY} \${CODAC_ROB_LIBRARY} \${CODAC_UNSUPPORTED_LIBRARY} \${CODAC_LIBRARY})
set(CODAC_INCLUDE_DIRS \${CODAC_INCLUDE_DIR} \${CODAC_ROB_INCLUDE_DIR} \${CODAC_UNSUPPORTED_INCLUDE_DIR})
set(CODAC_C_FLAGS \"\")
Expand Down
15 changes: 8 additions & 7 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@
${CMAKE_CURRENT_SOURCE_DIR}/tools/codac_Tools.h
${CMAKE_CURRENT_SOURCE_DIR}/tools/codac_Eigen.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tools/codac_Eigen.h
${CMAKE_CURRENT_SOURCE_DIR}/sivia/codac_sivia.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sivia/codac_sivia.h
)


Expand All @@ -169,12 +171,13 @@
${CMAKE_CURRENT_SOURCE_DIR}/contractors/static
${CMAKE_CURRENT_SOURCE_DIR}/contractors/dyn
${CMAKE_CURRENT_SOURCE_DIR}/cn
${CMAKE_CURRENT_SOURCE_DIR}/tools)
${CMAKE_CURRENT_SOURCE_DIR}/tools
${CMAKE_CURRENT_SOURCE_DIR}/sivia)
target_link_libraries(codac PUBLIC Ibex::ibex)

#set_property(TARGET codac PROPERTY CXX_STANDARD 17)
add_compile_options(-O3 -Wall)
#set_property(TARGET codac PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET codac PROPERTY CXX_STANDARD 17)
add_compile_options(-O3 -Wall -std=c++17)
set_property(TARGET codac PROPERTY CXX_STANDARD_REQUIRED ON)


################################################################################
Expand All @@ -196,9 +199,7 @@
set(CODAC_MAIN_HEADER ${CMAKE_CURRENT_BINARY_DIR}/codac.h)
file(WRITE ${CODAC_MAIN_HEADER} "/* This file is generated by CMake */\n\n")
file(APPEND ${CODAC_MAIN_HEADER} "#ifndef __CODAC_H__\n#define __CODAC_H__\n\n")
# Simplifying access to frequently used items of IBEX
file(APPEND ${CODAC_MAIN_HEADER} "\n")
file(APPEND ${CODAC_MAIN_HEADER} "#include <codac-rob.h>\n") # todo: clean this organization
file(APPEND ${CODAC_MAIN_HEADER} "\n#include <codac-rob.h>\n") # todo: clean this organization
foreach(header_path ${HDR})
get_filename_component(header_name ${header_path} NAME)
file(APPEND ${CODAC_MAIN_HEADER} "#include <${header_name}>\n")
Expand Down
122 changes: 122 additions & 0 deletions src/core/sivia/codac_sivia.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* SIVIA
* ----------------------------------------------------------------------------
* \date 2022
* \author Simon Rohou
* \copyright Copyright 2022 Codac Team
* \license This program is distributed under the terms of
* the GNU Lesser General Public License (LGPL).
*/

#include <list>
#include <iostream>

#include "codac_sivia.h"
#include "codac_VIBesFig.h"
#include <ibex_LargestFirst.h>
#include "vibes.h"

using namespace std;

namespace codac
{
vector<IntervalVector> box_diff(const IntervalVector& x0, const IntervalVector& x)
{
vector<IntervalVector> v;
IntervalVector* boxes;
int n = x0.diff(x, boxes);
v.assign(boxes, boxes + n);
return v;
}

void SIVIA(const IntervalVector& x0, Ctc* ctc, float precision, const SetColorMap& color_map)
{
assert(x0.size() >= 2);

ibex::LargestFirst bisector(0.);
deque<IntervalVector> stack = { x0 };
int k = 0;

while(!stack.empty())
{
k++;
IntervalVector x = stack.front();
stack.pop_front();
IntervalVector x_before_ctc(x);

ctc->contract(x);

vector<IntervalVector> x_out_l = box_diff(x_before_ctc.subvector(0,1), x.subvector(0,1));
for(const auto& o : x_out_l)
vibes::drawBox(o, color_map.at(SetValue::OUT));

if(x.is_empty())
continue;

else
{
if(x.max_diam() < precision)
vibes::drawBox(x.subvector(0,1), color_map.at(SetValue::UNKNOWN));

else
{
pair<IntervalVector,IntervalVector> p = bisector.bisect(x.subvector(0,1));
stack.push_back(cart_prod(p.first,x[2]));
stack.push_back(cart_prod(p.second,x[2]));
}
}
}

cout << "Number of contractions: " << k << endl;
//cout << "Number of boxes: " << 0 << endl;
}

void SIVIA(const IntervalVector& x0, ibex::Sep* sep, float precision, const SetColorMap& color_map)
{
assert(x0.size() >= 2);

ibex::LargestFirst bisector(0.);
deque<IntervalVector> stack = { x0 };
int k = 0;

while(!stack.empty())
{
k++;
IntervalVector x_before_ctc = stack.front();
stack.pop_front();
IntervalVector x_in(x_before_ctc), x_out(x_before_ctc);

sep->separate(x_in, x_out);

IntervalVector x = x_in & x_out;

vector<IntervalVector> x_in_l = box_diff(x_before_ctc, x_in);
for(const auto& i : x_in_l)
vibes::drawBox(i, color_map.at(SetValue::IN));

vector<IntervalVector> x_out_l = box_diff(x_before_ctc, x_out);
for(const auto& o : x_out_l)
vibes::drawBox(o, color_map.at(SetValue::OUT));

if(x.is_empty())
continue;

else
{

if(x.max_diam() < precision)
vibes::drawBox(x.subvector(0,1), color_map.at(SetValue::UNKNOWN));

else
{
pair<IntervalVector,IntervalVector> p = bisector.bisect(x);
stack.push_back(p.first);
stack.push_back(p.second);
}
}
}

cout << "Number of contractions: " << k << endl;
//cout << "Number of boxes: " << 0 << endl;
}
}
29 changes: 29 additions & 0 deletions src/core/sivia/codac_sivia.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* \file
* SIVIA
* ----------------------------------------------------------------------------
* \date 2022
* \author Simon Rohou
* \copyright Copyright 2022 Codac Team
* \license This program is distributed under the terms of
* the GNU Lesser General Public License (LGPL).
*/

#ifndef __CODAC_SIVIA_H__
#define __CODAC_SIVIA_H__

#include <ibex_Sep.h>
#include "codac_Ctc.h"
#include "codac_VIBesFigPaving.h"
#include "codac_IntervalVector.h"

namespace codac
{
void SIVIA(const IntervalVector& x, Ctc* ctc, float precision,
const SetColorMap& color_map = DEFAULT_SET_COLOR_MAP);

void SIVIA(const IntervalVector& x, ibex::Sep* sep, float precision,
const SetColorMap& color_map = DEFAULT_SET_COLOR_MAP);
}

#endif

0 comments on commit f37e819

Please sign in to comment.