Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Using the output of a ready function as a constraint

1 visualización (últimos 30 días)
Omar Morsy
Omar Morsy el 7 de Dic. de 2021
Cerrada: Matt J el 7 de Dic. de 2021
My objective finction is L*A (dot product).
I want to optimze my variables which are in matrix (A). But the elemnts of the output of optimizing A should be one value from a set of specific values.
For example: I want to set the output values of A (after the optimization) to be one of the following values { 1:37}.
I have a ready function (pfile) which I want to use in the optimization problem. That function works as follow:
[w, a, x] = ASU(A)
I get 3 outputs from that function and I would like to minimize my objective function subjected to the output (a) and (x) =0.
how can I use the output of the given ASU function as contraints to the optimization problem?
The problem is linear and I am using solver-based.
L is 345x1
A is 1x345
w,a and x are 1x1
ASU accepts only a 1x345 matrix.
If I multiple L*A by a constant (density) it will give me w which is the objective function that I want to minimize
Thanks

Respuestas (1)

Alan Weiss
Alan Weiss el 7 de Dic. de 2021
I think that the easiest approach is problem-based.
  1. Create optimization variables: the vector A in your case
  2. Create the objective function, dot(A,L)
  3. Create the constraints and put them in the problem
  4. Call solve to solve the problem
Let's follow the recipe.
A = optimvar('A',1,345,'Type','integer','LowerBound',1,'UpperBound',37);
prob = optimproblem;
% I assume that L and the function ASU exist in your workspace or path
prob.Objective = dot(A,L);
[w,a,x] = fcn2optimexpr(@ASU,A);
prob.Constraints.consa = a == 0;
prob.Constraints.consx = x == 0;
[sol,fval] = solve(prob)
You can set this problem up and solve it using the solver-based approach as well, but it is not as straightforward.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 comentarios
Omar Morsy
Omar Morsy el 7 de Dic. de 2021
Thanks for the reply.
I have tried the code in your comment. But I am getting the following error :-
Solving problem using fmincon.
Error using optim.problemdef.OptimizationProblem/solve
Nonlinear problem with integer variables not supported.
Error in Untitled4 (line 28)
[sol,fval] = solve(prob)
Alan Weiss
Alan Weiss el 7 de Dic. de 2021
I believe that you will need to have a recent version of Optimization Toolbox installed, and also Global Optimization Toolbox. Indeed, fmincon cannot solve problems with integer constraints. You will need to use ga or surrogateopt as the solver. A recent version of Optimization Toolbox will choose ga automatically.
I do not know what your function ASU does. If it is not nonlinear, then you have more options, including better solvers. In particular, if ASU is a linear function, then you can use intlinprog as the solver. However, if ASU is nonlinear, you have few choices.
Alan Weiss
MATLAB mathematical toolbox documentation

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by