#!/usr/bin/env python
# -*- coding:latin-1 -*-
import sys
import os
import os.path
import logging
import math
import pdb
import cvxopt.base as cb
import logging
logging.basicConfig(level=logging.DEBUG,format='%(levelname)s %(message)s')
def asymCoords(t):
size_upstream = t[0]
size_downstream = t[1]
assert size_upstream >= 1 and size_upstream <= 39
assert size_downstream >= 1 and size_downstream <= 39
if size_downstream > size_upstream:
asym_small = size_upstream
asym_big = size_downstream
else:
asym_small = size_downstream
asym_big = size_upstream
asym_offset = 0
for r in range(asym_small):
asym_offset += (40-r)-r;
return (asym_offset+asym_big)-1
def calcTuples(i,j):
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)])
class SIQP:
"""
This class is a wrapper for the cvxopt qp solver.
It has the purpose to support an iterative addition of
constraints to the original problem.
We want to solve a problem of the form
min 1/2 * C * x' * P * x + q' * x
x
s.t.
< w,f+ > - < w,f- > >= 1 - xi_i for all i, for all f-
where x is a vector storing information on the parameters w_i and the slacks xi_i
so x = [w;xi]
"""
def __init__(self,fSize,numExamples,c):
assert fSize > 0, 'You have to have at least one feature!'
assert numExamples > 0, 'You have to have at least one example!'
self.numFeatures = fSize
self.numExamples = numExamples
self.C = c
self.EPSILON = 10e-15
logging.debug("Starting SIQP solver with %d examples. A feature space dim. of %d.", numExamples,fSize)
logging.debug("Regularization parameters are C=%f."%(self.C))
self.old_w = None
self.dimP = self.numFeatures + self.numExamples
self.createRegularizer()
def createUnitRegularizer(self):
# set regularizer to zero
self.P = cb.matrix(0.0,(self.dimP,self.dimP))
for i in range(self.numFeatures):
self.P[i,i] = 1.0
# end of zeroing regularizer
def createRegularizer(self):
self.createUnitRegularizer()
q_array = [0.0]*self.numFeatures + [1.0]*self.numExamples
self.q = cb.matrix(q_array,(self.numFeatures+self.numExamples,1),'d')
self.q = self.C * self.q
self.G = cb.matrix(0.0,(self.numExamples,self.numFeatures+self.numExamples))
for i in range(self.numExamples):
self.G[i,self.numFeatures+i] = -1
self.h = cb.matrix(0,(self.numExamples,1),'d')
if __name__ == '__main__':
sq = SIQP(3,2,100.0)
"""
function [C_qp_penalties,C_qp_smoothness,C_lp_smoothness,C_qp_smallness,column_eps,Q,f,LB,UB] = paths_create_qp(N,C)
% function [C_qp_penalties,C_qp_smoothness,C_lp_smoothness,C_qp_smallness,column_eps,Q,f,LB,UB] = paths_create_qp(N,C)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% qp_solve: min ( + 1/2 res' Q res)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C_qp_penalties = C ; %was 0.01
C_qp_smoothness = C ; %was 0.01
C_lp_smoothness = 0.00 ; %linear problem parameters
C_qp_smallness = 1e-6 ; %was 1e-6
column_eps = 1e-3 ;
Q=zeros(2*126+N) ; % 1/2 * res' * Q * res
Q(1:90,1:90) = C_qp_smallness*diag(ones(1,90)) ;
Q(91:126,91:126) = C_qp_penalties*diag(ones(1,36)) ;
Q(127:2*126,127:2*126) = C_qp_smoothness*diag(ones(1,126)) ;
f = [zeros(1,126) C_lp_smoothness*ones(1,126) ones(1,N)/N] ; %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% qp_solve: LB <= res <= UB
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
INF = 1e20 ;
LB = [-INF*ones(1,126) zeros(1,126) zeros(1,N)] ; % lower bound for res
UB = [ INF*ones(1,2*126) INF*ones(1,N)] ; % upper bound for res
"""