Optimization expression operation not supported
Show older comments
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;
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!