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

16 Ansichten (letzte 30 Tage)
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 Kommentare
Walter Roberson
Walter Roberson am 29 Mai 2022
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 29 Mai 2022
Bearbeitet: Walter Roberson am 29 Mai 2022
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 Kommentare
Anastasia Lado
Anastasia Lado am 31 Mai 2022
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 am 31 Mai 2022
Bearbeitet: Anastasia Lado am 31 Mai 2022
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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by