Please, I have limited time for a project. I'll like to know what this code does

조회 수: 1 (최근 30일)
variables = {'as','aw','ah'};
N = length(variables);
%create variables for indexing
for v = 1:N
eval([variables{v},' = ', num2str(v),';']);
end
%Defining the lower bounds
lb = zeros(size(variables));
lb([as,aw,ah]) = [0,0,0];
%Defining the upper bounds
ub = Inf(size(variables));
ub([as,aw,ah]) = [Ns,Nw,Nh];
%Entrying linear inequality constraints
A = zeros(2,3);
A(1,[as,aw,ah]) = [-Ps(i),-Pw(i),-Ph(i)];
b(1) = -P_load(i);
A(2,[as,aw,ah]) = [Ps(i),Pw(i),Ph(i)];
b(2) = P_max;
%Linear Equality Constraints
Aeq=[];
beq=[];
%Objective Function
f = zeros(size(variables));
f([as aw ah]) = [Cus*Ps(i)*T Cuw*Pw(i)*T Cuh*Ph(i)*T];
%Solving the problem with linprog
[x fval] = linprog(f,A,b,Aeq,beq,lb,ub);
for d = 1:N
fprintf('%12.2f \t%s\n',x(d),variables{d});
end;
aso(i)=x(1), awo(i)=x(2), aho(i)=x(3);
P_Supply(i)=aso(i)*Ps(i)+awo(i)*Pw(i)+aho(i)*Ph(i);
cost(i)=fval/P_Supply(i);
  댓글 수: 2
John D'Errico
John D'Errico 2015년 6월 7일
편집: John D'Errico 2015년 6월 7일
If you have limited time, then perhaps you should have started sooner. Regardless, it is difficult to know what some random piece of lightly commented code does, given no real information. It is an optimization, but if you need to know more, then talk to the person you got it from.
Jan
Jan 2015년 6월 7일
@Nganyu Derick: Of course you have a limited time for your project. This is the nature of projects. If you want to get a useful answer soon, care for a meaningful question. Your question is very general and it is not clear if you need an explanation of the "=" operator or of the command length(). You've posted code, which does not run due to several errors. Therefore we do not have teh faintest chance to guess reliably, what it should do. In consequence posting this question consumes the time of you and the readers without the possibility to obtain a useful answer.

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

채택된 답변

Jan
Jan 2015년 6월 7일
The code is very strange. It is overly complicated and near to be obfuscated.
Original:
variables = {'as','aw','ah'};
N = length(variables);
%create variables for indexing
for v = 1:N
eval([variables{v},' = ', num2str(v),';']);
end
Simplified (as Walter has posted already):
as = 1;
aw = 2;
ah = 3;
Original:
lb = zeros(size(variables));
lb([as,aw,ah]) = [0,0,0];
ub = Inf(size(variables));
ub([as,aw,ah]) = [Ns,Nw,Nh];
Simplified:
lb = zeros(1, 3);
ub = [Ns,Nw,Nh]; % The values of Ns, Nw and Nh do not appear in your code
Original:
A = zeros(2,3);
A(1,[as,aw,ah]) = [-Ps(i),-Pw(i),-Ph(i)];
b(1) = -P_load(i);
A(2,[as,aw,ah]) = [Ps(i),Pw(i),Ph(i)];
b(2) = P_max;
Simplified:
A = [-Ps(i),-Pw(i),-Ph(i); ...
Ps(i),Pw(i),Ph(i)];
b = [-P_load(i), P_max];
But notice, that "i" is not defined in the posted code.
Original:
%Objective Function
f = zeros(size(variables));
f([as aw ah]) = [Cus*Ps(i)*T Cuw*Pw(i)*T Cuh*Ph(i)*T];
Simplified:
f = [Cus*Ps(i), Cuw*Pw(i), Cuh*Ph(i)] * T;
Seeing the original code let me assume, that the author does not want the program to be understandable on purpose. The missing variables i,Ns,Nw,Nh let me think, that this program will not run at all. The actual command linprog(f,A,b,Aeq,beq,lb,ub) is not reached.
  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 6월 7일
I think the poster extracted from the original. The original is odd.
Nganyu Derick
Nganyu Derick 2015년 6월 8일
Thanks very much, for your well presented answer. it was of great help

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

추가 답변 (2개)

Ayush
Ayush 2015년 6월 7일
편집: John D'Errico 2015년 6월 7일
@Nganyu Derick
ERROR: Undefined function or variable 'Ns'.
PLease re-check your code
  댓글 수: 2
John D'Errico
John D'Errico 2015년 6월 7일
편집: John D'Errico 2015년 6월 7일
So? I gave an answer, as much as it deserved. I told them to ask the source. Perhaps you should give them a complete dissertation on what this code does, since you were so willing to volunteer my time.
Nganyu Derick
Nganyu Derick 2015년 6월 7일
편집: Walter Roberson 2015년 6월 7일
I am not an expert in MATLAB, so please just help explain what each line of the following does
variables = {'as','aw','ah'};
N = length(variables);
%create variables for indexing
for v = 1:N
eval([variables{v},' = ', num2str(v),';']);
end
%Defining the lower bounds
lb = zeros(size(variables));
lb([as,aw,ah]) = [0,0,0];
%Defining the upper bounds
ub = Inf(size(variables));
ub([as,aw,ah]) = [Ns,Nw,Nh];

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


Walter Roberson
Walter Roberson 2015년 6월 7일
It appears to me that it does some kind of load-flow optimization for an electrical system with three suppliers.
I think I must be missing some of what it is doing, as it looks to me as if the problem should be mechanically solvable without using linprog by using the least expensive source first up to its maximum capacity, the next most expensive source next, and so on, until the load has been met. My mind is a bit cloudy at the moment so I do not have confidence in that analysis.
  댓글 수: 3
Walter Roberson
Walter Roberson 2015년 6월 7일
The first set of lines is a fancy way of making the assignment
as = 1; aw = 2; ah = 3;
lb is then set to a 1 x 3 vector of 0s, and then those locations in lb are overwritten by 0 (a useless step).
ub is set to a 1 x 3 vector of infinities, and then those values are overwritten by [Ns, Nw, Nh], leading to the same result as if you had just done ub = [Ns, Nw, Nh]
The code you quote here could be replaced with
lb = zeros(1,3);
ub = [Ns, Nw, Nh];

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

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by