kabzo
March 26, 2020, 6:47am
1
Hi,
is it possible to create discrete dynamics from python interface? The reason I would want discrete dynamics is that I would like to use variable timestep along the prediction horizon.
Thanks
FreyJo
March 27, 2020, 4:12pm
2
Hi @kabzo ,
I was implementing nonuniform discretizations for Python yesterday.
Please check out this branch:
https://github.com/FreyJo/acados/tree/nonuniform_templates/
Especially these 2 examples are doing the same thing, once with uniform, once with nonuniform discretization:
#
# Copyright 2019 Gianluca Frison, Dimitris Kouzoupis, Robin Verschueren,
# Andrea Zanelli, Niels van Duijkeren, Jonathan Frey, Tommaso Sartor,
# Branimir Novoselnik, Rien Quirynen, Rezart Qelibari, Dang Doan,
# Jonas Koenemann, Yutao Chen, Tobias Schöls, Jonas Schlagenhauf, Moritz Diehl
#
# This file is part of acados.
#
# The 2-Clause BSD License
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
This file has been truncated. show original
#
# Copyright 2019 Gianluca Frison, Dimitris Kouzoupis, Robin Verschueren,
# Andrea Zanelli, Niels van Duijkeren, Jonathan Frey, Tommaso Sartor,
# Branimir Novoselnik, Rien Quirynen, Rezart Qelibari, Dang Doan,
# Jonas Koenemann, Yutao Chen, Tobias Schöls, Jonas Schlagenhauf, Moritz Diehl
#
# This file is part of acados.
#
# The 2-Clause BSD License
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
This file has been truncated. show original
There might still be some changes on how it is to be used, e.g. I am thinking if it is more convenient to pass shooting nodes or time steps.
Feel free to try or ask if something is not clear
Cheers,
Jonathan
kabzo
March 31, 2020, 1:14am
3
Thanks @FreyJo !!! I did try the examples and it was exactly what I wanted, thanks!
I tried your latest PR with my OCP, https://github.com/acados/acados/pull/573 , and it works well!
Thanks
Juraj
1 Like
Hi Jonathan,
Thank you for the feature!
But one question. Is it possible to set nonuniform discretizations in runtime after a solver has been compiled?
Regards,
Alex
FreyJo
June 3, 2020, 11:45am
5
Hi Alex,
this is not possible at the moment.
There is at least one module which precomputes stuff by assuming the time for a shooting interval is constant.
That’s why I only added to option to set this when formulating the OCP.
Best,
Jonathan
Thank you for fast reply!
How is it difficult? If it does not touch on the core functionality, maybe I will be able to implement the feature?
If so, could you point out me for this module and give me couple hints how do it?
Regards,
Alex
What has to be done in C, is this:
double time_steps[N];
{%- for j in range(end=dims.N) %}
time_steps[{{ j }}] = {{ solver_options.time_steps[j] }};
{%- endfor %}
for (int i = 0; i < N; i++)
{
ocp_nlp_in_set(nlp_config, nlp_dims, nlp_in, i, "Ts", &time_steps[i]);
ocp_nlp_cost_model_set(nlp_config, nlp_dims, nlp_in, i, "scaling", &time_steps[i]);
}
In the Python interface it would have to be added in the setter:
else:
out = np.ascontiguousarray(np.zeros((1,)), dtype=np.float64)
out_data = cast(out.ctypes.data, POINTER(c_double))
self.shared_lib.ocp_nlp_get.argtypes = [c_void_p, c_void_p, c_char_p, c_void_p]
self.shared_lib.ocp_nlp_get(self.nlp_config, self.nlp_solver, field, out_data)
return out
# Note: this function should not be used anymore, better use cost_set, constraints_set
def set(self, stage_, field_, value_):
cost_fields = ['y_ref', 'yref']
constraints_fields = ['lbx', 'ubx', 'lbu', 'ubu']
out_fields = ['x', 'u', 'pi', 'lam', 't']
# cast value_ to avoid conversion issues
value_ = value_.astype(float)
field = field_
field = field.encode('utf-8')
In Matlab/Octave, this is the setter function:
#include <stdio.h>
#include <string.h>
// acados
#include "acados_c/ocp_nlp_interface.h"
#include "acados_c/sim_interface.h"
// mex
#include "mex.h"
#include "mex_macros.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
long long *ptr;
int acados_size;
mxArray *mex_field;
char fun_name[20] = "ocp_set";
char buffer [500]; // for error messages
/* RHS */
int min_nrhs = 5;
For the GNSF-IRK integrator it is assumed to be constant, I am not sure about other modules.
Since this might cause issues, I am still not sure if we want to have this in the master.
But I hope it is helpful for you.
Cheers
1 Like
Hi @FreyJo , I am trying to run the examples discussed here and I am not able to find them. Please, could you say me where I can get them?
Thanks in advance
josu