Using fmincon with a for loop
이전 댓글 표시
I am working on an optimization problem the first time for about 8 years. I am struggling with my for-loop.
The story behind the problem. I want to know, how high is the optimal investment for each period. I looked at you tube and found this video: "Application of Nonlinear Programming in Matlab"
The set up of my problem follows that in the video. First fmincon:
%%Setup for fmincon
LB = [0]; %vector with lower bounds
UB = inf; %vector with upper bounds
A = []; % No linear inequality constraints
B = [];
Aeq = []; %No linear equality contraints
Beq = [];
i0=0.1; %initial guess (starting point for iterations)
options = optimoptions('fmincon','Algorithm','sqp', 'Display','iter-detailed',...
'MaxFunctionEvaluations',100000,'MaxIterations',2000,'FunctionTolerance', 1e-10);
[i, present_value]=fmincon(@(i) obj_function_Diss(i), i0, A, B, Aeq, Beq, LB, UB, @(i) nonlcon_Diss(i), options)
Second the nonlcon:
function [cc, ceq] = nonlcon_Diss(i)
a=1; %lifetime of oldest machine
b=2;
k=0.8;
t=a+1;
z = 1;
y=b*i(z);
c = i(z)+k*(t-z)*b*i(z)/y;
ceq(1)=i(z)-c+k*(t-z)*b*i(z)/y;
ceq(2)=b*i(z)-y;
cc=[];
Third the objective function:
function present_value = obj_function_Diss(i)
a=1; %Age of oldest machine
b=2; %investment factor, should be bigger than 1
k=0.8; %cost factor
r=0.1; %interest rate
cum_present_value=0; %variable to cummulate the present value
t=a+1; %date of calculation
present_value = (b*i(z)-(i(z)+k*(t-z)*b*i(z)/b*i(z)))*exp(t*r*(-1));
cum_present_value=cum_present_value+present_value;
end
present_value=cum_present_value;
When you look at the code you can see, that I have set a=1. In this case it works. When I want to increase a e.g. to 4 or 7 or any other number, it is not working. I get the alert: "Index exceeds matrix dimensions."
Increasing "a" convert that single-period problem to a multi period problem. What do I have to change? And another question, imagine, I like to optimize i and a at the same time. How do I have to change the code?
If you need more information or have any other remarks please don't hesitate to answer. Thank you
답변 (1개)
Ameer Hamza
2018년 4월 21일
You can use fmincon to solve a multivariate optimization problem. You need to make following changes in the code.
- Change LB and UB for 2 variable case
LB = [0; 0]; %vector with lower bounds UB = [inf, inf]; %vector with upper bounds
- Define initial condition for both variables and combine them in one array. Here, I named combined array as x. Remember the first and second element of x correspond to values of a and i respectively.
i0=0.2; %initial guess (starting point for iterations) a0=0.5; x0=[a0 i0];
- Also change the nonlinear constraint function because the input is 2 element vector.
function [cc, ceq] = nonlcon_Diss(x) a=x(1); %lifetime of oldest machine i=x(2);
- Similarly change objective function
function present_value = obj_function_Diss(x) a=x(1); %Age of oldest machine i=x(2);
You can follow the same procedure to optimize several variables. The complete modified is also attached.
카테고리
도움말 센터 및 File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!