 ## How can I perform optimization over a discrete set of possible values in MATLAB?

### MathWorks Support Team (view profile)

on 7 Apr 2017
Latest activity Answered by Jeff Rothermel

### Jeff Rothermel (view profile)

on 18 Oct 2019 at 0:26
Accepted Answer by MathWorks Support Team

### MathWorks Support Team (view profile)

I would like to run an optimization to determine the best resistors for my circuit. Currently I am setting upper and lower bounds of the possible resistor values, solving the optimization in a continuous manner using "fmincon", and then mapping the solution to the nearest resistor value I have in stock. For example, the possible values may be something like: {15.5, 20, 27.2, 31.1, 33}.
Is there a way in MATLAB to solve this optimization problem directly considering the possible values, instead of solving the continuous problem and then picking the closest value?

### Release

R2016b ### MathWorks Support Team (view profile)

on 21 Aug 2019

One way to approach this problem is to treat it like an optimization with Integer Constraints, where you actually solve for an *index_ _*within the set possible solutions.
There are two primary optimization methods in MATLAB which support integer constraints: the Genetic Algorithm, and Linear Programming, although Linear Programming may not be a good fit for some problems. Even if you could implement an integer constraint on a function like "fmincon", the underlying algorithm would still search on a continuous space which does not make sense and is not efficient given the problem statement.
If the discrete solution space is small enough, you may even consider a direct search of all options, as opposed to some iterative optimization method. This approach may not be feasible if you have for example 40 possible resistor values and you are looking to solve for a vector of 8 resistor values. In such a situation, you end up with a search space of size 40^8 which is quite a few solutions to consider.
There is an example in the documentation which is quite similar to the described problem, and in this case the Genetic Algorithm is used with Discrete Non-Integer Constraints:
In this example, you can use the Genetic Algorithm to solve for an index, (i.e. 3) which then corresponds to some resistor value (i.e. 27.2). Given the inclusion of Integer Constraints, you will always find an exact index corresponding to one available resistor values, successfully implementing Discrete Non-Integer Variable Constraints on the optimization problem.