Message Boards Message Boards

0
|
12784 Views
|
22 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Problem with using NMinimize with a compiled function

Posted 10 years ago
POSTED BY: Tomáš Hruš
22 Replies
Posted 9 years ago

Ilian, thank you for the clear ansver!

POSTED BY: Tomáš Hruš
POSTED BY: Ilian Gachevski

You could put in a Print statement in the function and see what values NMinimize is calling. Perhaps it's passing the function values outside the range you expect.

POSTED BY: Frank Kampas
Posted 9 years ago

Problem with using NMinimize with a compiled function

POSTED BY: Tomáš Hruš

Looks like the iterator error message is from the function model.

POSTED BY: Frank Kampas
Posted 9 years ago

Yes, it looks so. I can use it in the manipulate command - its OK. I can sipmly write

In[6]:= krit[0.5, 0.3, f, X, Fexp]

Out[6]= 2.39701

...it works.

I can draw a Plot3d od krit as a function of k and t -- it also works...

But using krit in NMinimize doesn't works....

Attachments:
POSTED BY: Tomáš Hruš

Did you test krit to see if it works as expected?

POSTED BY: Frank Kampas
Posted 9 years ago

Yes, using construction

Manipulate[
 ListPlot[{model[k, t, f, X], Fexp}, 
  PlotLabel -> "krit=" <> ToString[krit[k, t, f, X, Fexp]]],
 {{k, 0.328}, 0, 1}, {{t, 0.4}, 1/f, 20/f}] 

can be seen, that it works OK. When model is close to Fexp, then krit is small, if modell is far away from Fexp, then krit is greater.

For example see attachement.

Attachments:
POSTED BY: Tomáš Hruš
Posted 9 years ago
POSTED BY: Tomáš Hruš
POSTED BY: Daniel Lichtblau
POSTED BY: Frank Kampas

It would be easier to figure out what the problem is if the compiled function only includes generating lists.

POSTED BY: Frank Kampas

Frank, ListConvolve in Compile is not the issue.

POSTED BY: Daniel Lichtblau

To repeat what I earlier stated, if you provide a standalone example that is failing to work, the chances of getting a viable response go up substantially.

POSTED BY: Daniel Lichtblau
Posted 10 years ago

With compiled function odezva it also works

odezva = Compile[{{k1o, _Real}, {k2o, _Real}, {n1o, _Real}, {n2o, 
_Real}, {d1o, _Integer}, {d2o, _Integer}, {e1o, _Real}, {e2o, _Real}, 
{Xo, _Real, 1}, {Vo, _Real, 1}},
  Module[{k1 = k1o, k2 = k2o, n1 = n1o, n2 = n2o, d1 = d1o, d2 = d2o, 
    e1 = e1o, e2 = e2o, X = Xo, V = Vo, Xu, Vu, jadroX, jadroV, 
    odezva, Fmean},
   jadroX = k1*(Table[(1 - i/d1)^n1, {i, 0, d1}]);
   jadroV = k2*(Table[(1 - i/d2)^n2, {i, 0, d2}]);
   Xu = Sign[X]*Abs[X]^e1;
   Vu = Sign[V]*Abs[V]^e2;

   odezva = 
    ListConvolve[jadroX, Xu, 1, 0] + ListConvolve[jadroV, Vu, 1, 0] ;
   odezva
   ]
  ]

with a message

CompiledFunction::cfsa: Argument k1 at position 1 should be a machine-size real number. >>

but cca 25% faster..

POSTED BY: Tomáš Hruš

Given the CompiledFunction error message, I think that the compiled function is defaulting to a symbolic evaluation. I would only compile the Table functions and leave the ListConvolve outside the compiled function.

POSTED BY: Frank Kampas
Posted 10 years ago

Do I have a bad day? This my older code is working....


In[1]:= ClearAll["Global`*"]
SetDirectory[NotebookDirectory[]];

In[3]:= data = 
  ReadList["test66-2-0000000949-MOD2.out", Real, RecordLists -> True];

In[4]:= X = data[[All, 2]];
V = data[[All, 3]];
Fexp = data[[All, 4]];
data =.;

In[8]:= odezva[k1o_, k2o_, n1o_, n2o_, d1o_Integer, d2o_Integer, e1o_,
   e2o_, Xo_, Vo_] :=
 Module[{k1 = k1o, k2 = k2o, n1 = n1o, n2 = n2o, d1 = d1o, d2 = d2o, 
   e1 = e1o, e2 = e2o, X = Xo, V = Vo, Xu, Vu, jadroX, jadroV, 
   odezva, Fmean},
  jadroX = k1*(Table[(1 - i/d1)^n1, {i, 0, d1}]);
  jadroV = k2*(Table[(1 - i/d2)^n2, {i, 0, d2}]);
  Xu = Sign[X]*Abs[X]^e1;
  Vu = Sign[V]*Abs[V]^e2;

  odezva = 
   ListConvolve[jadroX, Xu, 1, 0] + ListConvolve[jadroV, Vu, 1, 0] ;
  odezva
  ]

In[9]:= krit[k1o_, k2o_, n1o_, n2o_, d1o_Integer, d2o_Integer, e1o_, 
  e2o_, Fo_, Xo_, Vo_] :=
 Module[{k1 = k1o, k2 = k2o, n1 = n1o, n2 = n2o, d1 = d1o, d2 = d2o, 
   e1 = e1o, e2 = e2o, F = Fo, X = Xo, V = Vo, kriterium, o, Fmean},

  Fmean = Mean[Abs[F]];

  o = odezva[k1, k2, n1, n2, d1, d2, e1, e2, X, V];
  kriterium = 100.*Mean[Abs[o - F]]/Fmean;
  kriterium
  ]

In[10]:= krit[10000., 3000., 0.5, 0.01, 3000, 2000, 3.5, 1.8, Fexp, \
X, V]

Out[10]= 58.8654

In[11]:= res = 
 NMinimize[{krit[k1, k2, n1, n2, d1, d2, e1, e2, Fexp, X, V], 
   k1 > 150 && k2 > 10  && 
    d1 > 10 && d2 > 10  &&
     n1 > .01 && n2 > .01 && 
    e1 > .01 && e2 > .01}, 
  {k1, k2, n1, n2, d1 \[Element] Integers, d2 \[Element] Integers , 
   e1, e2}
  , MaxIterations -> 5
   ]

Speak["Calculation finished!"]



During evaluation of In[11]:= NMinimize::cvmit: Failed to converge to the requested accuracy or precision within 5 iterations. >>

Out[11]= {20.3068, {k1 -> 150.043, k2 -> 10.9215, n1 -> 1.94636, 
  n2 -> 0.712322, d1 -> 12, d2 -> 13, e1 -> 0.679406, e2 -> 0.95152}}
POSTED BY: Tomáš Hruš
Posted 10 years ago
POSTED BY: Tomáš Hruš

Does the code run if you don't compile the module?

POSTED BY: Frank Kampas

It would be much easier to address this if a self contained example were provided (one that does not require "test66-2-0000000949-MOD2.out").

POSTED BY: Daniel Lichtblau
Posted 10 years ago

Thank you, Frank, but it is still not clear for me...

I tried a very simple compiled function - NMinimze works with it. The non-compiled version of my function - also works.

It means, that some compiled functions cannot be manipulated symbolically (and cannot by used with NMinimize -- for example my function above), even his non-compiled version can be manipulated symbolically (and can by used with NMinimize) -- whereas other functions can by used with NMinimize even compiled?

POSTED BY: Tomáš Hruš

NMinimize is expecting a function it can manipulate symbolically.

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

Group Abstract Group Abstract