void enkf_tui_run_exp(void * enkf_main) { const int ens_size = enkf_main_get_ensemble_size( enkf_main ); bool_vector_type * iactive = bool_vector_alloc(0,false); state_enum init_state = ANALYZED; int start_report = 0; int init_step_parameters = 0; { char * prompt = util_alloc_sprintf("Which realizations to simulate (Ex: 1,3-5) <Enter for all> [M to return to menu] : " , ens_size); char * select_string; util_printf_prompt(prompt , PROMPT_LEN , '=' , "=> "); select_string = util_alloc_stdin_line(); enkf_tui_util_sscanf_active_list( iactive , select_string , ens_size); util_safe_free( select_string ); free( prompt ); } if (bool_vector_count_equal(iactive , true)) enkf_main_run_exp(enkf_main , iactive , true , init_step_parameters , start_report , init_state, true); bool_vector_free(iactive); }
matrix_type * matrix_alloc_column_compressed_copy(const matrix_type * src, const bool_vector_type * mask) { if (bool_vector_size( mask ) != matrix_get_columns( src )) util_abort("%s: size mismatch. Src matrix has %d rows mask has:%d elements\n", __func__ , matrix_get_rows( src ) , bool_vector_size( mask )); { int target_columns = bool_vector_count_equal( mask , true ); matrix_type * target = matrix_alloc( matrix_get_rows( src ) , target_columns ); matrix_column_compressed_memcpy( target , src , mask ); return target; } }
void rml_enkf_common_recover_state( const matrix_type * state , matrix_type * A , const bool_vector_type * ens_mask ) { const int ens_size = bool_vector_size( ens_mask ); const int active_size = bool_vector_count_equal( ens_mask , true ); const int rows = matrix_get_rows( state ); matrix_resize( A , rows , active_size , false ); { int active_index = 0; for (int iens = 0; iens < ens_size; iens++) { if (bool_vector_iget( ens_mask , iens )) matrix_copy_column( A , state , active_index , iens ); } } }
void matrix_column_compressed_memcpy(matrix_type * target, const matrix_type * src, const bool_vector_type * mask) { if (bool_vector_count_equal( mask , true ) != matrix_get_columns( target )) util_abort("%s: size mismatch. \n",__func__); if (bool_vector_size( mask ) != matrix_get_columns( src)) util_abort("%s: size mismatch. \n",__func__); { int target_col = 0; int src_col; for (src_col = 0; src_col < bool_vector_size( mask ); src_col++) { if (bool_vector_iget( mask , src_col)) { matrix_copy_column( target , src , target_col , src_col); target_col++; } } } }
void test_index_list() { int_vector_type * index_list = int_vector_alloc( 0 , 0 ); int_vector_append( index_list , 10 ); int_vector_append( index_list , 20 ); int_vector_append( index_list , 30 ); { bool_vector_type * mask = int_vector_alloc_mask( index_list ); test_assert_false( bool_vector_get_default( mask )); test_assert_int_equal( 31 , bool_vector_size( mask )); test_assert_true( bool_vector_iget( mask , 10 )); test_assert_true( bool_vector_iget( mask , 20 )); test_assert_true( bool_vector_iget( mask , 30 )); test_assert_int_equal( 3 , bool_vector_count_equal( mask , true )); bool_vector_free( mask ); } int_vector_free( index_list ); }
int stepwise_get_n_active( stepwise_type * stepwise ) { return bool_vector_count_equal( stepwise->active_set , true); }
static double stepwise_estimate__( stepwise_type * stepwise , bool_vector_type * active_rows) { matrix_type * X; matrix_type * E; matrix_type * Y; double y_mean = 0; int nvar = matrix_get_columns( stepwise->X0 ); int nsample = matrix_get_rows( stepwise->X0 ); nsample = bool_vector_count_equal( active_rows , true ); nvar = bool_vector_count_equal( stepwise->active_set , true ); matrix_set( stepwise->beta , 0 ); // It is essential to make sure that old finite values in the beta0 vector do not hang around. /* Extracting the data used for regression, and storing them in the temporary local matrices X and Y. Selecting data is based both on which varibles are active (stepwise->active_set) and which rows should be used for regression, versus which should be used for validation (@active_rows). */ if ((nsample < matrix_get_rows( stepwise->X0 )) || (nvar < matrix_get_columns( stepwise->X0 ))) { X = matrix_alloc( nsample , nvar ); E = matrix_alloc( nsample , nvar ); Y = matrix_alloc( nsample , 1); { int icol,irow; // Running over all values. int arow,acol; // Running over active values. arow = 0; for (irow = 0; irow < matrix_get_rows( stepwise->X0 ); irow++) { if (bool_vector_iget( active_rows , irow )) { acol = 0; for (icol = 0; icol < matrix_get_columns( stepwise->X0 ); icol++) { if (bool_vector_iget( stepwise->active_set , icol )) { matrix_iset( X , arow , acol , matrix_iget( stepwise->X0 , irow , icol )); matrix_iset( E , arow , acol , matrix_iget( stepwise->E0 , irow , icol )); acol++; } } matrix_iset( Y , arow , 0 , matrix_iget( stepwise->Y0 , irow , 0 )); arow++; } } } } else { X = matrix_alloc_copy( stepwise->X0 ); E = matrix_alloc_copy( stepwise->E0 ); Y = matrix_alloc_copy( stepwise->Y0 ); } { if (stepwise->X_mean != NULL) matrix_free( stepwise->X_mean); stepwise->X_mean = matrix_alloc( 1 , nvar ); if (stepwise->X_norm != NULL) matrix_free( stepwise->X_norm); stepwise->X_norm = matrix_alloc( 1 , nvar ); matrix_type * beta = matrix_alloc( nvar , 1); /* This is the beta vector as estimated from the OLS estimator. */ regression_augmented_OLS( X , Y , E, beta ); /* In this code block the beta/tmp_beta vector which is dense with fewer elements than the full model is scattered into the beta0 vector which has full size and @nvar elements. */ { int ivar,avar; avar = 0; for (ivar = 0; ivar < matrix_get_columns( stepwise->X0 ); ivar++) { if (bool_vector_iget( stepwise->active_set , ivar )) { matrix_iset( stepwise->beta , ivar , 0 , matrix_iget( beta , avar , 0)); avar++; } } } matrix_free( beta ); } matrix_free( X ); matrix_free( E ); matrix_free( Y ); return y_mean; }
void stepwise_estimate( stepwise_type * stepwise , double deltaR2_limit , int CV_blocks) { int nvar = matrix_get_columns( stepwise->X0 ); int nsample = matrix_get_rows( stepwise->X0 ); double currentR2 = -1; bool_vector_type * active_rows = bool_vector_alloc( nsample , true ); /*Reset beta*/ for (int i = 0; i < nvar; i++) { matrix_iset(stepwise->beta, i , 0 , 0.0); } bool_vector_set_all( stepwise->active_set , false ); double MSE_min = 10000000; double Prev_MSE_min = MSE_min; double minR2 = -1; while (true) { int best_var = 0; Prev_MSE_min = MSE_min; /* Go through all the inactive variables, and calculate the resulting prediction error IF this particular variable is added; keep track of the variable which gives the lowest prediction error. */ for (int ivar = 0; ivar < nvar; ivar++) { if (!bool_vector_iget( stepwise->active_set , ivar)) { double newR2 = stepwise_test_var(stepwise , ivar , CV_blocks); if ((minR2 < 0) || (newR2 < minR2)) { minR2 = newR2; best_var = ivar; } } } /* If the best relative improvement in prediction error is better than @deltaR2_limit, the corresponding variable is added to the active set, and we return to repeat the loop one more time. Otherwise we just exit. */ { MSE_min = minR2; double deltaR2 = MSE_min / Prev_MSE_min; if (( currentR2 < 0) || deltaR2 < deltaR2_limit) { bool_vector_iset( stepwise->active_set , best_var , true ); currentR2 = minR2; bool_vector_set_all(active_rows, true); stepwise_estimate__( stepwise , active_rows ); } else { /* The gain in prediction error is so small that we just leave the building. */ /* NB! Need one final compuation of beta (since the test_var function does not reset the last tested beta value !) */ bool_vector_set_all(active_rows, true); stepwise_estimate__( stepwise , active_rows ); break; } if (bool_vector_count_equal( stepwise->active_set , true) == matrix_get_columns( stepwise->X0 )) { stepwise_estimate__( stepwise , active_rows ); break; /* All variables are active. */ } } } stepwise_set_R2(stepwise, currentR2); bool_vector_free( active_rows ); }
void enkf_tui_run_manual_load__( void * arg ) { enkf_main_type * enkf_main = enkf_main_safe_cast( arg ); enkf_fs_type * fs = enkf_main_get_fs( enkf_main ); const int last_report = -1; const int ens_size = enkf_main_get_ensemble_size( enkf_main ); int step1,step2; bool_vector_type * iactive = bool_vector_alloc( 0 , false ); run_mode_type run_mode = ENSEMBLE_EXPERIMENT; enkf_main_init_run(enkf_main , run_mode); /* This is ugly */ step1 = 0; step2 = last_report; /** Observe that for the summary data it will load all the available data anyway. */ { char * prompt = util_alloc_sprintf("Which realizations to load (Ex: 1,3-5) <Enter for all> [M to return to menu] : [ensemble size:%d] : " , ens_size); char * select_string; util_printf_prompt(prompt , PROMPT_LEN , '=' , "=> "); select_string = util_alloc_stdin_line(); enkf_tui_util_sscanf_active_list( iactive , select_string , ens_size ); util_safe_free( select_string ); free( prompt ); } if (bool_vector_count_equal( iactive , true )) { int iens; arg_pack_type ** arg_list = util_calloc( ens_size , sizeof * arg_list ); thread_pool_type * tp = thread_pool_alloc( 4 , true ); /* num_cpu - HARD coded. */ for (iens = 0; iens < ens_size; iens++) { arg_pack_type * arg_pack = arg_pack_alloc(); arg_list[iens] = arg_pack; if (bool_vector_iget(iactive , iens)) { enkf_state_type * enkf_state = enkf_main_iget_state( enkf_main , iens ); arg_pack_append_ptr( arg_pack , enkf_state); /* 0: */ arg_pack_append_ptr( arg_pack , fs ); /* 1: */ arg_pack_append_int( arg_pack , step1 ); /* 2: This will be the load start parameter for the run_info struct. */ arg_pack_append_int( arg_pack , step1 ); /* 3: Step1 */ arg_pack_append_int( arg_pack , step2 ); /* 4: Step2 For summary data it will load the whole goddamn thing anyway.*/ arg_pack_append_bool( arg_pack , true ); /* 5: Interactive */ arg_pack_append_owned_ptr( arg_pack , stringlist_alloc_new() , stringlist_free__); /* 6: List of interactive mode messages. */ thread_pool_add_job( tp , enkf_state_load_from_forward_model_mt , arg_pack); } } thread_pool_join( tp ); thread_pool_free( tp ); printf("\n"); { qc_module_type * qc_module = enkf_main_get_qc_module( enkf_main ); runpath_list_type * runpath_list = qc_module_get_runpath_list( qc_module ); for (iens = 0; iens < ens_size; iens++) { if (bool_vector_iget(iactive , iens)) { const enkf_state_type * state = enkf_main_iget_state( enkf_main , iens ); runpath_list_add( runpath_list , iens , enkf_state_get_run_path( state ) , enkf_state_get_eclbase( state )); } } qc_module_export_runpath_list( qc_module ); } for (iens = 0; iens < ens_size; iens++) { if (bool_vector_iget(iactive , iens)) { stringlist_type * msg_list = arg_pack_iget_ptr( arg_list[iens] , 6 ); if (stringlist_get_size( msg_list )) enkf_tui_display_load_msg( iens , msg_list ); } } for (iens = 0; iens < ens_size; iens++) arg_pack_free( arg_list[iens]); free( arg_list ); } bool_vector_free( iactive ); }
void enkf_tui_run_manual_load__( void * arg ) { enkf_main_type * enkf_main = enkf_main_safe_cast( arg ); const int ens_size = enkf_main_get_ensemble_size( enkf_main ); bool_vector_type * iactive = bool_vector_alloc( 0 , false ); run_mode_type run_mode = ENSEMBLE_EXPERIMENT; int iter = 0; enkf_main_init_run(enkf_main , iactive , run_mode , INIT_NONE); /* This is ugly */ { char * prompt = util_alloc_sprintf("Which realizations to load (Ex: 1,3-5) <Enter for all> [M to return to menu] : [ensemble size:%d] : " , ens_size); char * select_string; util_printf_prompt(prompt , PROMPT_LEN , '=' , "=> "); select_string = util_alloc_stdin_line(); enkf_tui_util_sscanf_active_list( iactive , select_string , ens_size ); util_safe_free( select_string ); free( prompt ); } { const model_config_type * model_config = enkf_main_get_model_config( enkf_main ); if (model_config_runpath_requires_iter( model_config )) { const char * prompt = "Which iteration to load from [0...?) : "; char * input; bool OK; util_printf_prompt(prompt , PROMPT_LEN , '=' , "=> "); input = util_alloc_stdin_line(); if (input == NULL) return; OK = util_sscanf_int( input , &iter ); free( input ); if (!OK) return; } } if (bool_vector_count_equal( iactive , true )) { stringlist_type ** realizations_msg_list = util_calloc( ens_size , sizeof * realizations_msg_list ); int iens = 0; for (; iens < ens_size; ++iens) { realizations_msg_list[iens] = stringlist_alloc_new(); } enkf_main_load_from_forward_model(enkf_main, iter , iactive, realizations_msg_list); { qc_module_type * qc_module = enkf_main_get_qc_module( enkf_main ); runpath_list_type * runpath_list = qc_module_get_runpath_list( qc_module ); for (iens = 0; iens < ens_size; ++iens) { if (bool_vector_iget(iactive , iens)) { const enkf_state_type * state = enkf_main_iget_state( enkf_main , iens ); runpath_list_add( runpath_list , iens , enkf_state_get_run_path( state ) , enkf_state_get_eclbase( state )); } } qc_module_export_runpath_list( qc_module ); } for (iens = 0; iens < ens_size; ++iens) { stringlist_type * msg_list = realizations_msg_list[iens]; if (bool_vector_iget(iactive, iens)) { if (stringlist_get_size( msg_list )) { enkf_tui_display_load_msg( iens , msg_list ); } } stringlist_free(msg_list); } free(realizations_msg_list); } bool_vector_free( iactive ); }