[37] | 1 | function sys = optimizer(F,h,ops,x,u) |
---|
| 2 | %OPTIMIZER Container for optimization problem |
---|
| 3 | % |
---|
| 4 | % [OPT,PROBLEM] = OPTIMIZER(F,h,options,x,u) exports an object that |
---|
| 5 | % contains precompiled numerical data to be solved for varying arguments |
---|
| 6 | % x, returning the optimal value of the variable u. |
---|
| 7 | % |
---|
| 8 | % OPTIMIZER typically only makes sense if the varying data x enters the |
---|
| 9 | % optmization problem affinely. |
---|
| 10 | % |
---|
| 11 | % Example |
---|
| 12 | % The following problem creates an LP with varying upper and lower |
---|
| 13 | % bounds on the decision variable. |
---|
| 14 | % |
---|
| 15 | % The optimizing argument is obtained by indexing the optimizer object |
---|
| 16 | % with the point of interest. The argument should be a column vector (if |
---|
| 17 | % the argument has a width larger than 1, YALMIP assumes that the |
---|
| 18 | % optimal solution should be computed in several points) |
---|
| 19 | % |
---|
| 20 | % A = randn(10,3); |
---|
| 21 | % b = rand(10,1)*19; |
---|
| 22 | % c = randn(3,1); |
---|
| 23 | % |
---|
| 24 | % z = sdpvar(3,1); |
---|
| 25 | % sdpvar UB LB |
---|
| 26 | % |
---|
| 27 | % F = set(A*z < b) + set(LB < z < UB); |
---|
| 28 | % h = c'*z |
---|
| 29 | % optZ = optimizer(F,h,[],[LB; UB],z); |
---|
| 30 | % |
---|
| 31 | % % Solve for LB=1, UB = 3; |
---|
| 32 | % zopt = optZ([1; 3]) |
---|
| 33 | % |
---|
| 34 | % % Solve for (LB,UB) [1;3] and (LB,UB) [2;6] |
---|
| 35 | % zopt = optZ([[1; 3], [2;6]]) |
---|
| 36 | |
---|
| 37 | if nargin < 5 |
---|
| 38 | error('OPTIMIZER requires 5 inputs'); |
---|
| 39 | end |
---|
| 40 | |
---|
| 41 | x = x(:); |
---|
| 42 | n = length(x); |
---|
| 43 | [aux1,aux2,aux3,model] = export(set(x == repmat(pi,n,1))+F,h,ops,[],[],0); |
---|
| 44 | |
---|
| 45 | if norm(model.F_struc(1:n,1)-repmat(pi,length(x),1),inf) > 1e-10 |
---|
| 46 | error('Failed exporting the model (try to specify another solver)') |
---|
| 47 | end |
---|
| 48 | |
---|
| 49 | map = []; |
---|
| 50 | for i = 1:length(u) |
---|
| 51 | var = getvariables(u(i)); |
---|
| 52 | map = [map;find(var == model.used_variables)]; |
---|
| 53 | end |
---|
| 54 | |
---|
| 55 | sys.recover = aux2; |
---|
| 56 | sys.model = model; |
---|
| 57 | sys.n = n; |
---|
| 58 | sys.map = map; |
---|
| 59 | sys = class(sys,'optimizer'); |
---|