Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to improve time performance #278

Open
jnew123 opened this issue Mar 6, 2020 · 0 comments
Open

how to improve time performance #278

jnew123 opened this issue Mar 6, 2020 · 0 comments

Comments

@jnew123
Copy link

jnew123 commented Mar 6, 2020

Hi, I am learning ACADO. I wrote a simple code to simulate a thermostat control. The problem si basically minimize the cost of running a heater subject to two reference temperatures (xl and xu), i.e., a lower and upper desired setting. I have a cost of utilizing the heater, inequality constraints on the control, external temperature, initial conditions on the temperature and control and a simple model for the dynamics of heat transfer. The time horizon is 24.

I'll appreciate if you can give some tips how and where to improve the performance of the code

This is the code:

#include <acado/utils/acado_utils.hpp>
#include <acado/matrix_vector/matrix_vector.hpp>
#include <acado_toolkit.hpp>
#include <acado_optimal_control.hpp>
#include <acado_gnuplot.hpp>

int main( )
{

USING_NAMESPACE_ACADO

DifferentialState T("",24,1);
Control u("",24,1);
DifferentialEquation f;

const double t_start =  0.0;
const double t_end   = 23.0;

const double k = 0.05;
const double b = 5.0;

DVector xl(24);
	xl(0) = 25.0;
	xl(1) = 55.0;
	xl(2) = 55.0;
	xl(3) = 55.0;
	xl(4) = 55.0;
	xl(5) = 55.0;
	xl(6) = 55.0;
	xl(7) = 68.0;
	xl(8) = 68.0;
	xl(9) = 68.0;
	xl(10) =68.0;
	xl(11) =55.0;
	xl(12) =55.0;
	xl(13) =68.0;
	xl(14) =68.0;
	xl(15) =68.0;
	xl(16) =68.0;
	xl(17) =55.0;
	xl(18) =55.0;
	xl(19) =55.0;
	xl(20) =55.0;
	xl(21) =55.0;
	xl(22) =55.0;
	xl(23) =55.0;

DVector xu(24);
	xu(0) = 75.0;
	xu(1) = 75.0;
	xu(2) = 75.0;
	xu(3) = 75.0;
	xu(4) = 75.0;
	xu(5) = 75.0;
	xu(6) = 75.0;
	xu(7) = 70.0;
	xu(8) = 70.0;
	xu(9) = 70.0;
	xu(10) =70.0;
	xu(11) =75.0;
	xu(12) =75.0;
	xu(13) =70.0;
	xu(14) =70.0;
	xu(15) =70.0;
	xu(16) =70.0;
	xu(17) =75.0;
	xu(18) =75.0;
	xu(19) =75.0;
	xu(20) =75.0;
	xu(21) =75.0;
	xu(22) =75.0;
	xu(23) =75.0;

DVector T_ext(24);
	T_ext(0) = 20.0;
	T_ext(1) = 35.0;
	T_ext(2) = 40.0;
	T_ext(3) = 47.0;
	T_ext(4) = 46.0;
	T_ext(5) = 48.0;
	T_ext(6) = 55.0;
	T_ext(7) = 60.0;
	T_ext(8) = 60.0;
	T_ext(9) = 63.0;
	T_ext(10) = 60.0;
	T_ext(11) = 58.0;
	T_ext(12) = 54.0;
	T_ext(13) = 52.0;
	T_ext(14) = 52.0;
	T_ext(15) = 50.0;
	T_ext(16) = 48.0;
	T_ext(17) = 44.0;
	T_ext(18) = 41.0;
	T_ext(19) = 38.0;
	T_ext(20) = 34.0;
	T_ext(21) = 35.0;
	T_ext(22) = 33.0;
	T_ext(23) = 40.0;

DVector T_init(24);
	T_init(0) = 30.0;
	T_init(1) = 30.0;
	T_init(2) = 30.0;
	T_init(3) = 30.0;
	T_init(4) = 30.0;
	T_init(5) = 30.0;
	T_init(6) = 30.0;
	T_init(7) = 30.0;
	T_init(8) = 30.0;
	T_init(9) = 30.0;
	T_init(10) =30.0;
	T_init(11) =30.0;
	T_init(12) =30.0;
	T_init(13) =30.0;
	T_init(14) =30.0;
	T_init(15) =30.0;
	T_init(16) =30.0;
	T_init(17) =30.0;
	T_init(18) =30.0;
	T_init(19) =30.0;
	T_init(20) =30.0;
	T_init(21) =30.0;
	T_init(22) =30.0;
	T_init(23) =30.0;

DVector ul(24);
	ul(0) = 0.0;
	ul(1) = 0.0;
	ul(2) = 0.0;
	ul(3) = 0.0;
	ul(4) = 0.0;
	ul(5) = 0.0;
	ul(6) = 0.0;
	ul(7) = 0.0;
	ul(8) = 0.0;
	ul(9) = 0.0;
	ul(10) =0.0;
	ul(11) =0.0;
	ul(12) =0.0;
	ul(13) =0.0;
	ul(14) =0.0;
	ul(15) =0.0;
	ul(16) =0.0;
	ul(17) =0.0;
	ul(18) =0.0;
	ul(19) =0.0;
	ul(20) =0.0;
	ul(21) =0.0;
	ul(22) =0.0;
	ul(23) =0.0;

DVector up(24);
	up(0) = 1.0;
	up(1) = 1.0;
	up(2) = 1.0;
	up(3) = 1.0;
	up(4) = 1.0;
	up(5) = 1.0;
	up(6) = 1.0;
	up(7) = 1.0;
	up(8) = 1.0;
	up(9) = 1.0;
	up(10) =1.0;
	up(11) =1.0;
	up(12) =1.0;
	up(13) =1.0;
	up(14) =1.0;
	up(15) =1.0;
	up(16) =1.0;
	up(17) =1.0;
	up(18) =1.0;
	up(19) =1.0;
	up(20) =1.0;
	up(21) =1.0;
	up(22) =1.0;
	up(23) =1.0;

DVector u_init(24);
	u_init(0) = 0.0;
	u_init(1) = 0.0;
	u_init(2) = 0.0;
	u_init(3) = 0.0;
	u_init(4) = 0.0;
	u_init(5) = 0.0;
	u_init(6) = 0.0;
	u_init(7) = 0.0;
	u_init(8) = 0.0;
	u_init(9) = 0.0;
	u_init(10) =0.0;
	u_init(11) =0.0;
	u_init(12) =0.0;
	u_init(13) =0.0;
	u_init(14) =0.0;
	u_init(15) =0.0;
	u_init(16) =0.0;
	u_init(17) =0.0;
	u_init(18) =0.0;
	u_init(19) =0.0;
	u_init(20) =0.0;
	u_init(21) =0.0;
	u_init(22) =0.0;
	u_init(23) =0.0;

DMatrix cost(1,24);
	cost(0,0) = 0.05;
	cost(0,1) = 0.05;
	cost(0,2) = 0.05;
	cost(0,3) = 0.05;
	cost(0,4) = 0.05;
	cost(0,5) = 0.05;
	cost(0,6) = 0.05;
	cost(0,7) = 200.0;
	cost(0,8) = 200.0;
	cost(0,9) = 200.0;
	cost(0,10) = 200.0;
	cost(0,11) = 200.0;
	cost(0,12) = 200.0;
	cost(0,13) = 200.0;
	cost(0,14) = 200.0;
	cost(0,15) = 200.0;
	cost(0,16) = 200.0;
	cost(0,17) = 200.0;
	cost(0,18) = 200.0;
	cost(0,19) = 200.0;
	cost(0,20) = 200.0;
	cost(0,21) = 0.05;
	cost(0,22) = 0.05;
	cost(0,23) = 0.05;


f << dot(T) == k*(T_ext-T) + b*u;

OCP ocp( t_start, t_end, 24 );

ocp.minimizeLagrangeTerm( cost*u );
ocp.subjectTo( f );

ocp.subjectTo( AT_START, T ==  T_init );
ocp.subjectTo( AT_START, u ==  u_init );
ocp.subjectTo( xl <= T <= xu );
ocp.subjectTo( ul <= u <= up );

GnuplotWindow window;
window.addSubplot( T,"DifferentialState T" );
window.addSubplot( u,"DifferentialState u" );

OptimizationAlgorithm algorithm(ocp);

algorithm << window;
algorithm.solve();

return 0;
}

The same code in scipy optimize works great but in ACADO is taking an impressively huge amount to time to finish. So definitely the way I am coding it must be bogus (although it compiles and seems to be optimizing).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant