/* minimize 4*x1^2 + x2^2 subject to -x1 -x2 +1 <= 0 -(1/4)x1 - x2 +(1/2) <= 0 inequality conditions are converted into equational conditions shown below by introducing slack variables l1 and l2 minimize 4*x1^2 + x2^2 subject to -x1 -x2 +1 +l1^2 = 0 -(1/4)x1 - x2 +(1/2) +l2^2 = 0 this converted problem will be solved by use of the program shown below */ #include #include #include #include "opt.h" #define Debug 0 #define Static static Static dbl obj(int n, dbl *x) { dbl x1, x2, l1, l2; x1 = x[0]; x2 = x[1]; l1 = x[2]; l2 = x[3]; return 4.00*x1*x1 + x2*x2; } Static dbl *gradobj(int n, dbl *x, dbl *d) { dbl x1, x2, l1, l2; x1 = x[0]; x2 = x[1]; l1 = x[2]; l2 = x[3]; d[0] = 8.00*x1; d[1] = 2.00*x2; d[2] = 0.00; d[3] = 0.00; return d; } Static dbl *cond(int n, dbl *x, int l, dbl *d) { dbl x1, x2, l1, l2; x1 = x[0]; x2 = x[1]; l1 = x[2]; l2 = x[3]; d[0] = - x1 - x2 + l1*l1 + 1.00; d[1] = - 0.25*x1 - x2 + l2*l2 + 0.50; return d; } Static dbl **gradcond(int n, dbl *x, int l, dbl **d) { dbl x1, x2, l1, l2; x1 = x[0]; x2 = x[1]; l1 = x[2]; l2 = x[3]; (d[0])[0] = -1; (d[0])[1] = -1; (d[0])[2] = 2*l1; (d[0])[3] = 0; (d[1])[0] = -0.25; (d[1])[1] = -1; (d[1])[2] = 0; (d[1])[3] = 2*l2; return d; } #define NVAR 4 #define NCND 2 main() { dbl x[NVAR]; enum status stat; x[0] = 0.1; x[1] = 0.1; x[2] = 0.0001; x[3] = 0.0001; matrixprintformat(" %l16.10f "); MultiplierMethodSetConsts(1000, 1.0e-6); stat = MultiplierMethodEqConds(NVAR, obj, gradobj, NCND, cond, gradcond, x, 10, 1.0e-4); if (stat == success) { fprintf(stderr, "----- solution -----\n"); vectorfprint(stderr, NVAR, x); } else { fprintf(stderr, "status = %d\n", stat); } }