Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #8736 > unrolled thread
| Started by | EcoTheory <carroll.ian@gmail.com> |
|---|---|
| First post | 2012-02-25 06:58 +0000 |
| Last post | 2012-02-27 11:43 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
FindMinimum v. NMinimize and an external program EcoTheory <carroll.ian@gmail.com> - 2012-02-25 06:58 +0000
Re: FindMinimum v. NMinimize and an external program Szabolcs <szhorvat@gmail.com> - 2012-02-26 09:22 +0000
Re: FindMinimum v. NMinimize and an external program Szabolcs <szhorvat@gmail.com> - 2012-02-26 09:21 +0000
[solved] Re: FindMinimum v. NMinimize and an external program Ian <carroll.ian@gmail.com> - 2012-02-27 11:43 +0000
| From | EcoTheory <carroll.ian@gmail.com> |
|---|---|
| Date | 2012-02-25 06:58 +0000 |
| Subject | FindMinimum v. NMinimize and an external program |
| Message-ID | <jia0qc$1c4$1@smc.vnet.net> |
Can anyone say whether the following behavior is expected and why? I'd like to avoid learning how to use MathLink, but I need to use FindMinimum on a large external calculation and my way is broken.
Say there's a command line program called 'prog' that calculates y = (x-1)^2, reading x from a named pipe called 'in' and writing y to a named pipe called 'out'. Mathematica interacts with prog by reading and writing to the pipes. Like ...
> in = OpenWrite["in"];
> f[x_?NumericQ]:=(Write[in, CForm[x]]; First[ReadList["!cat out", Number]])
Calling NMinimize on f gives the correct answer, but FindMinimum doesn't. Specifically, FindMinimum[f[x], {x, 0}] returns the message FindMinimum::fmgz. That is, Mathematica thinks the gradient is zero.
I can't find a reason the two functions should behave differently. Any thoughts?
A couple details for the DIYers. Here's prog:
#include <stdio.h>
#include <math.h>
int main()
{
FILE * io;
int j;
double x,y;
while((j = scanf("%lf", &x)) != EOF)
{
y = pow((x-1.0), 2);
io = fopen("out", "w");
fprintf(io, "%e\n", y);
fclose(io);
}
return;
}
Run prog with a redirect for stdin, as in
/prog < in
[toc] | [next] | [standalone]
| From | Szabolcs <szhorvat@gmail.com> |
|---|---|
| Date | 2012-02-26 09:22 +0000 |
| Message-ID | <jictk5$emr$1@smc.vnet.net> |
| In reply to | #8736 |
On Saturday, 25 February 2012 08:58:20 UTC+2, Ian wrote:
> Can anyone say whether the following behavior is expected and why? I'd like to avoid learning how to use MathLink, but I need to use FindMinimum on a large external calculation and my way is broken.
>
> Say there's a command line program called 'prog' that calculates y = (x-1)^2, reading x from a named pipe called 'in' and writing y to a named pipe called 'out'. Mathematica interacts with prog by reading and writing to the pipes. Like ...
>
> > in = OpenWrite["in"];
> > f[x_?NumericQ]:=(Write[in, CForm[x]]; First[ReadList["!cat out", Number]])
>
> Calling NMinimize on f gives the correct answer, but FindMinimum doesn't. Specifically, FindMinimum[f[x], {x, 0}] returns the message FindMinimum::fmgz. That is, Mathematica thinks the gradient is zero.
>
> I can't find a reason the two functions should behave differently. Any thoughts?
>
> A couple details for the DIYers. Here's prog:
> #include <stdio.h>
> #include <math.h>
> int main()
> {
> FILE * io;
> int j;
> double x,y;
>
> while((j = scanf("%lf", &x)) != EOF)
> {
> y = pow((x-1.0), 2);
> io = fopen("out", "w");
> fprintf(io, "%e\n", y);
> fclose(io);
> }
> return;
> }
>
> Run prog with a redirect for stdin, as in
> /prog < in
Here's an actual example of using LibraryLink for your test function. Just copy and paste all the code as it is and evaluate it. The C code is included as strings for simplicity.
<< CCompilerDriver`
boilerplate =
"
#include \"WolframLibrary.h\"
DLLEXPORT mint WolframLibrary_getVersion(){
return WolframLibraryVersion;
}
DLLEXPORT int WolframLibrary_initialize( WolframLibraryData \
libData) {
return 0;
}
DLLEXPORT void WolframLibrary_uninitialize( WolframLibraryData \
libData) {
return;
}
DLLEXPORT int constantzero(WolframLibraryData libData, mint Argc, \
MArgument *Args, MArgument Res){
MArgument_setInteger(Res, 0);
return LIBRARY_NO_ERROR;
}
";
source =
"
#include <math.h>
DLLEXPORT int fun(WolframLibraryData libData,
mint Argc, MArgument *Args, MArgument Res) {
mreal R0;
mreal R1;
R0 = MArgument_getReal(Args[0]);
R1 = pow((R0-1),2);
MArgument_setReal(Res, R1);
return LIBRARY_NO_ERROR;
}
";
lib = CreateLibrary[boilerplate <> source, "test"]
fun = LibraryFunctionLoad[lib, "fun", {Real}, Real]
NMinimize[fun[x], x]
(* release lib so we can recompile if we want *)
LibraryUnload[lib]
[toc] | [prev] | [next] | [standalone]
| From | Szabolcs <szhorvat@gmail.com> |
|---|---|
| Date | 2012-02-26 09:21 +0000 |
| Message-ID | <jictj6$emj$1@smc.vnet.net> |
| In reply to | #8736 |
On Saturday, 25 February 2012 08:58:20 UTC+2, Ian wrote:
> Can anyone say whether the following behavior is expected and why? I'd like to avoid learning how to use MathLink, but I need to use FindMinimum on a large external calculation and my way is broken.
>
> Say there's a command line program called 'prog' that calculates y = (x-1)^2, reading x from a named pipe called 'in' and writing y to a named pipe called 'out'. Mathematica interacts with prog by reading and writing to the pipes. Like ...
>
> > in = OpenWrite["in"];
> > f[x_?NumericQ]:=(Write[in, CForm[x]]; First[ReadList["!cat out", Number]])
>
> Calling NMinimize on f gives the correct answer, but FindMinimum doesn't. Specifically, FindMinimum[f[x], {x, 0}] returns the message FindMinimum::fmgz. That is, Mathematica thinks the gradient is zero.
>
> I can't find a reason the two functions should behave differently. Any thoughts?
>
> A couple details for the DIYers. Here's prog:
> #include <stdio.h>
> #include <math.h>
> int main()
> {
> FILE * io;
> int j;
> double x,y;
>
> while((j = scanf("%lf", &x)) != EOF)
> {
> y = pow((x-1.0), 2);
> io = fopen("out", "w");
> fprintf(io, "%e\n", y);
> fclose(io);
> }
> return;
> }
>
> Run prog with a redirect for stdin, as in
> /prog < in
Not a direct answer, but I very much recommend learning LibraryLink (not MathLink). Please see the answers to a question I asked about which is the easiest way to integrate Mathematica with C/C++ code:
http://stackoverflow.com/questions/8140869/minimal-effort-method-for-integrating-c-functions-into-mathematica
There's a short but complete guide to setting up a LibraryLink program in Arnoud Buzing's answer. Please take a look at it. Your situation will be even easier because both your input and output are scalars.
Advantages of LibraryLink compared to pipes:
* fast -- you have direct access to Mathematica's memory (no need for I/O, converting between strings and numbers, slow process launching)
* more robust (no need to create named pipes---I don't even know how to do that on Windows)
* cross platform
I find it's less work and less worry than hackish approaches such as pipes, also it probably won't take you more than 30 minutes to get it working the first time.
[toc] | [prev] | [next] | [standalone]
| From | Ian <carroll.ian@gmail.com> |
|---|---|
| Date | 2012-02-27 11:43 +0000 |
| Subject | [solved] Re: FindMinimum v. NMinimize and an external program |
| Message-ID | <jifq97$so0$1@smc.vnet.net> |
| In reply to | #8757 |
Thanks to Szabolcs for the guidance on LibraryLink. It does look like the smart way to go.
However, I did figure out how to fix my (non-portable) use of pipes. For the record, the problem appears to just be that I was reading and writing for matted data. On Mathematica's side, replace calls using Read and Write with BinaryRead and BinaryWrite. On the C side, use fread and fwrite rather than scanf and printf. Get your data types lined up correctly, and it works as expected. My guess is that the "zero gradient" warning Mathematica gave using formatted data was accurate: not enough precision was included in the formatted strings to reflect the small changes Mathematica was looking for.
-Ian
On Sunday, February 26, 2012 1:21:42 AM UTC-8, Szabolcs wrote:
> On Saturday, 25 February 2012 08:58:20 UTC+2, Ian wrote:
> > Can anyone say whether the following behavior is expected and why? I'd like to avoid learning how to use MathLink, but I need to use FindMinimum on a large external calculation and my way is broken.
> >
> > Say there's a command line program called 'prog' that calculates y = (x-1)^2, reading x from a named pipe called 'in' and writing y to a named pipe called 'out'. Mathematica interacts with prog by reading and writing to the pipes. Like ...
> >
> > > in = OpenWrite["in"];
> > > f[x_?NumericQ]:=(Write[in, CForm[x]]; First[ReadList["!cat out", Number]])
> >
> > Calling NMinimize on f gives the correct answer, but FindMinimum doesn't. Specifically, FindMinimum[f[x], {x, 0}] returns the message FindMinimum:=
:fmgz. That is, Mathematica thinks the gradient is zero.
> >
> > I can't find a reason the two functions should behave differently. Any thoughts?
> >
> > A couple details for the DIYers. Here's prog:
> > #include <stdio.h>
> > #include <math.h>
> > int main()
> > {
> > FILE * io;
> > int j;
> > double x,y;
> >
> > while((j = scanf("%lf", &x)) != EOF)
> > {
> > y = pow((x-1.0), 2);
> > io = fopen("out", "w");
> > fprintf(io, "%e\n", y);
> > fclose(io);
> > }
> > return;
> > }
> >
> > Run prog with a redirect for stdin, as in
> > /prog < in
>
> Not a direct answer, but I very much recommend learning LibraryLink (not MathLink). Please see the answers to a question I asked about which is the easiest way to integrate Mathematica with C/C++ code:
>
> http://stackoverflow.com/questions/8140869/minimal-effort-method-for-integrating-c-functions-into-mathematica
>
> There's a short but complete guide to setting up a LibraryLink program in Arnoud Buzing's answer. Please take a look at it. Your situation will be even easier because both your input and output are scalars.
>
> Advantages of LibraryLink compared to pipes:
>
> * fast -- you have direct access to Mathematica's memory (no need for I/O, converting between strings and numbers, slow process launching)
>
> * more robust (no need to create named pipes---I don't even know how to do that on Windows)
>
> * cross platform
>
> I find it's less work and less worry than hackish approaches such as pipes, also it probably won't take you more than 30 minutes to get it working the first time.
On Sunday, February 26, 2012 1:21:42 AM UTC-8, Szabolcs wrote:
> On Saturday, 25 February 2012 08:58:20 UTC+2, Ian wrote:
> > Can anyone say whether the following behavior is expected and why? I'd like to avoid learning how to use MathLink, but I need to use FindMinimum on a large external calculation and my way is broken.
> >
> > Say there's a command line program called 'prog' that calculates y = (x-1)^2, reading x from a named pipe called 'in' and writing y to a named pipe called 'out'. Mathematica interacts with prog by reading and writing to the pipes. Like ...
> >
> > > in = OpenWrite["in"];
> > > f[x_?NumericQ]:=(Write[in, CForm[x]]; First[ReadList["!cat out", Number]])
> >
> > Calling NMinimize on f gives the correct answer, but FindMinimum doesn't. Specifically, FindMinimum[f[x], {x, 0}] returns the message FindMinimum:=
:fmgz. That is, Mathematica thinks the gradient is zero.
> >
> > I can't find a reason the two functions should behave differently. Any thoughts?
> >
> > A couple details for the DIYers. Here's prog:
> > #include <stdio.h>
> > #include <math.h>
> > int main()
> > {
> > FILE * io;
> > int j;
> > double x,y;
> >
> > while((j = scanf("%lf", &x)) != EOF)
> > {
> > y = pow((x-1.0), 2);
> > io = fopen("out", "w");
> > fprintf(io, "%e\n", y);
> > fclose(io);
> > }
> > return;
> > }
> >
> > Run prog with a redirect for stdin, as in
> > /prog < in
>
> Not a direct answer, but I very much recommend learning LibraryLink (not MathLink). Please see the answers to a question I asked about which is the easiest way to integrate Mathematica with C/C++ code:
>
> http://stackoverflow.com/questions/8140869/minimal-effort-method-for-integrating-c-functions-into-mathematica
>
> There's a short but complete guide to setting up a LibraryLink program in Arnoud Buzing's answer. Please take a look at it. Your situation will be even easier because both your input and output are scalars.
>
> Advantages of LibraryLink compared to pipes:
>
> * fast -- you have direct access to Mathematica's memory (no need for I/O, converting between strings and numbers, slow process launching)
>
> * more robust (no need to create named pipes---I don't even know how to do that on Windows)
>
> * cross platform
>
> I find it's less work and less worry than hackish approaches such as pipes, also it probably won't take you more than 30 minutes to get it working the first time.
On Sunday, February 26, 2012 1:21:42 AM UTC-8, Szabolcs wrote:
> On Saturday, 25 February 2012 08:58:20 UTC+2, Ian wrote:
> > Can anyone say whether the following behavior is expected and why? I'd like to avoid learning how to use MathLink, but I need to use FindMinimum on a large external calculation and my way is broken.
> >
> > Say there's a command line program called 'prog' that calculates y = (x-1)^2, reading x from a named pipe called 'in' and writing y to a named pipe called 'out'. Mathematica interacts with prog by reading and writing to the pipes. Like ...
> >
> > > in = OpenWrite["in"];
> > > f[x_?NumericQ]:=(Write[in, CForm[x]]; First[ReadList["!cat out", Number]])
> >
> > Calling NMinimize on f gives the correct answer, but FindMinimum doesn't. Specifically, FindMinimum[f[x], {x, 0}] returns the message FindMinimum:=
:fmgz. That is, Mathematica thinks the gradient is zero.
> >
> > I can't find a reason the two functions should behave differently. Any thoughts?
> >
> > A couple details for the DIYers. Here's prog:
> > #include <stdio.h>
> > #include <math.h>
> > int main()
> > {
> > FILE * io;
> > int j;
> > double x,y;
> >
> > while((j = scanf("%lf", &x)) != EOF)
> > {
> > y = pow((x-1.0), 2);
> > io = fopen("out", "w");
> > fprintf(io, "%e\n", y);
> > fclose(io);
> > }
> > return;
> > }
> >
> > Run prog with a redirect for stdin, as in
> > /prog < in
>
> Not a direct answer, but I very much recommend learning LibraryLink (not MathLink). Please see the answers to a question I asked about which is the easiest way to integrate Mathematica with C/C++ code:
>
> http://stackoverflow.com/questions/8140869/minimal-effort-method-for-integrating-c-functions-into-mathematica
>
> There's a short but complete guide to setting up a LibraryLink program in Arnoud Buzing's answer. Please take a look at it. Your situation will be even easier because both your input and output are scalars.
>
> Advantages of LibraryLink compared to pipes:
>
> * fast -- you have direct access to Mathematica's memory (no need for I/O, converting between strings and numbers, slow process launching)
>
> * more robust (no need to create named pipes---I don't even know how to do that on Windows)
>
> * cross platform
>
> I find it's less work and less worry than hackish approaches such as pipes, also it probably won't take you more than 30 minutes to get it working the first time.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web