最適化問題として最適な入力系列を計算させる場合、最適化変数が離散値になりますので勾配を使う fmincon では解けない場合があります。
また、混合整数線形計画法 (MILP)を取り扱える intlinprog も目的関数が非線形の場合には使えません。
整数制約の非線形最適化問題を解く一番簡単な方法はGlobal Optimization Toolboxの遺伝的アルゴリズム ga を使うことです。
簡単な例を下記に示します。
この例では x(tf)=xf の制約自体を目的関数に入れています。
少しでもご参考になれば幸いです。
%%バンバン制御の入力信号の最適化
% 乱数のシードを定義(再現性のため)
rng(4,'twister')
% 目標値
xf = [20; 0];
amax = 1;
% 目的関数
fun = @(x) (costFunc(x,xf));
% 初期値
x0 = zeros(100,1);
% (0,1)の範囲に限定
lb = zeros(100,1);
ub = ones(100,1);
% 遺伝的アルゴリズムを使って整数制約の非線形最適化問題を解く
opt = optimoptions('ga','PlotFcn',@gaplotbestf,...
'MaxGenerations',50,'MaxStallGenerations',50);
[xga,fval,exitflag,output] = ga(@(x)costFunc(x,xf,amax),100,[],[],[],[],lb,ub,[], ...
1:100,opt);
% 時系列の再計算と可視化
[~,xlog] = costFunc(xga,xf,amax);
figure, subplot(211), plot((2*xga-1)*amax);
title('入力信号 u');
subplot(212), plot(xlog);
hold on; plot(xf(1)*ones(100,1));
title('状態変数 x');
legend('位置','速度','位置の目標値');
costFunc.m:
function [J,x_log] = costFunc(u,xf,amax)
J = 0;
tf = 1;
A = [1 1; 0 1];
B = [0; 1];
x = [0; 0];
x_log = zeros(numel(u),2);
while tf <= 100
x = A*x + B*(round(u(tf))*2-1)*amax;
x_log(tf,:) = x;
tf = tf + 1;
J = J + abs(x(1)-xf(1));
end
end
実行結果:




