2 # -*- coding:latin-1 -*-
11 import cvxopt
.base
as cb
14 logging
.basicConfig(level
=logging
.DEBUG
,format
='%(levelname)s %(message)s')
16 import qpalma
.Configuration
as Configuration
20 This class is a wrapper for the cvxopt/cplex qp solvers.
21 It has the purpose to support an iterative addition of
22 constraints to the original problem.
24 We want to solve a problem of the form
26 min 1/2 * C * x' * P * x + q' * x
30 < w,f+ > - < w,f- > >= 1 - xi_i for all i, for all f-
32 where x is a vector storing information on the parameters w_i and the slacks xi_i
37 def __init__(self
,fSize
,numExamples
,c
):
38 assert fSize
> 0, 'You have to have at least one feature!'
39 assert numExamples
> 0, 'You have to have at least one example!'
40 self
.numFeatures
= fSize
41 self
.numExamples
= numExamples
45 logging
.debug("Starting SIQP solver with %d examples. A feature space dim. of %d.", numExamples
,fSize
)
46 logging
.debug("Regularization parameters are C=%f."%(self
.C
))
50 self
.dimP
= self
.numFeatures
+ self
.numExamples
51 self
.createRegularizer()
53 def createUnitRegularizer(self
):
54 # set regularizer to zero
55 self
.P
= cb
.matrix(0.0,(self
.dimP
,self
.dimP
))
56 for i
in range(self
.numFeatures
):
58 # end of zeroing regularizer
60 def createSmoothnessRegularizer(self
):
61 # set regularizer to zero
62 self
.P
= cb
.matrix(0.0,(self
.dimP
,self
.dimP
))
63 for i
in range(self
.numFeatures
):
66 lengthSP
= Configuration
.numLengthSuppPoints
67 donSP
= Configuration
.numDonSuppPoints
68 accSP
= Configuration
.numAccSuppPoints
69 mmatrixSP
= Configuration
.sizeMatchmatrix
[0]\
70 *Configuration
.sizeMatchmatrix
[1]
71 numq
= Configuration
.numQualSuppPoints
72 totalQualSP
= Configuration
.totalQualSuppPoints
73 numQPlifs
= Configuration
.numQualPlifs
75 #lengthGroupParam = 5*1e-9
76 #spliceGroupParam = 1e-8
78 lengthGroupParam
= 5*1e-9
79 spliceGroupParam
= 1e-9
81 self
.P
[0:lengthSP
,0:lengthSP
] *= lengthGroupParam
84 end
= lengthSP
+donSP
+accSP
85 self
.P
[beg
:end
,beg
:end
] *= spliceGroupParam
87 regC
= self
.numFeatures
/ (1.0*self
.numExamples
)
92 for j
in range(lengthSP
-1):
93 self
.P
[j
,j
+1] = -cfactor
94 self
.P
[j
+1,j
] = -cfactor
95 self
.P
[j
,j
] += cfactor
96 self
.P
[j
+1,j
+1] += cfactor
101 for j
in range(lengthSP
,lengthSP
+donSP
-1):
102 cfactor
= spliceGroupParam
103 self
.P
[j
,j
+1] = -cfactor
104 self
.P
[j
+1,j
] = -cfactor
105 self
.P
[j
,j
] += cfactor
106 self
.P
[j
+1,j
+1] += cfactor
108 for j
in range(lengthSP
+donSP
,lengthSP
+donSP
+accSP
-1):
109 cfactor
= spliceGroupParam
110 self
.P
[j
,j
+1] = -cfactor
111 self
.P
[j
+1,j
] = -cfactor
112 self
.P
[j
,j
] += cfactor
113 self
.P
[j
+1,j
+1] += cfactor
115 offset
= lengthSP
+donSP
+accSP
+mmatrixSP
116 for k
in range(numQPlifs
):
117 begin
= offset
+(k
*numq
)
118 end
= offset
+((k
+1)*numq
)
120 for j
in range(begin
,end
-1):
125 # 0.25 for each was already good
127 matchGroupParam
= 1.0
128 qualityGroupParam
= 1.0
130 # lengthGroupParam = 0.005
131 # spliceGroupParam = 0.005
132 # matchGroupParam = 0.495
133 # qualityGroupParam = 0.495
135 #lengthGroupParam = lengthSP / (1.0*lengthSP+donSP+accSP)
136 #spliceGroupParam = donSP+accSP / (1.0*lengthSP+donSP+accSP)
137 #matchGroupParam = mmatrixSP/(1.0*mmatrixSP+totalQualSP)
138 #qualityGroupParam = totalQualSP/(1.0*mmatrixSP+totalQualSP)
141 #denom = (1.0*lengthSP+donSP+accSP+mmatrixSP+totalQualSP)
142 #ratio1 = lengthSP+donSP+accSP / denom
143 #ratio2 = mmatrixSP+totalQualSP / denom
145 #lengthGroupParam *= ratio1
146 #spliceGroupParam *= ratio1
148 #matchGroupParam *= ratio2
149 #qualityGroupParam *= ratio2
152 beg
= lengthSP
+donSP
+accSP
153 end
= lengthSP
+donSP
+accSP
+mmatrixSP
154 self
.P
[beg
:end
,beg
:end
] *= matchGroupParam
156 beg
= lengthSP
+donSP
+accSP
+mmatrixSP
157 self
.P
[beg
:-self
.numExamples
,beg
:-self
.numExamples
] *= qualityGroupParam
159 self
.P
[0:-self
.numExamples
,0:-self
.numExamples
] *= regC
*0.1
162 def createRegularizer(self
):
163 #self.createUnitRegularizer()
164 self
.createSmoothnessRegularizer()
166 q_array
= [0.0]*self
.numFeatures
+ [1.0]*self
.numExamples
167 self
.q
= cb
.matrix(q_array
,(self
.numFeatures
+self
.numExamples
,1),'d')
168 self
.q
= self
.C
* self
.q
170 self
.G
= cb
.matrix(0.0,(self
.numExamples
,self
.numFeatures
+self
.numExamples
))
171 for i
in range(self
.numExamples
):
172 self
.G
[i
,self
.numFeatures
+i
] = -1
174 self
.h
= cb
.matrix(0,(self
.numExamples
,1),'d')
176 if __name__
== '__main__':
180 function [C_qp_penalties,C_qp_smoothness,C_lp_smoothness,C_qp_smallness,column_eps,Q,f,LB,UB] = paths_create_qp(N,C)
181 % function [C_qp_penalties,C_qp_smoothness,C_lp_smoothness,C_qp_smallness,column_eps,Q,f,LB,UB] = paths_create_qp(N,C)
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 % qp_solve: min ( <res, f'> + 1/2 res' Q res)
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 C_qp_penalties = C ; %was 0.01
187 C_qp_smoothness = C ; %was 0.01
188 C_lp_smoothness = 0.00 ; %linear problem parameters
189 C_qp_smallness = 1e-6 ; %was 1e-6
192 Q=zeros(2*126+N) ; % 1/2 * res' * Q * res
193 Q(1:90,1:90) = C_qp_smallness*diag(ones(1,90)) ;
194 Q(91:126,91:126) = C_qp_penalties*diag(ones(1,36)) ;
195 Q(127:2*126,127:2*126) = C_qp_smoothness*diag(ones(1,126)) ;
196 f = [zeros(1,126) C_lp_smoothness*ones(1,126) ones(1,N)/N] ; % <res, f'>
198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199 % qp_solve: LB <= res <= UB
200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 LB = [-INF*ones(1,126) zeros(1,126) zeros(1,N)] ; % lower bound for res
203 UB = [ INF*ones(1,2*126) INF*ones(1,N)] ; % upper bound for res