Skip to content

Commit

Permalink
[examples] added examples from pyIbex
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRohou committed Apr 12, 2022
1 parent 1d6b99d commit e52a3ad
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 1 deletion.
52 changes: 52 additions & 0 deletions examples/examples_from_pyibex/ex_ctcRaster.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions examples/examples_from_pyibex/example1.py
Original file line number Diff line number Diff line change
@@ -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()
47 changes: 47 additions & 0 deletions examples/examples_from_pyibex/example2.py
Original file line number Diff line number Diff line change
@@ -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()
52 changes: 52 additions & 0 deletions examples/examples_from_pyibex/example3.py
Original file line number Diff line number Diff line change
@@ -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()
28 changes: 28 additions & 0 deletions examples/examples_from_pyibex/example_paving_transform.py
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 23 additions & 0 deletions examples/examples_from_pyibex/example_polar.py
Original file line number Diff line number Diff line change
@@ -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)
84 changes: 84 additions & 0 deletions examples/examples_from_pyibex/example_projection1.py
Original file line number Diff line number Diff line change
@@ -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()
22 changes: 22 additions & 0 deletions examples/examples_from_pyibex/sphereProj.py
Original file line number Diff line number Diff line change
@@ -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()
Binary file added examples/examples_from_pyibex/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions examples/examples_from_pyibex/test_Img.py
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit e52a3ad

Please sign in to comment.