Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
%% Simple ball approximation
x = sdpvar(2,1);
ballApproximation = [];
for i = 1:10
v = randn(2,1);v = v/norm(v);
ballApproximation = [ballApproximation, v'*x <= 1];
end
clf
plot(ballApproximation,x,'blue');hold on
plot(x'*x <= 1,x,'red');
%% Animated improvements
x = sdpvar(2,1);
ops = sdpsettings('plot.shade',.1,'verbose',0);
ballApproximation = [];
clf;hold on
for k = 1:10
for i = 1:10
vi = randn(2,1);vi = vi/norm(vi);
ballApproximation = [ballApproximation, vi'*x <= 1];
end
plot(ballApproximation,x,'blue',200,ops);
optimize(ballApproximation,-sum(x),ops);
plot(value(x(1)),value(x(2)),'k*');drawnow
end
%% Same with optimizer
x = sdpvar(2,1);
v = sdpvar(2,1);
ops = sdpsettings('plot.shade',.1,'verbose',0,'solver','mosek');
ballApproximation = [];
paramModel = optimizer(v'*x <= 1,-sum(x),ops,v,x);
clf;hold on
for k = 1:10
for i = 1:10
vi = randn(2,1);vi = vi/norm(vi);
ballApproximation = [ballApproximation, paramModel(vi,'nosolve')];
end
plot(ballApproximation,x,'blue',200,ops);
xopt = ballApproximation();
plot(xopt(1),xopt(2),'k*');
drawnow
end
%% Optimizer with sampler attached
Model = [v'*x <= 1, uncertain(v,'unif',[-1;-1],[1;1])];
OneHyperPlaneModel = optimizer(Model, -sum(x),ops,v,x);
%% Intersect a bunch of random models
model = sample(OneHyperPlaneModel);
for i = 1:10
model = [model, sample(OneHyperPlaneModel)];
end
%% Alternative
model = sample(OneHyperPlaneModel,10);
%% Approximate L1-ball by samples
x = sdpvar(2,1);
v = sdpvar(2,1);
ops = sdpsettings('plot.shade',.1,'verbose',0,'solver','mosek');
rombApproximation = [];
Model = [v'*x <= 1, uncertain(v,'unif',-1,1)];
Model = optimizer(Model,-sum(x),ops,v,x);
clf;hold on
for k = 1:10
rombApproximation = [rombApproximation, sample(Model,10)];
plot(rombApproximation,x,'blue',200,ops);
xopt = rombApproximation();
plot(xopt(1),xopt(2),'k*');drawnow
end
Model = [v'*x <= 1, uncertain(-1 <= v <= 1)];
plot(Model,x,'red');
%% L1-ball again with non-redundant samples
x = sdpvar(2,1);
v = sdpvar(2,1);
ops = sdpsettings('plot.shade',.1,'verbose',0,'solver','mosek');
ballApproximation = [];
f = @(x)randn(2,1);g = @(x)(x/norm(x));
ballSampler = @(x)g(f());
Model = [v'*x <= 1, uncertain(v,ballSampler)];
Model = optimizer(Model,-sum(x),ops,v,x);
clf;hold on
for k = 1:10
ballApproximation = [ballApproximation, sample(Model,10)];
plot(ballApproximation,x,'blue',200,ops);
xopt = ballApproximation();
plot(xopt(1),xopt(2),'k*');drawnow
end
%% Weird set
x = sdpvar(2,1);
v = sdpvar(2,1);
ops = sdpsettings('plot.shade',.1,'verbose',0,'solver','mosek');
f = @(x)randn(2,1);g = @(x)(sin(x)/(1 + abs(sum(x))/5));
weirdSampler = @(x)g(f());
Model = [v'*x <= 1 + sum(v.^4-v.^3), uncertain(v,weirdSampler)];
Model = optimizer(Model,-sum(x),ops,v,x);
clf;hold on
setApproximation = [];
for k = 1:20
setApproximation = [setApproximation, sample(Model,10)];
plot(setApproximation,x,'blue',200,ops);
xopt = setApproximation();
plot(xopt(1),xopt(2),'k*');drawnow
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment