-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinitguess.m
74 lines (67 loc) · 1.65 KB
/
initguess.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
70
71
72
73
function [ x ] = initguess( model,A,b,xlb,xub )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
% computing total number of variables
ne = cell2mat(model.ensemble.ne);
nei = cell2mat(model.ensemble.nei);
nvr = cell2mat(model.ensemble.nvr);
nf = ne-1;
np = nf+nei+nvr;
nvar = sum(np);
%{
%constructing constraint matrices
A = zeros(length(ne),nvar);
b1 = ones(length(ne),1);
b = b1*1e-7;
pos = 0;
inds = zeros(1,nvar);
vrind = inds;
for i = 1:length(ne)
A(i,pos+1:pos+nf(i)+nei(i)) = 1;
inds(1,pos+1:pos+nf(i)+nei(i)) = 1;
vrind(1,pos+nf(i)+nei(i)+1:pos+np(i)) = model.d.flx(i);
pos = pos+np(i);
end
inds = logical(inds);
A1 = eye(nvar);
A1(inds,:) = [];
vrind(inds) = [];
vrind = vrind';
A = [A;-A;-A1];
b = [b1;-b;vrind];
%}
pos = 0;
inds = zeros(1,nvar);
vrind = inds;
for i = 1:length(ne)
inds(1,pos+1:pos+nf(i)+nei(i)) = 1;
vrind(1,pos+nf(i)+nei(i)+1:pos+np(i)) = model.d.flx{1}(i);
pos = pos+np(i);
end
x = rand(nvar,1);
x(~inds) = 100*x(~inds);
%{
xlb = 1e-7*ones(size(x));
xub = ones(size(xlb));
xub(~inds) = 100000000;
%}
%f = @(x) dgerr(x,model);
%x = fmincon(f,x,A,b,[],[],1e-7*ones(size(x)),ones(size(x)),[],optimset('Display','iter','MaxFunEvals',10000000));
x = fmincon(@(x) 1,x,A,b,[],[],xlb,xub,[],optimset('Display','iter','MaxFunEvals',10000000));
end
function r = dgerr(x,model)
ne = cell2mat(model.ensemble.ne);
nei = cell2mat(model.ensemble.nei);
nrev = cell2mat(model.ensemble.nrev);
nf = ne-1;
np = nf+nei+nrev;
r = zeros(size(ne));
nx = 0;
for i = 1:length(r)
nx = nx+np(i);
r(i) = prod(x(nx-nrev(i)+1:nx));
end
r = r-model.dG;
r = r(~model.p.exch)./model.dgsdv(~model.p.exch);
r = r'*r;
end