adding linear interpolation to a fitness equation

2 ビュー (過去 30 日間)
Kitt
Kitt 2024 年 5 月 7 日
回答済み: Nipun 2024 年 5 月 30 日
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 件のコメント
Sam Chak
Sam Chak 2024 年 5 月 7 日
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.
Kitt
Kitt 2024 年 5 月 7 日
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.

サインインしてコメントする。

採用された回答

Nipun
Nipun 2024 年 5 月 30 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTraveling Salesman (TSP) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by