Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Using the output of a ready function as a constraint

1 visualizzazione (ultimi 30 giorni)
Omar Morsy
Omar Morsy il 7 Dic 2021
Chiuso: Matt J il 7 Dic 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

Risposte (1)

Alan Weiss
Alan Weiss il 7 Dic 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 Commenti
Omar Morsy
Omar Morsy il 7 Dic 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 il 7 Dic 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

Questa domanda è chiusa.

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by