从Simulink呼叫fmincon [英] Calling fmincon from Simulink

查看:182
本文介绍了从Simulink呼叫fmincon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Simulink-Matlab框架中实现特定类型的模型预测控制.为此,我的计划是让Simulink中的动态模型调用一个外部Matlab S函数,该函数又运行一个优化,该优化调用一个不同的Simulink文件.因此,程序流程如下: Simulink-> Matlab(fminconquadprog)-> Simulink.

I am trying to implement a particular type of model predictive control in the Simulink-Matlab framework. To do so, my plan was to have the dynamic model in Simulink call an external Matlab S-function which in turns runs an optimization that calls a different Simulink file. Hence, the program flow would be as follows: Simulink -> Matlab (fmincon or quadprog) -> Simulink.

如您所见,Matlab S函数将调用fminconquadprog,但是我想对我的特定控件类型使用fmincon.请忽略目前为止与计算效率有关的任何问题.

As you can see, the Matlab S-function would call either fmincon or quadprog, but I would like to use fmincon for my particular control type. Please, ignore any issues related to computational efficiency so far.

我尝试了这种方法,但是有两个非常明显的问题: *首先,为了编译代码而没有错误(基本上是获取.mex文件,我不需要用C编程),我添加了命令

I tried this approach, but there are two very clear problems: * Firstly, in order to compile the code without errors (basically obtaining a .mex file, I do not need to program in C yet), I added the command

coder.extrinsic('fmincon');

coder.extrinsic('fmincon');

这是必需的,因为否则Simulink无法编译mex文件.但是,如果这样做,则会出现以下错误:

This was required because otherwise Simulink is unable to compile the mex file. However, if you do this, then you get the following error:

Function handles cannot be passed to extrinsic functions.

我试图将调用Simulink的成本函数更改为另一个更简单的成本函数(x.^2),但仍然出现错误.

I tried to change my cost function calling Simulink to another, simpler cost function (x.^2), but I still get the error.

在寻找问题的解决方案时,我在Mathworks博客上发现了相同的问题(即如何从Simulink中的Matlab函数调用fmincon),但没有答案(

Looking for a solution to the problem, I found the same question (i.e. how to call fmincon from a Matlab function within Simulink) on the Mathworks blog, but with no answer (https://uk.mathworks.com/matlabcentral/answers/65202-optimization-calling-fmincon-in-simulink-embedded-block).

有人可以帮我吗?预先感谢!

Could anyone give me a hand? Thanks in advance!

推荐答案

我通常不用编写fmincon函数上的coder.extrinsic,而是为需要作为.m文件解决的优化问题编写包装器. Matlab函数(opt_problem),并在Simulink Matlab函数中声明coder.extrinsic('opt_problem').我给你举一个简单的例子:

Instead of using coder.extrinsic on the fmincon function, I usually write a wrapper for the optimization problem that I have to solve as a .m file function for Matlab (namely opt_problem) and declare coder.extrinsic('opt_problem') in the simulink Matlab function. I'll give you a simple example:

考虑这个Simulink模型",其中在每个集成步骤中,我想解决一些生成数据的线性回归问题.优化问题的形式为:

Consider this Simulink "model", in which at each integration step I want to solve a linear regression problem on some generated data. The optimization problem is something in the form:

minimize (y - m x - q)²
subject to  0 ≤ m ≤ 1
            0 ≤ q ≤ 1

该方案非常简单,押注回归者调用fmincon:

The scheme is really simple, bet the regressor calls fmincon:

让我们看看回归器内部

function [m, q] = regressor(xs, ys, mic, qic)
  coder.extrinsic('opt_problem'); % <- Informing the Coder
  m = 0;
  q = 0;
  [m, q] = opt_problem(xs, ys, mic, qic); % <- Optimal problem wrapper call
end

此函数仅是外部函数opt_problem的包装.让我们看看(它内部有两个功能):

this function is only a wrapper for an external function opt_problem. Let'see it (it has two functions inside):

function [m, q] = opt_problem(xs, ys, mic, qic)
  fmincon_target = @(mq)(target(mq, xs, ys));
  mq = fmincon(fmincon_target, [mic; qic], [], [], [], [], [0; 0], [1; 1]);
  m = mq(1);
  q = mq(2);
end

function r = target(mq, xs, ys)
  r = norm(ys - xs.*mq(1) - mq(2));
end

仅此而已.如您在图片中所看到的,该方案正在运行,解决方案是m, q参数(在两个显示屏中),它们在遵守约束条件(m = 1.2 → m_opt = 1)的情况下最小化了目标函数.

and that's all. As you can see in the picture, the scheme runs and the solution are the m, q parameters (in the two displays) that minimize the target function while respecting the constraints (m = 1.2 → m_opt = 1).

这篇关于从Simulink呼叫fmincon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆