I am trying to minimize my objective function by doing iterations, and this can take a lot of time (maybe a day or more). If for some reason I have to stop the program or it freezes, then what is an effective way to save the results of iterations that could be executed until the program stops for some reason.
The variables I want to save are: best_obj_fun andk_to_perturb_annealing_best
NOTE: I save best_obj_funmine in the matrix, and I only need the value during the last iteration fork_to_perturb_annealing_best
See the last loop in the following code for the variables that I want to have when the program is somehow stopped (for example, CTRL + C):
%% Running iterations to find minimum objective function value
accept_iter=1;
for iter=1:100
%
nRandomPoints=100;
random_grid_points=randi(sqrt(nCell),[nRandomPoints,2]); %
repeated_set_of_coord=ismember(random_grid_points,fixed_grid_coordinates,'rows'); %
while any(repeated_set_of_coord)
random_grid_points(repeated_set_of_coord,:)=randi(sqrt(nCell),[sum(repeated_set_of_coord),2]); %
repeated_set_of_coord(repeated_set_of_coord)=ismember(random_grid_points(...
repeated_set_of_coord,:),fixed_grid_coordinates,'rows'); %
end
linear_index_for_perturbation=sub2ind([sqrt(nCell),sqrt(nCell)],random_grid_points(:,1),...
random_grid_points(:,2)); %
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);
%
[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
user238469