Reputation:
I am trying to minimize my objective function by running iterations and it could take a long time (may be a day or more). If for some reason I have to stop the program or it gets hung, what is the efficient way to save the results of the iterations that could have run till the time the program stops for some reason.
The variables I want to save are: best_obj_fun
and k_to_perturb_annealing_best
NOTE: I am storing my best_obj_fun
in matrix and I only want the value at the time of the last iteration for k_to_perturb_annealing_best
See the last loop in the following code for variables I want to have when the program is stopped somehow (e.g. 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
Upvotes: 4
Views: 1166
Reputation: 3330
If you stop the iteration without closing matlab then an easy way to do this is to use the assignin function inside your for loop. It lets you assign a value to a variable in your workspace, which means they will be there after you stop the iteration.
Upvotes: 1
Reputation: 42225
You can try using save
with the -append
option inside the loop.
Assuming you have initialized your variables before the loop begins, you can create a file before the loop starts, and update it each time. Here's an example.
A=zeros(...);%# this is an example of your variables that are initialized before the loop
save('filename','A');%# this creates a .mat file with the initialized variables
%#begin loop
for i=...
...
... %# some computations here
save('filename','A','-append') %# this just replaces the variables in the .mat file with their current values
end
%# end loop
You will have to save the entire variable to the file, i.e., you can't do
save('filename','A(i)','-append')
You can also do this every n
th iteration, instead of every iteration. If you're dealing with very large datasets and only one element changes at a time, then I'd recommend only doing it every n
iterations, or even better, writing your own binary file.
Upvotes: 3