How to reuse the previous solution of intlinprog in case of infesiable solution in MATLAB function (Simulink)
2 ビュー (過去 30 日間)
古いコメントを表示
Dear MATLAB staffs,
I'm currently developing an algorithm that incorporates the use of the intlinprog function within a MATLAB function block in Simulink. During certain timesteps, it is possible that there will be no feasible solution available. In such cases, I intend to utilize the previous optimal solution and set it as the output. The code snippet is provided below as a reference:
function x = algorimth(input1, input2, ... )
persistent x_old
if isempty(x_old)
x_old = zeros(1650, 1);
end
% preallocate size of x
x = zeros(1650, 1);
x = intlinprog(f, intcont, A, b, Aeq, beq, lbt, ubt);
if isempty(x)
disp("no solution");
x = x_old;
else
% update x_old
x_old = x;
end
end
I expect that if no solution is found, intlinprog should return x as an empty array ([]). In that case, I intend for x to be set equal to x_old, and I expect Simulink to continue running. However, Simulink displays an error and stops the simulation.
An error occurred while running the simulation and the simulation was terminated
Caused by:
Size mismatch for MATLAB expression '<output of intlinprog>'. Expected = 1650x1 Actual = 0x0
Component: Simulink | Category: Block error
Is there any way to work around this issue and keep the simulation running as expected?
Have a good day!
Best regards,
Linh
0 件のコメント
回答 (1 件)
Kirthi
2023 年 7 月 13 日
Hello Linh,
Please try assigning the output of 'intlinprong' to a temporary variable 'x_temp' and then, if 'x_temp' is empty, we can set 'x' to 'x_old'. Please refer to below code snippet for the same
% preallocate size of x
x = zeros(1650, 1);
x_temp = intlinprog(f, intcont, A, b, Aeq, beq, lbt, ubt);
if isempty(x_temp)
disp("no solution");
x = x_old;
else
% update x_old
x_old = x_temp;
x = x_temp;
end
Simulink being a dynamic tool expects consistent sizes variables, it simply expects 'x' to have size 1650x1 but actual is 0x0, so on the mismtach it raises an error.
Te above code modification should resolve the mismatch error and allow simulation to continue running without errors.
Hope this helps !
1 件のコメント
参考
カテゴリ
Help Center および File Exchange で Manual Performance Optimization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!