-
Notifications
You must be signed in to change notification settings - Fork 9
/
constr2.cpp
123 lines (97 loc) · 2.28 KB
/
constr2.cpp
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
// Non-linearly constrained problem:
// http://www-optima.amp.i.kyoto-u.ac.jp/member/student/hedar/Hedar_files/TestGO_files/Page2031.htm
#include <stdio.h>
#include "biteopt.h"
static const double tol = 1e-5;
const int N = 4;
class CTestOpt : public CBiteOptDeep
{
public:
int con_notmet;
double real_value;
virtual void getMinValues( double* const p ) const
{
p[ 0 ] = 0.0;
p[ 1 ] = 0.0;
p[ 2 ] = -0.55;
p[ 3 ] = -0.55;
}
virtual void getMaxValues( double* const p ) const
{
p[ 0 ] = 1200.0;
p[ 1 ] = 1200.0;
p[ 2 ] = 0.55;
p[ 3 ] = 0.55;
}
double penalty( const double v )
{
if( v > tol )
{
con_notmet++;
return( v - tol );
}
return( 0.0 );
}
double penalty0( double v )
{
v = fabs( v );
if( v > tol )
{
con_notmet++;
return( v - tol );
}
return( 0.0 );
}
virtual double optcost( const double* const p )
{
double cost = 3.0*p[1]+1e-6*pow(p[0],3.0)+2.0*p[1]+
2e-6/3.0*pow(p[1],3.0);
const int n_con = 5;
double pn[ n_con ];
con_notmet = 0;
pn[ 0 ] = penalty( p[2]-p[3]-0.55 );
pn[ 1 ] = penalty( p[3]-p[2]-0.55 );
pn[ 2 ] = penalty0( 1000*(sin(-p[2]-0.25)+sin(-p[3]-0.25))+894.8-p[0] );
pn[ 3 ] = penalty0( 1000*(sin(p[2]-0.25)+sin(p[2]-p[3]-0.25))+894.8-p[1] );
pn[ 4 ] = penalty0( 1000*(sin(p[3]-0.25)+sin(p[3]-p[2]-0.25))+1294.8 );
real_value = cost;
if( con_notmet > 0 )
{
const double ps = 1.0 + 1.0 / n_con;
double pns = 0.0;
int i;
for( i = 0; i < n_con; i++ )
{
const double v = pn[ i ];
pns = ( 1.0 + pns ) * ps + ( v + v * v + v * v * v ) * 0.33333;
}
cost += 1e10 * pns;
}
return( cost );
}
};
int main()
{
CBiteRnd rnd;
rnd.init( 1 ); // Needs to be seeded with different values on each run.
CTestOpt opt;
opt.updateDims( N, 6 );
opt.init( rnd );
int i;
for( i = 0; i < 2000000; i++ )
{
if( opt.optimize( rnd ) > N * 128 )
{
break;
}
}
opt.optcost( opt.getBestParams() );
printf( "Finished at iteration %i\n", i + 1 );
printf( "Objective = %.8g\n", opt.real_value );
printf( "Constraints not met: %i\n", opt.con_notmet );
for( i = 0; i < N; i++ )
{
printf( "x[%i] = %.10g\n", i, opt.getBestParams()[ i ]);
}
return( 0 );
}