Skip to content

Commit

Permalink
[ctc] CtcDelay now available in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRohou committed Nov 26, 2020
1 parent 4536e21 commit 3ab0551
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Unreleased yet (in development)
Features added
--------------

* ``CtcDelay`` now available in Python


Changes
-------

Expand Down
1 change: 1 addition & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
src/core/contractors/static/tubex_py_CtcConstell.cpp

src/core/contractors/dyn/tubex_py_DynCtc.cpp
src/core/contractors/dyn/tubex_py_CtcDelay.cpp
src/core/contractors/dyn/tubex_py_CtcDeriv.cpp
src/core/contractors/dyn/tubex_py_CtcEval.cpp
src/core/contractors/dyn/tubex_py_CtcPicard.cpp
Expand Down
44 changes: 44 additions & 0 deletions python/src/core/contractors/dyn/tubex_py_CtcDelay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* \file
* CtcDelay Python binding
* ----------------------------------------------------------------------------
* \date 2020
* \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 "pyIbex_type_caster.h"

#include "tubex_py_DynCtc.h"
#include "tubex_CtcDelay.h"
// Generated file from Doxygen XML (doxygen2docstring.py):
#include "tubex_py_CtcDelay_docs.h"

using namespace std;
using namespace ibex;
using namespace tubex;
namespace py = pybind11;
using namespace pybind11::literals;


void export_CtcDelay(py::module& m, py::class_<DynCtc, pyDynCtc>& dyn_ctc)
{
py::class_<CtcDelay> ctc_eval(m, "CtcDelay", dyn_ctc, CTCDELAY_MAIN);
ctc_eval

.def(py::init<>(),
CTCDELAY_CTCDELAY)

.def("contract", (void (CtcDelay::*)(Interval&,Tube&,Tube&))&CtcDelay::contract,
CTCDELAY_VOID_CONTRACT_INTERVAL_TUBE_TUBE,
"a"_a.noconvert(), "x"_a.noconvert(), "y"_a.noconvert())

.def("contract", (void (CtcDelay::*)(Interval&,TubeVector&,TubeVector&))&CtcDelay::contract,
CTCDELAY_VOID_CONTRACT_INTERVAL_TUBEVECTOR_TUBEVECTOR,
"a"_a.noconvert(), "x"_a.noconvert(), "y"_a.noconvert())
;
}
1 change: 1 addition & 0 deletions python/tubex_lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class ctc:

delay = CtcDelay()
deriv = CtcDeriv()
eval = CtcEval()
dist = CtcDist()
Expand Down
59 changes: 59 additions & 0 deletions python/tubex_lib/tests/test_ctcdelay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python

import unittest
import math
from pyibex import Interval, IntervalVector
from tubex_lib import *

class TestCtcDelay(unittest.TestCase):

def assertApproxIntv(self, first, second):
if first.is_empty() is False:
# if isinstance(second, Interval):
self.assertAlmostEqual(first.lb(), second.lb())
self.assertAlmostEqual(first.ub(), second.ub())
else:
self.assertEqual(first, second)

def test_ctcdelay_1(self):
tdomain = Interval(0.,10.)
x = Tube(tdomain, 0.01, TFunction("cos(t)"))
y = Tube(tdomain, 0.01)

pi = Interval.PI
ctc_delay = CtcDelay()
ctc_delay.contract(pi, x, y)

beginDrawing()
fig_x = VIBesFigTube("delay x", x)
fig_x.show()
fig_y = VIBesFigTube("delay y", y)
fig_y.show()
endDrawing()

self.assertEqual(y(Interval(0.,math.pi)), Interval.ALL_REALS)
self.assertApproxIntv(y(Interval(math.pi+0.01,10.)), Interval(-1.,1.))
self.assertTrue(y(5.).is_superset(x(5. - math.pi)))

def test_ctcdelay_2(self):
tdomain = Interval(0.,10.)
dt = 0.01
x = Tube(tdomain, dt, TFunction("cos(t)"))
y = Tube(tdomain, dt, TFunction("sin(t)"))

delay = Interval(0.,2.*math.pi)
ctc_delay = CtcDelay()
ctc_delay.contract(delay, x, y)

beginDrawing()
fig_x = VIBesFigTube("delay x", x)
fig_x.show()
fig_y = VIBesFigTube("delay y", y)
fig_y.show()
endDrawing()

self.assertTrue(delay.contains(math.pi/2.))
self.assertTrue(delay.diam() < 3.*dt)

if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletions python/tubex_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void export_CtcFunction(py::module& m);
void export_CtcConstell(py::module& m);

py::class_<DynCtc,pyDynCtc> export_DynCtc(py::module& m);
void export_CtcDelay(py::module& m, py::class_<DynCtc, pyDynCtc>& dyn_ctc);
void export_CtcDeriv(py::module& m, py::class_<DynCtc, pyDynCtc>& dyn_ctc);
void export_CtcEval(py::module& m, py::class_<DynCtc, pyDynCtc>& dyn_ctc);
void export_CtcPicard(py::module& m, py::class_<DynCtc, pyDynCtc>& dyn_ctc);
Expand Down Expand Up @@ -68,6 +69,7 @@ PYBIND11_MODULE(tube, m)
export_CtcConstell(m);

py::class_<DynCtc, pyDynCtc> dyn_ctc = export_DynCtc(m);
export_CtcDelay(m, dyn_ctc);
export_CtcDeriv(m, dyn_ctc);
export_CtcEval(m, dyn_ctc);
export_CtcPicard(m, dyn_ctc);
Expand Down
4 changes: 2 additions & 2 deletions src/core/contractors/tubex_predef_contractors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace tubex
{
namespace ctc
{
CtcDelay delay;
CtcDeriv deriv;
CtcEval eval;
CtcDist dist;
CtcDelay delay;
CtcEval eval;
}
}
4 changes: 2 additions & 2 deletions src/core/contractors/tubex_predef_contractors.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ namespace tubex
{
namespace ctc
{
extern CtcDelay delay; // delay constraint (a,x,y)
extern CtcDeriv deriv; // derivative constraint (x,v)
extern CtcEval eval; // evaluation constraint (t,z,y,w)
extern CtcDist dist; // distance constraint (a,b,d)
extern CtcDelay delay; // delay constraint (a,x,y)
extern CtcEval eval; // evaluation constraint (t,z,y,w)
}
}

Expand Down

0 comments on commit 3ab0551

Please sign in to comment.