Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
.. _sec-functions-differential-inclusions:

Differential Inclusions
=======================

Main author: `Damien Massé <https://labsticc.fr/fr/lab-sticc/annuaire/masse-damien>`_

The content of this page is limited to C++. Python bindings are missing for now.

Differential inclusions
-----------------------

Differential inclusions are equations of the form :math:`\dot{\mathbf{x}} \in \big\{ \mathbf{f}\big(t,\mathbf{x},\mathbf{u}\big) \big\}` where :math:`\mathbf{u}` are unknown (possibly non-continuous) functions varying
on a domain :math:`U`.

Codac enables to define differential inclusions and to integrate differential
inclusions. The variables for :math:`\mathbf{x}` and :math:`\mathbf{u}` can
be indifferently ``ScalarVar``, ``VectorVar`` or ``MatrixVar``. The
function :math:`\mathbf{f}` can be any function (linear or not).

For now, two integration schemes are implemented:

- ``RS_box``: a 0-order scheme using boxes (``IntervalVector``), very imprecise
- ``RS_zonotope``: a 1-order scheme using zonotopes, more precise. This scheme requires :math:`\mathbf{f}` to be differentiable (for all its variables).

Examples
--------

Direct integration
~~~~~~~~~~~~~~~~~~

We consider :math:`\mathbf{f}:\mathbb{R}^2 \times \mathbb{R} \to\mathbb{R}^2`:

.. math::
\mathbf{f}((x_1,x_2),u)= (-x_2+u, x_1-0.1 x_2)

We consider :math:`\mathbf{u}` varying in the interval :math:`[-0.03,0.03]`.
The following code performs a guaranteed integration (which constant
time steps) from
:math:`t=0` to :math:`t=10` using ``RS_box``, starting from a small box
:math:`[1,1.01]\times[0,0.01]` and draws the result.

.. tabs::

.. group-tab:: C++

.. literalinclude:: src1.cpp
:language: c++
:start-after: [1-beg]
:end-before: [1-end]
:dedent: 4

The next code performs the same integration using ``RS_zonotope``:

.. tabs::

.. group-tab:: C++

.. literalinclude:: src1.cpp
:language: c++
:start-after: [2-beg]
:end-before: [2-end]
:dedent: 4

And this code integrate starting from a large square (the maximum
order of zonotopes is deliberately set to a low value, which means
that the integration is done on parallelepipeds):

.. tabs::

.. group-tab:: C++

.. literalinclude:: src1.cpp
:language: c++
:start-after: [3-beg]
:end-before: [3-end]
:dedent: 4

The results are given below. A black line describes the computation result
when :math:`\mathbf{u}` is 0 (hence the equation is an ODE). The inclusion
of the final set of reachables sets from the large square (in black)
inside the initial set (in blue) proves the stability of the differential
inclusion.

.. image:: img/diff_inclusion_first.png
:alt: 2D-differential inclusion integration
:align: center
:width: 400px

Tube integration
~~~~~~~~~~~~~~~~

Codac enables to generate a tube :class:``~codac.SlicedTube`` from a set
of initial (or final) states, following a temporal domain
:class:`~codac.TDomain`. The contraction of an existing tube is also provided.
However, since the domain of zonotopes does not admit useful intersection
operators, the contraction is not very efficient for now. An example is
given below:

.. tabs::

.. group-tab:: C++

.. literalinclude:: src2.cpp
:language: c++
:start-after: [1-beg]
:end-before: [1-end]
:dedent: 4

The resulting plot is shown below.

.. image:: img/diff_inclusion_tube.png
:alt: tubes for differential inclusion
:align: center
:width: 400px

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions doc/manual/manual/functions/differentialinclusions/src1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <codac>

using namespace std;
using namespace codac2;

int main()
{
{
// [1-beg]
VectorVar x(2);
ScalarVar u;
AnalyticFunction f({x,u}, {-x[1]+u, x[0]-0.1*x[1]});
DiffInclusion<VectorVar,ScalarVar> di(f);

int q=0;
DI_Integration<DiffInclusion<VectorVar,ScalarVar>,
RS_box> integration0(di);
IntervalVector state {{1.0,1.01},{0.0,0.01}};
for (double t=0;t<2.0;t+=0.01) {
IntervalVector s1 = IntervalVector::constant(2,Interval());
IntervalVector s2 = IntervalVector::constant(2,Interval());
integration0.contract_one_step(Interval(t,t+0.01),
state,
s1,s2, Interval(-0.03,0.03),true);
state=s2;
if ((++q)%10==0)
DefaultFigure::draw_box(state,Color::purple());
else
DefaultFigure::draw_box(state,Color::purple(0.1));
}
// [1-end]


// [2-beg]
state = {{1.0,1.01},{0.0,0.01}};
Zonotope zstate(state);
DI_Integration<DiffInclusion<VectorVar,ScalarVar>,
RS_zonotope> integration1(di);
q=0;
/* high limit the number of zonotope generators */
integration1.get_reachabilitystep()->set_max_zonotope_order(200.0);
for (double t=0;t<10.0;t+=0.02) {
IntervalVector s1 = IntervalVector::constant(2,Interval());
Zonotope z2;
integration1.contract_one_step(Interval(t,t+0.02),
zstate,
s1,z2, Interval(-0.03,0.03),true);
zstate=z2;
if ((++q)%10==0) {
DefaultFigure::draw_zonotope(zstate,Color::red());
} else
DefaultFigure::draw_zonotope(zstate,Color::red(0.1));
}
// [2-end]


state = {{1.0,1.01},{0.0,0.01}};
zstate = Zonotope(state);
for (double t=0;t<10.0;t+=0.02) {
IntervalVector s1 = IntervalVector::constant(2,Interval());
Zonotope z2;
integration1.contract_one_step(Interval(t,t+0.02),
zstate,
s1,z2, Interval(-0.00,0.00),true);
zstate=z2;
if ((++q)%10==0) {
DefaultFigure::draw_zonotope(zstate,Color::black());
} else
DefaultFigure::draw_zonotope(zstate,Color::black(0.1));
}


// [3-beg]
/* low limit the number of zonotope generators */
integration1.get_reachabilitystep()->set_max_zonotope_order(2.0);
state = {{-2.0,2.0},{-2.0,2.0}};
zstate = Zonotope(state);
DefaultFigure::draw_zonotope(zstate,Color::blue());
q=0;
for (double t=0;t<11.0;t+=0.01) {
IntervalVector s1 = IntervalVector::constant(2,Interval());
Zonotope z2;
integration1.contract_one_step(Interval(t,t+0.01),
zstate,
s1,z2, Interval(-0.03,0.03),true);
zstate=z2;
if ((++q)%30==0) DefaultFigure::draw_zonotope(zstate,Color::red(0.2));
}
DefaultFigure::draw_zonotope(zstate,Color::black());
// [3-end]

}
}
34 changes: 34 additions & 0 deletions doc/manual/manual/functions/differentialinclusions/src2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <codac>

using namespace std;
using namespace codac2;

int main()
{
{
// [1-beg]
VectorVar x(2);
ScalarVar u;
AnalyticFunction f({x,u}, {-x[1]+u, x[0]-0.1*x[1]});
DiffInclusion<VectorVar,ScalarVar> di(f);

IntervalVector init = {{-0.3,0.3},{0.8,1.0}};
DI_Integration<DiffInclusion<VectorVar,ScalarVar>,
RS_zonotope> integration1(di);
auto tube = integration1.create_sliced_tube
(create_tdomain(Interval(0,10),0.03),
init, IntervalVector({{-10,10},{-10,10}}),
Interval(-0.03,0.03),true);

DefaultFigure::plot_tube(tube[0],Color::red(0.1));
DefaultFigure::plot_tube(tube[1],Color::blue(0.1));

tube.set(IntervalVector({{0.85,0.9},{0.15,0.2}}),Interval(5.0));
integration1.contract_sliced_tube
(tube,Interval(-0.03,0.03),TimePropag::FWD_BWD);

DefaultFigure::plot_tube(tube[0],Color::red(1.0));
DefaultFigure::plot_tube(tube[1],Color::blue(1.0));
// [1-end]
}
}
3 changes: 2 additions & 1 deletion doc/manual/manual/functions/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ Inclusion functions
analytic/analytic_functions.rst
analytic/analytic_operators.rst
parallelepiped/parallelepiped_eval.rst
peibos/peibos.rst
peibos/peibos.rst
differentialinclusions/differentialinclusions.rst
39 changes: 39 additions & 0 deletions examples/17_diffinclusions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ==================================================================
# codac / basics example - cmake configuration file
# ==================================================================

cmake_minimum_required(VERSION 3.10)
project(codac_example LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Adding Codac

# In case you installed Codac in a local directory, you need
# to specify its path with the CMAKE_PREFIX_PATH option.
# set(CMAKE_PREFIX_PATH "~/codac/build_install")

find_package(CODAC REQUIRED)
message(STATUS "Found Codac version ${CODAC_VERSION}")

# Initializating Ibex

ibex_init_common()

# Compilation

if(FAST_RELEASE)
add_compile_definitions(FAST_RELEASE)
message(STATUS "You are running Codac in fast release mode. (option -DCMAKE_BUILD_TYPE=Release is required)")
endif()

add_executable(${PROJECT_NAME} main.cpp)
target_compile_options(${PROJECT_NAME} PUBLIC ${CODAC_CXX_FLAGS})
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${CODAC_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${CODAC_LIBRARIES})

add_executable(diff_inclusion_tube main_tube.cpp)
target_compile_options(diff_inclusion_tube PUBLIC ${CODAC_CXX_FLAGS})
target_include_directories(diff_inclusion_tube SYSTEM PUBLIC ${CODAC_INCLUDE_DIRS})
target_link_libraries(diff_inclusion_tube PUBLIC ${CODAC_LIBRARIES})
81 changes: 81 additions & 0 deletions examples/17_diffinclusions/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <codac>

using namespace std;
using namespace codac2;

int main()
{
{
VectorVar x(2);
ScalarVar u;
AnalyticFunction f({x,u}, {-x[1]+u, x[0]-0.1*x[1]});
DiffInclusion<VectorVar,ScalarVar> di(f);

int q=0;
DI_Integration<DiffInclusion<VectorVar,ScalarVar>,
RS_box> integration0(di);
IntervalVector state {{1.0,1.01},{0.0,0.01}};
for (double t=0;t<2.0;t+=0.01) {
IntervalVector s1 = IntervalVector::constant(2,Interval());
IntervalVector s2 = IntervalVector::constant(2,Interval());
integration0.contract_one_step(Interval(t,t+0.01),
state,
s1,s2, Interval(-0.03,0.03),true);
state=s2;
if ((++q)%10==0)
DefaultFigure::draw_box(state,Color::purple());
else
DefaultFigure::draw_box(state,Color::purple(0.1));
}
state = {{1.0,1.01},{0.0,0.01}};
Zonotope zstate(state);
DI_Integration<DiffInclusion<VectorVar,ScalarVar>,
RS_zonotope> integration1(di);
q=0;
/* high limit the number of zonotope generators */
integration1.get_reachabilitystep()->set_max_zonotope_order(200.0);
for (double t=0;t<10.0;t+=0.02) {
IntervalVector s1 = IntervalVector::constant(2,Interval());
Zonotope z2;
integration1.contract_one_step(Interval(t,t+0.02),
zstate,
s1,z2, Interval(-0.03,0.03),true);
zstate=z2;
if ((++q)%10==0) {
DefaultFigure::draw_zonotope(zstate,Color::red());
} else
DefaultFigure::draw_zonotope(zstate,Color::red(0.1));
}
state = {{1.0,1.01},{0.0,0.01}};
zstate = Zonotope(state);
for (double t=0;t<10.0;t+=0.02) {
IntervalVector s1 = IntervalVector::constant(2,Interval());
Zonotope z2;
integration1.contract_one_step(Interval(t,t+0.02),
zstate,
s1,z2, Interval(-0.00,0.00),true);
zstate=z2;
if ((++q)%10==0) {
DefaultFigure::draw_zonotope(zstate,Color::black());
} else
DefaultFigure::draw_zonotope(zstate,Color::black(0.1));
}
/* low limit the number of zonotope generators */
integration1.get_reachabilitystep()->set_max_zonotope_order(2.0);
state = {{-2.0,2.0},{-2.0,2.0}};
zstate = Zonotope(state);
DefaultFigure::draw_zonotope(zstate,Color::blue());
q=0;
for (double t=0;t<11.0;t+=0.01) {
IntervalVector s1 = IntervalVector::constant(2,Interval());
Zonotope z2;
integration1.contract_one_step(Interval(t,t+0.01),
zstate,
s1,z2, Interval(-0.03,0.03),true);
zstate=z2;
if ((++q)%30==0) DefaultFigure::draw_zonotope(zstate,Color::red(0.2));
}
DefaultFigure::draw_zonotope(zstate,Color::black());

}
}
Loading
Loading