Skip to content

Commit

Permalink
added 2D GP example
Browse files Browse the repository at this point in the history
  • Loading branch information
noblec04 committed Jul 10, 2024
1 parent a8e2bc9 commit 6b26047
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion MatlabGP/+BO/EImin.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function [alpha, dalpha] = EI(Z,x)
function [alpha, dalpha] = EImin(Z,x)

ys = min(Z.Y);

Expand Down
2 changes: 1 addition & 1 deletion MatlabGP/+BO/argmax.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
lb = Z.lb_x;
ub = Z.ub_x;

x0 = lb + (ub-lb).*rand(length(lb));
x0 = lb + (ub-lb).*rand(1,length(lb));

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

Expand Down
6 changes: 3 additions & 3 deletions MatlabGP/+BO/argmin.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
for i = 1
x0 = lb + (ub-lb).*rand(1,length(lb));

opts = optimoptions('fmincon','SpecifyObjectiveGradient',true,'Display','none');
%opts = optimoptions('fmincon','SpecifyObjectiveGradient',true,'Display','none');

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

%[x{i},R(i),xv{i},fv{i}] = VSGD(@(x) FF(Z,x),x0,'lr',0.05,'lb',lb,'ub',ub,'gamma',1*10^(-2),'iters',100,'tol',1*10^(-6));
[x{i},R(i),xv{i},fv{i}] = VSGD(@(x) FF(Z,x),x0,'lr',0.05,'lb',lb,'ub',ub,'gamma',1*10^(-2),'iters',100,'tol',1*10^(-6));
end

[~,ii] = min(R);
Expand Down
16 changes: 8 additions & 8 deletions MatlabGP/+BO/opt.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function [x,R,Q,Z] = opt(f,FF,lb,ub)

x0 = lb + (ub - lb).*lhsdesign(20,length(lb));
x0 = lb + (ub - lb).*lhsdesign(3,length(lb));
y0 = f(x0);

a = means.linear(ones(1,length(lb)))*means.linear(ones(1,length(lb))) + means.linear(ones(1,length(lb))) + means.const(1);

b = kernels.RQ(1,1,0.2*ones(1,length(lb)));
b.signn = 0.0001;
b = kernels.Matern52(1,0.2*ones(1,length(lb)));
b.signn = 0;

Z = GP(a,b);
Z = Z.condition(x0,y0,lb,ub);
Expand All @@ -25,14 +25,14 @@
x0 = [x0;x];
y0 = [y0;y];

%Z = Z.condition(x0,y0,lb,ub);
Z = Z.resolve(x,y);
Z = Z.condition(x0,y0,lb,ub);
%Z = Z.resolve(x,y);
Z.lb_x = lb;
Z.ub_x = ub;

% if i<10
% Z = Z.train();
% end
if i==15
Z = Z.train();
end

[Q(i)] = min(Z.Y);

Expand Down
12 changes: 8 additions & 4 deletions MatlabGP/GP.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
dksf = obj.kernel.grad(xs,xx);
dm = obj.mean.grad(xs);

dy = dm + (dksf*obj.alpha)';
dy = dm + (dksf'*obj.alpha)';
end

end
Expand All @@ -88,7 +88,7 @@

if nargout>1
dksf = obj.kernel.grad(xs,xx);
dsig = -1*(-2*(ksf*obj.Kinv*dksf'));
dsig = -1*(-2*(ksf*obj.Kinv*dksf));
end

end
Expand Down Expand Up @@ -206,6 +206,7 @@
if regress
dnLL(end+1) = -1*sum(sum(2*sqrt(obj.kernel.signn)*(obj.alpha*obj.alpha' - obj.Kinv)));
end

end

end
Expand Down Expand Up @@ -268,11 +269,14 @@
%
% theta = sum(xxt.*LL')/sum(LL);

for i = 1:5
for i = 1:2
tx0 = tlb + (tub - tlb).*rand(1,length(tlb));

opts = optimoptions('fmincon','SpecifyObjectiveGradient',true,'Display','none');

[theta{i},val(i)] = fmincon(func,tx0,[],[],[],[],tlb,tub,[],opts);
%[theta{i},val(i)] = bads(func,tx0,tlb,tub);
[theta{i},val(i)] = VSGD(func,tx0,'lr',0.02,'lb',tlb,'ub',tub,'gamma',0.0001,'iters',400,'tol',1*10^(-4));
%[theta{i},val(i)] = VSGD(func,tx0,'lr',0.02,'lb',tlb,'ub',tub,'gamma',0.0001,'iters',400,'tol',1*10^(-4));

end

Expand Down
Binary file added MatlabGP/docs/TestBOClass.mlx
Binary file not shown.
Binary file modified MatlabGP/docs/TestGPClass.mlx
Binary file not shown.
Binary file added MatlabGP/docs/TestGPClass_2D.mlx
Binary file not shown.
10 changes: 5 additions & 5 deletions MatlabGP/docs/testNN.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
close all
clc

xx = [0;lhsdesign(8,1);1];
xx = [0;lhsdesign(10,1);1];
yy = forr(xx,0);

xmesh = linspace(0,1,100)';
ymesh = forr(xmesh,0);

layers{1} = NN.FF(1,20);
layers{2} = NN.FF(20,10);
layers{1} = NN.FF(1,5);
layers{2} = NN.FF(5,10);
layers{3} = NN.FF(10,3);

acts{1} = NN.SWISH(1);
acts{2} = NN.LIN();
acts{1} = NN.SNAKE(1);
acts{2} = NN.SWISH(1);

lss = NN.MSE();

Expand Down
2 changes: 0 additions & 2 deletions MatlabGP/warpGP.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,5 @@

end



end
end

0 comments on commit 6b26047

Please sign in to comment.