Group Abstract Group Abstract

Message Boards Message Boards

interface to external program / command line and optimisation [beginner]

Posted 11 years ago
POSTED BY: geotrupes a
6 Replies
Posted 11 years ago
POSTED BY: geotrupes a
Posted 11 years ago

Ok, I tried a simpler function. Using echo to print a square, made in Mathematica.

See this:

f[x_?NumericQ] := 
  RunProcess[{"echo", ToString[InputForm[x^2, NumberMarks -> False]]}, 
   "StandardOutput"];

It works for a single value of x, but then NMinimize complains that:

NMinimize[{f[y], 1 < y < 10}, {y}, Method -> "RandomSearch"]


NMinimize::nnum: The function value 1.9925755057065486`
 is not a number at {y} = {1.41159}. >>

What is the issue? I must confess to be rather lost at this stage :-( Thanks G>

POSTED BY: geotrupes a
Posted 11 years ago

You think you are seeing this behaving as you expect

In[1]:= f[x_?NumericQ] := RunProcess[{"echo", ToString[InputForm[x^2, NumberMarks->False]]},"StandardOutput"];
   f[3]

but if you then do this

Print[FullForm[%]]

I believe you will understand that your function is returning a string, not a numeric value, and trying to "minimize a string" isn't going to work.

If you are running Windows then

f[x_?NumericQ] := ToExpression[StringDrop[RunProcess[{"echo", ToString[InputForm[x^2, NumberMarks->False]]}, "StandardOutput"], -2]];
Print[FullForm[f[3]]]

might be closer to what you want. And if you aren't running Windows then the StringDrop may need to be adjusted to match your local conventions.

I think there should be a much better way of doing what you are trying to do.

POSTED BY: Bill Simpson
Posted 11 years ago

Thank you Bill, tried and it works

f[x_?NumericQ] := CompoundExpression[
  SetDirectory["~/Desktop/test"];
  Print[N[x]]; 
  Export["in.data", N[x], "Text"];
  Run["~/Desktop/test/run.sh"]; 
  result = N[ToExpression[Import["out.data"]]] ;
  Run["rm *.data"];
  Print[result]

There are few issues to resolve but this is great.

THANK YOU! G.

PS do you happen to know if this is running in a multicore fashion?

POSTED BY: geotrupes a
Posted 11 years ago
POSTED BY: geotrupes a
Posted 11 years ago

Perhaps this

POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard