Converting optimization output to struct

조회 수: 2 (최근 30일)
Pepijn Baart
Pepijn Baart 2019년 7월 24일
답변: Alan Weiss 2019년 8월 13일
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:
solvesol.PNG
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.

답변 (2개)

Alan Weiss
Alan Weiss 2019년 8월 13일
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

Matt J
Matt J 2019년 7월 24일
편집: Matt J 2019년 7월 24일
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
Pepijn Baart
Pepijn Baart 2019년 8월 12일
Thank you for your answer. The problem is that I do not have a sol structure to overwrite.
Matt J
Matt J 2019년 8월 12일
You can generate one by solving a silly version of the problem with a really simple fake objective and constraints.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear Programming and Mixed-Integer Linear Programming에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by