0

In Matlab I want to use fminsearch to optimize a interval of numbers given a object function fun to minimize. The integer numbers can be selected from 1 to 30, and the number of integers is fixed to 5 for now. Assume the step size is 1. It will optimize many vectors such as:

[1 2 3 4 5]
[2 3 4 5 6]
[7 8 9 10 11]
[12 13 14 15 16]

In the long run, I may also try to optimize the step size and number of integers in the vector.

I want to know how to use fminsearch to properly realize this or maybe use other functions in the toolbox? Any suggestion will be appreciated.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
SimaGuanxing
  • 673
  • 2
  • 10
  • 29
  • I'm a little confused. Is it that you have this objective function `fun` that takes an interval, e.g. `1:5`, as argument and you want to find the interval that minimizes `fun`? – saastn Nov 21 '19 at 23:20
  • 1
    Your problem formulation is unclear. why not use MILP? – Adam Nov 22 '19 at 05:21
  • @Adam I am sorry but I didn't explain it clearly. Basically, we want this optimizer can optimize other variables which is not integers later. So instead of giving it a range for searching, we want it can search through a giving list of numbers. – SimaGuanxing Nov 22 '19 at 15:11
  • @SimaGuanxing When you say search through a giving list of numbers, it is like finding index of optimum in the list. It's still integer programming. – saastn Nov 22 '19 at 20:02

1 Answers1

1

First of all, as stated in documentation, fminsearch can only find minimum of unconstrained functions. fminbnd, on the other hand, can handle the bound constraint, however, non of these functions are meant to solve discrete functions. So you probably want to think of other options, ga for example.

Despite the fact that fminsearch does not handle constraints, but still you can use it to solve your optimization problem with cost of some unnecessary extra iterations. In this answer I assume that there is a fun function that takes an interval from a specific range as its argument and the objective is to find the interval that minimizes it.

Since the interval has a fixed step size and length, the problem is single-variable and we only need to find its start point. We can use floor to convert a continuous problem to a discrete one. To cover the bound constraint we can check for feasibility of intervals and return Inf for infeasible ones. That will be something like this:

%% initialization of problem parameters
minval = 1;
maxval = 30;
step = 1;
count = 5;
%% the objective function
fun = @(interval) sum((interval-5).^2, 2);
%% a function that generates an interval from its initial value
getinterval = @(start) floor(start) + (0:(count-1)) * step;
%% a modified objective function that handles constraints
objective = @(start) f(start, fun, getinterval, minval, maxval);
%% finding the interval that minimizes the objective function
y = fminsearch(objective, (minval+maxval)/2);
best = getinterval(y);
eval = fun(best);
disp(best)
disp(eval)

where f function is:

function y = f(start, fun, getinterval, minval, maxval)
interval = getinterval(start);
if (min(interval) < minval) || (max(interval) > maxval)
    y = Inf;
else
    y = fun(interval);
end
end
saastn
  • 5,717
  • 8
  • 47
  • 78
  • Thanks for your answer. If I want two constrains, say I have another variable called Vx that is from 0.1 to 0.9 with 0.1 step increase in every search, how I can accommodate the functions you write to realize it? Thanks so much for your help! – SimaGuanxing Nov 22 '19 at 18:49
  • @SimaGuanxing In that case `start` would be a vector and you need to fix their steps and check their validity independently. It's like `Vx=floor(start(2) * 10)/10; if (Vx<0.1) || (Vx>0.9) y=Inf;. – saastn Nov 22 '19 at 19:56
  • I sort of understand what you suggest to change but still confused when I implement it. Should I use the same f function for different variables or use same function? I would be really appreciated if you can update the code to reflect the change for the second constrains. Thanks! – SimaGuanxing Nov 22 '19 at 20:04
  • Implement it in same function. `fminsearch` is capable of minimizing multivariate functions. So when you call fminsearch(X, Y), X and Y can both be vectors of same size. Now, when you want to check for feasibility of a solution, you need to check all of its variables. – saastn Nov 22 '19 at 20:11
  • @SimaGuanxing I really think that you need to ask a new question about multivariate functions using `fminsearch`. And try to describe what you have done, and what the problem is. – saastn Nov 22 '19 at 20:14
  • Okay. I will try to implement it first and open a new thread for multivariate functions using fminsearch if needed. Thanks! – SimaGuanxing Nov 22 '19 at 20:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202921/discussion-between-simaguanxing-and-saastn). – SimaGuanxing Nov 22 '19 at 21:33
  • I created a new thread for this. https://stackoverflow.com/questions/59002429/matlab-use-fminsearch-to-optimize-multi-variables – SimaGuanxing Nov 22 '19 at 22:13