00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "CouennePrecisions.hpp"
00012 #include "CouenneTypes.hpp"
00013 #include "CouenneObject.hpp"
00014
00015 #include "CouenneExprMul.hpp"
00016 #include "CouenneFunTriplets.hpp"
00017 #include "CouenneProjections.hpp"
00018
00019 using namespace Couenne;
00020
00023 CouNumber exprMul::selectBranch (const CouenneObject *obj,
00024 const OsiBranchingInformation *info,
00025 expression *&var,
00026 double * &brpts,
00027 double * &brDist,
00028
00029 int &way) {
00030
00031 if (brDist) {free (brDist); brDist = NULL;}
00032
00033 int xi = arglist_ [0] -> Index (),
00034 yi = arglist_ [1] -> Index (),
00035 wi = obj -> Reference () -> Index ();
00036
00037 assert ((xi >= 0) && (yi >= 0) && (wi >= 0));
00038
00039 CouNumber
00040 x0 = info -> solution_ [xi], y0 = info -> solution_ [yi],
00041 xl = info -> lower_ [xi], yl = info -> lower_ [yi],
00042 xu = info -> upper_ [xi], yu = info -> upper_ [yi];
00043
00044 #ifdef DEBUG
00045 printf (" branch MUL: %g [%g,%g] %g [%g,%g]\n",
00046 x0, xl, xu, y0, yl, yu);
00047 #endif
00048
00049 brpts = (double *) realloc (brpts, sizeof (double));
00050
00051
00052
00053 if (fabs (xu-xl) < COUENNE_EPS) {
00054
00055 if (fabs (yu-yl) < COUENNE_EPS) {
00056
00057 var = NULL;
00058 return 0.;
00059
00060 } else {
00061
00062 var = arglist_ [1];
00063 *brpts = 0.5 * (yl+yu);
00064 brDist = (double *) realloc (brDist, 2 * sizeof (double));
00065
00066 brDist [0] = projectSeg (x0, y0, yl, xl*yl, *brpts, *brpts * xl, 0);
00067 brDist [1] = projectSeg (x0, y0, *brpts, *brpts * xl, yu, xl*yu, 0);
00068
00069
00070 return CoinMin (brDist [0], brDist [1]);
00071 }
00072
00073 } else if (fabs (yu-yl) < COUENNE_EPS) {
00074
00075 var = arglist_ [0];
00076 *brpts = 0.5 * (xl+xu);
00077 brDist = (double *) realloc (brDist, 2 * sizeof (double));
00078
00079 brDist [0] = projectSeg (x0, y0, xl, xl*yl, *brpts, *brpts * yl, 0);
00080 brDist [1] = projectSeg (x0, y0, *brpts, *brpts * yl, xu, xu*yl, 0);
00081
00082
00083 return CoinMin (brDist [0], brDist [1]);
00084 }
00085
00086
00087
00088 if ((((var = arglist_ [0]) -> Index() >= 0) && (xl < -COUENNE_INFINITY) && (xu > COUENNE_INFINITY)) ||
00089 (((var = arglist_ [1]) -> Index() >= 0) && (yl < -COUENNE_INFINITY) && (yu > COUENNE_INFINITY))) {
00090
00091 *brpts = 0.;
00092 brDist = computeMulBrDist (info, xi, yi, wi, var -> Index (), brpts);
00093 way = (info -> solution_ [var -> Index ()] > *brpts) ? TWO_RIGHT : TWO_LEFT;
00094
00095 return CoinMin (brDist [0], brDist [1]);
00096 }
00097
00098
00099
00100
00101
00102 int ind = -1;
00103
00104 if (xl < -large_bound)
00105 {ind = xi; *brpts = obj -> midInterval (((x0 < 0.) ? 2 : 0.5) * x0, xl, xu, info); way = TWO_RIGHT;}
00106
00107 else if (xu > large_bound)
00108 {ind = xi; *brpts = obj -> midInterval (((x0 > 0.) ? 2 : 0.5) * x0, xl, xu, info); way = TWO_LEFT;}
00109
00110 else if (yl < -large_bound)
00111 {ind = yi; *brpts = obj -> midInterval (((y0 < 0.) ? 2 : 0.5) * y0, yl, yu, info); way = TWO_RIGHT;}
00112
00113 else if (yu > large_bound)
00114 {ind = yi; *brpts = obj -> midInterval (((y0 > 0.) ? 2 : 0.5) * y0, yl, yu, info); way = TWO_LEFT;}
00115
00116 else {
00117
00118
00119
00120 CouNumber delta = (yu-yl) - (xu-xl);
00121
00122 if (delta > +COUENNE_EPS) ind = yi;
00123 else if (delta < -COUENNE_EPS) ind = xi;
00124 else ind = (CoinDrand48 () < 0.5) ? xi : yi;
00125
00126 CouNumber
00127 pt = info -> solution_ [ind],
00128 lb = info -> lower_ [ind],
00129 ub = info -> upper_ [ind],
00130 margin = obj -> lp_clamp () * (ub - lb);
00131
00132 if ((lb < -COUENNE_EPS) &&
00133 (ub > COUENNE_EPS) &&
00134 (-lb/ub >= THRES_ZERO_SYMM) &&
00135 (-ub/lb >= THRES_ZERO_SYMM))
00136
00137 *brpts = 0.;
00138
00139 else if ((lb < - large_bound) &&
00140 (ub > large_bound) &&
00141 (fabs (pt) > large_bound))
00142 *brpts = 0.;
00143
00144 else switch (obj -> Strategy ()) {
00145 case CouenneObject::LP_CENTRAL: *brpts = pt; if ((pt < lb + margin) ||
00146 (pt > ub - margin))
00147 pt = .5 * (lb+ub); break;
00148 case CouenneObject::LP_CLAMPED: *brpts = CoinMax (lb + margin,
00149 CoinMin (ub - margin, pt)); break;
00150 case CouenneObject::MID_INTERVAL: *brpts = obj -> midInterval (pt, lb, ub, info); break;
00151 case CouenneObject::BALANCED: *brpts = balancedMul (info, (ind == xi) ? 0 : 1, wi); break;
00152 case CouenneObject::MIN_AREA:
00153
00154
00155 default: *brpts = (0.5 * (lb+ub)); break;
00156 }
00157
00158 way = (pt > *brpts) ? TWO_RIGHT : TWO_LEFT;
00159 }
00160
00161 assert (ind >= 0);
00162
00163 var = arglist_ [(ind == xi) ? 0 : 1];
00164
00165 brDist = computeMulBrDist (info, xi, yi, wi, ind, brpts);
00166
00167 #ifdef DEBUG
00168 printf (" MUL: br on x_%d %g [%g,%g] [%g,%g] (%g,%g)\n",
00169 ind, *brpts, xl, xu, yl, yu, x0, y0);
00170 #endif
00171
00172 return CoinMin (brDist [0], brDist [1]);
00173
00174 }
00175
00176
00177
00178 CouNumber exprMul::balancedMul (const OsiBranchingInformation *info, int index, int wind) {
00179
00180
00181
00182 int other;
00183
00184 if (index==0) {
00185 index = arglist_ [0] -> Index ();
00186 other = arglist_ [1] -> Index ();
00187 } else {
00188 index = arglist_ [1] -> Index ();
00189 other = arglist_ [0] -> Index ();
00190 }
00191
00192 assert ((index >= 0) && (other >= 0));
00193
00194 CouNumber
00195 xl = info -> lower_ [index], yl = info -> lower_ [other],
00196 xu = info -> upper_ [index], yu = info -> upper_ [other],
00197 x0 = info -> solution_ [index], y0 = info -> solution_ [other],
00198 w0 = info -> solution_ [wind];
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 powertriplet ft (2);
00265
00266
00267
00268
00269 bool above = (w0 > x0*y0);
00270
00271 CouNumber
00272 dx = xu-xl,
00273 dy = yu-yl,
00274 area = dx*dy,
00275 bA = yl*dx - xl*dy,
00276 bB = -yl*dx - xu*dy,
00277 m = 1. / sqrt (area),
00278 qA = -bA / (2*area),
00279 qB = bB / (2*area),
00280 z_opt = above ?
00281 minMaxDelta (&ft, -qA/m, (1-qA)/m):
00282 minMaxDelta (&ft, -qB/m, (1-qB)/m),
00283 t_optA = m*z_opt + qA,
00284 t_optB = m*z_opt + qB;
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 return (w0 > x0*y0) ?
00295 (xl + dx * t_optA) :
00296 (xu - dx * t_optB);
00297 }