-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnlc_example1.m
69 lines (51 loc) · 1.03 KB
/
snlc_example1.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
%snlc_example1 snlc example problem
%
% min sum(x.^3) + d'*x
% s/t sum(x) == 1
% -1 <= x <= 1
%
function snlc_example1
% settings
n = 10;
rng_seed = 210;
% seed the rng
RandStream.setDefaultStream(RandStream('mt19937ar','seed',rng_seed));
% generate d
d = randn(n,1);
% generate problem data
A = ones(1,n);
cl = 1;
cu = 1;
bl = -ones(n,1);
bu = ones(n,1);
usrfun = @(x) snlc_example_func(x,d);
% starting point
x0 = zeros(n,1);
% get problem structure
prob = snlc_solve();
% set problem structure
prob.A = A;
prob.cl = cl;
prob.cu = cu;
prob.x0 = x0;
prob.bl = bl;
prob.bu = bu;
prob.usrfun = usrfun;
% more options
prob.spc_save = 1;
% set up spec file
% call solver
out = snlc_solve(prob);
% simple output
fprintf('SNOPT returned exit code: %d\n',out.info);
% clean up
if out.info == 1
snlc_clean(prob);
end
end
function [f g] = snlc_example_func(x,d)
d = d(:);
x = x(:);
f = sum(x.^3) + d'*x;
g = 3*(x.^2) + d;
end