我试图通过运行迭代来最小化我的目标函数,这可能需要很长时间(可能是一天或更长时间)。如果由于某种原因我必须停止程序或者它被挂起,那么保存迭代结果的有效方法是什么,这些迭代可能一直运行到程序因某种原因停止时。
我要保存的变量是: best_obj_fun
and k_to_perturb_annealing_best
NOTE:我正在存储我的best_obj_fun
在矩阵中,我只想要最后一次迭代时的值k_to_perturb_annealing_best
请参阅以下代码中的最后一个循环,了解当程序以某种方式停止时我想要拥有的变量(例如 CTRL+C):
%% Running iterations to find minimum objective function value
accept_iter=1;
for iter=1:100
%# Choosing 'nRandomPoints' random points on the grid to sample from 'fixed_underlying_distribution'
nRandomPoints=100;
random_grid_points=randi(sqrt(nCell),[nRandomPoints,2]); %# two columns with x and y coord resp.
repeated_set_of_coord=ismember(random_grid_points,fixed_grid_coordinates,'rows'); %# find repeated sets of coordinates
while any(repeated_set_of_coord)
random_grid_points(repeated_set_of_coord,:)=randi(sqrt(nCell),[sum(repeated_set_of_coord),2]); %# create new set of coordinates
repeated_set_of_coord(repeated_set_of_coord)=ismember(random_grid_points(...
repeated_set_of_coord,:),fixed_grid_coordinates,'rows'); %# check the new coordinates
end
linear_index_for_perturbation=sub2ind([sqrt(nCell),sqrt(nCell)],random_grid_points(:,1),...
random_grid_points(:,2)); %# get a linear index into matrix to be perturbed
k_to_perturb_annealing_initial=k_sisim_for_annealing;
k_to_perturb_annealing_initial(linear_index_for_perturbation)=emprand(k_20_sampledata_sisim,nRandomPoints,1);
%# computing the value of objective function for perturbed perm values
[new_obj_fun,new_k_geomean]=obj_fun_for_gibbs_sampling(k_to_perturb_annealing_initial,x_200x200_gslib_format_vector...
,y_200x200_gslib_format_vector,gamma_k_underlying,mean_gamma_k_underlying,k_eff_WT);
if new_obj_fun < best_obj_fun(accept_iter)
best_obj_fun(accept_iter+1)=new_obj_fun;
k_to_perturb_annealing_best=k_to_perturb_annealing_initial;
best_k_geomean=new_k_geomean;
accept_iter=accept_iter+1;
end
end