adding linear interpolation to a fitness equation

So I'm creating a dynamic state-variable model and I want to add in a forgetting rate for an informational state, however that'll make the values non-integers and thus I need to do linear interpolation. How do I add that in to my fitness equation?
Sorry for the overwhelming amount of code
%constants
m1=0.9;
m2=0.3;
n1=0.3;
n2=0.9;
b=0.9;
crit=3;
cap=15;
term=20;
%arrays and zeros
opt=zeros(cap,cap, cap,term);
Fd=zeros(cap, cap, cap,term);
Fdd=zeros(cap,cap,cap,term);
Fdb=zeros(cap,cap, cap,term);
Fdbb=zeros(cap,cap,cap,term);
Ft=zeros(cap,cap,cap,term);
x=linspace(1,cap,cap)';
z=linspace(1,cap,cap)';
y=linspace(1,cap,cap)';
f=@(x,y) x-y;
M=f(z.',y);
p=zeros(size(M));
p2=zeros(size(M));
p3=zeros(size(M));
p4=zeros(size(M));
p5=zeros(size(M));
%z and y
z1=z+1; %dem went to patch 1 and found food
z1(z1>cap)=cap;
z2=z-1; %dem went to patch 1 and didn't find food
z2(z2<1)=1;
zp=(z1+1); %dem went to patch 1 and found food; obs went to patch 1 and found food
zp(zp>cap)=cap;
zpp=(z1-1); %dem went to patch1 and found food; obs went to patch 1 and didn't find food
zpp(zpp<1)=1;
zd=(z2+1); %dem went to patch1 and didn't find food; obs went to patch 1 and found food
zd(zd>cap)=cap;
zdd=(z2-1); %dem went to patch 1 and didn't find food; obs went to patch 1 and didn't find food
zdd(zdd<1)=1;
zg=(z+1); %obs went to patch 1 and found food
zg(zg>cap)=cap;
zgg=(z-1); %obs went to patch 1 and didn't find food
zgg(zgg<1)=1;
y1=y+1; %dem went to patch 2 and found food
y1(y1>cap)=cap;
y2=y-1; %dem went to patch 2 and didn't find food
y2(y2<1)=1;
yp=(y1+1); %dem went to patch 2 and found food; obs went to patch 2 and found food
yp(yp>cap)=cap;
ypp=(y1-1); %dem went to patch 2 and found food; obs went to patch 2 and didn't food
ypp(ypp<1)=1;
yd=(y2+1); %dem went to patch 2 and didn't find food; obs went to patch 2 and found food
yd(yd>cap)=cap;
ydd=(y2-1); %dem went to patch 2 and didn't find food; obs went to patch 2 and didn't find food
ydd(ydd<1)=1;
yg=(y+1); %obs went to patch 2 and found food
yg(yg>cap)=cap;
ygg=(y-1); %obs went to patch 2 and didn't find food
ygg(ygg<1)=1;
%physical states
xp=x-1-1+3; %obs watched dem and found food
xp(xp>cap)=cap;
xpp=x-1-1; %obs watched dem and didn't find food
xpp(xpp<1)=1;
xd=x-1+3; %obs didn't watch and found food
xd(xd>cap)=cap;
xdd=x-1; %obs didn't watch and didn't find food
xdd(xdd<1)=1;
%specify fitness
Fd(x<=crit,:, :, :)=0;
Fd(x>crit,:,:,:)=1;
Fdb(x<=crit,:,:,:)=0;
Fdb(x>crit,:,:,:)=1;
%specify prob matrix of going to patch 1 for obs
for j=1:15
for k=1:15
if M(j,z1(k))>0
p(j,z1(k))=0.9;
elseif M(j,z1(k))<0
p(j,z1(k))=0.1;
else
p(j,z1(k))=0.5;
end
if M(j,z2(k))>0
p2(j,z2(k))=0.9;
elseif M(j,z2(k))<0
p2(j,z2(k))=0.1;
else
p2(j,z2(k))=0.5;
end
if M(y1(j),k)>0
p3(y1(j),k)=0.9;
elseif M(y1(j),k)<0
p3(y1(j),k)=0.1;
else
p3(y1(j),k)=0.5;
end
if M(y2(j),k)>0
p4(y2(j),k)=0.9;
elseif M(y2(j),k)<0
p4(y2(j),k)=0.1;
else
p4(y2(j),k)=0.5;
end
if M(y(j),z(k))>0
p5(y(j),z(k))=0.9;
elseif M(y(j),z(k))<0
p5(y(j),z(k))=0.1;
else
p5(y(j),z(k))=0.5;
end
end
end
%fitness equations for watching a dem (Fdd) and not watching a dem (Fdbb)
for tt=19:-1:1
for i=1:15
for j=1:15
for k=1:15
Fdd(i,j,k,tt)= b*(...
m1*(...
p(j,z1(k))*(n1*Fd(xp(i),zp(j),y(k),tt+1) + (1-n1)*Fd(xpp(i),zpp(j),y(k),tt+1))+...
(1-p(j,z1(k)))*(n2*Fd(xp(i),z1(j),yg(k),tt+1) + (1-n2)*Fd(xpp(i),z1(j),ygg(k),tt+1)))+...
(1-m1)*(...
p2(j,z2(k))*(n1*Fd(xp(i),zd(j),y(k),tt+1) + (1-n1)*Fd(xpp(i),zdd(j),y(k),tt+1))+...
(1-p2(j,z2(k)))*(n2*Fd(xp(i),z2(j),yg(k),tt+1) + (1-n2)*Fd(xpp(i),z2(j),ygg(k),tt+1))...
))+...
(1-b)*( ...
m2*(...
p3(y1(j),k)*(n1*Fd(xp(i),zg(j),y1(k),tt+1) + (1-n1)*Fd(xpp(i) ,zgg(j),y1(k),tt+1))+ ...
(1-p3(y1(j),k))*(n2*Fd(xp(i),z(j),yp(k),tt+1) + (1-n2)*Fd(xpp(i) ,z(j),ypp(k),tt+1)))+ ...
(1-m2)*(p4(y2(j),k)*(n1*Fd(xp(i),zg(j),y2(k),tt+1) + (1-n1)*Fd(xpp(i),zgg(j),y2(k),tt+1))+...
(1-p4(y2(j),k))*(n2*Fd(xp(i),z(j),yd(k),tt+1) + (1-n2)*Fd(xpp(i),z(j),ydd(k),tt+1))));
Fdbb(i,j,k,tt)= p5(j,k)*( ...
n1*Fdb(xd(i),zg(j),y(k),tt+1) + (1-n1)*Fdb(xdd(i),zgg(j),y(k),tt+1) ...
)+...
(1-p5(j,k))*( ...
n2*Fdb(xd(i),z(j),yg(k),tt+1) + (1-n2)*Fdb(xdd(i),z(j),ygg(k),tt+1)) ...
;
%optimal decision and fitness
if Fdd(i,j,k,tt)>Fdbb(i,j,k,tt)
opt(i,j,k,tt)=1;
Ft(i,j,k,tt)=Fdd(i,j,k,tt);
elseif Fdd(i,j,k,tt)<Fdbb(i,j,k,tt)
opt(i,j,k,tt)=2;
Ft(i,j,k,tt)=Fdbb(i,j,k,tt);
elseif Fdd(i,j,k,tt)==Fdbb(i,j,k,tt)
opt(i,j,k,tt)=3;
Ft(i,j,k,tt)=Fdd(i,j,k,tt);
end
end
end
end
end
I would like to add in a forgetting rate so that it looks more like this:
l=0.95;
zp=l*(z1+1); %dem went to patch 1 and found food; obs went to patch 1 and found food
zp(zp>cap)=cap;
zpp=l*(z1-1); %dem went to patch1 and found food; obs went to patch 1 and didn't find food
zpp(zpp<1)=1;
zd=l*(z2+1); %dem went to patch1 and didn't find food; obs went to patch 1 and found food
zd(zd>cap)=cap;
zdd=l*(z2-1); %dem went to patch 1 and didn't find food; obs went to patch 1 and didn't find food
zdd(zdd<1)=1;
zg=l*(z+1); %obs went to patch 1 and found food
zg(zg>cap)=cap;
zgg=l*(z-1); %obs went to patch 1 and didn't find food
zgg(zgg<1)=1;
%this would be added to the y's as well
How do I edit the fitness equation to add in linear interpolation?
note that Fdd is a 4-D matrix, because it has a physical state (x), two infomational states (z and y), and then time (t)
most of the interpolation I see modifies 1-D matrices

4 Comments

@Kitt: So I'm creating a dynamic state-variable model and I want to add in a forgetting rate for an informational state, however that'll make the values non-integers and thus I need to do linear interpolation. How do I add that in to my fitness equation?
Hi, to be honest, I'm a bit confused. Based on the keyword 'state-variable', I assume you want to create a state-space model, which is typically described by ordinary differential equations. Is that correct?
If so, how are the 'forgetting rate' and 'fitness equation' related to the state-space model? I'm trying to understand the connection between them.
Basically, there are two patches an individual can choose to forage at and I'm trying to model an individual's fitness if they decide to watch a demonstrator forage at a patch before the individual forages at a patch.
So my model is about an individual's fitness based on a physical state, and two informational states throughout 20 timesteps.
The entire fitness equation is:
for tt=20:-1:1
for i=1:15
for j=1:15
for k=1:15
Fdd(i,j,k,tt)= b*(...
m1*(...
p(j,z1(k))*(n1*Fd(xp(i),zp(j),y(k),tt+1) + (1-n1)*Fd(xpp(i),zpp(j),y(k),tt+1))+...
(1-p(j,z1(k)))*(n2*Fd(xp(i),z1(j),yg(k),tt+1) + (1-n2)*Fd(xpp(i),z1(j),ygg(k),tt+1)))+...
(1-m1)*(...
p2(j,z2(k))*(n1*Fd(xp(i),zd(j),y(k),tt+1) + (1-n1)*Fd(xpp(i),zdd(j),y(k),tt+1))+...
(1-p2(j,z2(k)))*(n2*Fd(xp(i),z2(j),yg(k),tt+1) + (1-n2)*Fd(xpp(i),z2(j),ygg(k),tt+1))...
))+...
(1-b)*( ...
m2*(...
p3(y1(j),k)*(n1*Fd(xp(i),zg(j),y1(k),tt+1) + (1-n1)*Fd(xpp(i) ,zgg(j),y1(k),tt+1))+ ...
(1-p3(y1(j),k))*(n2*Fd(xp(i),z(j),yp(k),tt+1) + (1-n2)*Fd(xpp(i) ,z(j),ypp(k),tt+1)))+ ...
(1-m2)*(p4(y2(j),k)*(n1*Fd(xp(i),zg(j),y2(k),tt+1) + (1-n1)*Fd(xpp(i),zgg(j),y2(k),tt+1))+...
(1-p4(y2(j),k))*(n2*Fd(xp(i),z(j),yd(k),tt+1) + (1-n2)*Fd(xpp(i),z(j),ydd(k),tt+1))));
With Fdd being the fitness equation if an individual watches a demonstrator forage and Fd being a matrix of fitness that's based on the physical state, which is the first dimension. When x<= 3 Fd is 0 and when x>3 Fd is 1.
cap=15;
term=20;
Fd=zeros(cap, cap, cap,term);
Fd(x<=crit,:, :, :)=0;
Fd(x>crit,:,:,:)=1;
That matrix is then put into the fitness equation Fdd that accounts for all possible combinations of a demonstrator foraging between two patches and the observer foraging between two patches.
The patch an individual forages at is based on their information on patch 1, state z, - their information on patch 2, state y. They then either find food or they don't find food which changes their physical state, state x.
I'm trying to add in a forgetting rate, which will change the information states z and y to non-integers (I'll be multiplying them by 0.95, which I'm modeling after something I've seen in a similar paper).
I'm really sorry if this is more confusing than helping. I'm new to matlab and I'm trying to make this model after learning about stochastic dynamic modeling in a class. I'm kind of frankenstiening this model off of a model that was used in my class. It's totally possible I'm using terms incorrectly.
Didn't realize at the beginning of this that adding in a seemingly simple choice of watching a demonstrator would lead to a very complicated model.
No worries, and there's no need to apologize. It was actually my mistake to confuse the dynamic state-variable model with a state-space representation. Thank you for clarifying that for me.
However, if you could revise/update your Question/Problem to include some mathematical equations for the fitness, demonstrator forage, and informational states, it would allow others to verify your code against the provided mathematical equations.
Alright I revised it to inlude the whole code so that it runs. I feel bad including it all, since that's kind of one of the rules not to do, but there's just a lot of moving parts! hopefully this makes it more clear what it is I'm trying to do.

Sign in to comment.

 Accepted Answer

Hi Kitt,
I understand that you want to add linear interpolation to a fitness equation in MATLAB. Here's how you can do it using interp1 for linear interpolation within your fitness function.
Assume you have a set of data points and you want to interpolate these points within your fitness function. Here’s a concise example:
Example Data Points
x = [1, 2, 4, 5];
y = [1, 4, 2, 3];
Fitness Function with Linear Interpolation
function fitness = myFitnessFunction(xq)
% Given data points for interpolation
x = [1, 2, 4, 5];
y = [1, 4, 2, 3];
% Linear interpolation
yq = interp1(x, y, xq, 'linear');
% Define your fitness equation using interpolated values
fitness = sum(yq.^2); % Example fitness equation: sum of squares of interpolated values
end
Usage
% Query points
xq = [1.5, 3, 4.5];
% Calculate fitness
fitness_value = myFitnessFunction(xq);
disp(fitness_value);
Replace the fitness equation (sum(yq.^2)) with your actual fitness equation as needed. This example demonstrates how to use interp1 for linear interpolation within a custom fitness function.
Hope this helps.
Regards,
Nipun

More Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Asked:

on 7 May 2024

Answered:

on 30 May 2024

Community Treasure Hunt

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

Start Hunting!