导航菜单
首页 » 问答 » 正文

(工欲善其事 必先利其器)MATLAB+yalmip+gurobi+MPT工具箱

经过不懈地努力,公众号和知乎关注量终于破200!更一期软件安装,嘿嘿。

一、

是矩阵实验室( )的简称,是美国 公司出品的商业数学软件,用于算法开发、数据可视化、数据 分析以及数值计算的高级技术计算语言和交互式环境,主要包括 和 两大部分。

有很多安装途径,在此就不一一赘述了。

二、

真的是一个神器,作为一个工具箱,很好的节省了学习优化的时间成本,就不用去花费时间学习各种求解器语言。

下载链接:..io/

论坛链接:/forum/#!forum/

(这里不得不吹一下的论坛,遥想当年学习SOCP的对偶编程二阶锥对偶(SOC-dual)时,在论坛得到了不少的帮助,而且原作者回复很快)

装完以后,一定要记得在中设置路径!

三、

是由美国公司开发的新一代大规模数学规划优化器,在 Tree for 网站举行的第三方优化器评估中,展示出更快的优化速度和精度,成为优化器领域的新翘楚。

官网链接:/

用edu邮箱注册,可申请学术,很方便。

软件下载://

注册界面://end-user---/

安装完毕后,先申请,获得码。

win+r+cmd:输入申请的许可证,运行命令,申请成功。

设置在中的路径。

在中运行命令,检验是否安装成功

为了让查找到的位置,必须设置一个环境变量。

在系统属性界面内选择高级,然后点击环境变量。

如何设置网上有很多教程,这里也不一一赘述。

四、MPT

最后介绍一个新的工具包,主要也是在安装的时候出现了一些问题,所以才会特别介绍一下。

The Multi- (or MPT for short) is an open , -based for , and model .

下载链接:/

安装流程如下:运行代码自动安装

%% installation of tbxmanager with all submodels required for MPT
%
clc;
disp('----------------------------------------------');
disp('Installation of MPT using the Toolbox manager.');
disp('----------------------------------------------');
disp(' ');
fprintf(['Choose the installation directory where to install the Toolbox manager.\n',...
      'A new folder "tbxmanager" is going to be created in the specified location.\n',...
      'If you do not specify the folder, the Toolbox manager will be installed in the current directory.\n']);
% get the installation folder
default_dir = pwd;
c = uigetdir(pwd);
if isequal(c,0);
    fprintf(['No directory has been provided.\n',... 
        'Installing the toolbox manager in the current directory "%s"?\n'],default_dir);
    c = default_dir;
end
 
% create a new directory in that folder
d = [c,filesep,'tbxmanager'];
if isequal(exist(d,'dir'),7)
    error('The installation directory "%s" already exists.\nPlease, remove or rename the folder or change the installation path.',d);
end
disp('Creating the directory "tbxmanager".');
out = mkdir(d);
if ~out
    error(['An error appear when trying to create the folder "%s".\n',...
          'Please, install the Toolbox manager manually.'],c); 
end
% enter that directory
cd(d);
% remove MPT2 or YALMIP
disp(' ');
disp('Removing toolboxes that may conflict with MPT from the Matlab path.');
rmpath(genpath(fileparts(which('mpt_init'))));
rmpath(genpath(fileparts(which('yalmipdemo'))));
% download the tbxmanager
disp(' ');
disp('Downloading the Toolbox manager from the internet.');
[f, c] = urlwrite('http://www.tbxmanager.com/tbxmanager.m', 'tbxmanager.m');
rehash;
if isequal(c,0)
    error('Could not download the Toolbox manager from the internet. The installation cannot continue.');
end
% install all required modules
tbxmanager install mpt mptdoc cddmex fourier glpkmex hysdel lcp sedumi yalmip 
% create the initialization file to set the path 
disp(' ');
disp('Creating the initialization file "startup.m".');
p = which('startup.m');
if isempty(p)
    p = [d,filesep,'startup.m'];
end
fid = fopen(p,'a');
if isequal(fid,-1)
    error(['Could not modify the initialization file "startup.m".',...
           'Edit this file in the folder "%s" manually and insert there the line:  tbxmanager restorepath.'],p);
end
fprintf(fid,'tbxmanager restorepath\n');
fclose(fid);
disp('File has been created.');
% get back to the original directory
cd(default_dir);
% add path to tbxmanager
disp(' ');
disp('Adding path to Matlab.');
addpath(d);
% save path for future
disp(' ');
disp('Saving path for future sessions.');
status = savepath;
if status
    fprintf('Could not save the path to a default location,\nplease provide a location where you want to save the path.');
    cn = uigetdir(pwd);
    if isequal(cn,0)
        disp(' ');
        fprintf('No directory specified, saving the path to the current directory "%s".\n\n',default_dir);
        cn = default_dir;
    end
    sn = savepath([cn,filesep,'pathdef.m']);
    if sn
        error(['Could not save the path automatically.\n',...
            'Please, open the "Set Path" button in the Matlab menu and save the path manually to some location.']);
    end
end
disp(' ');
disp('Installation finished.');
disp('Next time you start Matlab the toolboxes will be automatically initialized.');
% initialize MPT
disp(' ');
disp('Initializing the MPT.')
mpt_init;

这里要注意一下,代码会自动卸载电脑中已有的和MPT,然后重新安装需要的工具包

tbxmanager uninstall mpt mptdoc cddmex fourier glpkmex hysdel lcp yalmip sedumi espresso

安装完成后,代码会将路径设置成功,无需手动添加。

高能环节:

我是先安装的后安装的MPT3,然后运行一直报错,如图

苦苦修改数日,终于发现解决办法如下:

将冲突的文件删除即可,问题解决!(困扰数日,可悲)

附上新结果,快乐一下:

close all
% load data provided by S. Riverso , University of Pavia
load dataSim
% construct discrete-time LTI model
% ( The prediction model contains measurable disturbance deltaPload which cannot
% be handled directly with the current modeling approach. Alternatively, we
% consider augmented model where the measurable disturbance is a part of
% dynamical matrix and formulate MPC problem not to penalize these additional states.)  
model = LTISystem('A',[A,L;zeros(4,16),eye(4)],'B',[B; zeros(4)],'C',[C,zeros(16,4)],'D',D,'Ts',Ts);
% simulation model x+ = A*x + B*u + L*deltaPload
simulation_model = LTISystem('A',A,'B',[B, L],'C',C,'D',[D, zeros(16,4)],'Ts',Ts);
%% formulate MPC problem 
ctrl = MPCController(model);
% set the prediction horizon
ctrl.N = 15;
% input constraints
ctrl.model.u.min = [-0.5; -0.65; -0.65; -0.55];
ctrl.model.u.max = [0.5; 0.65; 0.65; 0.55];
% state constraints
ctrl.model.x.min = [-0.1; -Inf(19,1)];
ctrl.model.x.max = [0.1; Inf(19,1)];
 
% add reference signals
ctrl.model.x.with('reference');
ctrl.model.u.with('reference');
% zero terminal set
ctrl.model.x.with('terminalSet');
% ctrl.model.x.with('terminalPenalty');
% ctrl.model.x.terminalPenalty = QuadFunction(eye(16));
% add quadratic penalty on the outputs
ctrl.model.u.penalty = QuadFunction(R);
% penalty must be positive definite
ctrl.model.x.penalty = QuadFunction(blkdiag(Q,zeros(4)));
%% Simulate the closed loop
x0 = zeros(16,1);
X = [];
U = [];
Y = [];
N_sim = length(deltaPref.signals.values);
simulation_model.initialize(x0);
eta = 0;
phi = 0;
x = zeros(16,1);
u = zeros(4,1);
for k=1:N_sim
    % time varying reference for inputs
    ctrl.model.u.reference = uO(k,:)';
    % time varying reference for states
    dPl = deltaPload.signals.values(k,:)';
    ctrl.model.x.reference = [xO(k,:)'; dPl];
    ctrl.model.x.terminalSet = Polyhedron('Ae',eye(20),'be',ctrl.model.x.reference);    
    % evaluate the performace criteria before state update
    eta = eta + (x(1:16)-ctrl.model.x.reference(1:16))'*Q*(x(1:16)-ctrl.model.x.reference(1:16)) + (u-dPl)'*R*(u-dPl);
    Ptie12 = 4*(x(1)-x(5));
    Ptie23 = 2*(x(5)-x(9));
    Ptie34 = 2*(x(9)-x(13));
    phi = phi + sum(abs(Ptie12)*Ts + abs(Ptie23)*Ts + abs(Ptie34)*Ts);
    
    % evaluate MPC
    u = ctrl.evaluate([x;dPl]);
    
    % load precomputed inputs from stored data
    %u = deltaPref.signals.values(k,:)';
    
    % update the process
    [x,y] = simulation_model.update([u;dPl]);
    
    % store the signals
    U = [U, u(1:4)];
    X = [X, x(1:16)];
    Y = [Y, y];
    fprintf('Simulating... %d/%d\n',k,N_sim);
end
% evaluate the performance criteria
eta = eta/N_sim
phi = phi/N_sim
% plot the state trajectories
figure
plot(1:N_sim,X,'LineWidth',2)
xlabel('Simulation steps')
title('States')
% plot the control inputs
figure
plot(1:N_sim,U,'LineWidth',2)
xlabel('Simulation steps')
title('Control inputs')

评论(0)

二维码