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

📄 asa.c

📁 模拟退火的源代码。关于matlab的大家好好学习以下吧
💻 C
📖 第 1 页 / 共 5 页
字号:
#if INCL_STDOUT
        ptr_asa_out = stdout;
#endif /* INCL_STDOUT */
      } else {
        ptr_asa_out = fopen (OPTIONS->Asa_Out_File, "a");
      }
#else
      if (!strcmp (ASA_OUT, "STDOUT")) {
#if INCL_STDOUT
        ptr_asa_out = stdout;
#endif /* INCL_STDOUT */
      } else {
        ptr_asa_out = fopen (ASA_OUT, "a");
      }
#endif
      fprintf (ptr_asa_out, "\n\n\t\t number_asa_open = %d\n",
               number_asa_open);
    }
#endif /* ASA_PRINT */
  } else {
    ++recursive_asa_open;
#if ASA_PRINT
    if (recursive_asa_open == 1) {
      /* open the output file */
#if ASA_SAVE
#if USER_ASA_OUT
      if (!strcmp (OPTIONS->Asa_Out_File, "STDOUT")) {
#if INCL_STDOUT
        ptr_asa_out = stdout;
#endif /* INCL_STDOUT */
      } else {
        ptr_asa_out = fopen (OPTIONS->Asa_Out_File, "a");
      }
#else
      if (!strcmp (ASA_OUT, "STDOUT")) {
#if INCL_STDOUT
        ptr_asa_out = stdout;
#endif /* INCL_STDOUT */
      } else {
        ptr_asa_out = fopen (ASA_OUT, "a");
      }
#endif
#else /* ASA_SAVE */
#if USER_ASA_OUT
      if (!strcmp (OPTIONS->Asa_Out_File, "STDOUT")) {
#if INCL_STDOUT
        ptr_asa_out = stdout;
#endif /* INCL_STDOUT */
      } else {
        ptr_asa_out = fopen (OPTIONS->Asa_Out_File, "w");
      }
#else
      if (!strcmp (ASA_OUT, "STDOUT")) {
#if INCL_STDOUT
        ptr_asa_out = stdout;
#endif /* INCL_STDOUT */
      } else {
        ptr_asa_out = fopen (ASA_OUT, "w");
      }
#endif
#endif /* ASA_SAVE */
    } else {
#if USER_ASA_OUT
      if (!strcmp (OPTIONS->Asa_Out_File, "STDOUT")) {
#if INCL_STDOUT
        ptr_asa_out = stdout;
#endif /* INCL_STDOUT */
      } else {
        ptr_asa_out = fopen (OPTIONS->Asa_Out_File, "a");
      }
#else
      if (!strcmp (ASA_OUT, "STDOUT")) {
#if INCL_STDOUT
        ptr_asa_out = stdout;
#endif /* INCL_STDOUT */
      } else {
        ptr_asa_out = fopen (ASA_OUT, "a");
      }
#endif
      fprintf (ptr_asa_out, "\n\n\t\t recursive_asa_open = %d\n",
               recursive_asa_open);
    }
#endif /* ASA_PRINT */
  }

#if ASA_PIPE_FILE
  ptr_asa_pipe = fopen ("asa_pipe", "a");
  fprintf (ptr_asa_pipe, "%s", "%generate");
  fprintf (ptr_asa_pipe, "\t%s", "accept");
  fprintf (ptr_asa_pipe, "\t%s", "best_cost");
  VFOR (index_v)
#if INT_ALLOC
    fprintf (ptr_asa_pipe, "\t%s-%d", "param", index_v);
#else
#if INT_LONG
    fprintf (ptr_asa_pipe, "\t%s-%ld", "param", index_v);
#else
    fprintf (ptr_asa_pipe, "\t%s-%d", "param", index_v);
#endif
#endif
  fprintf (ptr_asa_pipe, "\t%s", "cost_temp");
  VFOR (index_v)
#if INT_ALLOC
    fprintf (ptr_asa_pipe, "\t%s-%d", "param_temp", index_v);
#else
#if INT_LONG
    fprintf (ptr_asa_pipe, "\t%s-%ld", "param_temp", index_v);
#else
    fprintf (ptr_asa_pipe, "\t%s-%d", "param_temp", index_v);
#endif
#endif
  fprintf (ptr_asa_pipe, "\t%s", "last_cost");
  fprintf (ptr_asa_pipe, "\n");
  fflush (ptr_asa_pipe);
#endif /* ASA_PIPE_FILE */

#if ASA_PRINT
  /* print header information as defined by user */
  print_asa_options (ptr_asa_out, OPTIONS);

#if TIME_CALC
  /* print starting time */
  print_time ("start_asa", ptr_asa_out);
#endif
  fflush (ptr_asa_out);
#endif /* ASA_PRINT */

  /* set indices and counts to 0 */
  *best_number_generated_saved =
    *number_generated =
    *recent_number_generated = *recent_number_acceptances = 0;
  *index_cost_acceptances =
    *best_number_accepted_saved =
    *number_accepted = *number_acceptances_saved = 0;
  index_cost_repeat = 0;

  OPTIONS->N_Accepted = *number_accepted;
  OPTIONS->N_Generated = *number_generated;

#if ASA_SAMPLE
  OPTIONS->N_Generated = 0;
  OPTIONS->Average_Weights = 1.0;
#endif

  /* do not calculate curvatures initially */
  *curvature_flag = FALSE;

  /* allocate storage for all parameters */
  if ((current_generated_state->parameter =
       (double *) calloc (*number_parameters, sizeof (double))) == NULL) {
    strcpy (exit_msg, "asa(): current_generated_state->parameter");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
  if ((last_saved_state->parameter =
       (double *) calloc (*number_parameters, sizeof (double))) == NULL) {
    strcpy (exit_msg, "asa(): last_saved_state->parameter");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
  if ((best_generated_state->parameter =
       (double *) calloc (*number_parameters, sizeof (double))) == NULL) {
    strcpy (exit_msg, "asa(): best_generated_state->parameter");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
#if ASA_PARALLEL
  parallel_block_max = OPTIONS->Gener_Block_Max;
  parallel_generated = OPTIONS->Gener_Block;

  for (index_parallel = 0; index_parallel < parallel_block_max;
       ++index_parallel) {
    if ((gener_block_state[index_parallel].parameter =
         (double *) calloc (*number_parameters, sizeof (double))) == NULL) {
      strcpy (exit_msg, "asa(): gener_block_state[index_parallel].parameter");
      Exit_ASA (exit_msg);
      *exit_status = CALLOC_FAILED;
      return (-1);
    }
  }
#endif

  OPTIONS->Best_Cost = &(best_generated_state->cost);
  OPTIONS->Best_Parameters = best_generated_state->parameter;
  OPTIONS->Last_Cost = &(last_saved_state->cost);
  OPTIONS->Last_Parameters = last_saved_state->parameter;

  if ((initial_user_parameter_temp =
       (double *) calloc (*number_parameters, sizeof (double))) == NULL) {
    strcpy (exit_msg, "asa(): initial_user_parameter_temp");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
  if ((index_parameter_generations =
       (ALLOC_INT *) calloc (*number_parameters,
                             sizeof (ALLOC_INT))) == NULL) {
    strcpy (exit_msg, "asa(): index_parameter_generations");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }

  /* set all temperatures */
  if ((current_user_parameter_temp =
       (double *) calloc (*number_parameters, sizeof (double))) == NULL) {
    strcpy (exit_msg, "asa(): current_user_parameter_temp");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
#if USER_INITIAL_PARAMETERS_TEMPS
  VFOR (index_v)
    current_user_parameter_temp[index_v] =
    initial_user_parameter_temp[index_v] =
    OPTIONS->User_Parameter_Temperature[index_v];
#else
  VFOR (index_v)
    current_user_parameter_temp[index_v] =
    initial_user_parameter_temp[index_v] =
    OPTIONS->Initial_Parameter_Temperature;
#endif

  if ((temperature_scale_parameters =
       (double *) calloc (*number_parameters, sizeof (double))) == NULL) {
    strcpy (exit_msg, "asa(): temperature_scale_parameters");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
#if ASA_QUEUE
  if (OPTIONS->Queue_Size > 0) {
    queue_size_tmp = OPTIONS->Queue_Size;
  } else {
    queue_size_tmp = 1;
  }
  if ((save_queue_flag =
       (int *) calloc (queue_size_tmp, sizeof (int))) == NULL) {
    strcpy (exit_msg, "asa(): save_queue_flag");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
  if ((save_queue_cost =
       (double *) calloc (queue_size_tmp, sizeof (double))) == NULL) {
    strcpy (exit_msg, "asa(): save_queue_cost");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
  if ((save_queue_param =
       (double *) calloc ((*number_parameters) * queue_size_tmp,
                          sizeof (double))) == NULL) {
    strcpy (exit_msg, "asa(): save_queue_param");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
#endif /* ASA_QUEUE */

#if MULTI_MIN
  if ((multi_cost =
       (double *) calloc (OPTIONS->Multi_Number + 1,
                          sizeof (double))) == NULL) {
    strcpy (exit_msg, "asa(): *multi_cost");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
  multi_cost_qsort = multi_cost;
  if ((multi_sort =
       (int *) calloc (OPTIONS->Multi_Number + 1, sizeof (int))) == NULL) {
    strcpy (exit_msg, "asa(): *multi_sort");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
  if ((multi_params =
       (double **) calloc (OPTIONS->Multi_Number + 1,
                           sizeof (double *))) == NULL) {
    strcpy (exit_msg, "asa(): *multi_params");
    Exit_ASA (exit_msg);
    *exit_status = CALLOC_FAILED;
    return (-1);
  }
  for (multi_index = 0; multi_index <= OPTIONS->Multi_Number; ++multi_index) {
    if ((multi_params[multi_index] =
         (double *) calloc (*number_parameters, sizeof (double))) == NULL) {
      strcpy (exit_msg, "asa(): multi_params[multi_index]");
      Exit_ASA (exit_msg);
      *exit_status = CALLOC_FAILED;
      return (-1);
    }
  }
#endif /* MULTI_MIN */

#if USER_INITIAL_COST_TEMP
#if USER_ACCEPTANCE_TEST
  OPTIONS->Cost_Temp_Curr = OPTIONS->Cost_Temp_Init =
#endif
    *initial_cost_temperature = *current_cost_temperature =
    OPTIONS->User_Cost_Temperature[0];
#endif

  /* set parameters to the initial parameter values */
  VFOR (index_v)
    last_saved_state->parameter[index_v] =
    current_generated_state->parameter[index_v] =
    parameter_initial_final[index_v];
#if USER_ACCEPTANCE_TEST
  OPTIONS->Random_Seed = seed;
  OPTIONS->Random_Seed[0] = *seed;
  OPTIONS->User_Acceptance_Flag = TRUE;
  OPTIONS->Cost_Acceptance_Flag = FALSE;
#endif

#if ASA_PRINT
#if INT_LONG
  fprintf (ptr_asa_out, "Initial Random Seed = %ld\n\n", *seed);
#else
  fprintf (ptr_asa_out, "Initial Random Seed = %d\n\n", *seed);
#endif
#endif /* ASA_PRINT */

  /* save initial user value of OPTIONS->Sequential_Parameters */
  *start_sequence = OPTIONS->Sequential_Parameters;

#if ASA_PRINT
  fprintf (ptr_asa_out,
#if INT_ALLOC
           "*number_parameters = %d\n\n", *number_parameters);
#else
#if INT_LONG
           "*number_parameters = %ld\n\n", *number_parameters);
#else
           "*number_parameters = %d\n\n", *number_parameters);
#endif
#endif

  /* print the min, max, current values, and types of parameters */
  fprintf (ptr_asa_out, "index_v parameter_minimum parameter_maximum\
 parameter_value parameter_type \n");

#if ASA_PRINT_INTERMED
  VFOR (index_v) fprintf (ptr_asa_out,
#if INT_ALLOC
                          " %-8d %-*.*g \t\t %-*.*g \t %-*.*g %-7d\n",
#else
#if INT_LONG
                          " %-8ld %-*.*g \t\t %-*.*g \t %-*.*g %-7d\n",
#else
                          " %-8d %-*.*g \t\t %-*.*g \t %-*.*g %-7d\n",
#endif
#endif
                          index_v,
                          G_FIELD, G_PRECISION, parameter_minimum[index_v],
                          G_FIELD, G_PRECISION, parameter_maximum[index_v],
                          G_FIELD, G_PRECISION,
                          current_generated_state->parameter[index_v],
                          parameter_type[index_v]);

  fprintf (ptr_asa_out, "\n\n");
#endif /* ASA_PRINT_INTERMED */
  /* Print out user-defined OPTIONS */

⌨️ 快捷键说明

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