FMINCON runs with lower and upper bound variables switched, but doesn't run when bounds are specified correctly

조회 수: 12 (최근 30일)
I'm running fmincon function in python via this interface: https://github.com/byuflowlab/pyfmincon
If I specify upper and lower bounds as
ub = np.array([1.0, 30.0])
lb = np.array([0, 0])
it results in the following error:
Unable to resolve the name 'py.self.neg_log_likelihood'.
Error in my_optimize/fupdate (line 45)
eval(['output = py.', funcname, '(x);']) % output is a cell array with {J, cin}
Error in my_optimize/obj (line 63)
[J, cin, gJ, gcin] = fupdate(x);
Error in fmincon (line 577)
[initVals.f,initVals.g] = feval(funfcn{3},X,varargin{:});
Error in my_optimize (line 28)
[xopt, fopt, exitflag, output] = fmincon(@obj, x0, A, b, Aeq, beq, lb, ub, @con, options);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
However, if I switch upper and lower bounds the function runs without errors and prints out this message:
Exiting due to infeasibility: at least one lower bound exceeds the corresponding upper bound.
  댓글 수: 6
Walter Roberson
Walter Roberson 2022년 5월 29일
My suggestion to use dynamic field names would need to be modified if the supplied name includes periods. So for the moment go back to eval() version

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 5월 29일
편집: Walter Roberson 2022년 5월 29일
Let us examine how fmincon works.
The first thing that fmincon does is validate its parameters as being the correct data type and consistent sizes.
The next thing it does is to invoke the objective function passing in x0, in a section of code that has extra checks to see if the function returns a reasonable value -- in particular a finite numeric scalar.
Then it involves the objective function once for each variable, with a modified version of the initial conditions, in order to get locations and values that can be used to estimate the gradient. During this initial pass, nonlinear constraints are ignored.
Okay so as mentioned first it validates data types. What is permitted for the initial location, x0? https://www.mathworks.com/help/optim/ug/fmincon.html#d123e91785 Answer: real-valued double precision vector. If you had passed in initial values that the py interface was not able to convert to double precision, then if the interface did not catch it, then fmincon would catch it when checking the parameters.
Now the objective function is invoked with a double precision vector. The objective function is supplied by the py interface. It takes the double precision vector and passes it to a Python function, relying on the usual conversions of double precision to the appropriate Python data type. The Python function receives double precision, possibly np.array or something like that.
Now, in order for self to work, the recieved class (np.array?) would have to have the target method neg_log_likelihood. Does it? And if it does, then is it really necessary to invoke self.neg_log_likelihood specifically or would it figure it out based on the object passed in?
If you look at the example you will see that the example passes in a static method name as the function, not 'self.something'
  댓글 수: 14
Anastasia Lado
Anastasia Lado 2022년 5월 31일
Thanks a lot! Your suggestion worked.
My only problem now is that it runs for 6 hours and then my computer crashes. Is it because I didn't pass any parameters? Would you be able to help me with that since I don't know how to pass parameters to a function that is in quotes ''.
Anastasia Lado
Anastasia Lado 2022년 5월 31일
편집: Anastasia Lado 2022년 5월 31일
I tried replacing my function with a simplier one, and also tried printing out a line every time the objective function is called. But the message is never printed out, the code is running and crashing my computer. So it looks like the code never enters calc_nll(), because when i added a print message there at the very beginning it was never shown.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by