-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglods_si.m
More file actions
1319 lines (1162 loc) · 43.7 KB
/
Copy pathglods_si.m
File metadata and controls
1319 lines (1162 loc) · 43.7 KB
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
function [glods_profile,Plist,flist,alfa,radius,func_eval] = glods_si(func_f,file_ini,x_ini,lbound,ubound)
%
% GLODS_SI - GLODS with Scale-Invariant (two-space) formulation
%
% This solver is a scale-invariant adaptation of the GLODS framework:
% all geometric operations (polling, distances, merging) are performed in
% normalized coordinates y in [0,1]^n, while objective values are evaluated
% in the original variables x in [lbound,ubound] via an affine map.
%
% Purpose:
% Solve the bound constrained problem:
% min f(x) s.t. lbound <= x <= ubound,
% where x is a real vector of dimension n. Derivatives are not used.
%
% Input:
% func_f Objective f(x) defined in original space
% file_ini Initialization file name (only used when list==4)
% x_ini Initial point in original space (only used when list==0)
% lbound Lower bounds (original space)
% ubound Upper bounds (original space)
%
% Output:
% glods_profile Best-so-far record vs function evaluations
% Plist Approximations to local minimizers (returned in original space)
% flist Corresponding function values
% alfa Corresponding step size parameters
% radius Corresponding comparison radii
% func_eval Total number of NEW function evaluations performed
%
% -------------------------------------------------------------------------
% Provenance and attribution:
% Based on the MATLAB reference implementation of GLODS:
% A. L. Custodio and J. F. A. Madeira,
% "GLODS: Global and Local Optimization using Direct Search",
% Journal of Global Optimization, 62 (2015), 1-28.
%
% Scale-invariant (two-space) reformulation introduced in:
% J. F. A. Madeira,
% "GLODS-SI: Scale-Invariant Global-Local Direct Search for
% Engineering Design Optimization",
% Journal of Computational Design and Engineering, 2026.
% Manuscript ID JCDE-2026-065.
%
% Copyright (C) 2026 J. F. A. Madeira.
% SPDX-License-Identifier: LGPL-3.0-or-later
% -------------------------------------------------------------------------
tStart = tic;
format long e;
% Note: no global "warning off all" — silence specific warnings only if needed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 1: Load parameters first
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
parameters_glods_si;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 2: Define SI map (x<->y) and evaluation wrapper f(y)=f_orig(y2x(y))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ensure column vectors
lbound_orig = lbound(:);
ubound_orig = ubound(:);
n_vars = length(lbound_orig);
if length(ubound_orig) ~= n_vars
error('Error: lbound and ubound must have the same dimension.');
end
scale = ubound_orig - lbound_orig;
if any(scale == 0)
error('Error: some variables have zero range (ubound == lbound). Handle fixed vars explicitly.');
end
% Normalized bounds in y-space
lbound_norm = zeros(n_vars,1);
ubound_norm = ones(n_vars,1);
% Map y -> x
y2x = @(y) lbound_orig + y .* scale;
% Objective wrapper: ALWAYS takes y, evaluates in x
func_f_orig = func_f;
func_f_eval = @(y) feval(func_f_orig, y2x(y));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 3: Initialization (Pini always in y)
% - list==0 and list==4 normalization happens inside init_glods_si
% - list in {1,2,3,5,6} generated in y
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[Pini, f_ini, alfa_list, radius_list, n, nPini, has_f_ini] = init_glods_si( ...
list, file_ini, x_ini, lbound_norm, ubound_norm, ...
user_list_size, nPini, lbound_orig, ubound_orig, scale);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 4: Cache initialization
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
func_eval = 0;
% Cache containers (used only when cache ~= 0; harmless when cache == 0).
CacheP = [];
CachenormP = [];
CacheF = [];
% Seed for stochastic init strategies (only affects list==1 LHS and list==2 random).
% rng('default') gives reproducible runs; switch to rng('shuffle') for non-reproducible.
if (list == 1) || (list == 2)
rng('default');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 5: Build initial lists (evaluate+merge unless list==4 provides f_ini)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Plist = [];
flist = [];
alfa = [];
radius = [];
active = [];
% Initialize profile (pre-allocated to the evaluation budget).
% Falls back to 10000 if max_fevals is not set or non-positive.
if exist('max_fevals','var') && isnumeric(max_fevals) && max_fevals > 0
glods_profile = zeros(1, max_fevals);
else
glods_profile = zeros(1, 10000);
end
% We mimic original behaviour: iterate through Pini and MERGE sequentially.
% - If list==4: use f_ini/alfa_list/radius_list (no evaluations)
% - Otherwise: evaluate points via evaluate_with_cache (new evaluations)
%
% The while-loop remains to handle cases where all points are infeasible or f=Inf,
% especially for random/lds lists (1/2/5/6).
while isempty(flist)
% For random/lds lists, Pini may be regenerated externally if needed.
% (init_glods_si already creates Pini; but if everything fails, we can regenerate here)
% NOTE: list==3 is deterministic in y; list==0 deterministic given x_ini.
for i = 1:size(Pini,2)
y_ini = Pini(:,i);
% Feasibility in y
feasible = is_feasible_y(y_ini, lbound_norm, ubound_norm);
if feasible
if has_f_ini
% list==4: f_ini already known
ftemp = f_ini(i);
else
% Evaluate with cache (in y)
[ftemp, func_eval, CacheP, CachenormP, CacheF, ~] = evaluate_with_cache( ...
y_ini, func_f_eval, cache, CacheP, CachenormP, CacheF, tol_match, func_eval);
end
if isfinite(ftemp)
if isempty(flist)
% Start lists
flist = ftemp;
Plist = y_ini;
if has_f_ini
alfa = alfa_list(i);
radius = radius_list(i);
else
alfa = alfa_ini;
radius = radius_ini;
end
active = true(1,1);
% Profile update (only meaningful when we have evaluations)
if func_eval >= 1
glods_profile(func_eval) = ftemp;
end
else
% Merge subsequent points
if has_f_ini
alfa_aux = alfa_list(i);
radius_aux = radius_list(i);
else
alfa_aux = alfa_ini;
radius_aux = radius_ini;
end
[~,Plist,flist,alfa,radius,active,~] = merge( ...
y_ini, ftemp, alfa_aux, radius_aux, ...
Plist, flist, alfa, radius, active, ...
suf_decrease, 0, []);
if func_eval >= 1
if func_eval > length(glods_profile)
glods_profile(end+1:func_eval) = min(flist);
else
glods_profile(func_eval) = min(flist);
end
end
end
end
end
end
% If still empty, handle failure cases
if isempty(flist) && (list~=1) && (list~=2) && (list~=5) && (list~=6)
fprintf('Error: The optimizer did not generate a feasible point\n');
fprintf('or the initial point provided is not feasible.\n');
fprintf('Please try list=1 or list=2 in parameters file.\n\n');
return
end
if isempty(flist) && stop_feval && (func_eval >= max_fevals)
fprintf('Error: The optimizer did not generate a feasible point,\n');
fprintf('considering the budget of functions evaluations provided.\n\n');
return
end
% If random/lds list and everything failed, regenerate Pini and retry
if isempty(flist) && ((list==1)||(list==2)||(list==5)||(list==6))
Pini = generate_Pini_glods_si(list, n, nPini, lbound_norm, ubound_norm);
end
end
% Finalize profile if list==4 (no new evaluations)
if has_f_ini && func_eval == 0
glods_profile = min(flist); % scalar baseline
else
% truncate to current func_eval (later again at end)
if func_eval > 0
glods_profile = glods_profile(1:func_eval);
else
glods_profile = [];
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 6: Set seed for poll directions and initialize counters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (dir_dense == 1)
rng(1);
end
if search_size == 0
search_size = n;
end
halt = 0;
iter = 0;
iter_suc = 0;
unsuc_consec = 0;
grid_size = 1;
label_grid_size = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 7: Print header
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if output
fprintf('Iteration Report (GLODS_SI): \n\n');
fprintf('| iter | success | #active points | min fvalue | min alpha | max alpha |\n');
print_format = ['| %5d | %2s | %5d | %+13.8e | %+13.8e | %+13.8e |\n'];
active_indices = find(active);
active_indices = active_indices(active_indices <= length(flist));
active_indices = active_indices(active_indices <= length(alfa));
if ~isempty(active_indices)
min_f = min(flist(active_indices));
min_a = min(alfa(active_indices));
max_a = max(alfa(active_indices));
else
min_f = Inf; min_a = 0; max_a = 0;
end
fprintf(print_format, iter, '--', length(active_indices), min_f, min_a, max_a);
fresult = fopen('glods_report.txt','w');
fprintf(fresult,'Iteration Report (GLODS_SI): \n\n');
fprintf(fresult,'| iter | success | #active points | min fvalue | min alpha | max alpha |\n');
fprintf(fresult,print_format, iter, '--', length(active_indices), min_f, min_a, max_a);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MAIN LOOP (Search + Poll)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while (~halt)
func_iter = 0;
aux_success = 0;
success = 0;
poll = 1;
search = 0;
changes = zeros(1,size(flist,2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Search Step
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if iter ~= 0 && search_option ~= 0
if search_freq_type == 0
if search_freq == 0
search = 1;
else
if unsuc_consec == search_freq
search = 1;
end
end
else
index = find(alfa >= tol_active_points);
aux_active = active(index);
index = find(aux_active);
if length(index) <= min_active_points
search = 1;
end
end
end
finite = 0;
if search && ~halt
while ~finite
unsuc_consec = 0;
[Psearch,grid_size,label_grid_size] = search_step(search_option, ...
search_size, lbound_norm, ubound_norm, grid_size, label_grid_size);
if ~isempty(Psearch)
for i = 1:size(Psearch,2)
ytemp = Psearch(:,i);
if is_feasible_y(ytemp, lbound_norm, ubound_norm)
[ftemp, func_eval, CacheP, CachenormP, CacheF, ~] = evaluate_with_cache( ...
ytemp, func_f_eval, cache, CacheP, CachenormP, CacheF, tol_match, func_eval);
func_iter = func_iter + 1;
if isfinite(ftemp)
finite = 1;
[success,Plist,flist,alfa,radius,active,changes] = merge( ...
ytemp, ftemp, alfa_ini, radius_ini, ...
Plist, flist, alfa, radius, active, ...
suf_decrease, 0, changes);
aux_success = aux_success + success;
% Profile
if func_eval > length(glods_profile)
glods_profile(end+1:func_eval) = min(flist);
else
glods_profile(func_eval) = min(flist);
end
end
end
end
if stop_feval && (func_eval >= max_fevals)
halt = 1;
end
if aux_success > 0
success = 1;
poll = 0;
else
success = 0;
poll = 1;
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Poll Step
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if poll && ~halt
% Generate positive basis
if (~dir_dense)
D = [eye(n) -eye(n)];
else
v = 2*rand(n,1)-1;
[Q,R] = qr(v);
if ( R(1) > 1 )
D = Q * [ eye(n) -eye(n) ];
else
D = Q * [ -eye(n) eye(n) ];
end
end
% Reorder points: by f then by alpha stop threshold then active first
[flist,index] = sort(flist,'ascend');
Plist = Plist(:,index);
alfa = alfa(:,index);
radius = radius(:,index);
active = active(:,index);
index1 = find(alfa >= tol_stop);
index2 = find(alfa < tol_stop);
index1 = index1(index1 <= size(Plist,2));
index2 = index2(index2 <= size(Plist,2));
Plist = [Plist(:,index1),Plist(:,index2)];
flist = [flist(index1),flist(index2)];
alfa = [alfa(index1),alfa(index2)];
radius = [radius(index1),radius(index2)];
active = [active(index1),active(index2)];
active_indices = find(active);
inactive_indices = find(~active);
active_indices = active_indices(active_indices <= size(Plist,2));
inactive_indices = inactive_indices(inactive_indices <= size(Plist,2));
Plist = [Plist(:,active_indices),Plist(:,inactive_indices)];
flist = [flist(active_indices),flist(inactive_indices)];
alfa = [alfa(active_indices),alfa(inactive_indices)];
radius = [radius(active_indices),radius(inactive_indices)];
active = [true(1,length(active_indices)), false(1,length(inactive_indices))];
% Poll loop
nd = size(D,2);
count_d = 1;
changes = [1,zeros(1,length(flist)-1)];
while ~success && (count_d <= nd)
ytemp = Plist(:,1) + alfa(1) * D(:,count_d);
if is_feasible_y(ytemp, lbound_norm, ubound_norm)
[ftemp, func_eval, CacheP, CachenormP, CacheF, ~] = evaluate_with_cache( ...
ytemp, func_f_eval, cache, CacheP, CachenormP, CacheF, tol_match, func_eval);
if isfinite(ftemp)
[success,Plist,flist,alfa,radius,active,changes] = merge( ...
ytemp, ftemp, alfa_ini, radius_ini, ...
Plist, flist, alfa, radius, active, ...
suf_decrease, 1, changes);
% Profile
if func_eval > length(glods_profile)
glods_profile(end+1:func_eval) = min(flist);
else
glods_profile(func_eval) = min(flist);
end
end
end
count_d = count_d + 1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Update step sizes and stopping criteria
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if success
iter_suc = iter_suc + 1;
unsuc_consec = 0;
update_indices = find(changes & active);
update_indices = update_indices(update_indices <= length(alfa));
if ~isempty(update_indices)
alfa(update_indices) = alfa(update_indices) * gamma_par;
radius(update_indices) = max(radius(update_indices), alfa(update_indices));
end
else
unsuc_consec = unsuc_consec + 1;
update_indices = find(changes & active);
update_indices = update_indices(update_indices <= length(alfa));
if ~isempty(update_indices)
alfa(update_indices) = alfa(update_indices) * beta_par;
end
end
active_indices = find(active);
active_indices = active_indices(active_indices <= length(alfa));
if stop_alfa && ~isempty(active_indices) && (sum(alfa(active_indices) >= tol_stop) == 0)
halt = 1;
end
if stop_feval && (func_eval >= max_fevals)
halt = 1;
end
iter = iter + 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print iteration report
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if output
print_format = ['| %5d | %1d | %5d | %+13.8e | %+13.8e | %+13.8e |\n'];
active_indices = find(active);
active_indices = active_indices(active_indices <= length(flist));
active_indices = active_indices(active_indices <= length(alfa));
if ~isempty(active_indices)
min_f = min(flist(active_indices));
min_a = min(alfa(active_indices));
max_a = max(alfa(active_indices));
else
min_f = Inf; min_a = 0; max_a = 0;
end
fprintf(print_format,iter,success,length(active_indices),min_f,min_a,max_a);
fprintf(fresult,print_format,iter,success,length(active_indices),min_f,min_a,max_a);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print current active points (output == 2) in ORIGINAL space x
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (output == 2)
fglods = fopen('glods_partial_results.txt','w');
n_dim = size(Plist,1);
active_indices = find(active);
active_indices = active_indices(active_indices <= size(Plist,2));
active_indices = active_indices(active_indices <= length(flist));
active_indices = active_indices(active_indices <= length(alfa));
active_indices = active_indices(active_indices <= length(radius));
m = length(active_indices);
if m > 0
fprintf(fglods,'%d %d\n\n', n_dim, m);
% Build format string
format_str = repmat(' %+21.16e', 1, m);
format_str = [format_str, '\n'];
% Denormalize active points to x
Plist_orig_partial = zeros(n_dim, m);
for k = 1:m
Plist_orig_partial(:,k) = lbound_orig + Plist(:,active_indices(k)) .* scale;
end
% Write x points
for j = 1:n_dim
fprintf(fglods, format_str, Plist_orig_partial(j,:));
end
fprintf(fglods, '\n');
fprintf(fglods, format_str, flist(active_indices));
fprintf(fglods, '\n');
fprintf(fglods, format_str, alfa(active_indices));
fprintf(fglods, '\n');
fprintf(fglods, format_str, radius(active_indices));
else
fprintf(fglods, '%d %d\n\n', n_dim, 0);
end
fclose(fglods);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FINAL: Return active points in ORIGINAL space x
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
active_indices_final = find(active);
active_indices_final = active_indices_final(active_indices_final <= size(Plist,2));
active_indices_final = active_indices_final(active_indices_final <= length(flist));
active_indices_final = active_indices_final(active_indices_final <= length(alfa));
active_indices_final = active_indices_final(active_indices_final <= length(radius));
if ~isempty(active_indices_final)
Plist_y = Plist(:, active_indices_final);
% Denormalize to original space x
Plist = zeros(size(Plist_y));
for k = 1:size(Plist_y,2)
Plist(:,k) = lbound_orig + Plist_y(:,k) .* scale;
end
flist = flist(active_indices_final);
alfa = alfa(active_indices_final);
radius = radius(active_indices_final);
else
Plist = [];
flist = [];
alfa = [];
radius = [];
end
% Truncate profile to actual number of evaluations
if func_eval > 0
if length(glods_profile) >= func_eval
glods_profile = glods_profile(1:func_eval);
else
glods_profile(end+1:func_eval) = min(flist);
end
else
% list==4 or no evaluation happened
if ~isempty(flist)
glods_profile = min(flist);
else
glods_profile = [];
end
end
time = toc(tStart);
fprintf('\n Final Report (GLODS_SI - Scale-Invariant): \n\n');
fprintf('Elapsed Time = %10.3e \n\n', time);
fprintf('| #iter | #isuc | #active points | #fevals | min fvalue |\n');
fprintf('| %5d | %5d | %5d | %5d | %+13.8e |\n\n', ...
iter, iter_suc, size(Plist,2), func_eval, min(flist));
if output
fprintf(fresult,'\n Final Report (GLODS_SI): \n\n');
fprintf(fresult,'Elapsed Time = %10.3e \n\n', time);
fprintf(fresult,'| #iter | #isuc | #active points | #fevals | min fvalue |\n');
fprintf(fresult,'| %5d | %5d | %5d | %5d | %+13.8e |\n\n', ...
iter, iter_suc, size(Plist,2), func_eval, min(flist));
fclose(fresult);
end
end % end of glods_si
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Local helpers (can also be placed in separate .m files)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ftemp, func_eval, CacheP, CachenormP, CacheF, match] = evaluate_with_cache( ...
y, func_f_eval, cache, CacheP, CachenormP, CacheF, tol_match, func_eval)
% Evaluate f(y) using cache when enabled.
match = 0;
if cache ~= 0
y_norm1 = norm(y,1);
if ~isempty(CacheP)
[match, y, ftemp] = match_point(y, y_norm1, CacheP, CacheF, CachenormP, tol_match);
end
end
if ~match
ftemp = feval(func_f_eval, y);
func_eval = func_eval + 1;
if cache ~= 0
CacheP = [CacheP, y];
CachenormP = [CachenormP, norm(y,1)];
CacheF = [CacheF, ftemp];
end
end
end
function feasible = is_feasible_y(y, lbound_norm, ubound_norm)
% Bound feasibility test in normalized space.
y = y(:);
lbound_norm = lbound_norm(:);
ubound_norm = ubound_norm(:);
if length(y) ~= length(lbound_norm) || length(y) ~= length(ubound_norm)
error('Error: dimension mismatch in is_feasible_y.');
end
bound = [y - ubound_norm; -y + lbound_norm];
feasible = (sum(bound <= 0) == 2*length(y));
end
function [Pini, f_ini, alfa_list, radius_list, n, nPini, has_f_ini] = init_glods_si( ...
list, file_ini, x_ini, lbound_norm, ubound_norm, ...
user_list_size, nPini, lbound_orig, ubound_orig, scale) %#ok<INUSD>
% Outputs:
% Pini - n x nPini matrix of initial points in normalized space [0,1]^n
% f_ini - 1 x nPini vector of function values (only for list==4)
% alfa_list - 1 x nPini vector of alfa values (only for list==4)
% radius_list - 1 x nPini vector of radius values (only for list==4)
% n - problem dimension
% nPini - number of initial points (may differ from input nPini)
% has_f_ini - boolean indicating if f_ini is provided
%
% INIT_GLODS_SI
% Build initial list Pini in normalized space y in [0,1]^n.
%
% - list==0: x_ini is in ORIGINAL x (normalize x->y) or center (y). has_f_ini=false
% - list==4: file provides x in ORIGINAL space + f_ini/alfa_list/radius_list (normalize x->y). has_f_ini=true
% - list in {1,2,3,5,6}: generate Pini directly in y. has_f_ini=false
%
% Notes:
% - ubound_orig is used to recompute scale = ubound_orig - lbound_orig (robust).
% - Input "scale" is kept in signature for backward compatibility; it is not used.
% Defaults
f_ini = [];
alfa_list = [];
radius_list = [];
has_f_ini = false;
% Dimension (trust normalized bounds for n)
n = size(lbound_norm,1);
% Defensive shaping (column vectors)
lbound_orig = lbound_orig(:);
ubound_orig = ubound_orig(:);
if numel(lbound_orig) ~= n || numel(ubound_orig) ~= n
error('Error: lbound_orig and ubound_orig must have %d elements.', n);
end
% Recompute scale for consistency
scale = ubound_orig - lbound_orig;
if any(scale == 0)
error('Error: some variables have zero range (ubound == lbound). Handle fixed vars explicitly.');
end
% Decide nPini if user_list_size==0
if (user_list_size == 0)
nPini = n;
end
% -------------------------
% list == 0 (single point)
% -------------------------
if (list == 0)
if ~isempty(x_ini)
if size(x_ini,1) ~= n
error('Error: x_ini must have %d rows (one per variable).', n);
end
% Normalize ORIGINAL x_ini -> y
Pini = (x_ini - lbound_orig) ./ scale;
% Clamp to [0,1] (avoid tiny numerical violations)
Pini = min(max(Pini, 0), 1);
else
Pini = (lbound_norm + ubound_norm)/2;
end
nPini = size(Pini,2);
return;
end
% -------------------------
% list == 4 (file in x + f/alfa/radius)
% -------------------------
if (list == 4)
fpoints = fopen(file_ini,'r');
if fpoints < 0
error('Error: could not open initialization file: %s', file_ini);
end
c = onCleanup(@() fclose(fpoints)); %#ok<NASGU>
aux = str2num(fgetl(fpoints)); %#ok<ST2NM>
n_file = aux(1);
m = aux(2);
if n_file ~= n
error('Error: file dimension n=%d does not match bounds dimension n=%d.', n_file, n);
end
str2num(fgetl(fpoints)); %#ok<ST2NM> % keep file format (unused header line)
x_file = zeros(n,m);
for i = 1:n
line = fgetl(fpoints);
if ~ischar(line)
error('Error: unexpected end-of-file while reading x_file (row %d).', i);
end
aux = str2num(line); %#ok<ST2NM>
if numel(aux) < m
error('Error: not enough entries in x_file row %d (expected %d).', i, m);
end
x_file(i,:) = aux(1:m);
end
% Normalize x -> y (implicit expansion in recent MATLAB)
Pini = (x_file - lbound_orig) ./ scale;
% Clamp to [0,1]
Pini = min(max(Pini, 0), 1);
% Read f_ini
line = fgetl(fpoints);
if ~ischar(line), error('Error: unexpected EOF before f_ini header.'); end %#ok<NASGU>
aux = str2num(fgetl(fpoints)); %#ok<ST2NM>
if numel(aux) < m
error('Error: not enough entries for f_ini (expected %d).', m);
end
f_ini = aux(1:m);
% Read alfa_list
line = fgetl(fpoints);
if ~ischar(line), error('Error: unexpected EOF before alfa_list header.'); end %#ok<NASGU>
aux = str2num(fgetl(fpoints)); %#ok<ST2NM>
if numel(aux) < m
error('Error: not enough entries for alfa_list (expected %d).', m);
end
alfa_list = aux(1:m);
% Read radius_list
line = fgetl(fpoints);
if ~ischar(line), error('Error: unexpected EOF before radius_list header.'); end %#ok<NASGU>
aux = str2num(fgetl(fpoints)); %#ok<ST2NM>
if numel(aux) < m
error('Error: not enough entries for radius_list (expected %d).', m);
end
radius_list = aux(1:m);
nPini = size(Pini,2);
has_f_ini = true;
return;
end
% ------------------------------------
% list in {1,2,3,5,6}: generate in y
% ------------------------------------
Pini = generate_Pini_glods_si(list, n, nPini, lbound_norm, ubound_norm);
nPini = size(Pini,2); % important for list==3 (adds center)
end
function Pini = generate_Pini_glods_si(list, n, nPini, lbound_norm, ubound_norm)
% GENERATE_PINI_GLODS_SI - Generate initial points in normalized space y
if (list == 1)
% Latin Hypercube
Pini = repmat(lbound_norm,1,nPini) + lhsdesign(nPini,n)' .* ...
repmat((ubound_norm-lbound_norm),1,nPini);
elseif (list == 2)
% Random
Pini = repmat(lbound_norm,1,nPini) + rand(n,nPini) .* ...
repmat((ubound_norm-lbound_norm),1,nPini);
elseif (list == 3)
% Equally spaced points in the line segment + central point
center = (lbound_norm + ubound_norm)/2;
if (nPini <= 1)
Pini = center;
else
t = linspace(0, 1, nPini);
Pini = repmat(lbound_norm,1,nPini) + repmat(t,n,1) .* ...
repmat((ubound_norm-lbound_norm),1,nPini);
Pini = [Pini, center];
end
elseif (list == 5)
% Halton
Lhalton = haltonset(n,'Skip',n+1);
Pini = repmat(lbound_norm,1,nPini) + repmat((ubound_norm-lbound_norm),1,nPini) .* ...
Lhalton(1:nPini,:)';
elseif (list == 6)
% Sobol
Lsobol = sobolset(n,'Leap',2^n);
Pini = repmat(lbound_norm,1,nPini) + repmat((ubound_norm-lbound_norm),1,nPini) .* ...
Lsobol(1:nPini,:)';
else
Pini = [];
end
end
function [success,Plist,flist,alfa,radius,active,changes] = merge(x,f,...
alfa_ini,radius_ini,Plist,flist,alfa,radius,...
active,suf_decrease,poll,changes)
%
% Purpose:
%
% Function merge compares a new evaluated point with the current list of
% points, deciding if it should be added to it and updating the list.
%
% Input:
%
% x (Point to be compared.)
%
% f (Corresponding objective function value.)
%
% alfa_ini (Initial step size.)
%
% radius_ini (Initial radius of comparison.)
%
% Plist (Current list of points.)
%
% flist (Corresponding objective function values.)
%
% alfa (Corresponding step sizes.)
%
% radius (Corresponding radius of comparison.)
%
% active (Corresponding point status.)
%
% suf_decrease (0-1 variable: 1 if the algorithm uses a
% globalization strategy based in imposing a
% sufficient decrease condition; 0 otherwise.)
%
% poll (0-1 variable: 1 if merging is performed inside the poll
% step, 0 otherwise.)
%
% changes (Record of points under analysis.)
%
% Output:
%
% success (0-1 variable: 1 if a better point was found; 0 otherwise.)
%
% Plist (Updated list of points.)
%
% flist (Corresponding objective function values.)
%
% alfa (Corresponding step sizes.)
%
% radius (Corresponding radius of comparison.)
%
% active (Corresponding point status.)
%
% changes (Record of points analysed.)
%
% Functions called: forcing (Provided by the optimizer.)
%
% GLODS Version 0.2
%
% Copyright (C) 2014 A. L. Custódio and J. F. A. Madeira.
%
% This library is free software; you can redistribute it and/or
% modify it under the terms of the GNU Lesser General Public
% License as published by the Free Software Foundation; either
% version 3.0 of the License, or (at your option) any later version.
%
% This library is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public
% License along with this library; if not, write to the Free Software
% Foundation, Inc.,51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
%
%
success = 0;
dist_list = sqrt(sum((Plist-repmat(x,1,size(Plist,2))).^2,1));
if min(dist_list-radius)>0
success = 1;
Plist = [Plist,x];
flist = [flist,f];
alfa = [alfa,alfa_ini];
radius = [radius,radius_ini];
active = [active,1];
changes = [changes,1];
else
if min(dist_list) ~= 0
index = find(dist_list-radius<=0);
m_index = size(index,2);
active_new = 0;
alfa_new = 0;
radius_new = 0;
idom = 0;
pdom = 0;
icomp = 0;
for i=1:m_index
if f < flist(index(i)) - forcing(alfa(index(i)),suf_decrease)
icomp = 1;
idom = idom + active(index(i));
active(index(i)) = 0;
if alfa(index(i)) > alfa_new
alfa_new = alfa(index(i));
radius_new = radius(index(i));
end
else
if flist(index(i)) <= f - forcing(alfa(index(i)),suf_decrease)
pdom = 1;
end
end
end
if pdom == 0
active_new = 1;
success = 1;
end
if (idom > 0) || (suf_decrease == 0 && pdom == 0 && icomp == 1)
Plist = [Plist,x];
flist = [flist,f];
active = [active,active_new];
changes = [changes,1];
if poll
alfa = [alfa,alfa(1)];
radius = [radius,alfa(1)];
else
alfa = [alfa,alfa_new];
radius = [radius,radius_new];
end
end
end
end
%
% End of merge.
end