I tried to make faster my minimization using compiled function, but my code doesnt work. See below for the complete code.
Please, tell me, where to look for the problem!
In[1]:= ClearAll["Global`*"]
SetDirectory[NotebookDirectory[]];
In[3]:= data =
ReadList["test66-2-0000000949-MOD2.out", Real, RecordLists -> True];
X = data[[All, 2]];
V = data[[All, 3]];
Fexp = data[[All, 4]];
data =.;
{Length[X], Length[V], Length[Fexp]}
Out[8]= {4781, 4781, 4781}
In[9]:= (* X, V and Fexp are List of the same lengt *)
In[10]:= (* Now the function crit, which will be used for NMinimize *)
crit = Compile[{{k1, _Real}, {k2, _Real},
{n1, _Real}, {n2, _Real},
{d1, _Integer}, {d2, _Integer},
{e1, _Real}, {e2, _Real},
{F, _Real, 1}, {X, _Real, 1}, {V, _Real, 1}},
Module[{ kernX, kernV, Xu, Vu, xModel, critRes, Fmean, i},
kernX = k1*Table[(1. - i/d1)^n1, {i, 0, d1, 1}];
kernV = k2*Table[(1. - i/d2)^n2, {i, 0, d2, 1}];
Xu = Sign[X]*Abs[X]^e1;
Vu = Sign[V]*Abs[V]^e2;
xModel =
ListConvolve[kernX, Xu, 1, 0] + ListConvolve[kernV, Vu, 1, 0] ;
Fmean = Mean[Abs[F]];
critRes = 100.*Mean[Abs[xModel - F]]/Fmean;
critRes
]
];
Print["Test: ",
crit[121., 87., 1.2, 2.1, 163, 87, 0.26, 0.98, Fexp, X, V]]
During evaluation of In[10]:= Test: 6185.17
In[12]:= (* OK, function crit is working *)
(* but when I use it in NMinimize, I'll get a long list of error \
messages
in oposite with using almost the same function without compiling *)
In[13]:= res =
NMinimize[{crit[k1, k2, n1, n2, d1, d2, e1, e2, Fexp, X, V],
k1 > 50. && k2 > 10. &&
d1 > 10 && d2 > 10 &&
n1 > .1 && n2 > .1 &&
e1 > .1 && e2 > .1},
{k1, k2, n1, n2, d1 \[Element] Integers, d2 \[Element] Integers ,
e1, e2},
MaxIterations -> 5,
StepMonitor :> (NotebookDelete[cell];
cell = PrintTemporary[{k1, k2, n1, n2, d1, d2, e1, e2}]) ]
During evaluation of In[13]:= CompiledFunction::cfsa: Argument k1 at position 1 should be a machine-size real number. >>
During evaluation of In[13]:= Table::iterb: Iterator {i$892,0,d1,1} does not have appropriate bounds. >>
During evaluation of In[13]:= Table::iterb: Iterator {i$892,0,d2,1} does not have appropriate bounds. >>
During evaluation of In[13]:= ListConvolve::kldims: The kernel k1 Table[(1. -i$892/d1)^n1,{i$892,0,d1,1}] and list {1.00136*10^-6^e1,8.00371*10^-6^e1,0.0000222445^e1,0.0000370002^e1,0.0000517631^e1,0.0000670016^e1,0.000083009^e1,0.000100025^e1,0.000116025^e1,0.000134003^e1,<<31>>,0.000730019^e1,0.00077828^e1,0.000842307^e1,0.000945801^e1,0.000983019^e1,0.000984017^e1,0.00098402^e1,0.000979975^e1,0.000978019^e1,<<4731>>} are not both non-empty lists with the same tensor rank. >>
During evaluation of In[13]:= ListConvolve::kldims: The kernel k2 Table[(1. -i$892/d2)^n2,{i$892,0,d2,1}] and list {0.000175059^e2,0.000265539^e2,0.000362456^e2,0.000368983^e2,0.000375018^e2,0.000390574^e2,0.000412792^e2,0.000412703^e2,0.000424728^e2,0.000462279^e2,<<31>>,0.00108777^e2,0.0014036^e2,0.002094^e2,0.0017589^e2,0.000477701^e2,0.000012517^e2,-0.0000505149^e2,-0.0000750124^e2,-0.000287488^e2,<<4731>>} are not both non-empty lists with the same tensor rank. >>
Out[13]= $Aborted
(* It seems, that the kernX and kernV remains unevaluated, because
"Iterator {i,0,d1,1} does not have appropriate
bounds." and "Argument k1 at position 1 should be a machine-size real number" -- what does this means?
Values d1 and d2 should be integers and sould be positive (larger then 10),
so their values are correct. Value of k1 shoud be a real number > 50....?
In tutorial/ConstrainedOptimizationGlobalNumerical is written,
"Constraints are generally enforced by adding penalties". May be, d1
and d2 are not substituted as integers? But this modification of the
code of the function crit
kernX=k1*(Table[(1.-i/Ceiling[d1])^n1,{i,0,Ceiling[d1],1}]);
kernV=k2*(Table[(1.-i/Ceiling[d2])^n2,{i,0,Ceiling[d2],1}]);
doesnt help, so this is not the correct way.
*)