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!