From e52a3ada3ae959ad4697a87a0b197890972447b9 Mon Sep 17 00:00:00 2001 From: SimonRohou Date: Tue, 12 Apr 2022 22:15:00 +0200 Subject: [PATCH] [examples] added examples from pyIbex --- examples/examples_from_pyibex/ex_ctcRaster.py | 52 +++++++++++ examples/examples_from_pyibex/example1.py | 18 ++++ examples/examples_from_pyibex/example2.py | 47 ++++++++++ examples/examples_from_pyibex/example3.py | 52 +++++++++++ .../example_paving_transform.py | 28 ++++++ .../examples_from_pyibex/example_polar.py | 23 +++++ .../example_projection1.py | 84 ++++++++++++++++++ examples/examples_from_pyibex/sphereProj.py | 22 +++++ examples/examples_from_pyibex/test.png | Bin 0 -> 275 bytes examples/examples_from_pyibex/test_Img.py | 45 ++++++++++ python/codac_py_core.cpp | 4 + python/src/core/sivia/codac_py_sivia.cpp | 2 +- 12 files changed, 376 insertions(+), 1 deletion(-) create mode 100644 examples/examples_from_pyibex/ex_ctcRaster.py create mode 100644 examples/examples_from_pyibex/example1.py create mode 100644 examples/examples_from_pyibex/example2.py create mode 100644 examples/examples_from_pyibex/example3.py create mode 100644 examples/examples_from_pyibex/example_paving_transform.py create mode 100644 examples/examples_from_pyibex/example_polar.py create mode 100644 examples/examples_from_pyibex/example_projection1.py create mode 100644 examples/examples_from_pyibex/sphereProj.py create mode 100644 examples/examples_from_pyibex/test.png create mode 100644 examples/examples_from_pyibex/test_Img.py diff --git a/examples/examples_from_pyibex/ex_ctcRaster.py b/examples/examples_from_pyibex/ex_ctcRaster.py new file mode 100644 index 000000000..55d0aa8a4 --- /dev/null +++ b/examples/examples_from_pyibex/ex_ctcRaster.py @@ -0,0 +1,52 @@ +from codac import * +from codac.unsupported import * + +import os +import scipy.misc +import scipy.ndimage as ndimage +import numpy as np + +ain = np.zeros((200, 200), dtype=np.int64) +ain[75:125, 50:125] = 1 +ain[25:100, 25:50] = 1 + +# over approximation of the set X +aout = np.zeros((200, 200), dtype=np.int64) +aout[74:126, 49:126] = 1 +aout[24:101, 24:51] = 1 + +img_out = aout.cumsum(0).cumsum(1) +img_in = (1 - ain).cumsum(0).cumsum(1) + + +img = 127*(aout+ain).T +scipy.misc.imsave('test.png', img.astype(np.uint8)) + + + +ctcOut = CtcRaster(img_out, -5, 5, 0.1, -0.1) +ctcIn = CtcRaster(img_in, -5, 5, 0.1, -0.1) +sep = SepCtcPair(ctcIn, ctcOut) + + + +X0 = IntervalVector(2, [-15,15]) + +beginDrawing() +fig = VIBesFig('CtcImage') +fig.set_properties(x=100, y=100, width=600, height=600) +fig.axis_limits(X0) +# X0 = IntervalVector([[4, 10], [-6, -4]]) +# X0 = IntervalVector([[-2.47, -2.17], [-0.5, 0.17]]) +# X0 = IntervalVector([[-2.47, -2.17], [-0.1, 0.17]]) +# X0_res = IntervalVector([[-2.47, -2.17], [-0.1, 0]) +# X0 = IntervalVector([ [2.05, 2.15], [-0.08, -0.02] ]) +# X0 = IntervalVector([ [2.05, 2.15], [-0.08, -0.0] ]) +# +#vibes.drawRaster(os.path.abspath('test.png'), -5,5, 0.1, -0.1) +# vibes.drawBox(X0[0][0], X0[0][1], X0[1][0], X0[1][1], '[#FF000055]') +# self.ctcOut.contract(X0) +# vibes.drawBox(X0[0][0], X0[0][1], X0[1][0], X0[1][1], '[#0000FF55]') + +SIVIA(X0, sep, 0.2) +# pySIVIA(X0, sep, 0.05, use_patch=True) diff --git a/examples/examples_from_pyibex/example1.py b/examples/examples_from_pyibex/example1.py new file mode 100644 index 000000000..694a60bf3 --- /dev/null +++ b/examples/examples_from_pyibex/example1.py @@ -0,0 +1,18 @@ +# Codac - Examples +# Originated from the former pyIbex library (Benoît Desrochers) +# Author: Benoît Desrochers +# ---------------------------------------------------------------------------- +# Simple example of a separator + +from codac import * + +f = Function('x', 'y', 'x*cos(x-y)+y') +sep = SepFwdBwd(f, CmpOp.LEQ) +x = IntervalVector(2, [-10, 10]) + +beginDrawing() +fig = VIBesFig('Example') +fig.set_properties(x=100, y=100, width=600, height=600) +fig.axis_limits(x) +SIVIA(x, sep, 0.2) +endDrawing() \ No newline at end of file diff --git a/examples/examples_from_pyibex/example2.py b/examples/examples_from_pyibex/example2.py new file mode 100644 index 000000000..27da62912 --- /dev/null +++ b/examples/examples_from_pyibex/example2.py @@ -0,0 +1,47 @@ +# Codac - Examples +# Originated from the former pyIbex library (Benoît Desrochers) +# Author: Benoît Desrochers +# ---------------------------------------------------------------------------- +# Custom contractor +# Writes a custom contractor for the equation +# (x-a)^2 + (y-b)^2 \in r^2 + +from codac import * + +class MyCtc(Ctc): + + def __init__(self, a, b, r): + Ctc.__init__(self, 2) + self.a = Interval(a) + self.b = Interval(b) + self.r = r + + def contract(self, x): + i1 = x[0] - Interval(self.a) + i2 = x[1] - Interval(self.b) + + i3 = sqr(i1) + i4 = sqr(i2) + + i5 = i3 + i4 + i5 &= sqr(self.r) + + bwd_add(i5, i3, i4) + bwd_sqr(i4, i2) + bwd_sqr(i3, i1) + + bwd_sub(i1, x[0], Interval(self.a)) + bwd_sub(i2, x[1], Interval(self.b)) + + return x + + +ctc = MyCtc(1, 2, Interval(1, 2)) +x = IntervalVector(2, [-8, 8]) + +beginDrawing() +fig = VIBesFig('Result') +fig.set_properties(x=100, y=100, width=600, height=600) +fig.axis_limits(x) +SIVIA(x, ctc, 0.3) +endDrawing() \ No newline at end of file diff --git a/examples/examples_from_pyibex/example3.py b/examples/examples_from_pyibex/example3.py new file mode 100644 index 000000000..3dbeea266 --- /dev/null +++ b/examples/examples_from_pyibex/example3.py @@ -0,0 +1,52 @@ +# Codac - Examples +# Originated from the former pyIbex library (Benoît Desrochers) +# Author: Benoît Desrochers +# ---------------------------------------------------------------------------- +# Simple range-only localization using separators +# We consider the localisation of a robot measuring distances w.r.t three landmaks. + +from codac import * +import numpy as np + +# Theorical (unknown) position of the robot +robot = [-2,4] + +# Position of the landmarks +landmarks = [[6,12], [-2,-5], [-3,10]] + +# Distances measured w.r.t to landmaks +dist = [Interval(11,12), Interval(8,10), Interval(5,7)] + +# Creation of a separator +seps = [] # empty list of separators +# Iterate over landmarks and distances +for (m_x, m_y), d in zip(landmarks, dist): + # Create the constraint function + f = Function("x[2]", "(x[0]-%f)^2+(x[1]-%f)^2"%(m_x, m_y)) + # Create the fwdbwd separator for: f(x) \in d + sep = SepFwdBwd(f, sqr(d)) + seps.append(sep) + +# Create the separator as intersection of the previous separators +sep = SepInter(seps) + +# Create the initial box +x0 = IntervalVector([[-12,11], [-6,17]]) + +# Init graphics +beginDrawing() +fig = VIBesFig('Localization') +fig.set_properties(x=100, y=100, width=600, height=600) +fig.axis_limits(x0) + +# Running the paver +SIVIA(x0, sep, 0.1) + +# Displaying ranges of measurements +for (x, y), d in zip(landmarks, dist): + fig.draw_circle(x, y, 0.1, "[k]") + fig.draw_circle(x, y, d.lb(), "k") + fig.draw_circle(x, y, d.ub(), "k") +fig.draw_vehicle(robot[0], robot[1], np.rad2deg(0.3), 1, 'k[y]') + +endDrawing() \ No newline at end of file diff --git a/examples/examples_from_pyibex/example_paving_transform.py b/examples/examples_from_pyibex/example_paving_transform.py new file mode 100644 index 000000000..6ca26b87e --- /dev/null +++ b/examples/examples_from_pyibex/example_paving_transform.py @@ -0,0 +1,28 @@ +# Codac - Examples +# Originated from the former pyIbex library (Benoît Desrochers) +# Author: Benoît Desrochers +# ---------------------------------------------------------------------------- + +from codac import * +from codac.unsupported import * + +x0 = IntervalVector(2, [-5, 5]) +sep = SepFwdBwd(Function("x[2]", "x[0]^2 + x[1]^2"), Interval(-oo, 4)) +#P = SepPaving(x0, sep, 0.1) + +f = Function("x", 'y', '(0.5*x, 2*y)') +sep2 = SepInverse(sep, f) + +beginDrawing() + +fig = VIBesFig('test 1') +fig.set_properties(x=100, y=100, width=600, height=600) +fig.axis_limits(x0) +SIVIA(x0,sep,0.1) + +fig = VIBesFig('test 2') +fig.set_properties(x=100, y=100, width=600, height=600) +fig.axis_limits(x0) +SIVIA(x0,sep2,0.1) + +endDrawing() \ No newline at end of file diff --git a/examples/examples_from_pyibex/example_polar.py b/examples/examples_from_pyibex/example_polar.py new file mode 100644 index 000000000..acadf8ae8 --- /dev/null +++ b/examples/examples_from_pyibex/example_polar.py @@ -0,0 +1,23 @@ +# Codac - Examples +# Originated from the former pyIbex library (Benoît Desrochers) +# Author: Benoît Desrochers +# ---------------------------------------------------------------------------- + +from codac import * +from codac.unsupported import * +import numpy as np + +sep1 = SepPolarXY(Interval(4,5), Interval(np.deg2rad(-570), np.deg2rad(-450))) + +f1 = Function("x", 'y', '(x + 2; y - 2)') +f2 = Function("x", 'y', '(x - 2; y + 2)') +sep = SepTransform(sep1, f1, f2) + +x = IntervalVector([[-10,10],[-10,10]]) + + +beginDrawing() +fig = VIBesFig('SIVIA') +fig.set_properties(x=100, y=100, width=600, height=600) +fig.axis_limits(x) +SIVIA(x, sep1, 0.1) \ No newline at end of file diff --git a/examples/examples_from_pyibex/example_projection1.py b/examples/examples_from_pyibex/example_projection1.py new file mode 100644 index 000000000..47fb870bc --- /dev/null +++ b/examples/examples_from_pyibex/example_projection1.py @@ -0,0 +1,84 @@ +# Codac - Examples +# Originated from the former pyIbex library (Benoît Desrochers) +# Author: Benoît Desrochers +# ---------------------------------------------------------------------------- +# Example from Applied Interval Analysis (L. Jaulin et.al. ) §6.3.4 + +from codac import * +from codac.unsupported import * +import math + +def export_data(Y, T): + vibes.newFigure('Data') + vibes.setFigureProperties({'x': 0, 'y': 0, 'width': 1000, 'height': 1000}) + print("#"*50) + print("Data to use") + for i,(y,t) in enumerate(zip(Y[::-1], T[::-1])): + # print(" %d & %s & %s \\\\ \\hline"%(i+1,t,y)) + print("\\draw[boxColor] (%f, %f) rectangle (%f, %f);"%(t[0], y[0], t[1], y[1])) + # vibes.drawBox(t[0], t[1], y[0], y[1], 'k[lightGray]') + print("#"*50) + + +def test_paramEstim( useSepProj=False): + + f = Function("p1", "p2", "t", "20*exp(-p1*t)-8*exp(-p2*t)"); + + Y = [ + Interval(2.7,12.1), + Interval(1.04,7.14), + Interval(-0.13,3.61), + Interval(-0.95,1.15), + Interval(-4.85,-0.29), + Interval(-5.06,-0.36), + Interval(-4.1,-0.04), + Interval(-3.16,0.3), + Interval(-2.5,0.51), + Interval(-2,0.67) + ] + + T = [ + Interval(-0.25,1.75), + Interval(0.5,2.5), + Interval(1.25,3.25), + Interval(2,4), + Interval(5,7), + Interval(8,10), + Interval(12,14), + Interval(16,18), + Interval(20,22), + Interval(24,26) + ] + + seps = [] + + for y_i,t_i in zip(Y, T): + if useSepProj == False: + seps.append( SepCtcPairProj( SepFwdBwd(f, y_i ), t_i, 1e-3) ) + else: + seps.append( SepProj( SepFwdBwd(f, y_i ), t_i, 1e-3) ) + + sep = SepInter(seps) + + X0 = IntervalVector(2,Interval(0,1.2)); + X0[1] = Interval(0,0.5); + + + return sep, X0, 0.01 + + + + +sep, X0, eps = test_paramEstim(True) + + + +beginDrawing() +fig = VIBesFig('Result') +fig.set_properties(x=100, y=100, width=600, height=600) +fig.axis_limits(X0) + +# run SIVIA +SIVIA(X0, sep, eps) + +endDrawing() \ No newline at end of file diff --git a/examples/examples_from_pyibex/sphereProj.py b/examples/examples_from_pyibex/sphereProj.py new file mode 100644 index 000000000..51d044bbb --- /dev/null +++ b/examples/examples_from_pyibex/sphereProj.py @@ -0,0 +1,22 @@ +# Codac - Examples +# Originated from the former pyIbex library (Benoît Desrochers) +# Author: Benoît Desrochers +# ---------------------------------------------------------------------------- +# Simple example of the projection of a sphere + +from codac import * +from codac.unsupported import * + +f = Function("x[2]","y","x[0]^2+x[1]^2+y^2") +S1=SepFwdBwd(f,Interval(4,9)) +Y=IntervalVector([[-1,1]]) +#S2=SepCtcPairProj(S1,Y,0.01) +S2=SepProj(S1,Y,0.01) +X0 =IntervalVector([[-10,10],[-10,10]]); + +beginDrawing() +fig = VIBesFig('Proj') +fig.set_properties(x=100, y=100, width=600, height=600) +fig.axis_limits(X0) +SIVIA(X0,S2,0.1) +endDrawing() \ No newline at end of file diff --git a/examples/examples_from_pyibex/test.png b/examples/examples_from_pyibex/test.png new file mode 100644 index 0000000000000000000000000000000000000000..2e497396d0e4ac3273fe1f4dcd978ce075d8a44e GIT binary patch literal 275 zcmeAS@N?(olHy`uVBq!ia0vp^CqS43NHBC5e{=#;w>(`OLn`LHz2nH$>>$A4DE#UD zgY%ymzbo)~ORrkj&DUEYdEqfgeZ%w0wS4;~2cJ7RJNCDZKbyB%iOhRm<81;#%1hil zdM8LJnmT2*T;W(L2o^Y7wca#yr}FIE6~1{=@8gTkwd%}2@mFxM_qE&qp2}`{LBsLijKV+IL#~$fECUI8y85}Sb4q9e0OB8F{Qv*} literal 0 HcmV?d00001 diff --git a/examples/examples_from_pyibex/test_Img.py b/examples/examples_from_pyibex/test_Img.py new file mode 100644 index 000000000..2ecbef686 --- /dev/null +++ b/examples/examples_from_pyibex/test_Img.py @@ -0,0 +1,45 @@ +# Codac - Examples +# Originated from the former pyIbex library (Benoît Desrochers) +# Author: Benoît Desrochers +# ---------------------------------------------------------------------------- + +import numpy as np +import scipy.ndimage as ndi + +from codac import * +from codac.unsupported import * + +import os + +import matplotlib.pyplot as plt +# load image data +img_gray = ndi.imread('./test.png', 'L').T +img_out = np.zeros(img_gray.shape, dtype=np.uint64) +img_in = np.zeros(img_gray.shape, dtype=np.uint64) + +img_out[img_gray >= 127 ] = 1 +img_in[img_gray != 255] = 1 + +# compute summed tables +img_out = img_out.cumsum(0).cumsum(1) +img_in = img_in.cumsum(0).cumsum(1) + +# create contractors with CtcRaster +# top left corner is at position (-5, 5) +# pixel size is 0.1 by -0.1 +ctcOut = CtcRaster(img_out, -5, 5, 0.1, -0.1) +ctcIn = CtcRaster(img_in, -5, 5, 0.1, -0.1) +sep = SepCtcPair(ctcIn, ctcOut) + + +X0 = IntervalVector(2, [-15,15]) + +beginDrawing() +fig = VIBesFig('CtcImage') +fig.set_properties(x=100, y=100, width=600, height=600) +fig.axis_limits(X0) + +#vibes.drawRaster(os.path.abspath('test.png'), -5,5, 0.1, -0.1) +SIVIA(X0, sep, 0.2) + +endDrawing() \ No newline at end of file diff --git a/python/codac_py_core.cpp b/python/codac_py_core.cpp index bbf2c6077..3ad609328 100644 --- a/python/codac_py_core.cpp +++ b/python/codac_py_core.cpp @@ -79,6 +79,8 @@ void export_Set(py::module& m); void export_DataLoader(py::module& m); void export_TPlane(py::module& m); +void export_sivia(py::module& m, py::class_& ctc, py::class_& sep); + PYBIND11_MODULE(core, m) { @@ -137,6 +139,8 @@ PYBIND11_MODULE(core, m) export_DataLoader(m); export_TPlane(m); + export_sivia(m, ctc, sep); + // m.attr("ibex_version") = _IBEX_VERSION_; // return m.ptr(); } \ No newline at end of file diff --git a/python/src/core/sivia/codac_py_sivia.cpp b/python/src/core/sivia/codac_py_sivia.cpp index 03146e758..6adb039e4 100644 --- a/python/src/core/sivia/codac_py_sivia.cpp +++ b/python/src/core/sivia/codac_py_sivia.cpp @@ -27,7 +27,7 @@ namespace py = pybind11; using namespace pybind11::literals; -void export_unsupported_geometry(py::module& m, py::class_& ctc, py::class_& sep) +void export_sivia(py::module& m, py::class_& ctc, py::class_& sep) { m.def("SIVIA", [](const IntervalVector& x, Ctc& ctc, float precision, const SetColorMap& color_map) {