#ifndef _ASA_USER_ASA_H _
#define _ASA_USER_ASA_H _
#ifdef __cplusplus
extern "C" {
#endif
/*************** *************** *************** *************** ***********
* Adaptive Simulated Annealing (ASA)
* Lester Ingber <ingber@ingber. com>
* Copyright (c) 1987-2010 Lester Ingber. All Rights Reserved.
* The ASA-LICENSE file must be included with ASA code.
*************** *************** *************** *************** ***********/
/* $Id: asa_usr_asa.h,v 26.38 2010/01/04 23:43:10 ingber Exp ingber $ */
/* asa_usr_asa.h for Adaptive Simulated Annealing */
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h> /* misc defs on most machines */
#include <string.h>
/* required if use machine-defined {DBL_EPSILON DBL_MIN DBL_MAX} */
/* #include <float.h> */
/* test for memory leaks */
/* #include "/usr/local/include/leak.h" */
#define TRUE 1
#define FALSE 0
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
/* DEFAULT PARAMETERS SETTINGS */
/* Pre-Compile Options */
/* Special ASA_TEMPLATEs */
#ifndef MY_TEMPLATE
#define MY_TEMPLATE TRUE
#endif
#if MY_TEMPLATE /* MY_TEMPLATE_asa _user */
/* you can add your own set of #define here */
#endif /* MY_TEMPLATE */
#ifndef ASA_TEMPLATE_LI B
#define ASA_TEMPLATE_LI B FALSE
#endif
#if ASA_TEMPLATE_LI B
#define ASA_LIB TRUE
#define ASA_TEST TRUE
#endif
#ifndef ASA_TEMPLATE_AS A_OUT_PID
#define ASA_TEMPLATE_AS A_OUT_PID FALSE
#endif
#if ASA_TEMPLATE_AS A_OUT_PID
#define USER_ASA_OUT TRUE
#endif
#ifndef ASA_TEMPLATE_MU LTIPLE
#define ASA_TEMPLATE_MU LTIPLE FALSE
#endif
#if ASA_TEMPLATE_MU LTIPLE
#define COST_FILE FALSE
#define USER_ASA_OUT TRUE
#define ASA_TEST TRUE
#define QUENCH_COST TRUE
#define QUENCH_PARAMETE RS TRUE
#define OPTIONS_FILE FALSE
#endif
#ifndef ASA_TEMPLATE_SE LFOPT
#define ASA_TEMPLATE_SE LFOPT FALSE
#endif
#if ASA_TEMPLATE_SE LFOPT
#define COST_FILE FALSE
#define SELF_OPTIMIZE TRUE
#define OPTIONAL_DATA_D BL TRUE
#define USER_ASA_OUT TRUE
#define ASA_TEST TRUE
#define OPTIONS_FILE FALSE
#endif
#ifndef ASA_TEMPLATE_SA MPLE
#define ASA_TEMPLATE_SA MPLE FALSE
#endif
#if ASA_TEMPLATE_SA MPLE
#define COST_FILE FALSE
#define ASA_SAMPLE TRUE
#define USER_ACCEPTANCE _TEST TRUE
#define USER_COST_SCHED ULE TRUE
#define OPTIONS_FILE_DA TA FALSE
#define USER_ACCEPT_ASY MP_EXP TRUE
#endif
#ifndef ASA_TEMPLATE_PA RALLEL
#define ASA_TEMPLATE_PA RALLEL FALSE
#endif
#if ASA_TEMPLATE_PA RALLEL
#define COST_FILE FALSE
#define ASA_TEST TRUE
#define ASA_PARALLEL TRUE
#endif
#ifndef ASA_TEMPLATE_SA VE
#define ASA_TEMPLATE_SA VE FALSE
#endif
#if ASA_TEMPLATE_SA VE
#define COST_FILE FALSE
#define ASA_TEST TRUE
#define ASA_SAVE TRUE
#define QUENCH_PARAMETE RS TRUE
#define QUENCH_COST TRUE
#endif
#ifndef ASA_TEMPLATE_QU EUE
#define ASA_TEMPLATE_QU EUE FALSE
#endif
#if ASA_TEMPLATE_QU EUE
#define ASA_QUEUE TRUE
#define ASA_RESOLUTION FALSE
#define ASA_TEST TRUE
#define COST_FILE FALSE
#define ASA_PRINT_MORE TRUE
#endif
#ifndef ASA_TEST_POINT
#define ASA_TEST_POINT FALSE
#endif
#if ASA_TEST_POINT
#define ASA_TEST TRUE
#define COST_FILE FALSE
#define SMALL_FLOAT 1.0E-50
#define QUENCH_COST TRUE
#endif
#ifndef ASA_EXIT_ANYTIM E
#define ASA_EXIT_ANYTIM E FALSE
#endif
/* Standard Pre-Compile Options */
#ifndef USER_COST_FUNCT ION
#define USER_COST_FUNCT ION cost_function
#endif
#if SELF_OPTIMIZE
#ifndef RECUR_USER_COST _FUNCTION
#define RECUR_USER_COST _FUNCTION recur_cost_func tion
#endif
#ifndef INCL_STDOUT
#define INCL_STDOUT FALSE
#endif
#endif
#ifndef INCL_STDOUT
#define INCL_STDOUT TRUE
#endif
#if INCL_STDOUT
#ifndef TIME_CALC
#define TIME_CALC FALSE
#endif
#endif
#ifndef OPTIONS_FILE
#define OPTIONS_FILE TRUE
#endif
#if OPTIONS_FILE
#ifndef OPTIONS_FILE_DA TA
#define OPTIONS_FILE_DA TA TRUE
#endif
#else
#define OPTIONS_FILE_DA TA FALSE
#endif
#ifndef RECUR_OPTIONS_F ILE
#define RECUR_OPTIONS_F ILE FALSE
#endif
#if RECUR_OPTIONS_F ILE
#ifndef RECUR_OPTIONS_F ILE_DATA
#define RECUR_OPTIONS_F ILE_DATA FALSE
#endif
#else
#define RECUR_OPTIONS_F ILE_DATA FALSE
#endif
#ifndef COST_FILE
#define COST_FILE TRUE
#endif
#ifndef ASA_LIB
#define ASA_LIB FALSE
#endif
#ifndef HAVE_ANSI
#define HAVE_ANSI TRUE
#endif
#ifndef IO_PROTOTYPES
#define IO_PROTOTYPES FALSE
#endif
#ifndef TIME_CALC
#define TIME_CALC FALSE
#endif
#ifndef INT_LONG
#define INT_LONG TRUE
#endif
#if INT_LONG
#define LONG_INT long int
#else
#define LONG_INT int
#endif
#ifndef INT_ALLOC
#define INT_ALLOC FALSE
#endif
#if INT_ALLOC
#define ALLOC_INT int
#else
#define ALLOC_INT LONG_INT
#endif
/* You can define SMALL_FLOAT to better correlate to your machine's
precision, i.e., as used in asa */
#ifndef SMALL_FLOAT
#define SMALL_FLOAT 1.0E-18
#endif
/* You can define your machine's maximum and minimum doubles here */
#ifndef MIN_DOUBLE
#define MIN_DOUBLE ((double) SMALL_FLOAT)
#endif
#ifndef MAX_DOUBLE
#define MAX_DOUBLE ((double) 1.0 / (double) SMALL_FLOAT)
#endif
#ifndef EPS_DOUBLE
#define EPS_DOUBLE ((double) SMALL_FLOAT)
#endif
#ifndef CHECK_EXPONENT
#define CHECK_EXPONENT FALSE
#endif
#ifndef ASA_TEST
#define ASA_TEST FALSE
#endif
#ifndef ASA_TEMPLATE
#define ASA_TEMPLATE FALSE
#endif
#ifndef USER_INITIAL_CO ST_TEMP
#define USER_INITIAL_CO ST_TEMP FALSE
#endif
#ifndef RATIO_TEMPERATU RE_SCALES
#define RATIO_TEMPERATU RE_SCALES FALSE
#endif
#ifndef USER_INITIAL_PA RAMETERS_TEMPS
#define USER_INITIAL_PA RAMETERS_TEMPS FALSE
#endif
#ifndef DELTA_PARAMETER S
#define DELTA_PARAMETER S FALSE
#endif
#ifndef QUENCH_PARAMETE RS
#define QUENCH_PARAMETE RS FALSE
#endif
#ifndef QUENCH_COST
#define QUENCH_COST FALSE
#endif
#ifndef QUENCH_PARAMETE RS_SCALE
#define QUENCH_PARAMETE RS_SCALE TRUE
#endif
#ifndef QUENCH_COST_SCA LE
#define QUENCH_COST_SCA LE TRUE
#endif
#ifndef OPTIONAL_DATA_D BL
#define OPTIONAL_DATA_D BL FALSE
#endif
#ifndef OPTIONAL_DATA_I NT
#define OPTIONAL_DATA_I NT FALSE
#endif
#ifndef OPTIONAL_DATA_P TR
#define OPTIONAL_DATA_P TR FALSE
#endif
#if OPTIONAL_DATA_P TR
/* user must define USER_TYPE; if a struct, it must be declared above */
#ifndef OPTIONAL_PTR_TY PE
#define OPTIONAL_PTR_TY PE USER_TYPE
#endif
#endif /* OPTIONAL_DATA_P TR */
#ifndef USER_REANNEAL_C OST
#define USER_REANNEAL_C OST FALSE
#endif
#ifndef USER_REANNEAL_P ARAMETERS
#define USER_REANNEAL_P ARAMETERS FALSE
#endif
#ifndef MAXIMUM_REANNEA L_INDEX
#define MAXIMUM_REANNEA L_INDEX 50000
#endif
#ifndef REANNEAL_SCALE
#define REANNEAL_SCALE 10
#endif
#ifndef USER_COST_SCHED ULE
#define USER_COST_SCHED ULE FALSE
#endif
#ifndef USER_ACCEPT_ASY MP_EXP
#define USER_ACCEPT_ASY MP_EXP FALSE
#endif
#ifndef USER_ACCEPT_THR ESHOLD
#define USER_ACCEPT_THR ESHOLD FALSE
#endif
#ifndef USER_ACCEPTANCE _TEST
#define USER_ACCEPTANCE _TEST FALSE
#endif
#ifndef USER_GENERATING _FUNCTION
#define USER_GENERATING _FUNCTION FALSE
#endif
/* in asa.c, field-width.precision = G_FIELD.G_PRECI SION */
#ifndef G_FIELD
#define G_FIELD 12
#endif
#ifndef G_PRECISION
#define G_PRECISION 7
#endif
#define INTEGER_TYPE ((int) 1)
#define REAL_TYPE ((int) -1)
#define INTEGER_NO_REAN NEAL ((int) 2)
#define REAL_NO_REANNEA L ((int) -2)
/* Set this to TRUE to self-optimize the Program Options */
#ifndef SELF_OPTIMIZE
#define SELF_OPTIMIZE FALSE
#endif
#ifndef USER_OUT
#define USER_OUT "asa_usr_ou t"
#endif
#ifndef USER_ASA_OUT
#define USER_ASA_OUT FALSE
#endif
#ifndef ASA_SAMPLE
#define ASA_SAMPLE FALSE
#endif
#ifndef ASA_QUEUE
#define ASA_QUEUE FALSE
#endif
#ifndef ASA_RESOLUTION
#define ASA_RESOLUTION FALSE
#endif
#ifndef ASA_PARALLEL
#define ASA_PARALLEL FALSE
#endif
#ifndef ASA_SAVE_OPT
#define ASA_SAVE_OPT FALSE
#endif
#if ASA_SAVE_OPT
#define ASA_SAVE TRUE
#endif
#ifndef ASA_SAVE_BACKUP
#define ASA_SAVE_BACKUP FALSE
#endif
#if ASA_SAVE_BACKUP
#define ASA_SAVE TRUE
#endif
#ifndef ASA_SAVE
#define ASA_SAVE FALSE
#endif
#ifndef ASA_PIPE
#define ASA_PIPE FALSE
#endif
#ifndef ASA_PIPE_FILE
#define ASA_PIPE_FILE FALSE
#endif
#ifndef FDLIBM_POW
#define FDLIBM_POW FALSE
#endif
#if FDLIBM_POW
#define F_POW s_pow
#else
#define F_POW pow
#endif
#ifndef FDLIBM_LOG
#define FDLIBM_LOG FALSE
#endif
#if FDLIBM_LOG
#define F_LOG s_log
#else
#define F_LOG log
#endif
#ifndef FDLIBM_EXP
#define FDLIBM_EXP FALSE
#endif
#if FDLIBM_EXP
#define F_EXP s_exp
#else
#define F_EXP exp
#endif
#ifndef FITLOC
#define FITLOC FALSE
#endif
#ifndef FITLOC_ROUND
#define FITLOC_ROUND TRUE
#endif
#ifndef FITLOC_PRINT
#define FITLOC_PRINT TRUE
#endif
#ifndef MULTI_MIN
#define MULTI_MIN FALSE
#endif
/* Program Options */
typedef struct {
LONG_INT Limit_Acceptanc es;
LONG_INT Limit_Generated ;
int Limit_Invalid_G enerated_States ;
double Accepted_To_Gen erated_Ratio;
double Cost_Precision;
int Maximum_Cost_Re peat;
int Number_Cost_Sam ples;
double Temperature_Rat io_Scale;
double Cost_Parameter_ Scale_Ratio;
double Temperature_Ann eal_Scale;
#if USER_INITIAL_CO ST_TEMP
double *User_Cost_Temp erature;
#endif
int Include_Integer _Parameters;
int User_Initial_Pa rameters;
ALLOC_INT Sequential_Para meters;
double Initial_Paramet er_Temperature;
#if RATIO_TEMPERATU RE_SCALES
double *User_Temperatu re_Ratio;
#endif
#if USER_INITIAL_PA RAMETERS_TEMPS
double *User_Parameter _Temperature;
#endif
int Acceptance_Freq uency_Modulus;
int Generated_Frequ ency_Modulus;
int Reanneal_Cost;
int Reanneal_Parame ters;
double Delta_X;
#if DELTA_PARAMETER S
double *User_Delta_Par ameter;
#endif
int User_Tangents;
int Curvature_0;
#if QUENCH_PARAMETE RS
double *User_Quench_Pa ram_Scale;
#endif
#if QUENCH_COST
double *User_Quench_Co st_Scale;
#endif
LONG_INT N_Accepted;
LONG_INT N_Generated;
int Locate_Cost;
int Immediate_Exit;
double *Best_Cost;
double *Best_Parameter s;
double *Last_Cost;
double *Last_Parameter s;
#if OPTIONAL_DATA_D BL
ALLOC_INT Asa_Data_Dim_Db l;
double *Asa_Data_Dbl;
#endif
#if OPTIONAL_DATA_I NT
ALLOC_INT Asa_Data_Dim_In t;
LONG_INT *Asa_Data_Int;
#endif
#if OPTIONAL_DATA_P TR
ALLOC_INT Asa_Data_Dim_Pt r;
OPTIONAL_PTR_TY PE *Asa_Data_Ptr;
#endif
#if USER_ASA_OUT
char *Asa_Out_File;
#endif
/* Keep OPTIONS_TMP in parameter lists in asa_usr.[ch] as they are
* needed if using recursively, e.g., with SELF_OPTIMIZE=T RUE.
* Make (USER_DEFINE *) casts explicit within functions. */
#if USER_COST_SCHED ULE
#if HAVE_ANSI
double (*Cost_Schedule ) (double current_cost_te mperature,
const void *OPTIONS_TMP);
#else /* HAVE_ANSI */
double (*Cost_Schedule ) ();
#endif /* HAVE_ANSI */
#endif
#if USER_ACCEPT_ASY MP_EXP
double Asymp_Exp_Param ;
#endif
#if USER_ACCEPTANCE _TEST
#if HAVE_ANSI
void (*Acceptance_Te st) (double cost,
double *parameter_mini mum,
double *parameter_maxi mum,
ALLOC_INT * number_paramete rs,
const void *OPTIONS_TMP);
#else /* HAVE_ANSI */
void (*Acceptance_Te st) ();
#endif /* HAVE_ANSI */
int User_Acceptance _Flag;
int Cost_Acceptance _Flag;
double Cost_Temp_Curr;
double Cost_Temp_Init;
double Cost_Temp_Scale ;
double Prob_Bias;
LONG_INT *Random_Seed;
#endif
#if USER_GENERATING _FUNCTION
#if HAVE_ANSI
double (*Generating_Di strib) (LONG_INT * seed,
ALLOC_INT * parameter_dimen sion,
ALLOC_INT index_v,
double temperature_v,
double init_param_temp _v,
double temp_scale_para ms_v,
double parameter_v,
double parameter_range _v,
double *last_saved_par ameter,
const void *OPTIONS_TMP);
#else /* HAVE_ANSI */
double (*Generating_Di strib) ();
#endif /* HAVE_ANSI */
#endif
#if USER_REANNEAL_C OST
#if HAVE_ANSI
int (*Reanneal_Cost _Function) (double *cost_best,
double *cost_last,
double *initial_cost_t emperature,
double *current_cost_t emperature,
const void *OPTIONS_TMP);
#else /* HAVE_ANSI */
int (*Reanneal_Cost _Function) ();
#endif /* HAVE_ANSI */
#endif
#if USER_REANNEAL_P ARAMETERS
#if HAVE_ANSI
double (*Reanneal_Para ms_Function) (double current_temp,
double tangent,
double max_tangent,
const void *OPTIONS_TMP);
#else /* HAVE_ANSI */
double (*Reanneal_Para ms_Function) ();
#endif /* HAVE_ANSI */
#endif
#if ASA_SAMPLE
double Bias_Acceptance ;
double *Bias_Generated ;
double Average_Weights ;
double Limit_Weights;
#endif
#if ASA_QUEUE
ALLOC_INT Queue_Size;
double *Queue_Resoluti on;
#endif
#if ASA_RESOLUTION
double *Coarse_Resolut ion;
#endif
#if FITLOC
int Fit_Local;
int Iter_Max;
double Penalty;
#endif
#if MULTI_MIN
int Multi_Number;
double *Multi_Cost;
double **Multi_Params;
double *Multi_Grid;
int Multi_Specify;
#endif
#if ASA_PARALLEL
int Gener_Mov_Avr;
LONG_INT Gener_Block;
LONG_INT Gener_Block_Max ;
#endif
#if ASA_SAVE
ALLOC_INT Random_Array_Di m;
double *Random_Array;
#endif
int Asa_Recursive_L evel;
} USER_DEFINES;
/* system function prototypes */
#if HAVE_ANSI
/* This block gives trouble under some Ultrix */
#if FALSE
int fprintf (FILE * fp, const char *string, ...);
int sprintf (char *s, const char *format, ...);
FILE *popen (const char *command, const char *mode);
void exit (int code);
#endif
#if IO_PROTOTYPES
int fprintf ();
int sprintf ();
int fflush (FILE * fp);
int fclose (FILE * fp);
void exit ();
int fread ();
int fwrite ();
int pclose ();
#endif
double
asa (double (*user_cost_fun ction)
(double *, double *, double *, double *, double *, ALLOC_INT *,
int *, int *, int *, USER_DEFINES *),
double (*user_random_g enerator) (LONG_INT *), LONG_INT * rand_seed,
double *parameter_init ial_final, double *parameter_mini mum,
double *parameter_maxi mum, double *tangents, double *curvature,
ALLOC_INT * number_paramete rs, int *parameter_type ,
int *valid_state_ge nerated_flag, int *exit_status,
USER_DEFINES * OPTIONS);
#if TIME_CALC
void print_time (char *message, FILE * ptr_out);
#endif
#if FDLIBM_POW
double s_pow (double x, double y);
#endif
#if FDLIBM_LOG
double s_log (double x);
#endif
#if FDLIBM_EXP
double s_exp (double x);
#endif
#else /* HAVE_ANSI */
#if IO_PROTOTYPES
int fprintf ();
int sprintf ();
int fflush ();
int fclose ();
int fread ();
int fwrite ();
FILE *popen ();
int pclose ();
#endif
double asa ();
#if TIME_CALC
void print_time ();
#endif
#if FDLIBM_POW
double s_pow ();
#endif
#if FDLIBM_LOG
double s_log ();
#endif
#if FDLIBM_EXP
double s_exp ();
#endif
#endif /* HAVE_ANSI */
#ifdef __cplusplus
}
#endif
#endif /* _ASA_USER_ASA_H _ */