每次换电脑都要安装一遍,这次把过程记录下来
首先,为了将GPops2安装到matlab,需要先下载GPops2软件包。可以从GPops2官方网站或其他软件下载网站获取此软件包。下载地址如下:
下载完成后,将软件包解压缩到需要安装它的计算机上。
然后,matlab打开到指定的工作区,并打开这个m文件
然后运行,就会安装成功了
-------------------------------2025.05.20更新-------------------------------------
我这种方法得到的GPOPS2是v1.0,相对来说比较落后,某些算例可能不能正常运行,但是对于老版本的算例应该是可以的。
比如以下这个测试代码就可以运行,参考链接:matlab(r2023a)添加工具箱TooLbox的方法-以GPOPS为例_matlab_lonely-hermit-GitCode 开源社区
clc;clear;close all;
tic;
% 设置时间
t0 = 0;
tf = 2;
% 设置状态量初值
x10 = -2;
x20 = 1;
% 设置状态量边界条件
x1Min = -5;
x1Max = 5;
x2Min = -5;
x2Max = 5;
% 设置控制量边界条件
uMin = -1.5;
uMax = 1.5;
bounds.phase.initialtime.lower = t0;
bounds.phase.initialtime.upper = t0;
bounds.phase.finaltime.lower = tf;
bounds.phase.finaltime.upper = tf;
bounds.phase.initialstate.lower = [x10 x20];
bounds.phase.initialstate.upper = [x10 x20];
bounds.phase.state.lower = [x1Min x2Min];
bounds.phase.state.upper = [x1Max x2Max];
bounds.phase.finalstate.lower = [0 0];
bounds.phase.finalstate.upper = [0 0];
bounds.phase.control.lower = uMin;
bounds.phase.control.upper = uMax;
bounds.phase.integral.lower = 0;
bounds.phase.integral.upper = 10000;
guess.phase.time = [t0; tf];
guess.phase.state = [[x10; 0],[x20; 0]];
guess.phase.control = [1; uMin];
guess.phase.integral = 100;
setup.name = 'Vehicle-Stopping-OCP';
setup.functions.continuous = @vsopcContinuous;
setup.functions.endpoint = @vsopcEndpoint;
setup.bounds = bounds;
setup.guess = guess;
setup.nlp.solver = 'snopt';
setup.derivatives.supplier = 'sparseCD';
setup.derivatives.derivativelevel = 'second';
setup.mesh.method = 'hp1';
setup.mesh.tolerance = 1e-6;
setup.mesh.maxiteration = 45;
setup.mesh.colpointsmax = 4;
setup.mesh.colpointsmin = 10;
setup.mesh.phase.fraction = 0.1*ones(1,10);
setup.mesh.phase.colpoints = 4*ones(1,10);
setup.method = 'RPMintegration';
output = gpops2(setup);
solution = output.result.solution;
toc;
%% 函数模块部分
% ----------------------------------------------------------------------- %
% ------------------------- BEGIN: vsopcContinuous.m -------------------- %
% ----------------------------------------------------------------------- %
function phaseout = vsopcContinuous(input)
t = input.phase.time;
% x1 = input.phase.state(:,1);
x2 = input.phase.state(:,2);
u = input.phase.control(:,1);
dx1 = x2;
dx2 = u;
phaseout.dynamics = [dx1, dx2];
phaseout.integrand = 0.5*u.^2;
end
% ----------------------------------------------------------------------- %
% -------------------------- END: vsopcContinuous.m --------------------- %
% ----------------------------------------------------------------------- %
% ----------------------------------------------------------------------- %
% -------------------------- BEGIN: vsopcEndpoint.m --------------------- %
% ----------------------------------------------------------------------- %
function output = vsopcEndpoint(input)
J = input.phase.integral;
output.objective = J;
end
% ----------------------------------------------------------------------- %
% --------------------------- END: vsopcEndpoint.m ---------------------- %
% ----------------------------------------------------------------------- %
我的运行结果是:
但是针对这样的一些程序:
clc;clear;close all;
tic;
% 设置时间
t0 = 0;
tf = 2;
% 设置状态量初值
x10 = -2;
x20 = 1;
% 设置状态量边界条件
x1Min = -5;
x1Max = 5;
x2Min = -5;
x2Max = 5;
% 设置控制量边界条件
uMin = -1.5;
uMax = 1.5;
bounds.phase.initialtime.lower = t0;
bounds.phase.initialtime.upper = t0;
bounds.phase.finaltime.lower = tf;
bounds.phase.finaltime.upper = tf;
bounds.phase.initialstate.lower = [x10 x20];
bounds.phase.initialstate.upper = [x10 x20];
bounds.phase.state.lower = [x1Min x2Min];
bounds.phase.state.upper = [x1Max x2Max];
bounds.phase.finalstate.lower = [0 0];
bounds.phase.finalstate.upper = [0 0];
bounds.phase.control.lower = uMin;
bounds.phase.control.upper = uMax;
bounds.phase.integral.lower = 0;
bounds.phase.integral.upper = 10000;
guess.phase.time = [t0; tf];
guess.phase.state = [[x10; 0],[x20; 0]];
guess.phase.control = [1; uMin];
guess.phase.integral = 100;
setup.name = 'Vehicle-Stopping-OCP';
setup.functions.continuous = @vsopcContinuous;
setup.functions.endpoint = @vsopcEndpoint;
setup.bounds = bounds;
setup.guess = guess;
setup.nlp.solver = 'snopt';
setup.derivatives.supplier = 'sparseCD';
setup.derivatives.derivativelevel = 'second';
setup.mesh.method = 'hp1';
setup.mesh.tolerance = 1e-6;
setup.mesh.maxiteration = 45;
setup.mesh.colpointsmax = 4;
setup.mesh.colpointsmin = 10;
setup.mesh.phase.fraction = 0.1*ones(1,10);
setup.mesh.phase.colpoints = 4*ones(1,10);
setup.method = 'RPMintegration';
output = gpops2(setup);
solution = output.result.solution;
toc;
%% 函数模块部分
% ----------------------------------------------------------------------- %
% ------------------------- BEGIN: vsopcContinuous.m -------------------- %
% ----------------------------------------------------------------------- %
function phaseout = vsopcContinuous(input)
t = input.phase.time;
% x1 = input.phase.state(:,1);
x2 = input.phase.state(:,2);
u = input.phas
我就显示报错,报错信息如下:
我觉得应该是程序太新了,低版本的求解器不行。
期待有大佬解决问题。