Skip to content

Commit

Permalink
Merge pull request #186 from SimonRohou/codac2_dev
Browse files Browse the repository at this point in the history
Trajectories + proj
  • Loading branch information
SimonRohou authored Jan 20, 2025
2 parents 90d48ff + acfb48a commit fd4398b
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 47 deletions.
27 changes: 22 additions & 5 deletions doc/manual/development/info_dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ If you simply want to use the latest Codac release in Python, you can download t

1. **Ensure the following prerequisites are met**:

- the prerequisites for the :ref:`C++ installation of Codac <sec-install-cpp-prerequisites>`.
- the prerequisites for the :ref:`C++ installation of Codac <sec-install-cpp-prerequisites>`. On Linux systems, you can simply:

.. code-block:: bash
sudo apt-get install -y g++ gcc cmake git flex bison
- a supported version of Python (>=3.6).
- a recent `Doxygen <https://www.doxygen.nl>`_ version (for instance, release 1.13.0 or newest). On Linux systems, latest releases are not available as Debian packages, so we advice to install Doxygen from the sources:

Expand All @@ -60,13 +65,14 @@ If you simply want to use the latest Codac release in Python, you can download t

.. code-block:: bash
git clone -b master https://github.com/lebarsfa/ibex-lib.git $HOME/ibex-lib
git clone https://github.com/lebarsfa/ibex-lib.git $HOME/ibex-lib
cd $HOME/ibex-lib
You will need to compile both IBEX and Codac using the ``-fPIC`` options. This can be done with the following CMake configuration:

.. code-block:: bash
cd $HOME/ibex-lib/build
mkdir build ; cd build
cmake -DCMAKE_CXX_FLAGS="-fPIC" -DCMAKE_C_FLAGS="-fPIC" -DCMAKE_INSTALL_PREFIX=$HOME/ibex-lib/build_install -DCMAKE_BUILD_TYPE=Release ..
make ; make install
Expand All @@ -77,16 +83,27 @@ If you simply want to use the latest Codac release in Python, you can download t
.. code-block:: bash
git clone https://github.com/codac-team/codac $HOME/codac
cd $HOME/codac
.. admonition:: Using Codac v2 simultaneously with Codac v1

In case you want to use the two versions of Codac in the same Python script, you will have to compile the binaries of Codac v2 under a different name in order to avoid ``import`` conflicts. Things are already prepared in the branch ``codac2_renamed``, you can therefore:

.. code-block:: bash
git checkout codac2_renamed
Note that you will then have to ``import codac2`` instead of ``import codac`` in your Python scripts.

In addition to the ``-fPIC`` options, you will have to configure ``WITH_PYTHON=ON``. Note that the ``git submodule`` commands will automatically get the `pybind11 <https://pybind11.readthedocs.io>`_ files required for the binding.

.. code-block:: bash
cd $HOME/codac/build
# Get automatically pybind11 and eigen submodules:
git submodule init ; git submodule update
# Configure CMake
cmake -DCMAKE_CXX_FLAGS="-fPIC" -DCMAKE_C_FLAGS="-fPIC" -DWITH_PYTHON=ON -DCMAKE_INSTALL_PREFIX=$HOME/codac/build_install -DCMAKE_PREFIX_PATH=$HOME/ibex-lib/build_install -DCMAKE_BUILD_TYPE=Release ..
mkdir build ; cd build
cmake -DCMAKE_CXX_FLAGS="-fPIC" -DCMAKE_C_FLAGS="-fPIC" -DWITH_PYTHON=ON -DCMAKE_INSTALL_PREFIX=$HOME/codac/build_install -DCMAKE_PREFIX_PATH="$HOME/ibex-lib/build_install;$HOME/doxygen/build_install" -DCMAKE_BUILD_TYPE=Release ..
make ; make install
4. **Configure your Python environment**:
Expand Down
4 changes: 2 additions & 2 deletions doc/manual/manual/installation/cpp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ Troubleshooting
----------------
If you encounter issues during the installation process, consider the following:

- Ensure all prerequisites are installed and up to date.
- Check `the GitHub issues <https://github.com/codac-team/codac/issues>`_ page for known problems.
- ensure all prerequisites are installed and up to date.
- check `the GitHub issues <https://github.com/codac-team/codac/issues>`_ page for known problems.

If you need further assistance, reach out to the library maintainers via the GitHub repository's issue tracker or email support at `simon.rohou [at] ensta.fr`.

2 changes: 1 addition & 1 deletion examples/00_graphics/graphic_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@
t=ScalarVar()
# Fermat's spiral
f1=AnalyticFunction([t], [a*sqrt(t)*cos(t),a*sqrt(t)*sin(t)])
traj3=AnalyticTrajectory(f1, [0,100])
traj3=AnalyticTraj(f1, [0,100])
fig3.draw_trajectory(traj3, ColorMap.rainbow())
2 changes: 1 addition & 1 deletion examples/04_explored_area/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main()

Interval tdomain(0,5);
auto sampled_f = AnalyticTraj(f,tdomain).sampled(0.8);
sampled_f[6] = {0,-1}; // appending the position (0,-1) at t=6
sampled_f.set(6., {0,-1}); // appending the position (0,-1) at t=6

VectorVar w(3);
auto g = sampled_f.as_function();
Expand Down
2 changes: 1 addition & 1 deletion examples/04_explored_area/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

tdomain = [0,5]
sampled_f = AnalyticTraj(f,tdomain).sampled(0.8)
sampled_f[6] = [0,-1] # appending the position (0,-1) at t=6
sampled_f.set(6, [0,-1]) # appending the position (0,-1) at t=6

w = VectorVar(3)
g = sampled_f.as_function()
Expand Down
14 changes: 14 additions & 0 deletions python/src/core/trajectory/codac2_py_AnalyticTraj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ void _export_AnalyticTraj(py::module& m, const string& class_name)
ANALYTICTRAJ_OS_ANALYTICTRAJ_CONST_ANALYTICFUNCTION_O_REF_CONST_INTERVAL_REF,
"f"_a, "tdomain"_a)

.def("__call__", [](const AnalyticTraj<O>& x, double t)
{
return x(t);
},
VIRTUAL_S_ANALYTICTRAJ_OS_OPERATORCALL_DOUBLE_CONST,
"t"_a)

.def("__call__", [](const AnalyticTraj<O>& x, const Interval& t)
{
return x(t);
},
VIRTUAL_WRAPPER_S_DOMAIN_ANALYTICTRAJ_OS_OPERATORCALL_CONST_INTERVAL_REF_CONST,
"t"_a)

;
}

Expand Down
38 changes: 30 additions & 8 deletions python/src/core/trajectory/codac2_py_SampledTraj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ py::class_<SampledTraj<T>> _export_SampledTraj(py::module& m, const string& clas
else if constexpr(std::is_same_v<T,Vector>)
{
exported_class

.def(py::init(
[](const py::array_t<double>& l_t, const py::array_t<double>& l_x)
{
Expand Down Expand Up @@ -85,6 +86,18 @@ py::class_<SampledTraj<T>> _export_SampledTraj(py::module& m, const string& clas
}),
SAMPLEDTRAJ_T_SAMPLEDTRAJ_CONST_LIST_DOUBLE_REF_CONST_LIST_T_REF,
"l_t"_a, "l_x"_a)

.def("__getitem__", [](const SampledTraj<T>& x, Index_type i) -> SampledTraj<double>
{
matlab::test_integer(i);
return x[matlab::input_index(i)];
}, py::return_value_policy::reference_internal)

.def("subvector", [](const SampledTraj<T>& x, Index_type i, Index_type j) -> SampledTraj<Vector>
{
matlab::test_integer(i,j);
return x.subvector(matlab::input_index(i),matlab::input_index(j));
}, py::return_value_policy::reference_internal)
;
}

Expand All @@ -104,17 +117,26 @@ py::class_<SampledTraj<T>> _export_SampledTraj(py::module& m, const string& clas
SAMPLEDTRAJ_T_SAMPLEDTRAJ_T_SAMPLED_DOUBLE_BOOL_CONST,
"dt"_a, "keep_original_values"_a)

.def("__getitem__", [](const SampledTraj<T>& x, Index_type index) -> const T&
.def("__call__", [](const SampledTraj<T>& x, double t) -> T
{
matlab::test_integer(index);
return x.at(matlab::input_index(index));
}, py::return_value_policy::reference_internal)
return x(t);
},
VIRTUAL_T_SAMPLEDTRAJ_T_OPERATORCALL_DOUBLE_CONST,
"t"_a)

.def("__call__", [](const SampledTraj<T>& x, const Interval& t) -> typename Wrapper<T>::Domain
{
return x(t);
},
VIRTUAL_WRAPPER_T_DOMAIN_SAMPLEDTRAJ_T_OPERATORCALL_CONST_INTERVAL_REF_CONST,
"t"_a)

.def("__setitem__", [](SampledTraj<T>& x, Index_type index, const T& a)
.def("set", [](SampledTraj<T>& x, double ti, const T& xi)
{
matlab::test_integer(index);
x[matlab::input_index(index)] = a;
})
return x.set(ti,xi);
},
VOID_SAMPLEDTRAJ_T_SET_DOUBLE_CONST_T_REF,
"ti"_a, "xi"_a)

.def("__repr__", [](const SampledTraj<T>& x) {
std::ostringstream stream;
Expand Down
14 changes: 0 additions & 14 deletions python/src/core/trajectory/codac2_py_TrajBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,6 @@ void export_TrajBase(py::class_<S>& pyclass)
},
VIRTUAL_WRAPPER_T_DOMAIN_TRAJBASE_T_CODOMAIN_CONST)

.def("__call__", [](const S& x, double t)
{
return x(t);
},
VIRTUAL_T_TRAJBASE_T_OPERATORCALL_DOUBLE_CONST,
"t"_a)

.def("__call__", [](const S& x, const Interval& t)
{
return x(t);
},
VIRTUAL_WRAPPER_T_DOMAIN_TRAJBASE_T_OPERATORCALL_CONST_INTERVAL_REF_CONST,
"t"_a)

.def("nan_value", [](const S& x)
{
return x.nan_value();
Expand Down
1 change: 0 additions & 1 deletion src/core/contractors/codac2_CtcProj.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ namespace codac2
assert_release(*min_element(_xi.begin(),_xi.end()) >= 0);
assert_release(*max_element(_xi.begin(),_xi.end()) < size_of(c));
assert_release(size_of(c) >= (Index)_xi.size() && "cannot compute a projection of a set into a superset");
assert_release(y.is_bisectable());
assert_release(default_eps > 0.);
}

Expand Down
8 changes: 3 additions & 5 deletions src/core/domains/ellipsoid/codac2_Ellipsoid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,10 @@ namespace codac2 {
return e_res + elli_error;
}

Matrix nonlinear_mapping_base(const Matrix &G, const Matrix &J, const IntervalMatrix &J_box, const Vector& trig, const Vector& q) {

Index n = G.cols();

Matrix nonlinear_mapping_base(const Matrix &G, const Matrix &J, const IntervalMatrix &J_box, const Vector& trig, const Vector& q)
{
assert(G.is_squared() && J.is_squared() && J_box.is_squared());
assert(n == J.cols() && n == J_box.cols() && n == q.size());
assert(G.cols() == J.cols() && G.cols() == J_box.cols() && G.cols() == q.size());

auto JG = J * G; // note: reliability may be lost here!
auto G_ = G.template cast<Interval>();
Expand Down
45 changes: 41 additions & 4 deletions src/core/trajectory/codac2_SampledTraj.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <map>
#include "codac2_TrajBase.h"
#include "codac2_analytic_variables.h"
#include "codac2_template_tools.h"

namespace codac2
{
Expand All @@ -31,7 +32,7 @@ namespace codac2
auto it_t = l_t.begin(); auto it_x = l_x.begin();
while(it_t != l_t.end())
{
(*this)[*(it_t)] = *(it_x);
this->set(*it_t,*it_x);
it_t++; it_x++;
}
}
Expand Down Expand Up @@ -90,8 +91,8 @@ namespace codac2
++it;
}

(*this)[new_tdomain.lb()] = y_lb; // clean truncation
(*this)[new_tdomain.ub()] = y_ub;
this->set(new_tdomain.lb(), y_lb); // clean truncation
this->set(new_tdomain.ub(), y_ub);
}

virtual typename Wrapper<T>::Domain codomain() const
Expand Down Expand Up @@ -138,6 +139,12 @@ namespace codac2
}
}

void set(double t, const T& x)
{
assert(this->empty() || size_of(x) == this->size());
std::map<double,T>::operator[](t) = x;
}

virtual SampledTraj<T> sampled(double dt) const
{
return sampled(dt, true);
Expand All @@ -154,11 +161,41 @@ namespace codac2
{
// Appending values from the initial map:
for(const auto& [ti,xi] : *this)
straj[ti] = xi;
straj.set(ti, xi);
}

return straj;
}

template<typename T_=T>
requires std::is_same_v<T_,Vector>
SampledTraj<double> operator[](Index i) const
{
assert_release(i >= 0 && i < size());
std::map<double,double> m;
for(const auto& [t,y] : *this)
{
assert(i < y.size());
m[t] = y[i];
}

return { m };
}

template<typename T_=T>
requires std::is_same_v<T_,Vector>
SampledTraj<Vector> subvector(Index i, Index j) const
{
assert_release(i >= 0 && i <= j && j < size());
std::map<double,Vector> m;
for(const auto& [t,y] : *this)
{
assert(j < y.size());
m[t] = y.subvector(i,j);
}

return { m };
}
};

template<typename T>
Expand Down
10 changes: 5 additions & 5 deletions src/core/trajectory/codac2_TrajBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ namespace codac2
auto tdom = tdomain();
SampledTraj<T> straj;
for(double t = tdom.lb() ; t < tdom.ub() ; t+=dt)
straj[t] = (*this)(t);
straj[tdom.ub()] = (*this)(tdom.ub());
straj.set(t, (*this)(t));
straj.set(tdom.ub(), (*this)(tdom.ub()));
return straj;
}

Expand All @@ -67,20 +67,20 @@ namespace codac2

SampledTraj<T> p;
double t = tdomain().lb(), last_t = t;
p[t] = y0; t += dt;
p.set(t, y0); t += dt;
T y = y0;

while(t < tdomain().ub())
{
y += ((*this)(last_t)+(*this)(t))*dt/2.;
p[t] = y;
p.set(t, y);
last_t = t;
t += dt;
}

t = tdomain().ub();
y += ((*this)(last_t)+(*this)(t))*(t-last_t)/2.;
p[t] = y;
p.set(t, y);

return p;
}
Expand Down

0 comments on commit fd4398b

Please sign in to comment.