[37] | 1 | function [upper,x_min] = feaspump(p,x) |
---|
| 2 | |
---|
| 3 | % Assume we fail |
---|
| 4 | upper = inf; |
---|
| 5 | x_min = x; |
---|
| 6 | |
---|
| 7 | % These should be integer |
---|
| 8 | intvars = [p.integer_variables(:);p.binary_variables(:)]; |
---|
| 9 | n = length(p.c); |
---|
| 10 | |
---|
| 11 | % Create rounded goal variable |
---|
| 12 | xround = x; |
---|
| 13 | xround(intvars) = round(xround(intvars)); |
---|
| 14 | |
---|
| 15 | p.c = [0*p.c;ones(n,1)]; |
---|
| 16 | p.Q = blkdiag(0*p.Q,spalloc(n,n,0)); |
---|
| 17 | |
---|
| 18 | f1 = [p.F_struc(1:p.K.f,:) zeros(p.K.f,n)]; |
---|
| 19 | f2 = [p.F_struc(1+p.K.f:end,:) zeros(size(p.F_struc,1)-p.K.f,n)]; |
---|
| 20 | p.F_struc = [f1;xround -eye(n) eye(n);-xround eye(n) eye(n);f2]; |
---|
| 21 | p.K.l = p.K.l + 2*n; |
---|
| 22 | p.lb = [p.lb;-inf(n,1)]; |
---|
| 23 | p.ub = [p.ub;inf(n,1)]; |
---|
| 24 | |
---|
| 25 | % Append model with absolute value terms |
---|
| 26 | c = p.c; |
---|
| 27 | Q = p.Q; |
---|
| 28 | p.c = [0*p.c;ones(n,1)]; |
---|
| 29 | p.Q = blkdiag(0*p.Q,spalloc(n,n,0)); |
---|
| 30 | p.F_struc = [p.F_struc(1:p.K.f,:) zeros(p.K.f,n); |
---|
| 31 | xround -eye(n) eye(n); |
---|
| 32 | -xround eye(n) eye(n); |
---|
| 33 | p.F_struc zeros(size(p.F_struc,1)-p.K.f,n)]; |
---|
| 34 | p.monomtable = blkdiag(p.monomtable,eye(n)); |
---|
| 35 | p.variabletype = [p.variabletype zeros(1,n)]; |
---|
| 36 | p.K.l = p.K.l + 2*n; |
---|
| 37 | p.lb = [p.lb;-inf(n,1)]; |
---|
| 38 | p.ub = [p.ub;inf(n,1)]; |
---|
| 39 | |
---|
| 40 | iter = 1; |
---|
| 41 | xold = x; |
---|
| 42 | while iter < 10 & isinf(upper) |
---|
| 43 | |
---|
| 44 | % Append model with absolute value terms |
---|
| 45 | ii = p.integer_variables; |
---|
| 46 | jj = p.binary_variables; |
---|
| 47 | p.integer_variables = []; |
---|
| 48 | p.binary_variables = []; |
---|
| 49 | p.F_struc(1+p.K.f:p.K.f+2*n,1) = [xround;-xround]; |
---|
| 50 | |
---|
| 51 | pumpsol = feval(p.solver.lower.call,p); |
---|
| 52 | p.integer_variables = ii; |
---|
| 53 | p.binary_variables = jj; |
---|
| 54 | |
---|
| 55 | % Fix the varying data |
---|
| 56 | p.F_struc(1+p.K.f:1+p.K.f+2*n-1,1) = [xround;-xround]; |
---|
| 57 | |
---|
| 58 | % Solve pumping problem |
---|
| 59 | pumpsol = feval(p.solver.lower.call,p); |
---|
| 60 | |
---|
| 61 | % New solution (extract original variables) |
---|
| 62 | xnew = pumpsol.Primal(1:n); |
---|
| 63 | |
---|
| 64 | if all(abs(xnew(intvars)-round(xnew(intvars))) < 1e-5) |
---|
| 65 | % Feasible |
---|
| 66 | upper = c'*xnew + xnew'*Q*xnew; |
---|
| 67 | x_min = xnew(1:n); |
---|
| 68 | else |
---|
| 69 | % Stall? |
---|
| 70 | if norm(xold-xnew) < 1e-3 |
---|
| 71 | % Yep, so flip the goal on some integers |
---|
| 72 | [ii,jj] = sort(abs(xnew(intvars)-xround(intvars))); |
---|
| 73 | flips = jj(end-(1+ceil(5*rand(1))):end); |
---|
| 74 | % for i =1:length(jj) |
---|
| 75 | xround(intvars(flips)) = (xround(intvars(flips))-1).^2; |
---|
| 76 | % end |
---|
| 77 | else |
---|
| 78 | xround = xnew;xold = xnew; |
---|
| 79 | xround(intvars) = round(xround(intvars)); |
---|
| 80 | end |
---|
| 81 | end |
---|
| 82 | iter = iter + 1; |
---|
| 83 | end |
---|