Optimization expression operation not supported

I am trying to code a linear programming optimization problem but defining the constraint is proving a little difficult. The constraint is determined as:
  • difference of asset and liabilities cashflows is calculated, and
  • then the cumulative net cashflows are compounded by interest rates (element wise).
  • each element of the resultant matrix / vector is divided by a constant (53.844) and
  • constraint is that each element should be less than or equal to 0.05
I have given my code below but the constraint uses the "cumsum" function, which the problem-based approach doesn't entertain. I have also tried an alternative formulation (shown in the end) to avoid use of cumsum but still I did not had any success.
Please can anyone help to correct the code? Also, it would be useful to know how to formulate this problem in the Solver-based approach. It is the code for setting out the constraint that looks difficult to me.
prices = [99.74 91.22 98.71 103.75 97.15];
cashFlows = [4 5 2.5 5 4; 4 5 2.5 5 4; 4 5 2.5 5 4; 4 5 2.5 5 4; 4 5 102.5 5 4;4 5 0 105 104;4 105 0 0 0; 104 0 0 0 0];
obligations = [5 7 7 6 8 7 20 0]';
nt=size(cashFlows,1)
nb=size(cashFlows,2)
Rates = [0.01; 0.015; 0.017;0.019;0.02;0.025;0.027;0.029];
%Number of bonds available
nBonds = [10;100;20;30;5]
ALM = optimproblem;
bonds = optimvar('bonds',nb,'Type','integer','LowerBound',0,'UpperBound',nBonds);
ALM.ObjectiveSense = 'minimize';
ALM.Objective = prices*bonds;
%Define the constraint
ALM.Constraints.Const1 = (cumsum(cashFlows*bonds-obligations,2).*(1+Rates)')/53.844 <=0.05;
showproblem(ALM)
Solution = solve(ALM);
Solution.bonds = round(Solution.bonds);
Solution.bonds
%Alternative formulation of the constraint to avoid use of cumsum
B = triu(ones(8,5));
C=cashFlows*bonds-obligations;
D=C.*B;
E=sum(D,1);
ALM.Constraints.Const1 = (E.*(1+Rates)')/53.844 <=0.05;

 采纳的回答

Maybe this is what you intended,
C=cashFlows*bonds-obligations;
E=C.'*triu(ones(8));
ALM.Constraints.Const1 = (E.*(1+Rates)')/53.844 <=0.05;
Otherwise, it is not clear what the constraint is supposed to be. C is an 8x1 column vector, so cumsum(C,2) along dimension 2 wouldn't really make much sense. It would just leave C unchanged, as in the numeric example below.
>> a=rand(8,1)
a =
0.4897
0.3395
0.9516
0.9203
0.0527
0.7379
0.2691
0.4228
>> cumsum(a,2)
ans =
0.4897
0.3395
0.9516
0.9203
0.0527
0.7379
0.2691
0.4228
Also, it would be useful to know how to formulate this problem in the Solver-based approach.
That can be done with
prob2struct(ALM)

3 个评论

Thanks Matt By cumsum I intended to add successive rows/columns to get cumulative cash flows.
Your correction has made the code to work but I am getting zeros as answer. I maybe missing on something or it could be there is no answer. Do you have any suggestions for rectifying this?
(The constraint is intended to ensure that each element in the matrix is less than or equal to 0.05).
your correction has made the code to work but I am getting zeros as answer
In light of your objective,
ALM.ObjectiveSense = 'minimize';
ALM.Objective = prices*bonds;
it makes a lot of sense to me that prices*bounds will be minimized when all bonds(i)=0.
Thank you. I managed to figure out. It was just an error in the way I defined the constraint.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Linear Programming and Mixed-Integer Linear Programming 的更多信息

产品

版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by