How to loop a objective function using the optimization toolbox?

조회 수: 7 (최근 30일)
BOWEN LI
BOWEN LI 2019년 7월 5일
답변: Bartlomiej Mroczek 2021년 4월 27일
Hi everyone,
I have a question that how can I put a "loop" structure in a optmization problem.
In my problem I have two binary decision variables and a objective function based on these two variables, and I am trying to use optmization toolbox to minimize the objective function.
There are 4 time periods in total, and the binary decision variables are changing(differs) in each time period. My objective function is built on the decision variable so that the objective function is for one time period as well. My goal is to minimize my objective function over all time periods.
For example, I have two binary decision variabe "y" and "s" that differs in each time period,
for n=4 %4 years in total
t=1:n
y=optimvar('y',[4,1],'Type','integer','LowerBound',0,'UpperBound',1);%binary decision variable
s=optimvar('s',[4,1],'Type','integer','LowerBound',0,'UpperBound',1);%binary decision variable
obj=optimproblem
obj.Objective = 2y+3s %minimize 2y+3s
obj.Constraints.precedence=y(t)-y(t-1)>=0 % a precedence constraint about y
[sol,fval] = solve(obj) % maybe use other method, i am supposed to use simmulated annealing

채택된 답변

Matt J
Matt J 2019년 7월 5일
편집: Matt J 2019년 7월 6일
My objective function is built on the decision variable so that the objective function is for one time period as well.
You cannot solve this as 4 separate problems, because your precedence constraint creates a dependence between y(t) and y(t-1). The following might be what you are looking for (and it doesn't require a loop):
y=optimvar('y',[4,1],'Type','integer','LowerBound',0,'UpperBound',1);%binary decision variable
s=optimvar('s',[4,1],'Type','integer','LowerBound',0,'UpperBound',1);%binary decision variable
obj=optimproblem;
obj.Objective = 2*sum(y)+3*sum(s); %minimize
obj.Constraints.precedence=diff(eye(4))*y>=0; % a precedence constraint about y
[sol,fval] = solve(obj)
  댓글 수: 2
BOWEN LI
BOWEN LI 2019년 7월 6일
Thank you so much, really appreciate your help all the way!
But one question that you mentioned it does not need a loop, while my objective function: obj.Objective = 2*sum(y)+3*sum(s), which is for one time period, and I have like 4 time periods in total. Is this one can be formulated as the precedence constraint?
Thank you!
Matt J
Matt J 2019년 7월 6일
편집: Matt J 2019년 7월 6일
obj.Objective = 2*sum(y)+3*sum(s), which is for one time period,
It's not for one time period anymore. Originally, you had
obj.Objective = 2*y(t)+3*s(t)
or at least I think that's what you meant. But in my answer, I changed it to
obj.Objective = 2*sum(y)+3*sum(s)
thus summing over all time periods. This was just a guess on my part of what objective function you really want. Only you can know what you're actually trying to solve.

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

추가 답변 (1개)

Bartlomiej Mroczek
Bartlomiej Mroczek 2021년 4월 27일
Hello to all Geeks:)
I work in the optimization area of the defined function f_PV.
The goal is to find optimal values of the vector a_PV for which ranges are defined.
The function f_PV only exists in a certain range x_PV.
Optimization is about looking for a mini. the values of the vector a in the range of the function.
Code snippet.
Defining the optimization vector
a_PV = optimvar("a_PV", [1,9],"LowerBound",[0.1; 0.1; 0.1; 0.1; 0.1; 0.1; 0.1; 0.1; 0.1]);
Defining a range for x_PV
x_PV = DANE_P_minus(idx0b:idxEb,1)
Defining the function f (a)
for i= x_PV(1):x_PV(end)
f_PV =a_PV(1)*sin(e1*x_PV(i)+f1) + a_PV(2)*sin(e2*x_PV(i)+f2) + a_PV(3)*sin(e3*x_PV(i)+f3) + a_PV(4)*sin(e4*x_PV(i)+f4) + a_PV(5)*sin(e5*x_PV(i)+f5) + a_PV(6)*sin(e6*x_PV(i)+f6) + a_PV(7)*sin(e7*x_PV(i)+f7) + a_PV(8)*sin(e8*x_PV(i)+f8) + a_PV(9)*sin(e9*x_PV(i)+f9);
end
Defining the problem
prob = optimproblem('ObjectiveSense', 'min');
prob.Objective = f_PV;
show(prob)
the values of e and f are defined - calculated
Request for help in compiling the code.

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by