2 # -*- coding:latin-1 -*-
11 import cvxopt
.base
as cb
14 logging
.basicConfig(level
=logging
.DEBUG
,format
='%(levelname)s %(message)s')
18 size_downstream
= t
[1]
19 assert size_upstream
>= 1 and size_upstream
<= 39
20 assert size_downstream
>= 1 and size_downstream
<= 39
22 if size_downstream
> size_upstream
:
23 asym_small
= size_upstream
24 asym_big
= size_downstream
26 asym_small
= size_downstream
27 asym_big
= size_upstream
30 for r
in range(asym_small
):
31 asym_offset
+= (40-r
)-r
;
33 return (asym_offset
+asym_big
)-1
36 return filter(lambda t
: t
[0]<=t
[1] and t
[0]>0 and t
[0]<40 and t
[1]>0 and t
[1]<40,[(i
+1,j
),(i
-1,j
),(i
,j
+1),(i
,j
-1)])
40 This class is a wrapper for the cvxopt qp solver.
41 It has the purpose to support an iterative addition of
42 constraints to the original problem.
44 We want to solve a problem of the form
46 min 1/2 * C * x' * P * x + q' * x
50 < w,f+ > - < w,f- > >= 1 - xi_i for all i, for all f-
52 where x is a vector storing information on the parameters w_i and the slacks xi_i
57 def __init__(self
,fSize
,numExamples
,c
):
58 assert fSize
> 0, 'You have to have at least one feature!'
59 assert numExamples
> 0, 'You have to have at least one example!'
60 self
.numFeatures
= fSize
61 self
.numExamples
= numExamples
65 logging
.debug("Starting SIQP solver with %d examples. A feature space dim. of %d.", numExamples
,fSize
)
66 logging
.debug("Regularization parameters are C=%f."%(self
.C
))
70 self
.dimP
= self
.numFeatures
+ self
.numExamples
71 self
.createRegularizer()
73 def createUnitRegularizer(self
):
74 # set regularizer to zero
75 self
.P
= cb
.matrix(0.0,(self
.dimP
,self
.dimP
))
76 for i
in range(self
.numFeatures
):
78 # end of zeroing regularizer
80 def createRegularizer(self
):
81 self
.createUnitRegularizer()
83 q_array
= [0.0]*self
.numFeatures
+ [1.0]*self
.numExamples
84 self
.q
= cb
.matrix(q_array
,(self
.numFeatures
+self
.numExamples
,1),'d')
85 self
.q
= self
.C
* self
.q
87 self
.G
= cb
.matrix(0.0,(self
.numExamples
,self
.numFeatures
+self
.numExamples
))
88 for i
in range(self
.numExamples
):
89 self
.G
[i
,self
.numFeatures
+i
] = -1
91 self
.h
= cb
.matrix(0,(self
.numExamples
,1),'d')
93 if __name__
== '__main__':
97 function [C_qp_penalties,C_qp_smoothness,C_lp_smoothness,C_qp_smallness,column_eps,Q,f,LB,UB] = paths_create_qp(N,C)
98 % function [C_qp_penalties,C_qp_smoothness,C_lp_smoothness,C_qp_smallness,column_eps,Q,f,LB,UB] = paths_create_qp(N,C)
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 % qp_solve: min ( <res, f'> + 1/2 res' Q res)
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 C_qp_penalties = C ; %was 0.01
104 C_qp_smoothness = C ; %was 0.01
105 C_lp_smoothness = 0.00 ; %linear problem parameters
106 C_qp_smallness = 1e-6 ; %was 1e-6
109 Q=zeros(2*126+N) ; % 1/2 * res' * Q * res
110 Q(1:90,1:90) = C_qp_smallness*diag(ones(1,90)) ;
111 Q(91:126,91:126) = C_qp_penalties*diag(ones(1,36)) ;
112 Q(127:2*126,127:2*126) = C_qp_smoothness*diag(ones(1,126)) ;
113 f = [zeros(1,126) C_lp_smoothness*ones(1,126) ones(1,N)/N] ; % <res, f'>
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % qp_solve: LB <= res <= UB
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 LB = [-INF*ones(1,126) zeros(1,126) zeros(1,N)] ; % lower bound for res
122 UB = [ INF*ones(1,2*126) INF*ones(1,N)] ; % upper bound for res