libMC.so.0.1.1
is a library defining a C++ class for calculating the McCormick relaxation of a nonconvex function on a box, as well as valid subgradients of these relaxations. Any function can be considered, provided it is factorable.The relaxation is calculated based on the operator overloading and function overloading capabilities of C++. The overloaded operators are: `+', `-', `*', and `/'; the overloaded functions are: `exp', `log', `pow', 'sqrt', 'fabs', and 'xlog'. (Trigonometric functions such as `cos', `sin' and `tan' aren't currently supported.)
Future versions of libAD
will allow dealing with a wider variety of mathematical functions. In particular, trigonometric functions such as `cos', `sin' and `tan' shall be implemented. Also, only the forward mode of AD is implemented presently, and future versions will also allow using the reverse mode of AD for relaxation calculation.
First, we shall define the variables and
. This is done as follows:
McCormick X( -2., 1., 0. );
McCormick Y( -2., 1., 0. );
Essentially, the first line means that X
is a variable of class McCormick, that it belongs to the interval , and that its current value is
. The same holds for the McCormick variable
Y
.
Once and
have been defined, the McCormick's convex and concave relaxations of
at
are simply calculated as
McCormick Z = X*pow(exp(X)-Y,2);
In particular, the value of the McCormick's convex underestimator and the McCormick's concave overestimator of on
at
are obtained as
double Zcvx = Z.cv();
double Zccv = Z.cc();
Likewise, a lower bound and an upper bound for the values of on
are obtained as
double Zlb = Z.l();
double Zub = Z.u();
McCormick::np(2);
Then, the variables and
are declared as before, except that the component index is now specified for the variables. For example, if
and
are considered to be components
and
, respectively, we write
McCormick X( -2., 1., 0., 0 );
McCormick Y( -2., 1., 0., 1 );
The McCormick's convex and concave relaxations of at
, as well as a subgradient of these relaxations, are simply calculated as
McCormick Z = X*pow(exp(X)-Y,2);
Finally, a subgradient of the McCormick's convex underestimator of on
at
is obtained as
const double* dZcvx = Z.dcvdp();
Alternatively, the components of this subgradient can be obtained separately as
double dZcvx_X = Z.dcvdp(0);
double dZcvx_Y = Z.dcvdp(1);
Analogously, a subgradient of the McCormick's concave overestimator of on
at
is obtained as
const double* dZccv = Z.dccdp();
double dZccv_X = Z.dccdp(0);
double dZccv_Y = Z.dccdp(1);
Note that whenever a McCormick relaxation is differentiable at a point, then the components of the subgradient correspond to the partial derivatives of the relaxation at that point.
Possible errors encountered during the calculation of a McCormick relaxation are:
Number | Description |
---|---|
1 | The size of the problem has changed (number of parameters) |
2 | Error during the relaxation of a division term (division by zero) |
3 | Error during the relaxation of an inverse function (zero in range) |
4 | Error during the relaxation of a logarithm function (negative values in range) |
5 | Error during the relaxation of a square root function (non positive values in range) |
6 | Error during the relaxation of an Arrhenius-like function (negative values in range, or negative coefficient) |