-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptim.txt
More file actions
50 lines (39 loc) · 951 Bytes
/
Copy pathoptim.txt
File metadata and controls
50 lines (39 loc) · 951 Bytes
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
function w = optim(A,truth,C)
%matrix size params
SizeA = size(A) ;
cl = SizeA(2) ;
r = SizeA(1) ;
lb= zeros (r, 1) ;
%objective vector
f = ones(2*cl+r,1);
f(1:r,1) = C ;
%constraint values vector
b = zeros(2*cl+r,1);
b(1:r,1) = -1 ;
%constraint equations matrix
constraints = zeros (2*cl+r, 2*cl+r) ;
%populate area of vector weightings
for i= 1:r
if (truth(i) == 1)
for j = 1:cl
%truths negative b/c solving min
constraints(i,r+cl+j) = - A(i,j) ;
end
else
for j = 1:cl
constraints(i,r+cl+j) = A(i,j) ;
end
end
end
for i = 1:r
constraints(i,i) = -1 ;
end
for i = 1:cl
constraints(r+i,r+i) = - 1 ;
constraints(r+i,cl+r+i) = 1 ;
constraints(r+cl+i,r+i) = -1 ;
constraints(r+cl+i,cl+r+i) = -1 ;
end
%call lin program function, min f'*x sub to constraints*x <= b, lb < x
x = linprog(f',constraints,b,[],[],lb) ;
w = x ;