How to reuse the previous solution of intlinprog in case of infesiable solution in MATLAB function (Simulink)

2 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Kirthi
Kirthi il 13 Lug 2023
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 Commento
TRONG LINH VU
TRONG LINH VU il 13 Lug 2023
Modificato: TRONG LINH VU il 13 Lug 2023
Hi Kirthi,
Since you define a new avariable "x_temp" inside MATLAB fuction block, Simulink requires to define its size as well. I tried to define the size of "x_temp" but it went into the original problem.
Below is the following error that I got when applying your approach:
Simulink does not have enough information to determine output sizes for this block. If you think the errors below are inaccurate, try specifying types for the block inputs and/or sizes for the block outputs.
Component: MATLAB Function | Category: Coder error
Expected either a logical, char, int, fi, single, or double. Found an mxArray. MxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may only be used on the right-hand side of assignments and as arguments to extrinsic functions.
~Linh

Accedi per commentare.

Categorie

Scopri di più su Manual Performance Optimization in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by