First of all, i'd like to come clean and say that the following question is for school so don't be too harsh on me :)
I'm having a bit of a problem modelling an optimization problem in matlab using a recursive algorithm (which is a requirement).
The problem's definition is:
Decide the quantity of fish to catch each year considering a time window of 10 years knowing there are presently 10000 fishes in the lake, year 1, the growing rate of fish is the number of fishes present in the lake at the beginning of each year + 20%.
Let x be the quantity of fish to catch, $5 the price of each fish and the cost of catching fish:
0.4x + 100 if x is <= 5000;
0.3x + 5000 if 5000 <= x <= 10000;
0.2x + 10000 if x > 10000;
decide the number of fish to catch each year, for 10 years, in order to maximize the profit.
Future gains are depreciated by a factor of 0.2/year, which means that earning $1 in year 1 is the same as $0.8 in year 2 and so on.
I currently have defined the following objective function:
x -> quantity of fish to catch
b-> quantity of fish availavable in the beginning of year i
c(x,b) -> cost of catching x fish with b fishes available
f_i(b) = max {(5x - c(x,b)) + 0.8 * f_i+1((b - x) * 1.2)}
How would i go about implementing this in matlab?
This is what i have so far:
Main file
clear;
global M Kdep Cost RecursiveProfit ValorF prop
Kdep=[10; 20; 30; 40; 50; 60; 70; 80; 90; 100]; %max nr of fish in the lake at the beginning of each year, 10 years, in thousands. Growth factor = 20%
M=1000;
%Cost and Profit of selling i fishes given that there are j at the beginning of the year
for i = 1:50
for j = 1:11
Cost(i,j) = 0.2 * i + 10;
RecursiveProfit(i,j) = 5 * i - Cost(i, j);
end
end
for i = 1:10
for j = 1:10
Cost(i,j) = 0.3 * i + 5;
RecursiveProfit(i,j) = 5 * i - Cost(i, j);
end
end
for i = 1:5
for j = 1:5
Cost(i,j) = 0.4 * i + 0.1;
RecursiveProfit(i,j) = 5 * i - Cost(i, j);
end
end
%prop = 1 : 10;
ValorF = -M * ones(10, 50);
for a = 1:5
ValorF(10, a) = 5 * a - (0.4 * a + 1); %On Year 10, if there are <= a thousand fishes in the lake ...
prop(10, a) = a;
end
for b = 6:10
ValorF(10, b) = 5 * b - (0.3 * b + 5); %On Year 10, if there are 6 <= a <= 10 thousand fishes in the lake ...
prop(10, b) = b;
end
for c = 10:41
ValorF(10, c) = 5 * c - (0.2 * c + 10);
prop(10, c) = c;
end
MaxProfit = RecursiveProfit(1, 10)
k1 = prop(1,10)
kant=k1;
y = 6 - Cost(kant,10);
for q=2:10
if(kant == 0)
kant = kant + 1;
end
kq=prop(q,y)
kant=kq;
y = y - Cost(kant,q);
end %for i
Function
function y=RecursiveProfit(j,x)
global M Kdep Cost Prof ValorF prop
y=ValorF(j,x);
if y~= -M
return
end %if
auxMax=-M;
decision=0;
for k=1:Kdep(j)
if Prof(k,j) <= x-k
aux=Prof(k,j)+RecursiveProfit(j+1, (x - k));
if auxMax < aux
auxMax=aux;
decision=k;
end %if aux
else break
end %if Cost
end %for k
ValorF(j,x)=auxMax;
prop(j,x)=decision;
y=auxMax;
This only computes for the case where the year is 10 and b = 10 (value in thousands). This is the same poblem describes as "Discounted Profits Problem" in book
Any help you can give me will be greatly appreciated.
EDIT 1: I'm really stuck here guys. If you could help me implement this in say Java i would try and port it to Matlab.
EDIT 2: I edited the code to the most recent version. Now i'm getting
"Maximum recursion limit of 500 reached."
Can you help me?
EDIT 3: I managed to get it working but it only returns 0.
EDIT 4: Code updated. Now i'm getting
Attempted to access prop(2,0); index must be a positive integer or logical.
Error in Main (line 66) kq=prop(q,y)
pesca_recyou keep callingpesca_rec(Year+1,...)that doesn't seem to terminate - probably will give you errors.pesca_recyou keep callingpesca_rec(Year+1,...)and both will write to MaxProfit - so I wonder if it contains the value you intend it to have.dbstop if errorthe error that you get for edit4 is trivial if you do this and inspect the relevant variables.