I'm supervising exercise sessions about LINGO and LP and today an exercise came up about investing. This is the intended solution:
MODEL:
SETS:
year /t0 t1 t2 t3/: capital;
investment /A B C D E/: allocated;
table(investment, year): cash_flow;
ENDSETS
DATA:
cash_flow = -1 0.5 1 0
0 -1 0.5 1
-1 1.2 0 0
-1 0 0 1.9
0 0 -1 1.5;
ENDDATA
max = capital(4);
! Capital(i) indicates the amount of money left after time i;
! Initial capital;
capital(1) = 100000 + @SUM(investment(j): allocated(j)*cash_flow(j, 1));
! Capital after one year is last year's capital plus interest
and the cash flows times investments of the current year;
@FOR(year(i) | i #GT# 1: capital(i) = 1.08*capital(i - 1)
+ @SUM(investment(j): allocated(j)*cash_flow(j, i)));
! Capital must be positive after each time period;
@FOR(year: capital >= 0);
! No more than $75000 may be invested in a single investment;
@FOR(investment: allocated <= 75000);
@FOR(investment: allocated >= 0);
END
One student tried to substitute the net cash flow per year by a variable to remove some code duplication. However, just by adding the variable net_cashflow to year's derived sets in the sets-section and adding the following constraint to the body:
@FOR(year(i): net_cashflow(i) = @SUM(investment(j): cash_flow(j, i)*allocated(j)));
LINGO's solution is now wrong. All net_cashflow(i)'s are set to 0 and as a result all allocated(j)'s are 0 too. When I try to force net_cashflow(1) = -100000 (as it should be in the optimal solution) with this constraint:
net_cashflow(1) = -100000;
I get the following error:
[Error Code: 72]
Unable to solve for fixed variable:
NET_CASHFLOW( T0)
in constraint:
10
Loosening the variable's bounds may help.
The error occurred on or near the following line:
31]net_cashflow(1) = -100000;
But even if I replace this constraint by very loose bounds (like net_cashflow(1) <= -1), LINGO tells me there is no feasible solution, as if 0 is the only feasible value for all net_cashflows. I've looked at this problem for over an hour now and still don't understand what's going on. Can anyone explain this?
(Since no one has responded and I have finally found the solution, it may be useful for future readers to post the answer myself:)
Apparently LINGO variables are non-negative by default. This issue was resolved by adding the constraint (or rather relaxation):