-
Notifications
You must be signed in to change notification settings - Fork 1
/
asa.c
6474 lines (6089 loc) · 234 KB
/
asa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
* Adaptive Simulated Annealing (ASA)
* Lester Ingber <ingber@ingber.com>
* Copyright (c) 1993-2005 Lester Ingber. All Rights Reserved.
* The LICENSE file must be included with ASA code.
***********************************************************************/
#define ASA_ID \
"/* $Id: asa.c,v 25.27 2005/04/03 17:44:54 ingber Exp ingber $ */"
#include "asa.h"
char exit_msg[160]; /* temp storage for exit messages */
/***********************************************************************
* asa
* This procedure implements the full ASA function optimization.
***********************************************************************/
#if !ASA_SAVE && !ASA_FILE_PIPE
#define fprintf if (ptr_asa_out)\
fprintf
#define fflush if (ptr_asa_out)\
fflush
#endif /*!ASA_SAVE && !ASA_FILE_PIPE*/
#if HAVE_ANSI
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 * 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)
#else
double
asa (user_cost_function,
user_random_generator,
seed,
parameter_initial_final,
parameter_minimum,
parameter_maximum,
tangents,
curvature,
number_parameters,
parameter_type, valid_state_generated_flag, exit_status, OPTIONS)
double (*user_cost_function) ();
double (*user_random_generator) ();
LONG_INT *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;
#endif /* HAVE_ANSI */
{
#if USER_INITIAL_COST_TEMP
#if USER_REANNEAL_COST
#else
int index_cost_constraint; /* index cost functions averaged */
#endif /* USER_REANNEAL_COST */
#else /* USER_INITIAL_COST_TEMP */
int index_cost_constraint; /* index cost functions averaged */
#endif /* USER_INITIAL_COST_TEMP */
int index_cost_repeat, /* test OPTIONS->Cost_Precision when =
OPTIONS->Maximum_Cost_Repeat */
tmp_var_int, tmp_var_int1, tmp_var_int2; /* temporary integers */
ALLOC_INT index_v, /* iteration index */
*start_sequence; /* initial OPTIONS->Sequential_Parameters
used if >= 0 */
double final_cost, /* best cost to return to user */
tmp_var_db, tmp_var_db1, tmp_var_db2; /* temporary doubles */
int *curvature_flag;
FILE *ptr_asa_out; /* file ptr to output file */
/* The 3 states that are kept track of during the annealing process */
STATE *current_generated_state, *last_saved_state, *best_generated_state;
#if ASA_SAVE
FILE *ptr_save, *ptr_comm;
int asa_read;
char asa_save_comm[100];
#if ASA_SAVE_OPT
char read_option[80];
char read_if[4], read_ASAFALSE[6], read_comm1[3], read_ASA_SAVE[9],
read_comm2[3];
int read_int;
#if INT_LONG
LONG_INT read_long;
#endif
double read_double;
FILE *ptr_save_opt;
#endif
#endif /* ASA_SAVE */
#if ASA_PIPE_FILE
FILE *ptr_asa_pipe;
#endif
int immediate_flag; /* save Immediate_Exit */
int asa_exit_value;
double xnumber_parameters[1];
/* The array of tangents (absolute value of the numerical derivatives),
and the maximum |tangent| of the array */
double *maximum_tangent;
/* ratio of acceptances to generated points - determines when to
test/reanneal */
double *accepted_to_generated_ratio;
/* temperature parameters */
double temperature_scale, *temperature_scale_parameters;
/* relative scalings of cost and parameters to temperature_scale */
double *temperature_scale_cost;
double *current_user_parameter_temp;
double *initial_user_parameter_temp;
double *current_cost_temperature;
double *initial_cost_temperature;
double log_new_temperature_ratio; /* current *temp = initial *temp *
exp(log_new_temperature_ratio) */
ALLOC_INT *index_exit_v; /* information for asa_exit */
/* counts of generated states and acceptances */
LONG_INT *index_parameter_generations;
LONG_INT *number_generated, *best_number_generated_saved;
LONG_INT *recent_number_generated, *number_accepted;
LONG_INT *recent_number_acceptances, *index_cost_acceptances;
LONG_INT *number_acceptances_saved, *best_number_accepted_saved;
/* Flag indicates that the parameters generated were
invalid according to the cost function validity criteria. */
LONG_INT *number_invalid_generated_states;
LONG_INT repeated_invalid_states;
#if ASA_QUEUE
int queue_new; /* flag to add new entry */
int *save_queue_flag; /* save valid_state_generated_flag */
LONG_INT queue; /* index of queue */
LONG_INT queue_v; /* index of parameters in queue */
LONG_INT save_queue_test; /* test if all parameters are present */
LONG_INT save_queue; /* last filled position in queue */
LONG_INT save_queue_indx; /* current position in queue */
double *save_queue_cost, *save_queue_param; /* saved states */
ALLOC_INT queue_size_tmp;
#endif
#if MULTI_MIN
int multi_index;
int multi_test, multi_test_cmp, multi_test_dim;
int *multi_sort;
double *multi_cost;
double **multi_params;
#endif /* MULTI_MIN */
#if ASA_PARALLEL
LONG_INT *parallel_sort;
LONG_INT index_parallel, sort_index; /* count of parallel generated states */
LONG_INT parallel_generated; /* saved *recent_number_generated */
LONG_INT parallel_block_max; /* saved OPTIONS->Gener_Block_Max */
STATE *gener_block_state;
#endif
/* used to index repeated and recursive calls to asa */
/* This assumes that multiple calls (>= 1) _or_ recursive
calls are being made to asa */
static int asa_open = ASAFALSE;
static int number_asa_open = 0;
static int recursive_asa_open = 0;
/* initializations */
if ((curvature_flag = (int *) calloc (1, sizeof (int))) == NULL) {
strcpy (exit_msg, "asa(): curvature_flag");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((maximum_tangent = (double *) calloc (1, sizeof (double))) == NULL) {
strcpy (exit_msg, "asa(): maximum_tangent");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((accepted_to_generated_ratio =
(double *) calloc (1, sizeof (double))) == NULL) {
strcpy (exit_msg, "asa(): accepted_to_generated_ratio");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((temperature_scale_cost =
(double *) calloc (1, sizeof (double))) == NULL) {
strcpy (exit_msg, "asa(): temperature_scale_cost");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((current_cost_temperature =
(double *) calloc (1, sizeof (double))) == NULL) {
strcpy (exit_msg, "asa(): current_cost_temperature");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((initial_cost_temperature =
(double *) calloc (1, sizeof (double))) == NULL) {
strcpy (exit_msg, "asa(): initial_cost_temperature");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((index_exit_v = (ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (exit_msg, "asa(): index_exit_v");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((start_sequence = (ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (exit_msg, "asa(): start_sequence");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((number_generated =
(ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (exit_msg, "asa(): number_generated");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((best_number_generated_saved =
(ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (exit_msg, "asa(): best_number_generated_saved");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((recent_number_generated =
(ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (exit_msg, "asa(): recent_number_generated");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((number_accepted =
(ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (exit_msg, "asa(): number_accepted");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((recent_number_acceptances =
(ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (exit_msg, "asa(): recent_number_acceptances");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((index_cost_acceptances =
(ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (exit_msg, "asa(): index_cost_acceptances");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((number_acceptances_saved =
(ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (exit_msg, "asa(): number_acceptances_saved");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((best_number_accepted_saved =
(ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (exit_msg, "asa(): best_number_accepted_saved");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((number_invalid_generated_states =
(ALLOC_INT *) calloc (1, sizeof (ALLOC_INT))) == NULL) {
strcpy (exit_msg, "asa(): number_invalid_generated_states");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((current_generated_state =
(STATE *) calloc (1, sizeof (STATE))) == NULL) {
strcpy (exit_msg, "asa(): current_generated_state");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((last_saved_state = (STATE *) calloc (1, sizeof (STATE))) == NULL) {
strcpy (exit_msg, "asa(): last_saved_state");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
if ((best_generated_state = (STATE *) calloc (1, sizeof (STATE))) == NULL) {
strcpy (exit_msg, "asa(): best_generated_state");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
#if ASA_PARALLEL
if ((gener_block_state =
(STATE *) calloc (OPTIONS->Gener_Block_Max, sizeof (STATE))) == NULL) {
strcpy (exit_msg, "asa(): gener_block_state");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
gener_block_state_qsort = gener_block_state;
if ((parallel_sort =
(LONG_INT *) calloc (OPTIONS->Gener_Block_Max,
sizeof (LONG_INT))) == NULL) {
strcpy (exit_msg, "asa(): parallel_sort");
Exit_ASA (exit_msg);
*exit_status = CALLOC_FAILED;
return (-1);
}
#endif
/* set default */
ptr_asa_out = (FILE *) NULL;
OPTIONS->Immediate_Exit = ASAFALSE;
if (asa_open == ASAFALSE) {
asa_open = ASATRUE;
++number_asa_open;
#if ASA_PRINT
if (number_asa_open == 1) {
/* open the output file */
#if USER_ASA_OUT
if (!strcmp (OPTIONS->Asa_Out_File, "STDOUT")) {
#if INCL_STDOUT
ptr_asa_out = stdout;
#endif /* INCL_STDOUT */
}
else if (strcmp (OPTIONS->Asa_Out_File, "NULL")){
#if ASA_SAVE
ptr_asa_out = fopen (OPTIONS->Asa_Out_File, "a");
#else
ptr_asa_out = fopen (OPTIONS->Asa_Out_File, "w");
#endif
}
#else /* USER_ASA_OUT */
if (!strcmp (ASA_OUT, "STDOUT")) {
#if INCL_STDOUT
ptr_asa_out = stdout;
#endif /* INCL_STDOUT */
} else {
#if ASA_SAVE
ptr_asa_out = fopen (ASA_OUT, "a");
#else
ptr_asa_out = fopen (ASA_OUT, "w");
#endif
}
#endif /* USER_ASA_OUT */
} else {
#if USER_ASA_OUT
if (!strcmp (OPTIONS->Asa_Out_File, "STDOUT")) {
#if INCL_STDOUT
ptr_asa_out = stdout;
#endif /* INCL_STDOUT */
} else if(strcmp (OPTIONS->Asa_Out_File, "NULL")) {
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 */
if (ptr_asa_out)
print_asa_options (ptr_asa_out, OPTIONS);
#if TIME_CALC
/* print starting time */
if (ptr_asa_out)
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 = ASAFALSE;
/* 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 = ASATRUE;
OPTIONS->Cost_Acceptance_Flag = ASAFALSE;
#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 */
#if DELTA_PARAMETERS
VFOR (index_v) fprintf (ptr_asa_out,
#if INT_ALLOC
"OPTIONS->User_Delta_Parameter[%d] = %*.*g\n",
#else
#if INT_LONG
"OPTIONS->User_Delta_Parameter[%ld] = %*.*g\n",
#else
"OPTIONS->User_Delta_Parameter[%d] = %*.*g\n",
#endif
#endif
index_v,
G_FIELD, G_PRECISION,
OPTIONS->User_Delta_Parameter[index_v]);
fprintf (ptr_asa_out, "\n");
#endif /* DELTA_PARAMETERS */
#if QUENCH_PARAMETERS
VFOR (index_v) fprintf (ptr_asa_out,
#if INT_ALLOC
"OPTIONS->User_Quench_Param_Scale[%d] = %*.*g\n",
#else
#if INT_LONG
"OPTIONS->User_Quench_Param_Scale[%ld] = %*.*g\n",
#else
"OPTIONS->User_Quench_Param_Scale[%d] = %*.*g\n",
#endif
#endif
index_v,
G_FIELD, G_PRECISION,
OPTIONS->User_Quench_Param_Scale[index_v]);
#endif /* QUENCH_PARAMETERS */
#if QUENCH_COST
fprintf (ptr_asa_out,
"\nOPTIONS->User_Quench_Cost_Scale = %*.*g\n\n",
G_FIELD, G_PRECISION, OPTIONS->User_Quench_Cost_Scale[0]);
#endif /* QUENCH_COST */
#if USER_INITIAL_PARAMETERS_TEMPS
VFOR (index_v) fprintf (ptr_asa_out,
#if INT_ALLOC
"OPTIONS->User_Parameter_Temperature[%d] = %*.*g\n",
#else
#if INT_LONG
"OPTIONS->User_Parameter_Temperature[%ld] = %*.*g\n",
#else
"OPTIONS->User_Parameter_Temperature[%d] = %*.*g\n",
#endif
#endif
index_v,
G_FIELD, G_PRECISION,
initial_user_parameter_temp[index_v]);
#endif /* USER_INITIAL_PARAMETERS_TEMPS */
#if RATIO_TEMPERATURE_SCALES
VFOR (index_v) fprintf (ptr_asa_out,
#if INT_ALLOC
"OPTIONS->User_Temperature_Ratio[%d] = %*.*g\n",
#else
#if INT_LONG
"OPTIONS->User_Temperature_Ratio[%ld] = %*.*g\n",
#else
"OPTIONS->User_Temperature_Ratio[%d] = %*.*g\n",
#endif
#endif
index_v,
G_FIELD, G_PRECISION,
OPTIONS->User_Temperature_Ratio[index_v]);
#endif /* RATIO_TEMPERATURE_SCALES */
#if USER_INITIAL_COST_TEMP
fprintf (ptr_asa_out,
"OPTIONS->User_Cost_Temperature[0] = %*.*g\n",
G_FIELD, G_PRECISION, *initial_cost_temperature);
#endif /* USER_INITIAL_COST_TEMP */
fflush (ptr_asa_out);
#endif /* ASA_PRINT */
#if MULTI_MIN
#if ASA_PRINT
fprintf (ptr_asa_out, "\n");
fprintf (ptr_asa_out, "Multi_Number = %d\n", OPTIONS->Multi_Number);
fprintf (ptr_asa_out, "Multi_Specify = %d\n", OPTIONS->Multi_Specify);
#if ASA_RESOLUTION
#else
VFOR (index_v) {
fprintf (ptr_asa_out,
#if INT_ALLOC
"Multi_Grid[%d] = %*.*g\n",
#else
#if INT_LONG
"Multi_Grid[%ld] = %*.*g\n",
#else
"Multi_Grid[%d] = %*.*g\n",
#endif
#endif
index_v, G_FIELD, G_PRECISION, OPTIONS->Multi_Grid[index_v]);
}
#endif /* ASA_RESOLUTION */
fprintf (ptr_asa_out, "\n");
fflush (ptr_asa_out);
#endif /* ASA_PRINT */
#endif /* MULTI_MIN */
#if ASA_PARALLEL
#if ASA_PRINT
fprintf (ptr_asa_out,
#if INT_LONG
"Initial ASA_PARALLEL OPTIONS->\n\t Gener_Block = %ld\n\
\t Gener_Block_Max = %ld\n \t Gener_Mov_Avr= %d\n\n",
#else
"ASA_PARALLEL OPTIONS->\n\t Gener_Block = %d\n\
\t Gener_Block_Max = %d\n \t Gener_Mov_Avr= %d\n\n",
#endif
OPTIONS->Gener_Block, OPTIONS->Gener_Block_Max,
OPTIONS->Gener_Mov_Avr);
#endif
#endif /* ASA_PARALLEL */
#if ASA_SAMPLE
#if ASA_PRINT
fprintf (ptr_asa_out, "OPTIONS->Limit_Weights = %*.*g\n\n",
G_FIELD, G_PRECISION, OPTIONS->Limit_Weights);
#endif
#endif
if (OPTIONS->Asa_Recursive_Level > asa_recursive_max)
asa_recursive_max = OPTIONS->Asa_Recursive_Level;
#if ASA_SAVE
if (OPTIONS->Asa_Recursive_Level > 0)
sprintf (asa_save_comm, "asa_save_%d", OPTIONS->Asa_Recursive_Level);
else
sprintf (asa_save_comm, "asa_save");
if ((ptr_save = fopen (asa_save_comm, "r")) == NULL) {
asa_read = ASAFALSE;
} else {
#if ASA_PRINT
fprintf (ptr_asa_out, "\n\n\trestart after ASA_SAVE\n\n");
#endif
fclose (ptr_save);
asa_read = ASATRUE;
/* give some value to avoid any problems with other OPTIONS */
#if USER_ACCEPTANCE_TEST
OPTIONS->Cost_Temp_Curr = OPTIONS->Cost_Temp_Init =
#endif
current_generated_state->cost
= *initial_cost_temperature = *current_cost_temperature = 3.1416;
}
#endif
tmp_var_int = cost_function_test (current_generated_state->cost,
current_generated_state->parameter,
parameter_minimum,
parameter_maximum, number_parameters,
xnumber_parameters);
/* compute temperature scales */
tmp_var_db1 = -F_LOG ((OPTIONS->Temperature_Ratio_Scale));
tmp_var_db2 = F_LOG (OPTIONS->Temperature_Anneal_Scale);
temperature_scale =
tmp_var_db1 * F_EXP (-tmp_var_db2 / *xnumber_parameters);
/* set here in case not used */
tmp_var_db = ZERO;
#if QUENCH_PARAMETERS
#if RATIO_TEMPERATURE_SCALES
VFOR (index_v) temperature_scale_parameters[index_v] = tmp_var_db1 * F_EXP
#if QUENCH_PARAMETERS_SCALE
(-(tmp_var_db2 * OPTIONS->User_Quench_Param_Scale[index_v])
#else
(-(tmp_var_db2)
#endif
/ *xnumber_parameters)
* OPTIONS->User_Temperature_Ratio[index_v];
#else
VFOR (index_v) temperature_scale_parameters[index_v] = tmp_var_db1 * F_EXP
#if QUENCH_PARAMETERS_SCALE
(-(tmp_var_db2 * OPTIONS->User_Quench_Param_Scale[index_v])
#else
(-(tmp_var_db2)
#endif
/ *xnumber_parameters);
#endif /* RATIO_TEMPERATURE_SCALES */
#else /* QUENCH_PARAMETERS */
#if RATIO_TEMPERATURE_SCALES
VFOR (index_v)
temperature_scale_parameters[index_v] =
tmp_var_db1 * F_EXP (-(tmp_var_db2) / *xnumber_parameters)
* OPTIONS->User_Temperature_Ratio[index_v];
#else
VFOR (index_v)
temperature_scale_parameters[index_v] =
tmp_var_db1 * F_EXP (-(tmp_var_db2) / *xnumber_parameters);
#endif /* RATIO_TEMPERATURE_SCALES */
#endif /* QUENCH_PARAMETERS */
#if USER_ACCEPTANCE_TEST
OPTIONS->Cost_Temp_Scale =
#endif
*temperature_scale_cost =
#if QUENCH_COST
#if QUENCH_COST_SCALE
tmp_var_db1 * F_EXP (-(tmp_var_db2 * OPTIONS->User_Quench_Cost_Scale[0])
#else
tmp_var_db1 * F_EXP (-(tmp_var_db2)
#endif
/ *xnumber_parameters) *
OPTIONS->Cost_Parameter_Scale_Ratio;
#else /* QUENCH_COST */
tmp_var_db1 * F_EXP (-(tmp_var_db2)
/ *xnumber_parameters) *
OPTIONS->Cost_Parameter_Scale_Ratio;
#endif /* QUENCH_COST */
/* set the initial index of parameter generations to 1 */
VFOR (index_v) index_parameter_generations[index_v] = 1;
/* test user-defined options before calling cost function */
tmp_var_int = asa_test_asa_options (seed,
parameter_initial_final,
parameter_minimum,
parameter_maximum,
tangents,
curvature,
number_parameters,
parameter_type,
valid_state_generated_flag,
exit_status, ptr_asa_out, OPTIONS);
if (tmp_var_int > 0) {
#if ASA_PRINT
fprintf (ptr_asa_out, "total number invalid OPTIONS = %d\n", tmp_var_int);
fflush (ptr_asa_out);
#endif
*exit_status = INVALID_USER_INPUT;
goto EXIT_ASA;
}
#if USER_INITIAL_COST_TEMP
#else
#if ASA_SAVE
if (asa_read == ASATRUE)
OPTIONS->Number_Cost_Samples = 1;
#endif
/* calculate the average cost over samplings of the cost function */
if (OPTIONS->Number_Cost_Samples < -1) {
tmp_var_db1 = ZERO;
tmp_var_db2 = ZERO;
tmp_var_int = -OPTIONS->Number_Cost_Samples;