/***********************************************************************
* Adaptive Simulated Annealing (ASA)
* Lester Ingber <ingber@ingber.com>
* Copyright (c) 1993-1996 Lester Ingber.  All Rights Reserved.
* The LICENSE file must be included with ASA code.
***********************************************************************/

 /* $Id: asa_user.h,v 13.5 1996/08/16 10:06:49 ingber Exp ingber $ */

 /* asa_user.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>

/* test for memory leaks */
/* #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

#ifndef ASA_TEMPLATE_LIB
#define ASA_TEMPLATE_LIB FALSE
#endif
#if ASA_TEMPLATE_LIB
#define ASA_LIB TRUE
#define ASA_TEST TRUE
#endif

#ifndef ASA_TEMPLATE_ASA_OUT_PID
#define ASA_TEMPLATE_ASA_OUT_PID FALSE
#endif
#if ASA_TEMPLATE_ASA_OUT_PID
#define USER_ASA_OUT TRUE
#endif

#ifndef ASA_TEMPLATE_MULTIPLE
#define ASA_TEMPLATE_MULTIPLE FALSE
#endif
#if ASA_TEMPLATE_MULTIPLE
#define COST_FILE FALSE
#define USER_ASA_OUT TRUE
#define ASA_TEST TRUE
#define QUENCH_COST TRUE
#define QUENCH_PARAMETERS TRUE
#define OPTIONS_FILE FALSE
#endif

#ifndef ASA_TEMPLATE_SELFOPT
#define ASA_TEMPLATE_SELFOPT FALSE
#endif
#if ASA_TEMPLATE_SELFOPT
#define COST_FILE FALSE
#define SELF_OPTIMIZE TRUE
#define OPTIONAL_DATA TRUE
#define USER_ASA_OUT TRUE
#define ASA_TEST TRUE
#define OPTIONS_FILE FALSE
#endif

#ifndef ASA_TEMPLATE_SAMPLE
#define ASA_TEMPLATE_SAMPLE FALSE
#endif
#if ASA_TEMPLATE_SAMPLE
#define COST_FILE FALSE
#define ASA_SAMPLE TRUE
#define USER_ACCEPTANCE_TEST TRUE
#define USER_COST_SCHEDULE TRUE
#define OPTIONS_FILE_DATA FALSE
#define USER_ACCEPT_ASYMP_EXP TRUE
#endif

#ifndef ASA_TEMPLATE_PARALLEL
#define ASA_TEMPLATE_PARALLEL FALSE
#endif
#if ASA_TEMPLATE_PARALLEL
#define COST_FILE FALSE
#define ASA_TEST TRUE
#define ASA_PARALLEL TRUE
#endif

#ifndef ASA_TEMPLATE_SAVE
#define ASA_TEMPLATE_SAVE FALSE
#endif
#if ASA_TEMPLATE_SAVE
#define COST_FILE FALSE
#define ASA_TEST TRUE
#define ASA_SAVE TRUE
#define OPTIONAL_DATA TRUE
#define QUENCH_PARAMETERS TRUE
#define QUENCH_COST TRUE
#endif

#ifndef ASA_TEMPLATE_QUEUE
#define ASA_TEMPLATE_QUEUE FALSE
#endif
#if ASA_TEMPLATE_QUEUE
#define ASA_QUEUE TRUE
#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

 /* Standard Pre-Compile Options */

#ifndef OPTIONS_FILE
#define OPTIONS_FILE TRUE
#endif

#if OPTIONS_FILE
#ifndef OPTIONS_FILE_DATA
#define OPTIONS_FILE_DATA TRUE
#endif
#else
#define OPTIONS_FILE_DATA FALSE
#endif

#ifndef RECUR_OPTIONS_FILE
#define RECUR_OPTIONS_FILE FALSE
#endif

#if RECUR_OPTIONS_FILE
#ifndef RECUR_OPTIONS_FILE_DATA
#define RECUR_OPTIONS_FILE_DATA FALSE
#endif
#else
#define RECUR_OPTIONS_FILE_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 TRUE
#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_COST_TEMP
#define USER_INITIAL_COST_TEMP FALSE
#endif

#ifndef RATIO_TEMPERATURE_SCALES
#define RATIO_TEMPERATURE_SCALES FALSE
#endif

#ifndef USER_INITIAL_PARAMETERS_TEMPS
#define USER_INITIAL_PARAMETERS_TEMPS FALSE
#endif

#ifndef DELTA_PARAMETERS
#define DELTA_PARAMETERS FALSE
#endif

#ifndef QUENCH_PARAMETERS
#define QUENCH_PARAMETERS FALSE
#endif

#ifndef QUENCH_COST
#define QUENCH_COST FALSE
#endif

#ifndef QUENCH_PARAMETERS_SCALE
#define QUENCH_PARAMETERS_SCALE TRUE
#endif

#ifndef QUENCH_COST_SCALE
#define QUENCH_COST_SCALE TRUE
#endif

#ifndef OPTIONAL_DATA
#define OPTIONAL_DATA FALSE
#endif

#ifndef OPTIONAL_DATA_INT
#define OPTIONAL_DATA_INT FALSE
#endif

#ifndef USER_REANNEAL_COST
#define USER_REANNEAL_COST FALSE
#endif

#ifndef USER_REANNEAL_PARAMETERS
#define USER_REANNEAL_PARAMETERS FALSE
#endif

#ifndef MAXIMUM_REANNEAL_INDEX
#define MAXIMUM_REANNEAL_INDEX 50000
#endif

#ifndef REANNEAL_SCALE
#define REANNEAL_SCALE 10
#endif

#ifndef USER_COST_SCHEDULE
#define USER_COST_SCHEDULE FALSE
#endif

#ifndef USER_ACCEPT_ASYMP_EXP
#define USER_ACCEPT_ASYMP_EXP FALSE
#endif

#ifndef USER_ACCEPTANCE_TEST
#define USER_ACCEPTANCE_TEST FALSE
#endif

#ifndef USER_GENERATING_FUNCTION
#define USER_GENERATING_FUNCTION FALSE
#endif

#define INTEGER_TYPE		((int) 1)
#define REAL_TYPE		((int) -1)
#define INTEGER_NO_REANNEAL	((int) 2)
#define REAL_NO_REANNEAL	((int) -2)

 /* Set this to TRUE to self-optimize the Program Options */
#ifndef SELF_OPTIMIZE
#define SELF_OPTIMIZE FALSE
#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
#define ASA_SAVE 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

 /* Program Options */

typedef struct
  {
    LONG_INT Limit_Acceptances;
    LONG_INT Limit_Generated;
    int Limit_Invalid_Generated_States;
    double Accepted_To_Generated_Ratio;

    double Cost_Precision;
    int Maximum_Cost_Repeat;
    int Number_Cost_Samples;
    double Temperature_Ratio_Scale;
    double Cost_Parameter_Scale_Ratio;
    double Temperature_Anneal_Scale;
#if USER_INITIAL_COST_TEMP
    double *User_Cost_Temperature;
#endif

    int Include_Integer_Parameters;
    int User_Initial_Parameters;
    ALLOC_INT Sequential_Parameters;
    double Initial_Parameter_Temperature;
#if RATIO_TEMPERATURE_SCALES
    double *User_Temperature_Ratio;
#endif
#if USER_INITIAL_PARAMETERS_TEMPS
    double *User_Parameter_Temperature;
#endif

    int Acceptance_Frequency_Modulus;
    int Generated_Frequency_Modulus;
    int Reanneal_Cost;
    int Reanneal_Parameters;

    double Delta_X;
#if DELTA_PARAMETERS
    double *User_Delta_Parameter;
#endif
    int User_Tangents;
    int Curvature_0;

#if QUENCH_PARAMETERS
    double *User_Quench_Param_Scale;
#endif
#if QUENCH_COST
    double *User_Quench_Cost_Scale;
#endif

    LONG_INT N_Accepted;
    LONG_INT N_Generated;
    int Immediate_Exit;

#if OPTIONAL_DATA
    ALLOC_INT Asa_Data_Dim;
    double *Asa_Data;
#endif
#if OPTIONAL_DATA_INT
    ALLOC_INT Asa_Data_Dim_Int;
    LONG_INT *Asa_Data_Int;
#endif
#if USER_ASA_OUT
    char *Asa_Out_File;
#endif
#if USER_COST_SCHEDULE
    double (*Cost_Schedule) ();
#endif
#if USER_ACCEPT_ASYMP_EXP
    double Asymp_Exp_Param;
#endif
#if USER_ACCEPTANCE_TEST
    void (*Acceptance_Test) ();
    int User_Acceptance_Flag;
    int Cost_Acceptance_Flag;
    double Last_Cost;
    double Cost_Temp_Curr;
    double Cost_Temp_Init;
    double Cost_Temp_Scale;
    double Prob_Bias;
    LONG_INT *Random_Seed;
#endif
#if USER_GENERATING_FUNCTION
    double (*Generating_Distrib) ();
#endif
#if USER_REANNEAL_COST
    int (*Reanneal_Cost_Function) ();
#endif
#if USER_REANNEAL_PARAMETERS
    double (*Reanneal_Params_Function) ();
#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_Resolution;
#endif
#if ASA_RESOLUTION
    double *Coarse_Resolution;
#endif
#if ASA_PARALLEL
    int Gener_Mov_Avr;
    LONG_INT Gener_Block;
    LONG_INT Gener_Block_Max;
#endif
  }
USER_DEFINES;

 /* system function prototypes */

#if HAVE_ANSI

/* This block gives trouble under some Ultrix */
#if FALSE
int fprintf (FILE * fp, char *string,...);
void exit (int code);
#endif

#if IO_PROTOTYPES
//int fprintf ();
int fflush (FILE * fp);
int fclose (FILE * fp);
void exit ();
//int fread ();
//int fwrite ();
int system ();
#endif

double asa (double (*user_cost_function) (
			   double *, double *, double *, double *, double *,
			  ALLOC_INT *, int *, int *, int *, USER_DEFINES *),
	    double (*user_random_generator) (LONG_INT *),
	    LONG_INT * rand_seed,
	    double *parameter_initial_final,
	    double *parameter_minimum,
	    double *parameter_maximum,
	    double *tangents,
	    double *curvature,
	    ALLOC_INT * number_parameters,
	    int *parameter_type,
	    int *valid_state_generated_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 fflush ();
int fclose ();
int fread ();
int fwrite ();
int system ();
#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 */
#include <malloc.h>
//extern void *memalign(size_t,size_t);
//#define mycxalloc(nelem,elsize) memalign(sizeof(double), (nelem)*(elsize))
#define mycxalloc(nelem,elsize) malloc((nelem)*(elsize))
