Trying to:
-1- define a function which takes a number [step 1], write to a file [step 2], runs an external bash script [step 3], read a text file and return the result as a number [step 4]
-2- trying to optimise this function with NMinimize
Here is the code of the function:
f[x_] := CompoundExpression[SetDirectory["~/Desktop/test"];
Print[x]; (*this is for debugging*)
Export["in.data", N[x],
"Text"]; (*this saves the value x in a text file*)
Run["~/Desktop/test/run.sh"]; (*this runs a shell script which \
takes the text file,
read it and makes the square of the number in in,
a place holder for more to come*)
N[ToExpression[
Import["out.data"]]](*this reads the number from the script \
output and convert to number*) ]
If I use:
f[10], all if fine. the result is 100.
However if I use:
NMinimize[{f[y], 0 < y < 10}, {y}, Method -> "SimulatedAnnealing"]
The results is nonsense as the function writes “y” in the first text file as symbolic form.
I seem to be stuck.
When I evaluate the function on its own all is fine.
Even when the optimisation starts,
but gets stack after 2 cycles.
It looks like there is a multicore batch happening, therefore the output is messed up.
I tried switching off cores and nothing happens.
I also tried to change the code so an index keeps each run separate, but at no avail.
NMinimize[{f[y], 0 < y < 10}, {y}, Method -> "SimulatedAnnealing"]
0
1
0.20914
0
1
0.20914
NMinimize::nnum: The function value Null is not a number at {y} = {0.457318}. >>
0
1
0.20914
0
1
0.20914
NMinimize::nnum: The function value Null is not a number at {y} = {0.457318}. >>
0
1
0.20914
0
1
0.20914
NMinimize::nnum: The function value Null is not a number at {y} = {0.457318}. >>
General::stop: Further output of NMinimize::nnum will be suppressed during this calculation. >>
0
1
0.20914
0
1
0.20914
NMinimize[{f[y], 0 < y < 10}, {y}, Method -> "SimulatedAnnealing"]
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>
The ToExpression solves the issue and all works!!
I am very new to Mathematica an trying to run without knowing how to walk
If there is a better way and you have some idea please shoot!
Thank you,
G>