Skip to content

Commit

Permalink
Added a test problem
Browse files Browse the repository at this point in the history
  • Loading branch information
noblec04 committed Aug 7, 2024
1 parent 89318e7 commit ae11845
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 4 deletions.
1 change: 0 additions & 1 deletion MatlabGP/+BO/argmax.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

[x,R] = fmincon(@(x) FF(Z,x),x0,[],[],[],[],lb,ub,[],opts);


%[x,R] = VSGD(@(x) FF(Z,x),x0,'lr',0.03,'lb',lb,'ub',ub,'gamma',0.01,'iters',100,'tol',1*10^(-3));

R = -1*R;
Expand Down
27 changes: 27 additions & 0 deletions MatlabGP/+kernels/Matern12.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
classdef Matern12<kernels.Kernel

properties
theta
end

methods

function obj = Matern12(scale,theta)
obj.scale = scale;
obj.scales{1} = scale;
obj.theta = theta;
obj.thetas{1} = theta;
obj.kernels{1} = obj;
obj.w.map = 'none';
obj.warping{1} = obj.w;
end

function [K] = forward_(obj,x1,x2,theta)

d = obj.dist(x1./theta,x2./theta);

K = exp(-d);
end

end
end
29 changes: 29 additions & 0 deletions MatlabGP/+kernels/Matern32.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
classdef Matern32<kernels.Kernel

properties
theta
end

methods

function obj = Matern32(scale,theta)
obj.scale = scale;
obj.scales{1} = scale;
obj.theta = theta;
obj.thetas{1} = theta;
obj.kernels{1} = obj;
obj.w.map = 'none';
obj.warping{1} = obj.w;
end

function [K] = forward_(obj,x1,x2,theta)

d = obj.dist(x1./theta,x2./theta);

K = (1 + sqrt(3)*d);

K = K.*exp(-sqrt(3)*d);
end

end
end
6 changes: 3 additions & 3 deletions MatlabGP/GP.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
res = obj.Y - obj.mean.eval(obj.X);
end

kkp = pinv(obj.K,0);
kkp = pinv(obj.K,1*10^(-4));

sigp = sqrt(abs(res'*kkp*res./(size(obj.Y,1))));

Expand All @@ -165,7 +165,7 @@

obj.K = obj.K + diag(0*xx(:,1)+obj.kernel.signn);

obj.Kinv = pinv(obj.K,0);
obj.Kinv = pinv(obj.K,1*10^(-4));

obj.alpha = obj.Kinv*(res);

Expand Down Expand Up @@ -329,7 +329,7 @@

obj.mean = obj.mean.setHPs(theta(1:ntm));
obj.kernel = obj.kernel.setHPs(theta(ntm+1:end));
obj = obj.condition(obj.X,obj.Y);
obj = obj.condition(obj.X,obj.Y,obj.lb_x,obj.ub_x);

end

Expand Down
16 changes: 16 additions & 0 deletions MatlabGP/StressedPlate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function [y] = StressedPlate(x,i)

T = 25000;
E = 210*10^9;
nu = 0.3;
rho = 7800;

switch i
case 1
y = sqrt(1./(rho*x(:,3))).*sqrt(((E*x(:,3).^3)./(12*(1-nu))).*((pi^2)./x(:,1).^2 + (pi^2)./x(:,2).^2).^2 + T*((pi^2)./x(:,1).^2 + (pi^2)./x(:,2).^2)) - 120;
case 2
y = sqrt(1./(rho*x(:,3))).*sqrt(((E*x(:,3).^3)./(12*(1-nu))).*((pi^2)./x(:,1).^2 + (pi^2)./x(:,2).^2).^2) - 120;
end


end
110 changes: 110 additions & 0 deletions MatlabGP/TestAppliedAviationProblem.asv
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

clear all
clc

lb = [0.5 0.5 2.5*10^(-3)];
ub = [1.5 1.5 7.5*10^(-3)];

xx = lb + (ub - lb).*lhsdesign(50000,3);
yy = StressedPlate(xx,1);

x1 = lb + (ub - lb).*lhsdesign(5,3);
y1 = StressedPlate(x1,1);

x2 = [lb + (ub - lb).*lhsdesign(100,3)];
y2 = StressedPlate(x2,2);

x{1} = x1;
x{2} = x2;

y{1} = y1;
y{2} = y2;

%%
ma = means.linear([1 1 1 1]);
mb = means.linear([1 1 1]);

a = kernels.Matern52(1,[0.1 0.2 0.1 0.3]);%.periodic(1,10);
b = kernels.EQ(1,[0.2 0.2 0.2]);
a.signn = 0;
b.signn = 0;

%%
tic
for i = 1:2
Z{i} = GP(mb,b);
Z{i} = Z{i}.condition(x{i},y{i},lb,ub);
Z{i} = Z{i}.train();
end
toc

%%
tic
MF = NLMFGP(Z,ma,a);
MF = MF.condition();
MF = MF.train();
toc


%%
figure
hold on
utils.plotLineOut(Z{1},(lb+ub)/2,1,'color','r')
utils.plotLineOut(Z{2},(lb+ub)/2,1,'color','b')

%%

figure
hold on
utils.plotLineOut(MF,(lb+ub)/2,1)

%%

1 - mean((yy - Z{1}.eval_mu(xx)).^2)./var(yy)
max(abs(yy - Z{1}.eval_mu(xx)))./std(yy)

1 - mean((yy - MF.eval_mu(xx)).^2)./var(yy)
max(abs(yy - MF.eval_mu(xx)))./std(yy)

%%
for jj = 1:40

[xn,Rn] = BO.argmax(@BO.maxVAR,MF);

x{1} = [x{1}; xn];
x{2} = [x{2}; xn];

y{1} = [y{1}; StressedPlate(xn,1)];
y{2} = [y{2}; StressedPlate(xn,2)];

for ii = 1:2
%Z{ii} = GP(mb,b);
Z{i} = Z{i}.condition(x{i},y{i},lb,ub);
%Z{ii} = Z{ii}.train();
end

MF.GPs = Z;
MF = MF.condition();
MF = MF.train();

R2z(jj) = 1 - mean((yy - Z{1}.eval_mu(xx)).^2)./var(yy);
RMAEz(jj) = max(abs(yy - Z{1}.eval_mu(xx)))./std(yy);

R2MF(jj) = 1 - mean((yy - MF.eval_mu(xx)).^2)./var(yy);
RMAEMF(jj) = max(abs(yy - MF.eval_mu(xx)))./std(yy);

figure(1)
clf(1)
hold on
plot(1:jj,R2z)
plot(1:jj,R2MF)

figure(2)
clf(2)
hold on
plot(1:jj,RMAEz)
plot(1:jj,RMAEMF)

drawnow

end
107 changes: 107 additions & 0 deletions MatlabGP/TestAppliedAviationProblem.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

clear all
clc

lb = [0.5 0.5 2.5*10^(-3)];
ub = [1.5 1.5 7.5*10^(-3)];

xx = lb + (ub - lb).*lhsdesign(50000,3);
yy = StressedPlate(xx,1);

x1 = lb + (ub - lb).*lhsdesign(5,3);
y1 = StressedPlate(x1,1);

x2 = [lb + (ub - lb).*lhsdesign(20,3)];
y2 = StressedPlate(x2,2);

x{1} = x1;
x{2} = x2;

y{1} = y1;
y{2} = y2;

%%
ma = means.linear([1 1 1 1]);
mb = means.linear([1 1 1]);

a = kernels.EQ(1,[0.1 0.2 0.1 0.3]);%.periodic(1,10);
b = kernels.EQ(1,[0.2 0.2 0.2]);
a.signn = 0;
b.signn = 0;

%%
tic
for i = 1:2
Z{i} = GP(mb,b);
Z{i} = Z{i}.condition(x{i},y{i},lb,ub);
Z{i} = Z{i}.train();
end
toc

%%
tic
MF = NLMFGP(Z,ma,a);
MF = MF.condition();
MF = MF.train();
toc


%%
figure
hold on
utils.plotLineOut(Z{1},(lb+ub)/2,1,'color','r')
utils.plotLineOut(Z{2},(lb+ub)/2,1,'color','b')

%%

figure
hold on
utils.plotLineOut(MF,(lb+ub)/2,1)

%%

1 - mean((yy - Z{1}.eval_mu(xx)).^2)./var(yy)
max(abs(yy - Z{1}.eval_mu(xx)))./std(yy)

1 - mean((yy - MF.eval_mu(xx)).^2)./var(yy)
max(abs(yy - MF.eval_mu(xx)))./std(yy)

%%
for jj = 1:50

[xn,Rn] = BO.argmax(@BO.maxVAR,MF);

x{1} = [x{1}; xn];
x{2} = [x{2}; xn];

y{1} = [y{1}; StressedPlate(xn,1)];
y{2} = [y{2}; StressedPlate(xn,2)];

for ii = 1:2
Z{ii} = Z{ii}.condition(x{ii},y{ii},lb,ub);
end

MF.GPs = Z;
MF = MF.condition();

R2z(jj) = 1 - mean((yy - Z{1}.eval_mu(xx)).^2)./var(yy);
RMAEz(jj) = max(abs(yy - Z{1}.eval_mu(xx)))./std(yy);

R2MF(jj) = 1 - mean((yy - MF.eval_mu(xx)).^2)./var(yy);
RMAEMF(jj) = max(abs(yy - MF.eval_mu(xx)))./std(yy);

figure(1)
clf(1)
hold on
plot(1:jj,R2z)
plot(1:jj,R2MF)

figure(2)
clf(2)
hold on
plot(1:jj,RMAEz)
plot(1:jj,RMAEMF)

drawnow

end

0 comments on commit ae11845

Please sign in to comment.