MATLAB Answers

Converting optimization output to struct

1 view (last 30 days)
Pepijn Baart on 24 Jul 2019
Answered: Alan Weiss on 13 Aug 2019
I am optimising a OtpimizationProblem with the follwoing variables:
SI = optimvar('SI', 1, 1, J,N,'Type','integer','Lowerbound',0,'Upperbound',1);
SO = optimvar('SO', 1, 1, J,N,'Type','integer','Lowerbound',0,'Upperbound',1);
SD = optimvar('SD', 1, 1, KD+J,N,'Type','integer','Lowerbound',0,'Upperbound',1);
X = optimvar('X', 1, numel(I),J,N,'Type','integer','Lowerbound',0,'Upperbound',1);
Y = optimvar('Y', 1, numel(I),J,N,'Type','integer','Lowerbound',0,'Upperbound',1);
test= optimvar('test',1, numel(I),J,N,'Type','integer','Lowerbound',0,'Upperbound',1);
Z = optimvar('Z', 1, 1, J,N,'Lowerbound',0,'Upperbound',1);
E = optimvar('E', 1, 1, J,N,'Lowerbound',0,'Upperbound',1);
W = optimvar('W', 1, 1, J,N,'Lowerbound',0,'Upperbound',1);
T = optimvar('T', 1, 1, 1,N,'Lowerbound',0,'Upperbound',H);
TLB = optimvar('TLB',1, 1, J,N,'Lowerbound',0);
TEE = optimvar('TEE',1, 1, J,N,'Lowerbound',0);
TS = optimvar('TS', 1, 1, J,N,'Lowerbound',0);
TW = optimvar('TW', 1, 1, J,N,'Lowerbound',0);
BS = optimvar('BS', 1, numel(I),J,N,'Lowerbound',0);
BE = optimvar('BE', 1, numel(I),J,N,'Lowerbound',0);
BP = optimvar('BP', 1, numel(I),J,N,'Lowerbound',0);
II = optimvar('II', numel(M),1, J,N,'Lowerbound',0);
IO = optimvar('IO', numel(M),1, J,N,'Lowerbound',0);
IV = optimvar('IV', numel(M),1, K+J,N,'Lowerbound',0);
FVU = optimvar('FVU', numel(M),K+J,J,N,'Lowerbound',0);
FUV = optimvar('FUV', numel(M),J,K+J,N,'Lowerbound',0);
FUU = optimvar('FUU', numel(M),J,J,N,'Lowerbound',0);
FVV = optimvar('FVV', numel(M),K+J,K+J,N,'Lowerbound',0);
Q = optimvar('Q', 1,1,numel(R),N,'Lowerbound',0);
In order to optimize the problem I can either use
solve(scheduleprob)
or
SP=prob2struct(scheduleprob);
[sol2,fval2, exitflag2, output2] = intlinprog(SP.f,SP.intcon,SP.Aineq,SP.bineq,...
SP.Aeq,SP.beq,SP.lb,SP.ub,SP.x0,SP.options)
The first method gives the solution in the following form:
This form is easy to use, and therefor prefferable for me.
The seconde method gives its result as a 4599x1 double.
Is there a way to convert the second type of result into the first type?
I am aware that in this example there is no difference in which method I use, but if I use cplex, which is a lot faster, the results will be presented in the second form.

0 Comments

Sign in to comment.

Answers (2)

Alan Weiss on 13 Aug 2019
You might be interested in the function mapSolution. You need to make the problem structure, but then, given the x output from cplex, it will give you the sol solution structure that you want.
Alan Weiss
MATLAB mathematical toolbox documentation

0 Comments

Sign in to comment.

Matt J on 24 Jul 2019
Edited: Matt J on 24 Jul 2019
I'm a bit surprised that OptimizationProblem class doesn't have a class method for this, but the example below shows how you can over-write an existing sol structure with the pure numeric output from linprog and other similar solvers. The disadvantage is that you have to have a template sol struct already lying around somewhere.
x=optimvar('x',[4,1],'LowerBound',[1:4]*10);
y=optimvar('y',[3,1],'LowerBound',[5:7]*10);
prob=optimproblem;
prob.Objective=sum(x)+sum(y);
sfprob=prob2struct(prob);
xnum=linprog(sfprob)
sol=solve(prob)
sol2=overwrite_sol(sol,xnum) %convert xnum to the same structure form as sol
function solnew=overwrite_sol(sol,x)
f=fieldnames(sol);
I=sol;
c=0;
for i=1:numel(f)
I.(f{i})=c+(1:numel(sol.(f{i})));
c=I.(f{i})(end);
end
solnew=sol;
for i=1:numel(f)
solnew.(f{i})=x(I.(f{i}));
end
end

2 Comments

Pepijn Baart on 12 Aug 2019
Thank you for your answer. The problem is that I do not have a sol structure to overwrite.
Matt J on 12 Aug 2019
You can generate one by solving a silly version of the problem with a really simple fake objective and constraints.

Sign in to comment.

Sign in to answer this question.