-
Notifications
You must be signed in to change notification settings - Fork 9
/
biteopt.h
1982 lines (1628 loc) · 48.9 KB
/
biteopt.h
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
//$ nocpp
/**
* @file biteopt.h
*
* @brief The inclusion file for the CBiteOpt and CBiteOptDeep classes.
*
* Description is available at https://github.com/avaneev/biteopt
*
* E-mail: [email protected] or [email protected]
*
* @section license License
*
* Copyright (c) 2016-2024 Aleksey Vaneev
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef BITEOPT_INCLUDED
#define BITEOPT_INCLUDED
#define BITEOPT_VERSION "2024.6"
#include "spheropt.h"
#include "mbopt.h"
/**
* BiteOpt optimization class. Implements a stochastic non-linear
* bound-constrained derivative-free optimization method.
*
* Description is available at https://github.com/avaneev/biteopt
*/
class CBiteOpt : public CBiteOptBase< int64_t >
{
public:
typedef int64_t ptype; ///< Parameter value storage type (should be a
///< signed integer type, same as CBiteOptBase template parameter).
CBiteOpt()
: ParOpt( this )
, ParOpt2( this )
{
addSel( MethodSel, "MethodSel" );
addSel( M1Sel, "M1Sel" );
addSel( M1ASel, "M1ASel" );
addSel( M1BSel, "M1BSel" );
addSel( M1CSel, "M1CSel" );
addSel( M2Sel, "M2Sel" );
addSel( M2BSel, "M2BSel" );
addSel( PopChangeIncrSel, "PopChangeIncrSel" );
addSel( PopChangeDecrSel, "PopChangeDecrSel" );
addSel( ParOpt2Sel, "ParOpt2Sel" );
addSel( ParPopPSel[ 0 ], "ParPopPSel[ 0 ]" );
addSel( ParPopPSel[ 1 ], "ParPopPSel[ 1 ]" );
addSel( ParPopPSel[ 2 ], "ParPopPSel[ 2 ]" );
addSel( ParPopPSel[ 3 ], "ParPopPSel[ 3 ]" );
addSel( ParPopPSel[ 4 ], "ParPopPSel[ 4 ]" );
addSel( ParPopPSel[ 5 ], "ParPopPSel[ 5 ]" );
addSel( ParPopPSel[ 6 ], "ParPopPSel[ 6 ]" );
addSel( ParPopPSel[ 7 ], "ParPopPSel[ 7 ]" );
addSel( AltPopPSel, "AltPopPSel" );
addSel( AltPopSel[ 0 ], "AltPopSel[ 0 ]" );
addSel( AltPopSel[ 1 ], "AltPopSel[ 1 ]" );
addSel( AltPopSel[ 2 ], "AltPopSel[ 2 ]" );
addSel( AltPopSel[ 3 ], "AltPopSel[ 3 ]" );
addSel( OldPopSel, "OldPopSel" );
addSel( MinSolPwrSel[ 0 ], "MinSolPwrSel[ 0 ]" );
addSel( MinSolPwrSel[ 1 ], "MinSolPwrSel[ 1 ]" );
addSel( MinSolPwrSel[ 2 ], "MinSolPwrSel[ 2 ]" );
addSel( MinSolPwrSel[ 3 ], "MinSolPwrSel[ 3 ]" );
addSel( MinSolMulSel[ 0 ], "MinSolMulSel[ 0 ]" );
addSel( MinSolMulSel[ 1 ], "MinSolMulSel[ 1 ]" );
addSel( MinSolMulSel[ 2 ], "MinSolMulSel[ 2 ]" );
addSel( MinSolMulSel[ 3 ], "MinSolMulSel[ 3 ]" );
addSel( Gen1AllpSel, "Gen1AllpSel" );
addSel( Gen1MoveAsyncSel, "Gen1MoveAsyncSel" );
addSel( Gen1MoveSpanSel, "Gen1MoveSpanSel" );
addSel( Gen2ModeSel, "Gen2ModeSel" );
addSel( Gen2bModeSel, "Gen2bModeSel" );
addSel( Gen2cModeSel, "Gen2cModeSel" );
addSel( Gen2dModeSel, "Gen2dModeSel" );
addSel( Gen3ModeSel, "Gen3ModeSel" );
addSel( Gen4MixFacSel, "Gen4MixFacSel" );
addSel( Gen5bModeSel, "Gen5bModeSel" );
addSel( Gen7PowFacSel, "Gen7PowFacSel" );
addSel( Gen8ModeSel, "Gen8ModeSel" );
addSel( Gen8NumSel, "Gen8NumSel" );
addSel( Gen8SpanSel[ 0 ], "Gen8SpanSel[ 0 ]" );
addSel( Gen8SpanSel[ 1 ], "Gen8SpanSel[ 1 ]" );
}
/**
* Function updates dimensionality of *this object. Function does nothing
* if dimensionality has not changed since the last call. This function
* should be called at least once before calling the init() function.
*
* @param aParamCount The number of parameters being optimized.
* @param PopSize0 The number of elements in population to use. If set to
* 0 or negative, the default formula will be used.
*/
void updateDims( const int aParamCount, const int PopSize0 = 0 )
{
const int aPopSize = ( PopSize0 > 0 ? PopSize0 :
calcPopSizeBiteOpt( aParamCount ));
if( aParamCount == ParamCount && aPopSize == PopSize )
{
return;
}
initBuffers( aParamCount, aPopSize );
setParPopCount( 5 );
ParOpt.updateDims( aParamCount, 11 + aPopSize / 3 );
ParOptPop.initBuffers( aParamCount, aPopSize );
ParOpt2.updateDims( aParamCount, aPopSize );
ParOpt2Pop.initBuffers( aParamCount, aPopSize );
OldPops[ 0 ].initBuffers( aParamCount, aPopSize );
OldPops[ 1 ].initBuffers( aParamCount, aPopSize );
}
/**
* Function initializes *this optimizer. Does not perform objective
* function evaluations.
*
* @param rnd Random number generator.
* @param InitParams If not NULL, initial parameter vector, also used as
* centroid for initial population vectors.
* @param InitRadius Initial radius, multiplier relative to the default
* sigma value.
*/
void init( CBiteRnd& rnd, const double* const InitParams = NULL,
const double InitRadius = 1.0 )
{
initCommonVars( rnd );
StartSD = 0.25 * InitRadius;
setStartParams( InitParams );
ParOpt.init( rnd, InitParams, InitRadius );
ParOpt2.init( rnd, InitParams, InitRadius );
UseParOpt = 0;
ParOptPop.resetCurPopPos();
ParOpt2Pop.resetCurPopPos();
OldPops[ 0 ].resetCurPopPos();
OldPops[ 1 ].resetCurPopPos();
}
/**
* Function performs the parameter optimization iteration that involves 1
* objective function evaluation.
*
* @param rnd Random number generator.
* @param PushOpt Optimizer where the recently obtained solution should be
* "pushed", used for deep optimization algorithm.
* @return The number of non-improving iterations so far. A high value
* means optimizer has reached an optimization plateau. The suggested
* threshold value is ParamCount * 128. When this value was reached, the
* probability of plateau is high. This value, however, should not be
* solely relied upon when considering a stopping criteria: a hard
* iteration limit should be always used as in some cases convergence time
* may be very high with small, but frequent improving steps. This value
* is best used to allocate iteration budget between optimization attempts
* more efficiently.
*/
int optimize( CBiteRnd& rnd, CBiteOpt* const PushOpt = NULL )
{
int i;
if( DoInitEvals )
{
ptype* const Params = getCurParams();
genInitParams( rnd, Params );
NewCosts[ 0 ] = fixCostNaN( optcost( NewValues ));
updateBestCost( NewCosts[ 0 ], NewValues,
updatePop( NewCosts[ 0 ], Params ));
if( CurPopPos == PopSize )
{
updateCentroid();
for( i = 0; i < ParPopCount; i++ )
{
ParPops[ i ] -> copy( *this );
}
DoInitEvals = false;
}
return( 0 );
}
DoEval = true;
const int SelMethod = select( MethodSel, rnd );
if( SelMethod == 0 )
{
generateSol2( rnd );
}
else
if( SelMethod == 1 )
{
const int SelM1 = select( M1Sel, rnd );
if( SelM1 == 0 )
{
const int SelM1A = select( M1ASel, rnd );
if( SelM1A == 0 )
{
generateSol2b( rnd );
}
else
if( SelM1A == 1 )
{
generateSol2c( rnd );
}
else
{
generateSol2d( rnd );
}
}
else
if( SelM1 == 1 )
{
const int SelM1B = select( M1BSel, rnd );
if( SelM1B == 0 )
{
generateSol4( rnd );
}
else
if( SelM1B == 1 )
{
generateSol5b( rnd );
}
else
if( SelM1B == 2 )
{
generateSol5c( rnd );
}
else
{
generateSol13( rnd );
}
}
else
if( SelM1 == 2 )
{
const int SelM1C = select( M1CSel, rnd );
if( SelM1C == 0 )
{
generateSol5( rnd );
}
else
if( SelM1C == 1 )
{
generateSol10( rnd );
}
else
{
generateSol11( rnd );
}
}
else
{
generateSol6( rnd );
}
}
else
if( SelMethod == 2 )
{
if( select( M2Sel, rnd ))
{
generateSol1( rnd );
}
else
{
const int SelM2B = select( M2BSel, rnd );
if( SelM2B == 0 )
{
generateSol3( rnd );
}
else
if( SelM2B == 1 )
{
generateSol7( rnd );
}
else
if( SelM2B == 2 )
{
generateSol8( rnd );
}
else
if( SelM2B == 3 )
{
generateSol9( rnd );
}
else
{
generateSol12( rnd );
}
}
}
else
{
generateSolPar( rnd );
}
if( DoEval )
{
// Evaluate objective function with new parameters, if the
// solution was not provided by the parallel optimizer.
// Wrap parameter values so that they stay in the [0; 1] range.
for( i = 0; i < ParamCount; i++ )
{
TmpParams[ i ] = wrapParam( rnd, TmpParams[ i ]);
NewValues[ i ] = getRealValue( TmpParams, i );
}
NewCosts[ 0 ] = fixCostNaN( optcost( NewValues ));
LastCosts = NewCosts;
LastValues = NewValues;
}
const int p = updatePop( LastCosts[ 0 ], TmpParams, true, 3 );
if( p > CurPopSize1 )
{
// Upper bound cost constraint check failed, reject this solution.
applySelsDecr( rnd );
StallCount++;
if( DoEval && CurPopSize < PopSize )
{
if( select( PopChangeIncrSel, rnd ))
{
// Increase population size on fail.
incrCurPopSize();
}
}
}
else
{
updateBestCost( LastCosts[ 0 ], LastValues, p );
applySelsIncr( rnd, 1.0 - p * CurPopSizeI );
StallCount = 0;
ptype* const OldParams = getParamsOrdered( CurPopSize1 );
if( rnd.get() < ParamCountI )
{
OldPops[ 0 ].updatePop( *getObjPtr( OldParams ), OldParams,
false );
}
if( rnd.get() < 2.0 * ParamCountI )
{
OldPops[ 1 ].updatePop( *getObjPtr( OldParams ), OldParams,
false );
}
if( PushOpt != NULL && PushOpt != this &&
!PushOpt -> DoInitEvals && p > 1 )
{
PushOpt -> updatePop( LastCosts[ 0 ], TmpParams, true, 3 );
PushOpt -> updateParPop( LastCosts[ 0 ], TmpParams );
}
if( DoEval && CurPopSize > PopSize / 2 )
{
if( select( PopChangeDecrSel, rnd ))
{
// Decrease population size on success.
decrCurPopSize();
}
}
}
// "Diverging populations" technique.
updateParPop( LastCosts[ 0 ], TmpParams );
return( StallCount );
}
protected:
CBiteSel< 4 > MethodSel; ///< Population generator 4-method selector.
CBiteSel< 4 > M1Sel; ///< Method 1's sub-method selector.
CBiteSel< 3 > M1ASel; ///< Method 1's sub-sub-method A selector.
CBiteSel< 4 > M1BSel; ///< Method 1's sub-sub-method B selector.
CBiteSel< 3 > M1CSel; ///< Method 1's sub-sub-method C selector.
CBiteSel< 2 > M2Sel; ///< Method 2's sub-method selector.
CBiteSel< 5 > M2BSel; ///< Method 2's sub-sub-method B selector.
CBiteSel< 2 > PopChangeIncrSel; ///< Population size change increase
///< selector.
CBiteSel< 2 > PopChangeDecrSel; ///< Population size change decrease
///< selector.
CBiteSel< 2 > ParOpt2Sel; ///< Parallel optimizer 2 use selector.
CBiteSel< 2 > ParPopPSel[ 8 ]; ///< Parallel population use
///< probability selectors.
CBiteSel< 2 > AltPopPSel; ///< Alternative population use selector.
CBiteSel< 2 > AltPopSel[ 4 ]; ///< Alternative population type use
///< selectors.
CBiteSel< 2 > OldPopSel; ///< Old population use selector.
CBiteSel< 4 > MinSolPwrSel[ 4 ]; ///< Power factor selectors, for
///< least-cost population index selection.
CBiteSel< 4 > MinSolMulSel[ 4 ]; ///< Multiplier selectors, for
///< least-cost population index selection.
CBiteSel< 2 > Gen1AllpSel; ///< Generator method 1's Allp selector.
CBiteSel< 2 > Gen1MoveAsyncSel; ///< Generator method 1's Move async
///< selector.
CBiteSel< 4 > Gen1MoveSpanSel; ///< Generator method 1's Move span
///< selector.
CBiteSel< 2 > Gen2ModeSel; ///< Generator method 2's Mode selector.
CBiteSel< 2 > Gen2bModeSel; ///< Generator method 2b's Mode selector.
CBiteSel< 2 > Gen2cModeSel; ///< Generator method 2c's Mode selector.
CBiteSel< 2 > Gen2dModeSel; ///< Generator method 2d's Mode selector.
CBiteSel< 4 > Gen3ModeSel; ///< Generator method 3's Mode selector.
CBiteSel< 4 > Gen4MixFacSel; ///< Generator method 4's mixing count
///< selector.
CBiteSel< 2 > Gen5bModeSel; ///< Generator method 5b's Mode selector.
CBiteSel< 4 > Gen7PowFacSel; ///< Generator method 7's Power selector.
CBiteSel< 2 > Gen8ModeSel; ///< Generator method 8's mode selector.
CBiteSel< 4 > Gen8NumSel; ///< Generator method 8's NumSols selector.
CBiteSel< 4 > Gen8SpanSel[ 2 ]; ///< Generator method 8's random span
///< selectors.
CBitePop OldPops[ 2 ]; ///< Populations of older solutions, updated
///< probabilistically.
bool DoEval; ///< Temporary variable which equals to "true" if the
///< newly-generated solution should be evaluated via the optcost()
///< function.
CBiteOptOwned< CSpherOpt > ParOpt; ///< Parallel optimizer.
CBitePop ParOptPop; ///< Population of parallel optimizer's solutions.
///< Includes only its solutions.
CBiteOptOwned< CMiniBiteOpt > ParOpt2; ///< Parallel optimizer 2.
CBitePop ParOpt2Pop; ///< Population of parallel optimizer 2's solutions.
///< Includes only its solutions.
int UseParOpt; ///< Parallel optimizer currently being in use.
/**
* Function updates an appropriate parallel population.
*
* @param UpdCost Cost of the new solution.
* @param UpdParams New parameter values.
*/
void updateParPop( const double UpdCost, const ptype* const UpdParams )
{
const int p = getMinDistParPop( UpdCost, UpdParams );
if( p >= 0 )
{
ParPops[ p ] -> updatePop( UpdCost, UpdParams, true, 0 );
}
}
/**
* Function selects a parallel population to use for solution generation.
* With certain probability, *this object's own population will be
* returned instead of parallel population.
*
* @param gi Solution generator index (0-7).
* @param rnd PRNG object.
*/
CBitePop& selectParPop( const int gi, CBiteRnd& rnd )
{
if( select( ParPopPSel[ gi ], rnd ))
{
return( *ParPops[ rnd.getInt( ParPopCount )]);
}
return( *this );
}
/**
* Function selects an alternative, parallel optimizer's, population, to
* use in some solution generators.
*
* @param gi Solution generator index (0-3).
* @param rnd PRNG object.
*/
CBitePop& selectAltPop( const int gi, CBiteRnd& rnd )
{
if( select( AltPopPSel, rnd ))
{
if( select( AltPopSel[ gi ], rnd ))
{
if( ParOptPop.getCurPopPos() >= CurPopSize )
{
return( ParOptPop );
}
}
else
{
if( ParOpt2Pop.getCurPopPos() >= CurPopSize )
{
return( ParOpt2Pop );
}
}
}
return( *this );
}
/**
* Function returns a dynamically-selected minimal population index, used
* in some solution generation methods.
*
* @param gi Solution generator index (0-3).
* @param rnd PRNG object.
* @param ps Population size.
*/
int getMinSolIndex( const int gi, CBiteRnd& rnd, const int ps )
{
static const double pp[ 4 ] = { 0.05, 0.125, 0.25, 0.5 };
const double r = ps * rnd.getPow( ps *
pp[ select( MinSolPwrSel[ gi ], rnd )]);
static const double rm[ 4 ] = { 0.0, 0.125, 0.25, 0.5 };
return( (int) ( r * rm[ select( MinSolMulSel[ gi ], rnd )]));
}
/**
* The original "bitmask inversion with random move" solution generator.
* Most of the time adjusts only a single parameter of a better solution,
* yet manages to produce excellent "reference points".
*/
void generateSol1( CBiteRnd& rnd )
{
ptype* const Params = TmpParams;
const CBitePop& ParPop = selectParPop( 0, rnd );
const int ParPopSize = ParPop.getCurPopSize();
copyParams( Params, ParPop.getParamsOrdered(
getMinSolIndex( 0, rnd, ParPopSize )));
// Select a single random parameter or all parameters for further
// operations.
int a;
int b;
bool DoAllp = false;
if( rnd.get() < 1.8 * ParamCountI )
{
if( select( Gen1AllpSel, rnd ))
{
DoAllp = true;
}
}
if( DoAllp )
{
a = 0;
b = ParamCount;
}
else
{
a = rnd.getInt( ParamCount );
b = a + 1;
}
// Bitmask inversion operation, works as the main "driver" of
// optimization process.
const double r1 = rnd.get();
const double r12 = r1 * r1;
const int ims = (int) ( r12 * r12 * 48.0 );
const ptype imask = ( ims > IntMantBits ? 0 : IntMantMask >> ims );
const int im2s = rnd.getSqrInt( 96 );
const ptype imask2 = ( im2s > IntMantBits ? 0 : IntMantMask >> im2s );
const int si1 = (int) ( r1 * r12 * ParPopSize );
const ptype* const rp1 = ParPop.getParamsOrdered( si1 );
int i;
for( i = a; i < b; i++ )
{
Params[ i ] = (( Params[ i ] ^ imask ) +
( rp1[ i ] ^ imask2 )) >> 1;
}
if( rnd.get() < 1.0 - ParamCountI )
{
const ptype* const rp2 = ParPop.getParamsOrdered(
rnd.getSqrInt( ParPopSize ));
if( rnd.get() < sqrt( ParamCountI ))
{
if( select( Gen1MoveAsyncSel, rnd ))
{
a = 0;
b = ParamCount;
}
}
// Random move around a random previous solution vector.
static const double SpanMults[ 4 ] = { 0.5, 1.5, 2.0, 2.5 };
const double m = SpanMults[ select( Gen1MoveSpanSel, rnd )];
const double m1 = rnd.getTPDF() * m;
const double m2 = rnd.getTPDF() * m;
for( i = a; i < b; i++ )
{
Params[ i ] += (ptype) (( rp2[ i ] - Params[ i ]) * m1 );
Params[ i ] += (ptype) (( rp2[ i ] - Params[ i ]) * m2 );
}
}
}
/**
* The "Differential Evolution"-based solution generator. Note that
* compared to a usual DE, this generator does not use crossover, and
* it uses one, or an average of two best solutions as the base.
*/
void generateSol2( CBiteRnd& rnd )
{
ptype* const Params = TmpParams;
const CBitePop& ParPop = *this;
const int ParPopSize = ParPop.getCurPopSize();
const int ParPopSize1 = ParPopSize - 1;
const int si1 = getMinSolIndex( 1, rnd, ParPopSize );
const ptype* const rp1 = ParPop.getParamsOrdered( si1 );
const ptype* const rp3 = ParPop.getParamsOrdered( ParPopSize1 - si1 );
const int si2 = 1 + rnd.getInt( ParPopSize1 );
const ptype* const rp2 = ParPop.getParamsOrdered( si2 );
const int si4 = rnd.getSqrInt( ParPopSize );
const ptype* const rp4 = ParPop.getParamsOrdered( si4 );
const ptype* const rp5 = ParPop.getParamsOrdered( ParPopSize1 - si4 );
// The "step in the right direction" (Differential Evolution
// "mutation") operation towards the best (minimal) and away from
// the worst (maximal) parameter vector, plus a difference of two
// random vectors.
const int Mode = select( Gen2ModeSel, rnd );
int i;
if( Mode == 0 )
{
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] = rp1[ i ] + ((( rp2[ i ] - rp3[ i ]) +
( rp4[ i ] - rp5[ i ])) >> 1 );
}
}
else
{
const ptype* const rp1b = ParPop.getParamsOrdered(
rnd.getSqrInt( ParPopSize ));
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] = (( rp1[ i ] + rp1b[ i ]) +
( rp2[ i ] - rp3[ i ]) + ( rp4[ i ] - rp5[ i ])) >> 1;
}
}
}
/**
* An alternative "Differential Evolution"-based solution generator.
*/
void generateSol2b( CBiteRnd& rnd )
{
ptype* const Params = TmpParams;
// rand/2/none DE-alike mutation.
const int si1 = getMinSolIndex( 2, rnd, CurPopSize );
const ptype* const rp1 = getParamsOrdered( si1 );
const int si2 = rnd.getInt( CurPopSize );
const ptype* const rp2 = getParamsOrdered( si2 );
const ptype* const rp3 = getParamsOrdered( CurPopSize1 - si2 );
const CBitePop& AltPop = selectAltPop( 0, rnd );
const int si4 = rnd.getInt( CurPopSize );
const ptype* const rp4 = AltPop.getParamsOrdered( si4 );
const ptype* const rp5 = AltPop.getParamsOrdered( CurPopSize1 - si4 );
const int Mode = select( Gen2bModeSel, rnd );
int i;
if( Mode == 0 )
{
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] = rp1[ i ] + ((( rp2[ i ] - rp3[ i ]) +
( rp4[ i ] - rp5[ i ])) >> 1 );
}
}
else
{
const ptype* const rp1b = getParamsOrdered(
rnd.getSqrInt( CurPopSize ));
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] = ( rp1[ i ] + rp1b[ i ] +
( rp2[ i ] - rp3[ i ]) + ( rp4[ i ] - rp5[ i ])) >> 1;
}
}
}
/**
* "Differential Evolution"-based solution generator, almost an exact
* replica of the CDEOpt optimizer.
*/
void generateSol2c( CBiteRnd& rnd )
{
ptype* const Params = TmpParams;
zeroParams( Params );
const int si1 = rnd.getPowInt( 4.0, CurPopSize / 2 );
const ptype* const rp1 = getParamsOrdered( si1 );
const int PairCount = 3;
const int pc = 1 + 2 * PairCount;
int PopIdx[ pc ];
PopIdx[ 0 ] = si1;
int pp = 1;
int i;
int j;
if( CurPopSize1 <= pc )
{
while( pp < pc )
{
PopIdx[ pp ] = rnd.getInt( CurPopSize );
pp++;
}
}
else
{
while( pp < pc )
{
const int sii = rnd.getInt( CurPopSize );
for( j = 0; j < pp; j++ )
{
if( PopIdx[ j ] == sii )
{
break;
}
}
if( j == pp )
{
PopIdx[ pp ] = sii;
pp++;
}
}
}
const ptype* const rp2 = getParamsOrdered( PopIdx[ 1 ]);
const ptype* const rp3 = getParamsOrdered( PopIdx[ 2 ]);
const ptype* const rp4 = getParamsOrdered( PopIdx[ 3 ]);
const ptype* const rp5 = getParamsOrdered( PopIdx[ 4 ]);
const ptype* const rp6 = getParamsOrdered( PopIdx[ 5 ]);
const ptype* const rp7 = getParamsOrdered( PopIdx[ 6 ]);
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] = ( rp2[ i ] - rp3[ i ]) + ( rp4[ i ] - rp5[ i ]) +
( rp6[ i ] - rp7[ i ]);
}
if( rnd.getBit() && rnd.getBit() )
{
const int k = rnd.getInt( ParamCount );
// Produce sparsely-random bit-strings.
const ptype v1 = rnd.getRaw() & rnd.getRaw() & rnd.getRaw() &
rnd.getRaw() & rnd.getRaw() & IntMantMask;
const ptype v2 = rnd.getRaw() & rnd.getRaw() & rnd.getRaw() &
rnd.getRaw() & rnd.getRaw() & IntMantMask;
Params[ k ] += v1 - v2; // Apply in TPDF manner.
}
const int Mode = select( Gen2cModeSel, rnd );
if( Mode == 0 )
{
int si2 = si1 + rnd.getBit() * 2 - 1;
if( si2 < 0 )
{
si2 = 1;
}
const ptype* const rp1b = getParamsOrdered( si2 );
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] = ( rp1[ i ] + rp1b[ i ] + Params[ i ]) >> 1;
}
}
else
{
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] = rp1[ i ] + ( Params[ i ] >> 1 );
}
}
}
/**
* An alternative "Differential Evolution"-based solution generator that
* uses "OldPop" population.
*/
void generateSol2d( CBiteRnd& rnd )
{
const CBitePop& OldPop = OldPops[ select( OldPopSel, rnd )];
if( OldPop.getCurPopPos() < 3 )
{
generateSol2c( rnd );
return;
}
ptype* const Params = TmpParams;
const ptype* const rp1 = getParamsOrdered(
rnd.getSqrInt( CurPopSize ));
const ptype* const rp2 = getParamsOrdered(
rnd.getInt( CurPopSize ));
const ptype* const rp3 = OldPop.getParamsOrdered(
rnd.getInt( OldPop.getCurPopPos() ));
const int Mode = select( Gen2dModeSel, rnd );
int i;
if( Mode == 0 )
{
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] = rp1[ i ] + (( rp2[ i ] - rp3[ i ]) >> 1 );
}
}
else
{
const ptype* const rp1b = getParamsOrdered(
rnd.getSqrInt( CurPopSize ));
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] = (( rp1[ i ] + rp1b[ i ]) +
( rp2[ i ] - rp3[ i ])) >> 1;
}
}
}
/**
* "Centroid mix with DE" solution generator, works well for convex
* functions. For DE operation, uses a better solution and a random
* worse solution.
*/
void generateSol3( CBiteRnd& rnd )
{
ptype* const Params = TmpParams;
const CBitePop& ParPop = selectParPop( 2, rnd );
const int ParPopSize = ParPop.getCurPopSize();
const ptype* const rp1 = ParPop.getParamsOrdered(
getMinSolIndex( 3, rnd, ParPopSize ));
const ptype* const rp2 = ParPop.getParamsOrdered(
rnd.getSqrIntInv( ParPopSize ));
const int Mode = select( Gen3ModeSel, rnd );
int i;
if( Mode == 0 )
{
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] = rp1[ i ] + ( rp1[ i ] - rp2[ i ]);
}
}
else
{
static const double CentProb[ 4 ] = { 0.0, 0.25, 0.5, 0.75 };
const double p = CentProb[ Mode ];
const ptype* const cp = getCentroid();
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] = ( rnd.get() < p ? cp[ i ] :
rp1[ i ] + ( rp1[ i ] - rp2[ i ]));
}
}
}
/**
* "Entropy bit mixing"-based solution generator. Performs crossing-over
* of an odd number (this is important) of random solutions via XOR
* operation. Slightly less effective than the DE-based mixing, but makes
* the optimization method more diverse overall.
*/
void generateSol4( CBiteRnd& rnd )
{
ptype* const Params = TmpParams;
const CBitePop* UsePops[ 2 ];
UsePops[ 0 ] = &selectAltPop( 1, rnd );
UsePops[ 1 ] = &selectParPop( 3, rnd );
int UseSize[ 2 ];
UseSize[ 0 ] = CurPopSize;
UseSize[ 1 ] = UsePops[ 1 ] -> getCurPopSize();
const int km = 3 + ( select( Gen4MixFacSel, rnd ) << 1 );
int p = rnd.getBit();
const ptype* rp1 = UsePops[ p ] -> getParamsOrdered(
rnd.getSqrInt( UseSize[ p ]));
copyParams( Params, rp1 );
int k;
for( k = 1; k < km; k++ )
{
p = rnd.getBit();
rp1 = UsePops[ p ] -> getParamsOrdered(
rnd.getSqrInt( UseSize[ p ]));
int i;
for( i = 0; i < ParamCount; i++ )
{
Params[ i ] ^= rp1[ i ];
}
}
// Simple XOR randomize.
int b = rnd.getSqrInt( 54 );