# Dose who know about Optimization with Summation objective function?

67 views (last 30 days)
JunGi Kim on 29 Jan 2020
Commented: Alan Weiss on 30 Jan 2020
Hello, I want make objective function for 24-h Charging / Discharging scheduling. for 1:24
for 1:5
fun = a*P_char*C_cost1 - (1-a)P_dischar*C_cost2 %% a=charging signal, P = charging discharging power, C = charging discharging cost
end
end
but this function didn't consider other times cost, they just think current time step only.
How can I fix my function? Plz help me. thank you :)

Alan Weiss on 29 Jan 2020
Maybe you should update fun to remember the entire sum, not just the final one. And maybe you should name your indices.
fun = optimexpr;
for i = 1:24
for j = 1:5
fun = fun + a(i)*P_char(i)*C_cost1(j) - (1-a(i))*P_dischar(i)*C_cost2(j) %% a=charging signal, P = charging discharging power, C = charging discharging cost
end
end
I may have messed up the i and j indices, but I hope that you get the idea. You could probably create the expression without any loops, too, and it would be more efficient to do so. See Expressions for Objective Functions.
Alan Weiss
MATLAB mathematical toolbox documentation

JunGi Kim on 30 Jan 2020
for minlp, I write objective fuction like this, from above question.
fun = @(x) sum(x(1)*x(2)*cost_p(i) - x(3)*x(4)*cost_p(i));
How can I fix my func? to use j at x?
JunGi Kim on 30 Jan 2020
and I send my code for get answer.
cur_soc = Init_ev_SOC;
for i=1:5
for j=1:num_EV
%objective Function
% minimize
fun = @(x) x(1)*x(2)*cost_1(i) - x(3)*x(4)*cost_2(i);
% x(1) = charging mode (1,0) , x(2) = charging power,
% x(3) = discharging mode(1,0), x(4) = discharging power
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% constraints %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Non-linear Constraints (cl <= nlcon(x) <= cu)
nlcon = @(x) [
(x(1)*x(2) - x(3)*x(4))/ev_bat;
];
cl = [];
cu = [];
%Inequality Constraints (Ax <= b)
A = [];
b = [];
%Equality Constraints (Aeqx = beq)
Aeq = [];
beq = [];
% Initial Guess
x0 = [0 0 1 10];
% Bounds (lb<=x<=ub)
lb = zeros(4,1);
ub = [1, charging_rate, 1,charging_rate];
%range of n to solve for
% Set
xtype = 'BCBC';
Opt = opti('fun', fun,'ineq', A,b, 'eq', Aeq, beq, 'nl',nlcon,cl,cu,'bounds', lb, ub, 'x0', x0, 'xtype', xtype);
% Solve
[x,fval,exitflag,info] = solve(Opt)
deltaSoC = (x(1)*x(2) - x(3)*x(4))/ev_bat
cur_soc(j) = cur_soc(j) + deltaSoC
cur_soc(j,1);
end
end
Alan Weiss on 30 Jan 2020
I am still not sure that I understand you.
First, are you using the problem-based approach or the solver-based approach? I thought at first you were using the problem-based approach, but now I am not so sure.
For the problem-based approach, you need to create an optimization problem and optimization variables. Your objective function should not be a function handle, but instead should be an optimization expression in the optimization variables. You can use this approach only for recent MATLAB versions.
For the solver-based approach, you need to get your indices straight.
I do not understand what the command opti('fun', fun,...) does.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation