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

📄 asa_usr.c

📁 simulated annealing code ASA
💻 C
📖 第 1 页 / 共 5 页
字号:

  fscanf (ptr_options, "%s", read_option);
  fscanf (ptr_options, "%lf", &read_double);
  USER_OPTIONS->Delta_X = read_double;
  fscanf (ptr_options, "%s", read_option);
  fscanf (ptr_options, "%d", &read_int);
  USER_OPTIONS->User_Tangents = read_int;
  fscanf (ptr_options, "%s", read_option);
  fscanf (ptr_options, "%d", &read_int);
  USER_OPTIONS->Curvature_0 = read_int;

#else /* OPTIONS_FILE */
  /* USER_OPTIONS->Limit_Acceptances = 10000; */
  USER_OPTIONS->Limit_Acceptances = 1000;
  USER_OPTIONS->Limit_Generated = 99999;
  USER_OPTIONS->Limit_Invalid_Generated_States = 1000;
  /* USER_OPTIONS->Accepted_To_Generated_Ratio = 1.0E-6; */
  USER_OPTIONS->Accepted_To_Generated_Ratio = 1.0E-4;

  USER_OPTIONS->Cost_Precision = 1.0E-18;
  USER_OPTIONS->Maximum_Cost_Repeat = 5;
  USER_OPTIONS->Number_Cost_Samples = 5;
  USER_OPTIONS->Temperature_Ratio_Scale = 1.0E-5;
  USER_OPTIONS->Cost_Parameter_Scale_Ratio = 1.0;
  USER_OPTIONS->Temperature_Anneal_Scale = 100.0;

  USER_OPTIONS->Include_Integer_Parameters = FALSE;
  USER_OPTIONS->User_Initial_Parameters = FALSE;
  USER_OPTIONS->Sequential_Parameters = -1;
  USER_OPTIONS->Initial_Parameter_Temperature = 1.0;

  USER_OPTIONS->Acceptance_Frequency_Modulus = 100;
  USER_OPTIONS->Generated_Frequency_Modulus = 10000;
  USER_OPTIONS->Reanneal_Cost = 1;
  USER_OPTIONS->Reanneal_Parameters = TRUE;

  USER_OPTIONS->Delta_X = 0.001;
  USER_OPTIONS->User_Tangents = FALSE;
  USER_OPTIONS->Curvature_0 = FALSE;

#endif /* OPTIONS_FILE */

  /* ALLOCATE STORAGE */

#if ASA_SAVE
  /* Such data could be saved in a user_save file, but for
     convenience here everything is saved in asa_save. */
  USER_OPTIONS->Random_Array_Dim = SHUFFLE;
  USER_OPTIONS->Random_Array = random_array;
#endif /* ASA_SAVE */

#if USER_ASA_OUT
  if ((USER_OPTIONS->Asa_Out_File =
       (char *) calloc (80, sizeof (char))) == NULL) {
    strcpy (user_exit_msg, "main()/asa_main(): USER_OPTIONS->Asa_Out_File");
    Exit_USER (user_exit_msg);
    return (-2);
  }
#endif

  /* the number of parameters for the cost function */
#if OPTIONS_FILE_DATA
  fscanf (ptr_options, "%s", read_option);
  fscanf (ptr_options, "%s", read_option);

#if INT_ALLOC
  fscanf (ptr_options, "%d", &read_int);
  *parameter_dimension = read_int;
#else
#if INT_LONG
  fscanf (ptr_options, "%ld", &read_long);
  *parameter_dimension = read_long;
#else
  fscanf (ptr_options, "%d", &read_int);
  *parameter_dimension = read_int;
#endif
#endif

#else /* OPTIONS_FILE_DATA */
#if ASA_TEST
  *parameter_dimension = 4;
#endif /* ASA_TEST */
#endif /* OPTIONS_FILE_DATA */
#if MY_TEMPLATE                 /* MY_TEMPLATE_dim */
  /* If not using OPTIONS_FILE_DATA or data read from asa_opt,
     insert the number of parameters for the cost_function */
#endif /* MY_TEMPLATE dim */

#if ASA_TEMPLATE_SAMPLE
  *parameter_dimension = 2;
  USER_OPTIONS->Limit_Acceptances = 2000;
  USER_OPTIONS->User_Tangents = TRUE;
  USER_OPTIONS->Limit_Weights = 1.0E-7;
#endif
#if ASA_TEMPLATE_PARALLEL
  USER_OPTIONS->Gener_Block = 100;
  USER_OPTIONS->Gener_Block_Max = 512;
  USER_OPTIONS->Gener_Mov_Avr = 3;
#endif

  /* allocate parameter minimum space */
  if ((parameter_lower_bound =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg, "main()/asa_main(): parameter_lower_bound");
    Exit_USER (user_exit_msg);
    return (-2);
  }
  /* allocate parameter maximum space */
  if ((parameter_upper_bound =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg, "main()/asa_main(): parameter_upper_bound");
    Exit_USER (user_exit_msg);
    return (-2);
  }
  /* allocate parameter initial values; the parameter final values
     will be stored here later */
  if ((cost_parameters =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg, "main()/asa_main(): cost_parameters");
    Exit_USER (user_exit_msg);
    return (-2);
  }
  /* allocate the parameter types, real or integer */
  if ((parameter_int_real =
       (int *) calloc (*parameter_dimension, sizeof (int))) == NULL) {
    strcpy (user_exit_msg, "main()/asa_main(): parameter_int_real");
    Exit_USER (user_exit_msg);
    return (-2);
  }
  /* allocate space for parameter cost_tangents -
     used for reannealing */
  if ((cost_tangents =
       (double *) calloc (*parameter_dimension, sizeof (double))) == NULL) {
    strcpy (user_exit_msg, "main()/asa_main(): cost_tangents");
    Exit_USER (user_exit_msg);
    return (-2);
  }

  if (USER_OPTIONS->Curvature_0 == FALSE || USER_OPTIONS->Curvature_0 == -1) {
    /* allocate space for parameter cost_curvatures/covariance */
    if ((cost_curvature =
         (double *) calloc ((*parameter_dimension) *
                            (*parameter_dimension),
                            sizeof (double))) == NULL) {
      strcpy (user_exit_msg, "main()/asa_main(): cost_curvature");
      Exit_USER (user_exit_msg);
      return (-2);
    }
  } else {
    cost_curvature = (double *) NULL;
  }

#if USER_COST_SCHEDULE
  USER_OPTIONS->Cost_Schedule = user_cost_schedule;
#endif
#if USER_ACCEPTANCE_TEST
  USER_OPTIONS->Acceptance_Test = user_acceptance_test;
#endif
#if USER_ACCEPT_ASYMP_EXP
  USER_OPTIONS->Asymp_Exp_Param = 1.0;
#endif
#if USER_GENERATING_FUNCTION
  USER_OPTIONS->Generating_Distrib = user_generating_distrib;
#endif
#if USER_REANNEAL_COST
  USER_OPTIONS->Reanneal_Cost_Function = user_reanneal_cost;
#endif
#if USER_REANNEAL_PARAMETERS
  USER_OPTIONS->Reanneal_Params_Function = user_reanneal_params;
#endif

#if MY_TEMPLATE                 /* MY_TEMPLATE_pre_initialize */
  /* last changes before entering initialize_parameters() */
#endif

  initialize_parameters_value = 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);
#if OPTIONS_FILE
  fclose (ptr_options);
#endif
  if (initialize_parameters_value == -2)
    return (initialize_parameters_value);

  /* optimize the cost_function, returning the results in
     cost_value and cost_parameters */
#if ASA_TEMPLATE_MULTIPLE
  /* multiple asa() quenched calls + multiple asa_out files
     (To get longer quenched runs, decrease SMALL_FLOAT.) */
  for (n_asa = 1; n_asa <= *parameter_dimension; n_asa++) {
    asa_file[4] = 'A' + n_asa - 1;
    USER_OPTIONS->User_Quench_Cost_Scale[0] = (double) n_asa;
    for (index = 0; index < *parameter_dimension; ++index)
      USER_OPTIONS->User_Quench_Param_Scale[index] = (double) n_asa;
    for (n_trajectory = 0; n_trajectory < 3; ++n_trajectory) {
      asa_file[6] = 'a' + n_trajectory;
      strcpy (USER_OPTIONS->Asa_Out_File, asa_file);
#endif

#if ASA_TEMPLATE_ASA_OUT_PID
      pid_file[0] = 'a';
      pid_file[1] = 's';
      pid_file[2] = 'a';
      pid_file[3] = '_';
      pid_file[4] = 'o';
      pid_file[5] = 'u';
      pid_file[6] = 't';
      pid_file[7] = '_';

      pid_int = getpid ();
      if (pid_int < 0) {
        pid_file[7] = '0';
        pid_int = -pid_int;
      }

      strcpy (USER_OPTIONS->Asa_Out_File, pid_file);
#endif
      cost_value =
        asa (USER_COST_FUNCTION,
             randflt,
             rand_seed,
             cost_parameters,
             parameter_lower_bound,
             parameter_upper_bound,
             cost_tangents,
             cost_curvature,
             parameter_dimension,
             parameter_int_real, cost_flag, exit_code, USER_OPTIONS);
      if (*exit_code == -1) {
#if INCL_STDOUT
        printf ("\n\n*** error in calloc in ASA ***\n\n");
#endif /* INCL_STDOUT */
        fprintf (ptr_out, "\n\n*** error in calloc in ASA ***\n\n");
        fflush (ptr_out);
        return (-1);
      }
#if MULTI_MIN
      fprintf (ptr_out, "Multi_Specify = %d\n", USER_OPTIONS->Multi_Specify);
#if INT_LONG
      fprintf (ptr_out, "N_Accepted = %ld\n", USER_OPTIONS->N_Accepted);
#else
      fprintf (ptr_out, "N_Accepted = %d\n", USER_OPTIONS->N_Accepted);
#endif
#if ASA_RESOLUTION
      for (n_param = 0; n_param < *parameter_dimension; ++n_param) {
        fprintf (ptr_out,
#if INT_ALLOC
                 "Coarse_Resolution[%d] = %12.7g\n",
#else
#if INT_LONG
                 "Coarse_Resolution[%ld] = %12.7g\n",
#else
                 "Coarse_Resolution[%d] = %12.7g\n",
#endif
#endif
                 n_param, USER_OPTIONS->Coarse_Resolution[n_param]);
      }
#else /* ASA_RESOLUTION */
      for (n_param = 0; n_param < *parameter_dimension; ++n_param) {
        fprintf (ptr_out,
#if INT_ALLOC
                 "Multi_Grid[%d] = %12.7g\n",
#else
#if INT_LONG
                 "Multi_Grid[%ld] = %12.7g\n",
#else
                 "Multi_Grid[%d] = %12.7g\n",
#endif
#endif
                 n_param, USER_OPTIONS->Multi_Grid[n_param]);
      }
#endif /* ASA_RESOLUTION */
      fprintf (ptr_out, "\n");
      for (multi_index = 0; multi_index < USER_OPTIONS->Multi_Number;
           ++multi_index) {
        fprintf (ptr_out, "\n");
        fprintf (ptr_out, "Multi_Cost[%d] = %12.7g\n",
                 multi_index, USER_OPTIONS->Multi_Cost[multi_index]);
        for (n_param = 0; n_param < *parameter_dimension; ++n_param) {
          fprintf (ptr_out,
#if INT_ALLOC
                   "Multi_Params[%d][%d] = %12.7g\n",
#else
#if INT_LONG
                   "Multi_Params[%d][%ld] = %12.7g\n",
#else
                   "Multi_Params[%d][%d] = %12.7g\n",
#endif
#endif
                   multi_index, n_param,
                   USER_OPTIONS->Multi_Params[multi_index][n_param]);
        }
      }
      fprintf (ptr_out, "\n");
      fflush (ptr_out);

      cost_value = USER_OPTIONS->Multi_Cost[0];
      for (n_param = 0; n_param < *parameter_dimension; ++n_param) {
        cost_parameters[n_param] = USER_OPTIONS->Multi_Params[0][n_param];
      }
#endif /* MULTI_MIN */

#if FITLOC
      /* Fit_Local, Iter_Max and Penalty may be set adaptively */
      USER_OPTIONS->Penalty = 1000;
      USER_OPTIONS->Fit_Local = 1;
      USER_OPTIONS->Iter_Max = 500;
      if (USER_OPTIONS->Fit_Local >= 1) {
        cost_value = fitloc (USER_COST_FUNCTION,
                             cost_parameters,
                             parameter_lower_bound,
                             parameter_upper_bound,
                             cost_tangents,
                             cost_curvature,
                             parameter_dimension,
                             parameter_int_real,
                             cost_flag, exit_code, USER_OPTIONS, ptr_out);
      }
#endif /* FITLOC */
#if MY_TEMPLATE                 /* MY_TEMPLATE_post_asa */
#endif
#if ASA_TEMPLATE_LIB
      *main_cost_value = cost_value;
      for (n_param = 0; n_param < *parameter_dimension; ++n_param) {
        main_cost_parameters[n_param] = cost_parameters[n_param];
      }
      *main_exit_code = *exit_code;
#endif

      fprintf (ptr_out, "exit code = %d\n", *exit_code);
      fprintf (ptr_out, "final cost value = %12.7g\n", cost_value);
      fprintf (ptr_out, "parameter\tvalue\n");
      for (n_param = 0; n_param < *parameter_dimension; ++n_param) {
        fprintf (ptr_out,
#if INT_ALLOC
                 "%d\t\t%12.7g\n",
#else
#if INT_LONG
                 "%ld\t\t%12.7g\n",
#else
                 "%d\t\t%12.7g\n",
#endif
#endif
                 n_param, cost_parameters[n_param]);
      }

#if TIME_CALC
      /* print ending time */
      print_time ("end", ptr_out);
#endif
#if ASA_TEMPLATE_MULTIPLE
    }
  }
#endif

#if ASA_TEMPLATE_SAMPLE
  ptr_asa = fopen ("asa_out", "r");
  sample (ptr_out, ptr_asa);
#endif

  /* close all files */
  fclose (ptr_out);
#if OPTIONAL_DATA_DBL
  free (USER_OPTIONS->Asa_Data_Dbl);
#endif
#if OPTIONAL_DATA_INT
  free (USER_OPTIONS->Asa_Data_Int);
#endif
#if OPTIONAL_DATA_PTR
#if MY_TEMPLATE
  /* Instead of freeing Asa_Data_Ptr, if memory has been allocated
   * outside ASA, e.g., by the use of ASA_LIB, use the following: */
  /* USER_OPTIONS->Asa_Data_Ptr = NULL; */
#endif /* MY_TEMPLATE */
  free (USER_OPTIONS->Asa_Data_Ptr);
#endif
#if USER_ASA_OUT
  free (USER_OPTIONS->Asa_Out_File);
#endif
#if ASA_SAMPLE
  free (USER_OPTIONS->Bias_Generated);
#endif
#if ASA_QUEUE
#if ASA_RESOLUTION
#else
  free (USER_OPTIONS->Queue_Resolution);
#endif
#endif
#if ASA_RESOLUTION
  free (USER_OPTIONS->Coarse_Resolution);
#endif
  if (USER_OPTIONS->Curvature_0 == FALSE || USER_OPTIONS->Curvature_0 == -1)
    free (cost_curvature);
#if USER_INITIAL_PARAMETERS_TEMPS
  free (USER_OPTIONS->User_Parameter_Temperature);
#endif
#if USER_INITIAL_COST_TEMP
  free (USER_OPTIONS->User_Cost_Temperature);
#endif
#if DELTA_PARAMETERS
  free (USER_OPTIONS->User_Delta_Parameter);
#endif
#if QUENCH_PARAMETERS
  free (USER_OPTIONS->User_Quench_Param_Scale);
#endif
#if QUENCH_COST
  free (USER_OPTIONS->User_Quench_Cost_Scale);
#endif

⌨️ 快捷键说明

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