-
Notifications
You must be signed in to change notification settings - Fork 9
/
deopt.h
241 lines (197 loc) · 5.81 KB
/
deopt.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
//$ nocpp
/**
* @file deopt.h
*
* @version 2024.6
*
* @brief The inclusion file for the CDEOpt class.
*
* @section license License
*
* Copyright (c) 2021-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 DEOPT_INCLUDED
#define DEOPT_INCLUDED
#include "biteaux.h"
/**
* Differential Evolution-alike DFO solver.
*
* Description is available at https://github.com/avaneev/biteopt
*/
class CDEOpt : public CBiteOptBase< int64_t >
{
public:
typedef int64_t ptype; ///< Parameter value storage type (should be a
///< signed integer type, same as CBiteOptBase template parameter).
/**
* Function updates dimensionality of *this object.
*
* @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 : 30 * aParamCount );
if( aParamCount == ParamCount && aPopSize == PopSize )
{
return;
}
initBuffers( aParamCount, aPopSize );
}
/**
* Function initializes *this optimizer.
*
* @param rnd Random number generator.
* @param InitParams If not NULL, initial parameter vector, also used as
* centroid.
* @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 = 0x1p-4 * InitRadius;
setStartParams( InitParams );
}
/**
* Function performs the parameter optimization iteration that involves 1
* objective function evaluation.
*
* @param rnd Random number generator.
* @return The number of non-improving iterations so far.
*/
int optimize( CBiteRnd& rnd )
{
int i;
if( DoInitEvals )
{
ptype* const Params = getCurParams();
genInitParams( rnd, Params );
const double NewCost = fixCostNaN( optcost( NewValues ));
NewCosts[ 0 ] = NewCost;
updateBestCost( NewCost, NewValues, updatePop( NewCost, Params ));
if( CurPopPos == PopSize )
{
DoInitEvals = false;
}
return( 0 );
}
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 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++ )
{
TmpParams[ i ] = ( rp2[ i ] - rp3[ i ]) + ( rp4[ i ] - rp5[ i ]) +
( rp6[ i ] - rp7[ i ]);
}
// TPDF bit randomization.
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;
TmpParams[ k ] += v1 - v2; // Apply in TPDF manner.
}
if( rnd.getBit() )
{
int si2 = si1 + rnd.getBit() * 2 - 1;
if( si2 < 0 )
{
si2 = 1;
}
const ptype* const rp1b = getParamsOrdered( si2 );
for( i = 0; i < ParamCount; i++ )
{
TmpParams[ i ] = (( rp1[ i ] + rp1b[ i ]) >> 1 ) +
( TmpParams[ i ] >> 2 );
}
}
else
{
for( i = 0; i < ParamCount; i++ )
{
TmpParams[ i ] = rp1[ i ] + ( TmpParams[ i ] >> 2 );
}
}
for( i = 0; i < ParamCount; i++ )
{
TmpParams[ i ] = wrapParam( rnd, TmpParams[ i ]);
NewValues[ i ] = getRealValue( TmpParams, i );
}
const double NewCost = fixCostNaN( optcost( NewValues ));
NewCosts[ 0 ] = NewCost;
const int p = updatePop( NewCost, TmpParams );
if( p < CurPopSize )
{
updateBestCost( NewCost, NewValues, p );
StallCount = 0;
}
else
{
StallCount++;
}
return( StallCount );
}
};
#endif // DEOPT_INCLUDED