/** * Generates the different files and folder needed by the logger to store the * data if theses don't already exist */ static void logger_bbob2009_initialize(logger_bbob2009_t *data, coco_problem_t *inner_problem) { /* Creates/opens the data and index files */ char dataFile_path[COCO_PATH_MAX] = { 0 }; /* relative path to the .dat file from where the .info file is */ char folder_path[COCO_PATH_MAX] = { 0 }; char tmpc_funId[3]; /* serves to extract the function id as a char *. There should be a better way of doing this! */ char tmpc_dim[3]; /* serves to extract the dimension as a char *. There should be a better way of doing this! */ char indexFile_prefix[10] = "bbobexp"; /* TODO (minor): make the prefix bbobexp a parameter that the user can modify */ assert(data != NULL); assert(inner_problem != NULL); assert(inner_problem->problem_id != NULL); sprintf(tmpc_funId, "%d", coco_problem_get_suite_dep_function_id(inner_problem)); sprintf(tmpc_dim, "%lu", (unsigned long) inner_problem->number_of_variables); /* prepare paths and names */ strncpy(dataFile_path, "data_f", COCO_PATH_MAX); strncat(dataFile_path, tmpc_funId, COCO_PATH_MAX - strlen(dataFile_path) - 1); coco_join_path(folder_path, sizeof(folder_path), data->path, dataFile_path, NULL); coco_create_path(folder_path); strncat(dataFile_path, "/bbobexp_f", COCO_PATH_MAX - strlen(dataFile_path) - 1); strncat(dataFile_path, tmpc_funId, COCO_PATH_MAX - strlen(dataFile_path) - 1); strncat(dataFile_path, "_DIM", COCO_PATH_MAX - strlen(dataFile_path) - 1); strncat(dataFile_path, tmpc_dim, COCO_PATH_MAX - strlen(dataFile_path) - 1); /* index/info file */ logger_bbob2009_openIndexFile(data, data->path, indexFile_prefix, tmpc_funId, dataFile_path); fprintf(data->index_file, ", %ld", coco_problem_get_suite_dep_instance_id(inner_problem)); /* data files */ /* TODO: definitely improvable but works for now */ strncat(dataFile_path, "_i", COCO_PATH_MAX - strlen(dataFile_path) - 1); strncat(dataFile_path, bbob2009_infoFile_firstInstance_char, COCO_PATH_MAX - strlen(dataFile_path) - 1); logger_bbob2009_open_dataFile(&(data->fdata_file), data->path, dataFile_path, ".dat"); fprintf(data->fdata_file, bbob2009_file_header_str, data->optimal_fvalue); logger_bbob2009_open_dataFile(&(data->tdata_file), data->path, dataFile_path, ".tdat"); fprintf(data->tdata_file, bbob2009_file_header_str, data->optimal_fvalue); logger_bbob2009_open_dataFile(&(data->rdata_file), data->path, dataFile_path, ".rdat"); fprintf(data->rdata_file, bbob2009_file_header_str, data->optimal_fvalue); /* TODO: manage duplicate filenames by either using numbers or raising an error */ /* The coco_create_unique_path() function is available now! */ data->is_initialized = 1; }
static void logger_bbob2009_open_dataFile(FILE **target_file, const char *path, const char *dataFile_path, const char *file_extension) { char file_path[COCO_PATH_MAX] = { 0 }; char relative_filePath[COCO_PATH_MAX] = { 0 }; int errnum; strncpy(relative_filePath, dataFile_path, COCO_PATH_MAX - strlen(relative_filePath) - 1); strncat(relative_filePath, file_extension, COCO_PATH_MAX - strlen(relative_filePath) - 1); coco_join_path(file_path, sizeof(file_path), path, relative_filePath, NULL); if (*target_file == NULL) { *target_file = fopen(file_path, "a+"); errnum = errno; if (*target_file == NULL) { logger_bbob2009_error_io(*target_file, errnum); } } }
/** * Creates the index file fileName_prefix+problem_id+file_extension in * folde_path */ static void logger_bbob2009_openIndexFile(logger_bbob2009_t *data, const char *folder_path, const char *indexFile_prefix, const char *function_id, const char *dataFile_path) { /* to add the instance number TODO: this should be done outside to avoid redoing this for the .*dat files */ char used_dataFile_path[COCO_PATH_MAX] = { 0 }; int errnum, newLine; /* newLine is at 1 if we need a new line in the info file */ char function_id_char[3]; /* TODO: consider adding them to data */ char file_name[COCO_PATH_MAX] = { 0 }; char file_path[COCO_PATH_MAX] = { 0 }; FILE **target_file; FILE *tmp_file; strncpy(used_dataFile_path, dataFile_path, COCO_PATH_MAX - strlen(used_dataFile_path) - 1); if (bbob2009_infoFile_firstInstance == 0) { bbob2009_infoFile_firstInstance = data->instance_id; } sprintf(function_id_char, "%d", data->function_id); sprintf(bbob2009_infoFile_firstInstance_char, "%ld", bbob2009_infoFile_firstInstance); target_file = &(data->index_file); tmp_file = NULL; /* to check whether the file already exists. Don't want to use target_file */ strncpy(file_name, indexFile_prefix, COCO_PATH_MAX - strlen(file_name) - 1); strncat(file_name, "_f", COCO_PATH_MAX - strlen(file_name) - 1); strncat(file_name, function_id_char, COCO_PATH_MAX - strlen(file_name) - 1); strncat(file_name, "_i", COCO_PATH_MAX - strlen(file_name) - 1); strncat(file_name, bbob2009_infoFile_firstInstance_char, COCO_PATH_MAX - strlen(file_name) - 1); strncat(file_name, ".info", COCO_PATH_MAX - strlen(file_name) - 1); coco_join_path(file_path, sizeof(file_path), folder_path, file_name, NULL); if (*target_file == NULL) { tmp_file = fopen(file_path, "r"); /* to check for existence */ if ((tmp_file) &&(bbob2009_current_dim == data->number_of_variables) && (bbob2009_current_funId == data->function_id)) { /* new instance of current funId and current dim */ newLine = 0; *target_file = fopen(file_path, "a+"); if (*target_file == NULL) { errnum = errno; logger_bbob2009_error_io(*target_file, errnum); } fclose(tmp_file); } else { /* either file doesn't exist (new funId) or new Dim */ /* check that the dim was not already present earlier in the file, if so, create a new info file */ if (bbob2009_current_dim != data->number_of_variables) { int i, j; for (i = 0; i < bbob2009_number_of_dimensions && bbob2009_dimensions_in_current_infoFile[i] != 0 && bbob2009_dimensions_in_current_infoFile[i] != data->number_of_variables; i++) { ; /* checks whether dimension already present in the current infoFile */ } if (i < bbob2009_number_of_dimensions && bbob2009_dimensions_in_current_infoFile[i] == 0) { /* new dimension seen for the first time */ bbob2009_dimensions_in_current_infoFile[i] = data->number_of_variables; newLine = 1; } else { if (i < bbob2009_number_of_dimensions) { /* dimension already present, need to create a new file */ newLine = 0; file_path[strlen(file_path) - strlen(bbob2009_infoFile_firstInstance_char) - 7] = 0; /* truncate the instance part */ bbob2009_infoFile_firstInstance = data->instance_id; sprintf(bbob2009_infoFile_firstInstance_char, "%ld", bbob2009_infoFile_firstInstance); strncat(file_path, "_i", COCO_PATH_MAX - strlen(file_name) - 1); strncat(file_path, bbob2009_infoFile_firstInstance_char, COCO_PATH_MAX - strlen(file_name) - 1); strncat(file_path, ".info", COCO_PATH_MAX - strlen(file_name) - 1); } else {/*we have all dimensions*/ newLine = 1; } for (j = 0; j < bbob2009_number_of_dimensions; j++) { /* new info file, reinitialize list of dims */ bbob2009_dimensions_in_current_infoFile[j] = 0; } bbob2009_dimensions_in_current_infoFile[i] = data->number_of_variables; } } *target_file = fopen(file_path, "a+"); /* in any case, we append */ if (*target_file == NULL) { errnum = errno; logger_bbob2009_error_io(*target_file, errnum); } if (tmp_file) { /* File already exists, new dim so just a new line. ALso, close the tmp_file */ if (newLine) { fprintf(*target_file, "\n"); } fclose(tmp_file); } fprintf(*target_file, "funcId = %d, DIM = %lu, Precision = %.3e, algId = '%s'\n", (int) strtol(function_id, NULL, 10), data->number_of_variables, pow(10, -8), data->alg_name); fprintf(*target_file, "%%\n"); strncat(used_dataFile_path, "_i", COCO_PATH_MAX - strlen(used_dataFile_path) - 1); strncat(used_dataFile_path, bbob2009_infoFile_firstInstance_char, COCO_PATH_MAX - strlen(used_dataFile_path) - 1); fprintf(*target_file, "%s.dat", used_dataFile_path); /* dataFile_path does not have the extension */ bbob2009_current_dim = data->number_of_variables; bbob2009_current_funId = data->function_id; } } }
/** * @brief Initializes the biobjective logger. */ static coco_problem_t *logger_biobj(coco_observer_t *observer, coco_problem_t *inner_problem) { coco_problem_t *problem; logger_biobj_data_t *logger_biobj; observer_biobj_data_t *observer_biobj; const char nondom_folder_name[] = "archive"; char *path_name, *file_name = NULL, *prefix; size_t i; if (inner_problem->number_of_objectives != 2) { coco_error("logger_biobj(): The bi-objective logger cannot log a problem with %d objective(s)", inner_problem->number_of_objectives); return NULL; /* Never reached. */ } logger_biobj = (logger_biobj_data_t *) coco_allocate_memory(sizeof(*logger_biobj)); logger_biobj->observer = observer; logger_biobj->number_of_evaluations = 0; logger_biobj->number_of_variables = inner_problem->number_of_variables; logger_biobj->number_of_objectives = inner_problem->number_of_objectives; logger_biobj->suite_dep_instance = inner_problem->suite_dep_instance; observer_biobj = (observer_biobj_data_t *) observer->data; /* Copy values from the observes that you might need even if they do not exist any more */ logger_biobj->log_nondom_mode = observer_biobj->log_nondom_mode; logger_biobj->compute_indicators = observer_biobj->compute_indicators; logger_biobj->precision_x = observer->precision_x; logger_biobj->precision_f = observer->precision_f; if (((observer_biobj->log_vars_mode == LOG_VARS_LOW_DIM) && (inner_problem->number_of_variables > 5)) || (observer_biobj->log_vars_mode == LOG_VARS_NEVER)) logger_biobj->log_vars = 0; else logger_biobj->log_vars = 1; /* Initialize logging of nondominated solutions into the archive file */ if (logger_biobj->log_nondom_mode != LOG_NONDOM_NONE) { /* Create the path to the file */ path_name = coco_allocate_string(COCO_PATH_MAX); memcpy(path_name, observer->result_folder, strlen(observer->result_folder) + 1); coco_join_path(path_name, COCO_PATH_MAX, nondom_folder_name, NULL); coco_create_directory(path_name); /* Construct file name */ prefix = coco_remove_from_string(inner_problem->problem_id, "_i", "_d"); if (logger_biobj->log_nondom_mode == LOG_NONDOM_ALL) file_name = coco_strdupf("%s_nondom_all.adat", prefix); else if (logger_biobj->log_nondom_mode == LOG_NONDOM_FINAL) file_name = coco_strdupf("%s_nondom_final.adat", prefix); coco_join_path(path_name, COCO_PATH_MAX, file_name, NULL); if (logger_biobj->log_nondom_mode != LOG_NONDOM_NONE) coco_free_memory(file_name); coco_free_memory(prefix); /* Open and initialize the archive file */ logger_biobj->archive_file = fopen(path_name, "a"); if (logger_biobj->archive_file == NULL) { coco_error("logger_biobj() failed to open file '%s'.", path_name); return NULL; /* Never reached */ } coco_free_memory(path_name); /* Output header information */ fprintf(logger_biobj->archive_file, "%% instance = %ld, name = %s\n", inner_problem->suite_dep_instance, inner_problem->problem_name); if (logger_biobj->log_vars) { fprintf(logger_biobj->archive_file, "%% function evaluation | %lu objectives | %lu variables\n", inner_problem->number_of_objectives, inner_problem->number_of_variables); } else { fprintf(logger_biobj->archive_file, "%% function evaluation | %lu objectives \n", inner_problem->number_of_objectives); } } /* Initialize the AVL trees */ logger_biobj->archive_tree = avl_tree_construct((avl_compare_t) avl_tree_compare_by_last_objective, (avl_free_t) logger_biobj_node_free); logger_biobj->buffer_tree = avl_tree_construct((avl_compare_t) avl_tree_compare_by_time_stamp, NULL); problem = coco_problem_transformed_allocate(inner_problem, logger_biobj, logger_biobj_free); problem->evaluate_function = logger_biobj_evaluate; /* Initialize the indicators */ if (logger_biobj->compute_indicators) { for (i = 0; i < OBSERVER_BIOBJ_NUMBER_OF_INDICATORS; i++) logger_biobj->indicators[i] = logger_biobj_indicator(logger_biobj, inner_problem, observer_biobj_indicators[i]); observer_biobj->previous_function = (long) inner_problem->suite_dep_function; } return problem; }
/** * @brief Initializes the indicator with name indicator_name. * * Opens files for writing and resets counters. */ static logger_biobj_indicator_t *logger_biobj_indicator(const logger_biobj_data_t *logger, const coco_problem_t *problem, const char *indicator_name) { coco_observer_t *observer; observer_biobj_data_t *observer_biobj; logger_biobj_indicator_t *indicator; char *prefix, *file_name, *path_name; int info_file_exists = 0; indicator = (logger_biobj_indicator_t *) coco_allocate_memory(sizeof(*indicator)); observer = logger->observer; observer_biobj = (observer_biobj_data_t *) observer->data; indicator->name = coco_strdup(indicator_name); indicator->best_value = suite_biobj_get_best_value(indicator->name, problem->problem_id); indicator->next_target_id = 0; indicator->target_hit = 0; indicator->current_value = 0; indicator->additional_penalty = 0; indicator->overall_value = 0; /* Prepare the info file */ path_name = coco_allocate_string(COCO_PATH_MAX); memcpy(path_name, observer->result_folder, strlen(observer->result_folder) + 1); coco_create_directory(path_name); file_name = coco_strdupf("%s_%s.info", problem->problem_type, indicator_name); coco_join_path(path_name, COCO_PATH_MAX, file_name, NULL); info_file_exists = coco_file_exists(path_name); indicator->info_file = fopen(path_name, "a"); if (indicator->info_file == NULL) { coco_error("logger_biobj_indicator() failed to open file '%s'.", path_name); return NULL; /* Never reached */ } coco_free_memory(file_name); coco_free_memory(path_name); /* Prepare the tdat file */ path_name = coco_allocate_string(COCO_PATH_MAX); memcpy(path_name, observer->result_folder, strlen(observer->result_folder) + 1); coco_join_path(path_name, COCO_PATH_MAX, problem->problem_type, NULL); coco_create_directory(path_name); prefix = coco_remove_from_string(problem->problem_id, "_i", "_d"); file_name = coco_strdupf("%s_%s.tdat", prefix, indicator_name); coco_join_path(path_name, COCO_PATH_MAX, file_name, NULL); indicator->tdat_file = fopen(path_name, "a"); if (indicator->tdat_file == NULL) { coco_error("logger_biobj_indicator() failed to open file '%s'.", path_name); return NULL; /* Never reached */ } coco_free_memory(file_name); coco_free_memory(path_name); /* Prepare the dat file */ path_name = coco_allocate_string(COCO_PATH_MAX); memcpy(path_name, observer->result_folder, strlen(observer->result_folder) + 1); coco_join_path(path_name, COCO_PATH_MAX, problem->problem_type, NULL); coco_create_directory(path_name); file_name = coco_strdupf("%s_%s.dat", prefix, indicator_name); coco_join_path(path_name, COCO_PATH_MAX, file_name, NULL); indicator->dat_file = fopen(path_name, "a"); if (indicator->dat_file == NULL) { coco_error("logger_biobj_indicator() failed to open file '%s'.", path_name); return NULL; /* Never reached */ } /* Output header information to the info file */ if (!info_file_exists) { /* Output algorithm name */ fprintf(indicator->info_file, "algorithm = '%s', indicator = '%s', folder = '%s'\n%% %s", observer->algorithm_name, indicator_name, problem->problem_type, observer->algorithm_info); } if (observer_biobj->previous_function != problem->suite_dep_function) { fprintf(indicator->info_file, "\nfunction = %2lu, ", problem->suite_dep_function); fprintf(indicator->info_file, "dim = %2lu, ", problem->number_of_variables); fprintf(indicator->info_file, "%s", file_name); } coco_free_memory(prefix); coco_free_memory(file_name); coco_free_memory(path_name); /* Output header information to the dat file */ fprintf(indicator->dat_file, "%%\n%% index = %ld, name = %s\n", problem->suite_dep_index, problem->problem_name); fprintf(indicator->dat_file, "%% instance = %ld, reference value = %.*e\n", problem->suite_dep_instance, logger->precision_f, indicator->best_value); fprintf(indicator->dat_file, "%% function evaluation | indicator value | target hit\n"); /* Output header information to the tdat file */ fprintf(indicator->tdat_file, "%%\n%% index = %ld, name = %s\n", problem->suite_dep_index, problem->problem_name); fprintf(indicator->tdat_file, "%% instance = %ld, reference value = %.*e\n", problem->suite_dep_instance, logger->precision_f, indicator->best_value); fprintf(indicator->tdat_file, "%% function evaluation | indicator value\n"); return indicator; }