我正在监督有关LINGO和LP的练习,今天有一个关于投资的练习。这是预期的解决方案:
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
一个学生试图用一个变量代替每年的净现金流量,以消除某些重复的代码。但是,只需在sets-节中将变量net_cashflow添加到Year的派生集合中,并在主体中添加以下约束即可:
@FOR(year(i): net_cashflow(i) = @SUM(investment(j): cash_flow(j, i)*allocated(j)));
LINGO的解决方案现在是错误的。所有net_cashflow(i)都设置为0,结果所有分配的j都为0。当我尝试使用此约束强制net_cashflow(1)= -100000(因为它应该在最佳解决方案中)时:
net_cashflow(1) = -100000;
我收到以下错误:
[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;
但是,即使我用非常宽松的边界(例如net_cashflow(1)<= -1)替换了此约束,LINGO仍告诉我没有可行的解决方案,好像0是所有net_cashflows的唯一可行值。我已经研究了这个问题一个多小时,但仍然不知道发生了什么。谁能解释一下?
答案 0 :(得分:0)
(由于没有人回答,我终于找到了解决方案,因此对以后的读者自己发布答案可能很有用:)
显然,默认情况下,LINGO变量为非负数。通过添加约束(或更确切地说是放宽)解决了该问题:
@FOR(year: @FREE(net_cashflow));