bugGNU Octave - Bugs: bug #41888, fminsearch: should pass additional...

 
 

bug #41888: fminsearch: should pass additional arguments to called function

Submitter:  Hartmut <hardy>
Submitted:  Mon 17 Mar 2014 08:02:18 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * 3.8.1 Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Tue 24 Jan 2017 08:02:52 PM UTC, comment #18: 

Thanks for the quick reply!

The old scripts are working now.

Anonymous
Tue 24 Jan 2017 07:44:55 PM UTC, comment #17: 

The change was made on the development branch of Octave which will come out as Octave 4.4.0.  Until that time, you will need to get the code from the Mercurial repository.  Since this is just an m-file, you can copy fminsearch.m from the repository and put it in a local directory that is on the PATH for Octave.

Rik <rik5>
Group administrator
Tue 24 Jan 2017 07:33:12 PM UTC, comment #16: 

I just found this discussion as I had the same problem with lots of files from my older Matlab time.

It seems that the patch isn't applied to the optim package in octave 4.20. How can I get the updated fminsearch?

Anonymous
Wed 28 Dec 2016 08:24:27 PM UTC, comment #15: 

Thanks to Markus for improving the Matlab compatibility of this function, and thanks for applying this to the repo.

Note: There is a consecutive patch to fminsearch from Markus in bug #44220.

Hartmut <hardy>
Tue 27 Dec 2016 07:26:05 PM UTC, comment #14: 

I broke Markus' cset in two.  The first part concerning a bug in guarded_eval was applied on the stable branch here (http://hg.savannah.gnu.org/hgweb/octave/rev/fba8a6c742c6).

The second half which adds the new feature of calling fminsearch with additional arguments was added to the development branch here (http://hg.savannah.gnu.org/hgweb/octave/rev/3c896180df16).

Rik <rik5>
Group administrator
Sat 19 Nov 2016 11:16:07 AM UTC, comment #13: 

Are there any objections against applying Markus' patch now (see comment #9, file #37806)?

Hartmut <hardy>
Sat 16 Jul 2016 06:15:07 PM UTC, comment #12: 

Oops, yes I have tested this patch with the Octave "default" branch, not with "stable". Sorry for the confusion.

My expected fails are bug #46675 and bug #48514. Those are due to my OS Ubuntu 14.04 (old osmesa version and old gcc version). I have seperatly tested to compile the default branch with and without this patch, and the result of "make test" does NOT change at all. So this patch does not introduce any test failures.

Hartmut <hardy>
Sat 16 Jul 2016 03:53:49 PM UTC, comment #11: 

Thank you, Hartmut, for testing this patch.

Are you sure you applied the patch against the stable branch? The hg id 4c0f78b3c86f is in the default branch for me.

If I understood correctly, the expected fails you saw are un-related to this patch? Or are these some tests that you expect to fail because this patch here was applied?

Anyway, if you have legacy code relying on different syntax of fminsearch, you might also be interested in testing the change-set in bug #44220 which mainly adds support for more optimization options and additional output arguments.

Markus Mützel <mmuetzel>
Group administrator
Fri 15 Jul 2016 07:51:43 PM UTC, comment #10: 

I have tested Markus' second patch from comment #9 with the current "stable" branch from the octave repository (hg id 4c0f78b3c86f).

  • With this patch applied, Octave runs the small demo script from my original post just fine. So this patch fully solves original my issue.


  • The second allowed Matlab syntax (see comment #7) also works fine with this patch applied.



  • I have also done a "make check" and not had any other failing tests (except for exptected fails, see bug #48514).


I would be very happy to see this patch in the upcoming Octave 4.2 release. It would make a lot of our legacy Matlab code work out of the box.

Hartmut <hardy>
Mon 11 Jul 2016 04:14:51 PM UTC, comment #9: 

The attached patch is the just acosmetic change of the previuos one which adds some comments to the BISTs and groups them slightly differently.
This is for a follow up change-set I am going to upload shortly for bug #44220.

(file #37806)

Markus Mützel <mmuetzel>
Group administrator
Fri 08 Jul 2016 06:34:32 PM UTC, comment #8: 

Attached is a patch which passes additional arguments to fminsearch on to the called function. JWE's concerns in comment #3 should be addressed.
Tests are added for the two syntaxes in comment #7

Additionally, it fixes a bug (typo) when "FunValCheck" was set to "on" and adds some tests for this.

(file #37776)

Markus Mützel <mmuetzel>
Group administrator
Fri 21 Mar 2014 07:52:46 PM UTC, comment #7: 

If it's still interesting, here is the Matlab behavior:

  • Those two ways work fine:


result1 = fminsearch(@myfun, start, options, param)
result2 = fminsearch(@myfun, start,[], param)


  • This way does NOT work in Matlab:


result3 = fminsearch(@myfun, start, param)

It throws an error "Input argument "a" is undefined."

Hartmut <hardy>
Fri 21 Mar 2014 04:54:20 PM UTC, comment #6: 

So it doesn't just mean that you have to explicitly give the value of that parameter if you want to use the varargin?  That's what it appears to do.  It's just like having multiple parameters with default values.  If you want to give the value of some later one, then you have to give the values for the earlier ones (even if they're the same as the default).
So for instance, as a convoluted way to find the square root of some number y

function [z] = f(x,y)
  z = abs(x .^ 2 - y);
endfunction

function [r] = takesqrt(n)
  r = fminsearch(@f, 1, struct(), n);
endfunction

AFAICT, this works fine.

David Spies <dspyz>
Thu 20 Mar 2014 06:59:15 PM UTC, comment #5: 

Appending varargin after an argument with a default value doesn't seem to me like it's going to work properly and should probably be a parser error.

Does Matlab allow extra parameters when the options struct is not provided?

John W. Eaton <jwe>
Group administrator
Thu 20 Mar 2014 06:52:30 PM UTC, comment #4: 

I just looked and it seems like it's already implemented.  The extra arguments are just disallowed for some reason.  Here's a patch that fixes it.

(file #30974)

David Spies <dspyz>
Tue 18 Mar 2014 04:32:24 PM UTC, comment #3: 

I'm not interested in this change for myself and currently I don't have much time for volunteer work for things that don't interest me.  So I see a couple of options.  You could submit a patch for it yourself, or hire someone to do it (me?) or wait for some other kind soul to make the change for you.

John W. Eaton <jwe>
Group administrator
Tue 18 Mar 2014 08:19:51 AM UTC, comment #2: 

You are right, in the current documentation of Matlab this behavior is not documented any more. They suggest to use anonymous functions (or nested functions) instead, like you suggested as well.

But I am pretty sure, that a couple of years ago, this WAS the documented behaviour of Matlab. And in current Matlab versions this still works today. (Here is an old Matlab doc I found: http://nf.nci.org.au/facilities/software/Matlab/techdoc/ref/fminsearch.html )

Therefore, quite some of our legacy matlab code still uses this (nowadays undocumented) behavior.

What harm would it do to implement this behavior in Octave as well? It doesn't sound like a huge change in code to my amateur ears. It would be compatible to the former Octave behavior, and it would increase the compatibility with Matlab. And we could properly document it in the Octave documentation.

Hartmut <hardy>
Mon 17 Mar 2014 09:07:07 PM UTC, comment #1: 

That doesn't seem to be the documented behavior of Matlab's fminsearch function.

I think the "modern" way of doing this is to use an anonymous function, something like this:


result = fminsearch (@(x) myfun (x, param), start, options);


John W. Eaton <jwe>
Group administrator
Mon 17 Mar 2014 08:02:18 PM UTC, original submission:  

Here is an example of a (for me and my colleagues) normal usage of the fminsearch function. It works perfectly well in Matlab, but it throws an error in Octave (no Forge packages):

  • myfun.m contains:


function f = myfun(x,a)
f = x(1)^2 + a*x(2)^2;

  • demo script with fminsearch:


param = 1.5; % define a parameter for the function myfun
options = optimset;
start = [1,3];
result = fminsearch(@myfun, start, options, param)

In Matlab the "surplus" parameter param is handed over to the called function (here: myfun) as a static (i.e. non variable) parameter. This will also work with more than one surplus parameters (if the called function expects them) in Matlab.

It would be nice to have the same behavior in Octave, because this is a very handy way to hand over parameters to an "error function", and not to use global variables for this purpose.

Hartmut <hardy>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #30974:  extra_args.diff added by dspyz (1KiB - text/x-patch)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Updated the item)
  • -email is unavailable- added by mmuetzel (Updated the item)
  • -email is unavailable- added by dspyz (Updated the item)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by hardy (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-12-27 rik5 StatusPatch Submitted Fixed
        Open/ClosedOpen Closed
    2016-11-21 rik5 StatusNone Patch Submitted
    2016-07-11 mmuetzel Attached File- Added fminsearch_additional_argument_v2.patch, #37806
    2016-07-08 mmuetzel Attached File- Added fminsearch_additional_argument.patch, #37776
    2014-03-20 dspyz Attached File- Added extra_args.diff, #30974

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code