📄 asa_usr.c
字号:
/***********************************************************************
* Adaptive Simulated Annealing (ASA)
* Lester Ingber <ingber@ingber.com>
* Copyright (c) 1993-2007 Lester Ingber. All Rights Reserved.
* The ASA-LICENSE file must be included with ASA code.
***********************************************************************/
#define USER_ID "/* $Id: asa_usr.c,v 26.23 2007/01/31 20:13:28 ingber Exp ingber $ */"
#include "asa_usr.h"
#if MY_TEMPLATE /* MY_TEMPLATE_includes */
/* add your own include files as required */
#endif
#if ASA_LIB
static LONG_INT *asa_rand_seed;
#endif
#if ASA_SAVE
static double random_array[SHUFFLE];
#endif
#if SELF_OPTIMIZE
static LONG_INT funevals = 0;
#else
char user_exit_msg[160]; /* temp storage for exit messages */
FILE *ptr_out;
/***********************************************************************
* main
* This is a sample calling program to optimize using ASA
***********************************************************************/
#if HAVE_ANSI
#if ASA_LIB
int
asa_main (
#if ASA_TEMPLATE_LIB
double *main_cost_value,
double *main_cost_parameters, int *main_exit_code
#endif
)
#else /* ASA_LIB */
int
main (int argc, char **argv)
#endif /* ASA_LIB */
#else /* HAVE_ANSI */
#if ASA_LIB
int
asa_main (
#if ASA_TEMPLATE_LIB
main_cost_value, main_cost_parameters, main_exit_code
#endif
)
#if ASA_TEMPLATE_LIB
double *main_cost_value;
double *main_cost_parameters;
int *main_exit_code;
#endif
#else /* ASA_LIB */
int
main (argc, argv)
int argc;
char **argv;
#endif /* ASA_LIB */
#endif /* HAVE_ANSI */
{
int *exit_code;
ALLOC_INT n_param;
#if ASA_LIB
#else
int compile_cnt;
#endif
#if ASA_TEMPLATE_SAMPLE
FILE *ptr_asa;
#endif
#if ASA_TEMPLATE_ASA_OUT_PID
char pid_file[18];
int pid_int;
#endif
#if MULTI_MIN
int multi_index;
#endif
/* pointer to array storage for asa arguments */
double *parameter_lower_bound, *parameter_upper_bound, *cost_parameters,
*cost_tangents, *cost_curvature;
double cost_value;
int initialize_parameters_value;
/* the number of parameters to optimize */
ALLOC_INT *parameter_dimension;
/* pointer to array storage for parameter type flags */
int *parameter_int_real;
/* valid flag for cost function */
int *cost_flag;
/* seed for random number generator */
LONG_INT *rand_seed;
USER_DEFINES *USER_OPTIONS;
#if OPTIONS_FILE
FILE *ptr_options;
char read_option[80];
char read_if[4], read_FALSE[6], read_comm1[3], read_ASA_SAVE[9],
read_comm2[3];
int read_int;
#if INT_LONG
LONG_INT read_long;
#endif
double read_double;
#endif
#if MY_TEMPLATE /* MY_TEMPLATE_main_decl */
/* add some declarations if required */
#endif
#if ASA_TEMPLATE_MULTIPLE
int n_asa, n_trajectory;
ALLOC_INT index;
#if HAVE_ANSI
char asa_file[8] = "asa_x_y";
#else
char asa_file[8];
#endif /* HAVE_ANSI */
#endif /* ASA_TEMPLATE_MULTIPLE */
#if ASA_TEMPLATE_MULTIPLE
#if HAVE_ANSI
#else
asa_file[0] = asa_file[2] = 'a';
asa_file[1] = 's';
asa_file[3] = asa_file[5] = '_';
asa_file[4] = 'x';
asa_file[6] = 'y';
asa_file[7] = '\0';
#endif /* HAVE_ANSI */
#endif /* ASA_TEMPLATE_MULTIPLE */
if ((USER_OPTIONS =
(USER_DEFINES *) calloc (1, sizeof (USER_DEFINES))) == NULL) {
strcpy (user_exit_msg, "main()/asa_main(): USER_DEFINES");
Exit_USER (user_exit_msg);
return (-2);
}
#if OPTIONAL_DATA_PTR
#if ASA_TEMPLATE
USER_OPTIONS->Asa_Data_Dim_Ptr = 256;
if ((USER_OPTIONS->Asa_Data_Ptr =
(OPTIONAL_PTR_TYPE *) calloc (USER_OPTIONS->Asa_Data_Dim_Ptr,
sizeof (OPTIONAL_PTR_TYPE))) == NULL) {
strcpy (user_exit_msg, "main()/asa_main(): USER_OPTIONS->Asa_Data_Ptr");
Exit_USER (user_exit_msg);
return (-2);
}
#endif /* ASA_TEMPLATE */
#endif /* OPTIONAL_DATA_PTR */
#if ASA_TEMPLATE_ASA_OUT_PID
pid_file[0] = 'a';
pid_file[1] = 's';
pid_file[2] = 'a';
pid_file[3] = '_';
pid_file[4] = 'u';
pid_file[5] = 's';
pid_file[6] = 'r';
pid_file[7] = '_';
pid_file[8] = 'o';
pid_file[9] = 'u';
pid_file[10] = 't';
pid_file[11] = '_';
pid_file[17] = '\0';
pid_int = getpid ();
if (pid_int < 0) {
pid_int = -pid_int;
}
if (pid_int > 99999) {
pid_file[11] = '1';
pid_int = pid_int % 100000;
}
if (pid_int < 10 && pid_int > 0) {
pid_file[12] = '0';
pid_file[13] = '0';
pid_file[14] = '0';
pid_file[15] = '0';
pid_file[16] = '0' + pid_int;
} else if (pid_int >= 10 && pid_int < 100) {
pid_file[12] = '0';
pid_file[13] = '0';
pid_file[14] = '0';
pid_file[15] = '0' + (int) (pid_int / 10);
pid_file[16] = '0' + (pid_int % 10);
} else if (pid_int >= 100 && pid_int < 1000) {
pid_file[12] = '0';
pid_file[13] = '0';
pid_file[14] = '0' + (int) (pid_int / 100);
pid_file[15] = '0' + (int) ((pid_int % 100) / 10);
pid_file[16] = '0' + ((pid_int % 100) % 10);
} else if (pid_int >= 1000 && pid_int < 10000) {
pid_file[12] = '0';
pid_file[13] = '0' + (int) (pid_int / 1000);
pid_file[14] = '0' + (int) ((pid_int % 1000) / 100);
pid_file[15] = '0' + (int) (((pid_int % 1000) % 100) / 10);
pid_file[16] = '0' + (((pid_int % 1000) % 100) % 10);
} else if (pid_int >= 10000 && pid_int <= 99999) {
pid_file[12] = '0' + (int) (pid_int / 10000);
pid_file[13] = '0' + (int) ((pid_int % 10000) / 1000);
pid_file[14] = '0' + (int) (((pid_int % 10000) % 1000) / 100);
pid_file[15] = '0' + (int) (((pid_int % 10000) % 1000) % 100 / 10);
pid_file[16] = '0' + ((((pid_int % 10000) % 1000) % 100) % 10);
} else {
pid_file[11] = '0';
pid_file[12] = '0';
pid_file[13] = '0';
pid_file[14] = '0';
pid_file[15] = '0';
pid_file[16] = '0';
}
ptr_out = fopen (pid_file, "w");
#else /* ASA_TEMPLATE_ASA_OUT_PID */
/* open the output file */
/* set "w" to "a" to save data from multiple runs */
#if ASA_SAVE
if (!strcmp (USER_OUT, "STDOUT")) {
#if INCL_STDOUT
ptr_out = stdout;
#endif /* INCL_STDOUT */
} else {
ptr_out = fopen (USER_OUT, "a");
}
#else
if (!strcmp (USER_OUT, "STDOUT")) {
#if INCL_STDOUT
ptr_out = stdout;
#endif /* INCL_STDOUT */
} else {
ptr_out = fopen (USER_OUT, "w");
}
#endif
#endif /* ASA_TEMPLATE_ASA_OUT_PID */
#if INCL_STDOUT
/* use this instead if you want output to stdout */
#endif /* INCL_STDOUT */
#if FALSE
#if INCL_STDOUT
ptr_out = stdout;
#endif /* INCL_STDOUT */
#endif
fprintf (ptr_out, "%s\n\n", USER_ID);
#if ASA_LIB
#else
/* print out compile options set by user in Makefile */
if (argc > 1) {
fprintf (ptr_out, "CC = %s\n", argv[1]);
for (compile_cnt = 2; compile_cnt < argc; ++compile_cnt) {
fprintf (ptr_out, "\t%s\n", argv[compile_cnt]);
}
fprintf (ptr_out, "\n");
}
#endif
#if TIME_CALC
/* print starting time */
print_time ("start", ptr_out);
#endif
fflush (ptr_out);
if ((rand_seed = (ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (user_exit_msg, "main()/asa_main(): rand_seed");
Exit_USER (user_exit_msg);
return (-2);
}
#if ASA_LIB
*rand_seed = (asa_rand_seed ? *asa_rand_seed : (LONG_INT) 696969);
#else
*rand_seed = 696969;
#endif
/* initialize random number generator with first call */
resettable_randflt (rand_seed, 1);
/* Initialize the users parameters, allocating space, etc.
Note that the default is to have asa generate the initial
cost_parameters that satisfy the user's constraints. */
if ((parameter_dimension =
(ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (user_exit_msg, "main()/asa_main(): parameter_dimension");
Exit_USER (user_exit_msg);
return (-2);
}
if ((exit_code = (int *) calloc (1, sizeof (int))) == NULL) {
strcpy (user_exit_msg, "main()/asa_main(): exit_code");
Exit_USER (user_exit_msg);
return (-2);
}
if ((cost_flag = (int *) calloc (1, sizeof (int))) == NULL) {
strcpy (user_exit_msg, "main()/asa_main(): cost_flag");
Exit_USER (user_exit_msg);
return (-2);
}
#if OPTIONS_FILE
/* Test to see if asa_opt is in correct directory.
This is useful for some PC and Mac compilers. */
if ((ptr_options = fopen ("asa_opt", "r")) == NULL) {
fprintf (ptr_out, "\n\n*** fopen asa_opt failed *** \n\n");
fflush (ptr_out);
#if INCL_STDOUT
printf ("\n\n*** EXIT fopen asa_opt failed *** \n\n");
#endif /* INCL_STDOUT */
return (-6);
}
fscanf (ptr_options, "%s%s%s%s%s",
read_if, read_FALSE, read_comm1, read_ASA_SAVE, read_comm2);
if (strcmp (read_if, "#if") || strcmp (read_FALSE, "FALSE") ||
strcmp (read_comm1, "/*") || strcmp (read_ASA_SAVE, "ASA_SAVE") ||
strcmp (read_comm2, "*/")) {
fprintf (ptr_out, "\n\n*** not asa_opt for this version *** \n\n");
fflush (ptr_out);
#if INCL_STDOUT
printf ("\n\n*** EXIT not asa_opt for this version *** \n\n");
#endif /* INCL_STDOUT */
return (-6);
}
#if INT_LONG
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%ld", &read_long);
USER_OPTIONS->Limit_Acceptances = read_long;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%ld", &read_long);
USER_OPTIONS->Limit_Generated = read_long;
#else
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Limit_Acceptances = read_int;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Limit_Generated = read_int;
#endif
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Limit_Invalid_Generated_States = read_int;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%lf", &read_double);
USER_OPTIONS->Accepted_To_Generated_Ratio = read_double;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%lf", &read_double);
USER_OPTIONS->Cost_Precision = read_double;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Maximum_Cost_Repeat = read_int;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Number_Cost_Samples = read_int;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%lf", &read_double);
USER_OPTIONS->Temperature_Ratio_Scale = read_double;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%lf", &read_double);
USER_OPTIONS->Cost_Parameter_Scale_Ratio = read_double;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%lf", &read_double);
USER_OPTIONS->Temperature_Anneal_Scale = read_double;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Include_Integer_Parameters = read_int;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->User_Initial_Parameters = read_int;
#if INT_ALLOC
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Sequential_Parameters = read_int;
#else
#if INT_LONG
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%ld", &read_long);
USER_OPTIONS->Sequential_Parameters = read_long;
#else
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Sequential_Parameters = read_int;
#endif
#endif
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%lf", &read_double);
USER_OPTIONS->Initial_Parameter_Temperature = read_double;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Acceptance_Frequency_Modulus = read_int;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Generated_Frequency_Modulus = read_int;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Reanneal_Cost = read_int;
fscanf (ptr_options, "%s", read_option);
fscanf (ptr_options, "%d", &read_int);
USER_OPTIONS->Reanneal_Parameters = read_int;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -