This question is closed. Reopen it to edit or answer.

Using the output of a ready function as a constraint

1 view (last 30 days)
Omar Morsy
Omar Morsy on 7 Dec 2021
Closed: Matt J on 7 Dec 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

Answers (1)

Alan Weiss
Alan Weiss on 7 Dec 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
Alan Weiss
Alan Weiss on 7 Dec 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




Community Treasure Hunt

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

Start Hunting!