How can I dynamically add optimization variables for me problem in a loop?

Hey :)
I have defined an optimization problem as follows:
prob = optimproblem;
Now I wanna add optimization variables in a loop, such as
for i = 1:10
x = optimvar('Test', 'LowerBound', 0);
end
where within the first iteration, the name of the variable should be x_1, in the second iteration it should be x_2 and so on.
I tried something like this, but it didn`t work:
['x_' int2str(i)] = optimvar(ans, 'LowerBound',0);
My next approach was defining a struct for the variables like the following:
for i = 1:10
ans = mat2str(this_is_a_test(i,:));
ans = regexprep(ans, '\[(.*)\]', '$1');
ans = strrep(ans,' ','');
ans = strcat('Test_', ans);
var = ans;
variables(.var) = optimvar(ans, 'LowerBound',0);
end
After obtaining the struct with my optimization variables, I cannot "transfer" it into prob.Variables
I would be extremely glad if someone could help me with this :)
If you need more information, don't hesitate to ask.
Best regards,
Mike

8 comentarios

Wouldn't this be much easier if you simply define a vector?. The documentation contains this:
"x = optimvar(name,n) creates an n-by-1 vector of optimization variables."
Hey, thanks for the quick response!
I was thinking of it too, but my problem was, that I couldn't think of a way to create the name vector.
I created a vector (e.g. 3x10 char) and I got this error:
Error using optimvar
Name must be a character vector or a scalar string.
Have you got any idea how to solve this issue?
Thanks in advance! :)
"I created a vector (e.g. 3x10 char) "
A 3x10 char is not a vector.
"Have you got any idea how to solve this issue?"
As the error message states, the name must be a character vector or a string scalar.
So there is no solution for the approach with "x = optimvar(name,n)"? Because for my problem it is crucial that I name the variables in a specific way. Like x_1, x_2 and example.
For anyone interested in this topic, I found a solution.
var_names = [];
for i = 1:10
ans = mat2str(this_is_a_test(i,:));
ans = regexprep(ans, '\[(.*)\]', '$1');
ans = strrep(ans,' ','');
ans = strcat('Test_', ans);
var_names = [var_names; ans];
end
var_names = string(var_names);
x = optimvar('x', var_names, 'Type', 'integer');
Thanks a lot!
@Mike: what class and content does THIS_IS_A_TEST have?
It's a n*m double. It's content is only consisting of ones and twos.
"It's a n*m double. It's content is only consisting of ones and twos."
this_is_a_test = randi(1:2,10,3)
this_is_a_test = 10×3
2 1 1 1 2 2 2 1 1 1 2 1 2 2 2 1 1 1 2 1 2 2 1 1 1 2 1 1 2 2
var_names = [];
for i = 1:10
ans = mat2str(this_is_a_test(i,:));
ans = regexprep(ans, '\[(.*)\]', '$1');
ans = strrep(ans,' ','');
ans = strcat('Test_', ans);
var_names = [var_names; ans];
end
V0 = string(var_names)
V0 = 10×1 string array
"Test_211" "Test_122" "Test_211" "Test_121" "Test_222" "Test_111" "Test_212" "Test_211" "Test_121" "Test_122"
Here is a simpler approach to generate those names:
V1 = "Test_"+join(string(this_is_a_test),"")
V1 = 10×1 string array
"Test_211" "Test_122" "Test_211" "Test_121" "Test_222" "Test_111" "Test_212" "Test_211" "Test_121" "Test_122"

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Preguntada:

el 22 de Mzo. de 2023

Comentada:

el 22 de Mzo. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by