One of the biggest annoyances in Mathematica is that it will often hang up without any way to abort (i.e. Alt + .
or other methods can't stop the computation). Is there a way to prevent this? I am basically looking for a version of TimeConstrained
that actually works. The problem seems to come from a kernel that gets stuck computing and does not check for an abort signal, see Why doesn't Mathematica abort evaluation when I tell it to?. Could we improve on TimeConstrained so that it actually works? I am no expert in these things but might it be possible to start a new parallel kernel and evaluate the expression there. The main kernel (or another parallel kernel) might than be able to kill the parallel kernel if it gets stuck (it could use its own absolute clock so there is basically no need for any feedback from the kernel that got stuck).
Having such a function improvedTimeConstrained
would have many advantages over simply killing the main kernel and starting over. For example it would allow code of the form:
improvedTimeConstrained[PossiblyTooExpensiveComputation, 10, CheaperApproximationOfTheComputation]
Below I include a (not so) minimal example. Probably someone else can find a more minimal example but this should suffice. Note that I am not interested in finding the solution to this specific task. (I can do that easily by cleaning up the points a bit). This example just serves as an instance of the freezing behavior and the failing of TimeConstrained as requested by MarcoB.
First we define a function to find a continuous fit between two sets of points (see Fitting a curve between two sets of points):
rescalexy[xylist_,{xminmax_,yminmax_},{xminmaxgoal_,yminmaxgoal_}]:=Transpose[{Rescale[xylist[[All,1]],xminmax,xminmaxgoal],Rescale[xylist[[All,2]],yminmax,yminmaxgoal]}]
splinefit[upperBoundLowerBoundList_,nparam_Integer,degr_Integer]:=
Module[{pts,knots,vars,fitaux,smoothness,constraints,solution,xminmax,yminmax},
pts={upData,downData}=upperBoundLowerBoundList;
{xminmax,yminmax}={MinMax[pts[[All,All,1]]],MinMax[pts[[All,All,2]]]};
pts=(rescalexy[#,{xminmax,yminmax},{{0,1},{0,1}}])&/@pts;
{upData,downData}=pts;
vars=c/@Range[nparam];
knots=Join[ConstantArray[0,degr],Subdivide[nparam-degr],ConstantArray[1,degr]];
fitaux[x_]:=Table[BSplineBasis[{degr,knots},i,x],{i,0,nparam-1}].vars;
smoothness=Total[Differences[vars,2]^2];
constraints=Flatten[{fitaux[#[[1]]]<=#[[2]]&/@upData,fitaux[#[[1]]]>=#[[2]]&/@downData}];
constraints=constraints/.{c[a_]->c[a]};
solution=FindMinimum[{sm=smoothness,co=constraints},va=vars];
Plot[
Rescale[fitaux[(x-xminmax[[1]])/(xminmax[[2]]-xminmax[[1]])]/.solution[[2]],{0,1},yminmax]
,{x,Sequence@@xminmax}
,AspectRatio->1,ImageSize->400,PlotRange->All]
]
Then TimeConstrained[splinefit[data, 150, 3], 10]
will hang when applied to the data:
data={{{0.8539, 10.5206}, {0.8556, 10.5051}, {0.8592, 10.53}, {0.8636,
10.555}, {0.8643, 13.6494}, {0.8643, 19.9987}, {0.8698,
10.5645}, {0.8721, 10.5962}, {0.8786, 10.6034}, {0.8811,
10.6194}, {0.8824, 10.6735}, {0.8869, 13.6494}, {0.8869,
19.9987}, {0.8884, 10.622}, {0.8954, 10.6581}, {0.8972,
10.6349}, {0.904, 10.6517}, {0.9068, 11.045}, {0.9095,
13.6494}, {0.9095, 19.9987}, {0.914, 10.6649}, {0.9151,
10.7121}, {0.9224, 10.6942}, {0.9296, 10.6991}, {0.9321,
13.6494}, {0.9321, 19.9987}, {0.9389, 10.8126}, {0.9418,
10.7148}, {0.9496, 10.7351}, {0.9503, 10.7663}, {0.9547,
13.6494}, {0.9547, 19.9987}, {0.9552, 10.7543}, {0.9603,
10.7646}, {0.9683, 10.7761}, {0.9696, 10.8508}, {0.9726,
10.8018}, {0.9773, 13.6494}, {0.9773, 19.9987}, {0.9792,
10.799}, {0.9841, 10.9972}, {0.9852, 10.8168}, {0.993,
10.8259}, {0.9964, 11.3662}, {1., 13.6494}, {1., 19.9987}, {1.0001,
10.8571}, {1.0019, 10.8108}, {1.0087, 10.8102}, {1.013,
10.8282}, {1.015, 10.8132}, {1.0194, 10.9288}, {1.0206,
10.8197}, {1.0247, 13.4745}, {1.0248, 11.209}, {1.0275,
10.836}, {1.0283, 10.8022}, {1.0287, 11.6805}, {1.0357,
13.2118}, {1.036, 10.7757}, {1.0393, 13.1242}, {1.0395,
11.7853}, {1.0405, 13.095}, {1.0409, 13.0853}, {1.0411,
13.082}, {1.0411, 13.081}, {1.0411, 13.0806}, {1.0436,
10.7512}, {1.0475, 10.7325}, {1.0514, 11.3404}, {1.0514,
11.3298}, {1.0514, 11.2981}, {1.0515, 11.2239}, {1.0516,
11.0658}, {1.0517, 10.7875}, {1.0533, 10.7307}, {1.0538,
10.7876}, {1.0539, 10.8557}, {1.054, 10.8885}, {1.0596,
10.686}, {1.0596, 10.9559}, {1.065, 10.686}, {1.0654,
10.8729}, {1.0694, 10.686}, {1.078, 10.8729}, {1.0807,
10.6584}, {1.0838, 11.2466}, {1.0885, 10.7483}, {1.1107,
10.7066}, {1.1614, 10.654}, {1.1692, 10.2988}, {1.1959,
10.0731}, {1.2164, 10.2331}, {1.2458, 9.8058}, {1.2662,
10.8644}, {1.3176, 9.517}, {1.4179, 9.799}, {1.4402,
10.2199}, {1.4602, 9.9541}, {1.4942, 10.917}, {1.5076,
10.0063}, {1.5401, 10.2214}, {1.5702, 10.9346}, {1.5818,
10.2238}, {1.5955, 10.9404}, {1.6064, 12.3771}, {1.6328,
10.591}, {1.6746, 11.1102}, {1.6762, 10.77}}, {{0.8523,
10.4477}, {0.8528, 10.4803}, {0.8584, 6.3921}, {0.8598,
10.4422}, {0.8601, 10.5144}, {0.8629, 5.4032}, {0.8643,
7.3}, {0.8658, 10.5372}, {0.8736, 10.5423}, {0.8761,
9.8091}, {0.8781, 10.5757}, {0.881, 10.3277}, {0.8827,
10.5946}, {0.8869, 7.3}, {0.8888, 10.5886}, {0.8931,
6.4151}, {0.8933, 10.6143}, {0.8984, 5.4952}, {0.9002,
6.1774}, {0.9026, 10.0077}, {0.9028, 10.6323}, {0.9095,
7.3}, {0.9135, 5.7788}, {0.9136, 10.6491}, {0.9226,
10.6714}, {0.9241, 10.6502}, {0.9293, 10.6856}, {0.9321,
7.3}, {0.9356, 10.6862}, {0.9402, 6.4304}, {0.9446,
5.6102}, {0.9461, 10.7102}, {0.9472, 9.4165}, {0.9532,
10.7093}, {0.9547, 7.3}, {0.955, 10.7319}, {0.9623,
10.7513}, {0.9659, 6.5377}, {0.9691, 10.7427}, {0.9724,
10.7658}, {0.9749, 10.782}, {0.9773, 7.3}, {0.9793,
5.7022}, {0.9798, 10.7785}, {0.9858, 10.7426}, {0.9864,
10.793}, {0.9937, 10.8037}, {0.9941, 10.7837}, {0.9952,
6.5377}, {1., 7.3}, {1.0024, 10.7953}, {1.0103, 5.7635}, {1.0114,
10.7916}, {1.0154, 9.8888}, {1.0214, 10.7948}, {1.0254,
10.7569}, {1.0289, 10.7035}, {1.0299, 10.7782}, {1.0327,
10.7372}, {1.0361, 6.691}, {1.0374, 10.7547}, {1.0397,
5.9398}, {1.0399, 10.7263}, {1.043, 9.9294}, {1.0433,
10.6904}, {1.0455, 10.7158}, {1.0491, 10.6823}, {1.0492,
10.5803}, {1.0493, 10.7096}, {1.0522, 10.6607}, {1.0525,
10.5688}, {1.0541, 10.5693}, {1.0545, 10.2275}, {1.0548,
10.6022}, {1.0551, 10.6358}, {1.056, 10.2713}, {1.0565,
10.3387}, {1.0566, 10.4693}, {1.0566, 10.4496}, {1.0566,
10.4084}, {1.0574, 6.7754}, {1.0599, 10.5482}, {1.061,
6.0625}, {1.0617, 9.6178}, {1.0617, 9.6105}, {1.0618,
9.5883}, {1.062, 9.5207}, {1.0627, 9.3148}, {1.0653,
8.6872}, {1.0657, 10.3331}, {1.0702, 10.6255}, {1.0743,
6.7754}, {1.0749, 10.5824}, {1.076, 8.737}, {1.0773,
10.3331}, {1.0823, 8.5491}, {1.0823, 6.1621}, {1.0842,
10.5407}, {1.0844, 8.4865}, {1.085, 8.4657}, {1.0853,
8.4587}, {1.0857, 9.9252}, {1.0861, 10.4047}, {1.0864,
9.7131}, {1.0867, 9.64}, {1.0987, 10.4172}, {1.1253,
10.1805}, {1.1617, 9.8886}, {1.2035, 9.6675}, {1.2842,
9.4229}, {1.3314, 9.0844}, {1.3673, 9.3552}, {1.3728,
7.7866}, {1.4127, 9.5395}, {1.453, 9.4643}, {1.4645,
9.7022}, {1.5226, 9.8433}, {1.5714, 9.8589}, {1.6026,
10.0763}, {1.6099, 9.5096}, {1.6494, 9.4711}, {1.6545,
10.146}, {1.6695, 10.4486}, {1.7212, 9.1818}, {1.7212, 10.6088}}};
TimeConstrained
does not work as expected? $\endgroup$