⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asa_usr.c

📁 simulated annealing code ASA
💻 C
📖 第 1 页 / 共 5 页
字号:
#if RATIO_TEMPERATURE_SCALES
  free (USER_OPTIONS->User_Temperature_Ratio);
#endif
#if MULTI_MIN
  free (USER_OPTIONS->Multi_Cost);
  free (USER_OPTIONS->Multi_Grid);
  for (multi_index = 0; multi_index < USER_OPTIONS->Multi_Number;
       ++multi_index) {
    free (USER_OPTIONS->Multi_Params[multi_index]);
  }
  free (USER_OPTIONS->Multi_Params);
#endif /* MULTI_MIN */
  free (USER_OPTIONS);
  free (parameter_dimension);
  free (exit_code);
  free (cost_flag);
  free (parameter_lower_bound);
  free (parameter_upper_bound);
  free (cost_parameters);
  free (parameter_int_real);
  free (cost_tangents);
  free (rand_seed);
  return (0);
  /* NOTREACHED */
}
#endif /* SELF_OPTIMIZE */

/***********************************************************************
* initialize_parameters - sample parameter initialization function
*	This depends on the users cost function to optimize (minimum).
*	The routine allocates storage needed for asa. The user should
*	define the number of parameters and their ranges,
*	and make sure the initial parameters are within
*	the minimum and maximum ranges. The array
*	parameter_int_real should be REAL_TYPE (-1) for real parameters,
*	and INTEGER_TYPE (1) for integer values
***********************************************************************/
#if HAVE_ANSI
int
initialize_parameters (double *cost_parameters,
                       double *parameter_lower_bound,
                       double *parameter_upper_bound,
                       double *cost_tangents,
                       double *cost_curvature,
                       ALLOC_INT * parameter_dimension,
                       int *parameter_int_real,
#if OPTIONS_FILE_DATA
                       FILE * ptr_options,
#endif
                       USER_DEFINES * USER_OPTIONS)
#else
int
initialize_parameters (cost_parameters,
                       parameter_lower_bound,
                       parameter_upper_bound,
                       cost_tangents,
                       cost_curvature,
                       parameter_dimension, parameter_int_real,
#if OPTIONS_FILE_DATA
                       ptr_options,
#endif
                       USER_OPTIONS)
     double *cost_parameters;
     double *parameter_lower_bound;
     double *parameter_upper_bound;
     double *cost_tangents;
     double *cost_curvature;
     ALLOC_INT *parameter_dimension;
     int *parameter_int_real;
#if OPTIONS_FILE_DATA
     FILE *ptr_options;
#endif
     USER_DEFINES *USER_OPTIONS;
#endif
{
  ALLOC_INT index;
#if OPTIONS_FILE_DATA
  char read_option[80];
  ALLOC_INT read_index;
#endif
#if MULTI_MIN
  int multi_index;
#endif
#if MY_TEMPLATE                 /* MY_TEMPLATE_init_decl */
  /* add some declarations if required */
#endif

  index = 0;
#if OPTIONS_FILE_DATA
  fscanf (ptr_options, "%s", read_option);

  for (index = 0; index < *parameter_dimension; ++index) {
#if MY_TEMPLATE                 /* MY_TEMPLATE_read_opt */
    /* put in some code as required to alter lines read from asa_opt */
#endif
#if INT_ALLOC
    fscanf (ptr_options, "%d", &read_index);
#else
#if INT_LONG
    fscanf (ptr_options, "%ld", &read_index);
#else
    fscanf (ptr_options, "%d", &read_index);
#endif
#endif
    fscanf (ptr_options, "%lf%lf%lf%d",
            &(parameter_lower_bound[read_index]),
            &(parameter_upper_bound[read_index]),
            &(cost_parameters[read_index]),
            &(parameter_int_real[read_index]));
  }
#else /* OPTIONS_FILE_DATA */
#if ASA_TEST
  /* store the parameter ranges */
  for (index = 0; index < *parameter_dimension; ++index)
    parameter_lower_bound[index] = -10000.0;
  for (index = 0; index < *parameter_dimension; ++index)
    parameter_upper_bound[index] = 10000.0;

  /* store the initial parameter types */
  for (index = 0; index < *parameter_dimension; ++index)
    parameter_int_real[index] = REAL_TYPE;

  /* store the initial parameter values */
  for (index = 0; index < *parameter_dimension / 4.0; ++index) {
    cost_parameters[4 * (index + 1) - 4] = 999.0;
    cost_parameters[4 * (index + 1) - 3] = -1007.0;
    cost_parameters[4 * (index + 1) - 2] = 1001.0;
    cost_parameters[4 * (index + 1) - 1] = -903.0;
  }
#endif /* ASA_TEST */
#endif /* OPTIONS_FILE_DATA */
#if ASA_TEMPLATE_SAMPLE
  for (index = 0; index < *parameter_dimension; ++index)
    parameter_lower_bound[index] = 0;
  for (index = 0; index < *parameter_dimension; ++index)
    parameter_upper_bound[index] = 2.0;
  for (index = 0; index < *parameter_dimension; ++index)
    parameter_int_real[index] = REAL_TYPE;
  for (index = 0; index < *parameter_dimension; ++index)
    cost_parameters[index] = 0.5;
#endif

#if USER_INITIAL_PARAMETERS_TEMPS
  if ((USER_OPTIONS->User_Parameter_Temperature =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->User_Parameter_Temperature");
    Exit_USER (user_exit_msg);
    return (-2);
  }
#if ASA_TEMPLATE
  for (index = 0; index < *parameter_dimension; ++index)
    USER_OPTIONS->User_Parameter_Temperature[index] = 1.0;
#endif
#endif /* USER_INITIAL_PARAMETERS_TEMPS */
#if USER_INITIAL_COST_TEMP
  if ((USER_OPTIONS->User_Cost_Temperature =
       (double *) calloc (1, sizeof (double))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->User_Cost_Temperature");
    Exit_USER (user_exit_msg);
    return (-2);
  }
#if ASA_TEMPLATE
  USER_OPTIONS->User_Cost_Temperature[0] = 5.936648E+09;
#endif
#endif /* USER_INITIAL_COST_TEMP */
#if DELTA_PARAMETERS
  if ((USER_OPTIONS->User_Delta_Parameter =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->User_Delta_Parameter");
    Exit_USER (user_exit_msg);
    return (-2);
  }
#if ASA_TEMPLATE
  for (index = 0; index < *parameter_dimension; ++index)
    USER_OPTIONS->User_Delta_Parameter[index] = 0.001;
#endif
#endif /* DELTA_PARAMETERS */
#if QUENCH_PARAMETERS
  if ((USER_OPTIONS->User_Quench_Param_Scale =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->User_Quench_Param_Scale");
    Exit_USER (user_exit_msg);
    return (-2);
  }
#if ASA_TEMPLATE
  for (index = 0; index < *parameter_dimension; ++index)
    USER_OPTIONS->User_Quench_Param_Scale[index] = 1.0;
#endif
#if ASA_TEMPLATE_MULTIPLE
  for (index = 0; index < *parameter_dimension; ++index)
    USER_OPTIONS->User_Quench_Param_Scale[index] = 1.0;
#endif
#if ASA_TEMPLATE_SAVE
  for (index = 0; index < *parameter_dimension; ++index)
    USER_OPTIONS->User_Quench_Param_Scale[index] = 1.0;
#endif
#endif /* QUENCH_PARAMETERS */
#if QUENCH_COST
  if ((USER_OPTIONS->User_Quench_Cost_Scale =
       (double *) calloc (1, sizeof (double))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->User_Quench_Cost_Scale");
    Exit_USER (user_exit_msg);
    return (-2);
  }
#if ASA_TEMPLATE
  USER_OPTIONS->User_Quench_Cost_Scale[0] = 1.0;
#endif
#if ASA_TEMPLATE_MULTIPLE
  USER_OPTIONS->User_Quench_Cost_Scale[0] = 1.0;
#endif
#if ASA_TEMPLATE_SAVE
  USER_OPTIONS->User_Quench_Cost_Scale[0] = 1.0;
#endif
#endif /* QUENCH_COST */

  /* use asa_opt to read in QUENCH USER_OPTIONS */
#if OPTIONS_FILE_DATA
#if QUENCH_COST
  fscanf (ptr_options, "%s", read_option);
  fscanf (ptr_options, "%s", read_option);
  fscanf (ptr_options, "%lf", &(USER_OPTIONS->User_Quench_Cost_Scale[0]));

#if QUENCH_PARAMETERS
  fscanf (ptr_options, "%s", read_option);
  fscanf (ptr_options, "%s", read_option);
  for (index = 0; index < *parameter_dimension; ++index) {
#if INT_ALLOC
    fscanf (ptr_options, "%d", &read_index);
#else
#if INT_LONG
    fscanf (ptr_options, "%ld", &read_index);
#else
    fscanf (ptr_options, "%d", &read_index);
#endif
#endif
    fscanf (ptr_options, "%lf",
            &(USER_OPTIONS->User_Quench_Param_Scale[read_index]));
  }
#endif /* QUENCH_PARAMETERS */
#endif /* QUENCH_COST */
#endif /* OPTIONS_FILE_DATA */

#if RATIO_TEMPERATURE_SCALES
  if ((USER_OPTIONS->User_Temperature_Ratio =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->User_Temperature_Ratio");
    Exit_USER (user_exit_msg);
    return (-2);
  }
#if ASA_TEMPLATE
  for (index = 0; index < *parameter_dimension; ++index)
    USER_OPTIONS->User_Temperature_Ratio[index] = 1.0;
#endif
#endif /* RATIO_TEMPERATURE_SCALES */
  /* Defines the limit of collection of sampled data by asa */
#if ASA_SAMPLE
  /* create memory for Bias_Generated[] */
  if ((USER_OPTIONS->Bias_Generated =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->Bias_Generated");
    Exit_USER (user_exit_msg);
    return (-2);
  }
#endif

#if ASA_RESOLUTION
  if ((USER_OPTIONS->Coarse_Resolution =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->Coarse_Resolution");
    Exit_USER (user_exit_msg);
    return (-2);
  }
#if ASA_TEMPLATE
  for (index = 0; index < *parameter_dimension; ++index)
    USER_OPTIONS->Coarse_Resolution[index] = 1.0;
#endif
#endif /* ASA_RESOLUTION */
#if ASA_QUEUE
#if ASA_RESOLUTION
  USER_OPTIONS->Queue_Resolution = USER_OPTIONS->Coarse_Resolution;
#else /* ASA_RESOLUTION */
  if ((USER_OPTIONS->Queue_Resolution =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->Queue_Resolution");
    Exit_USER (user_exit_msg);
    return (-2);
  }
#if ASA_TEMPLATE_QUEUE
  for (index = 0; index < *parameter_dimension; ++index)
    USER_OPTIONS->Queue_Resolution[index] = 0.001;
#endif
#endif /* ASA_RESOLUTION */
#if ASA_TEMPLATE_QUEUE
  USER_OPTIONS->Queue_Size = 100;
#endif
#endif /* ASA_QUEUE */
#if MULTI_MIN
#if ASA_TEMPLATE
  USER_OPTIONS->Multi_Number = 2;
#endif
  if ((USER_OPTIONS->Multi_Cost =
       (double *) calloc (USER_OPTIONS->Multi_Number,
                          sizeof (double))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->Multi_Cost");
    Exit_USER (user_exit_msg);
    return (-2);
  }
  if ((USER_OPTIONS->Multi_Grid =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->Multi_Grid");
    Exit_USER (user_exit_msg);
    return (-2);
  }
  if ((USER_OPTIONS->Multi_Params =
       (double **) calloc (USER_OPTIONS->Multi_Number,
                           sizeof (double *))) == NULL) {
    strcpy (user_exit_msg,
            "initialize_parameters(): USER_OPTIONS->Multi_Params");
    Exit_USER (user_exit_msg);
    return (-2);
  }
  for (multi_index = 0; multi_index < USER_OPTIONS->Multi_Number;
       ++multi_index) {
    if ((USER_OPTIONS->Multi_Params[multi_index] =
         (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
      strcpy (user_exit_msg,
              "initialize_parameters(): USER_OPTIONS->Multi_Params[multi_index]");
      Exit_USER (user_exit_msg);
      return (-2);
    }
  }
#if ASA_TEST
  for (index = 0; index < *parameter_dimension; ++index) {
    USER_OPTIONS->Multi_Grid[index] = 0.05;
  }
  USER_OPTIONS->Multi_Specify = 0;
#endif
#if ASA_TEMPLATE
  for (index = 0; index < *parameter_dimension; ++index) {
    USER_OPTIONS->Multi_Grid[index] =
      (parameter_upper_bound[index] - parameter_lower_bound[index]) / 100.0;
  }
  USER_OPTIONS->Multi_Specify = 0;
#endif /* ASA_TEMPLATE */
#endif /* MULTI_MIN */
  USER_OPTIONS->Asa_Recursive_Level = 0;

#if MY_TEMPLATE                 /* MY_TEMPLATE_params */
  /* If not using RECUR_OPTIONS_FILE_DATA or data read from asa_opt,
     store the parameter ranges
     store the parameter types
     store the initial parameter values
     other changes needed for initialization */
#endif /* MY_TEMPLATE params */

  return (0);
}

#if COST_FILE
#else
/***********************************************************************
* double cost_function
*	This is the users cost function to optimize
*	(find the minimum).
*	cost_flag is set to TRUE if the parameter set
*	does not violates any constraints
*       parameter_lower_bound and parameter_upper_bound may be
*       adaptively changed during the search.
***********************************************************************/

#if HAVE_ANSI
double
cost_function (double *x,
               double *parameter_lower_bound,
               double *parameter_upper_bound,
               double *cost_tangents,
               double *cost_curvature,
               ALLOC_INT * parameter_dimension,
               int *parameter_int_real,
               int *cost_flag, int *exit_code, USER_DEFINES * USER_OPTIONS)
#else
double
cost_function (x,
               parameter_lower_bound,
               parameter_upper_bound,
               cost_tangents,
               cost_curvature,
               parameter_dimension,
               parameter_int_real, cost_flag, exit_code, USER_OPTIONS)
     double *x;
     double *parameter_lower_bound;
     double *parameter_upper_bound;
     double *cost_tangents;
     double *cost_curvature;
     ALLOC_INT *parameter_dimension;
     int *parameter_int_real;
     int *cost_flag;
     int *exit_code;
     USER_DEFINES *USER_OPTIONS;
#endif
{

#if ASA_TEST                    /* ASA test problem */
  /* Objective function from
   * %A A. Corana

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -