+ added configuration variables to main config file
[qpalma.git] / qpalma / SIQP_CPX.py
index b6635f6..3ca654a 100644 (file)
@@ -56,8 +56,8 @@ class SIQPSolver(SIQP):
    # Define types and constants
    Inf   = CPX_INFBOUND
 
    # Define types and constants
    Inf   = CPX_INFBOUND
 
-   def __init__(self,fSize,numExamples,c,proto):
-      SIQP.__init__(self,fSize,numExamples,c)
+   def __init__(self,fSize,numExamples,c,proto,run):
+      SIQP.__init__(self,fSize,numExamples,c,run)
       self.numFeatures = fSize
       self.numExamples = numExamples
       self.numVariables = self.numFeatures + self.numExamples
       self.numFeatures = fSize
       self.numExamples = numExamples
       self.numVariables = self.numFeatures + self.numExamples
@@ -75,6 +75,7 @@ class SIQPSolver(SIQP):
 
       CPX.setintparam(self.env, CPX_PARAM_SCRIND, CPX_ON) # print >> self.protocol, info to screen
       CPX.setintparam(self.env, CPX_PARAM_DATACHECK, CPX_ON)
 
       CPX.setintparam(self.env, CPX_PARAM_SCRIND, CPX_ON) # print >> self.protocol, info to screen
       CPX.setintparam(self.env, CPX_PARAM_DATACHECK, CPX_ON)
+      #CPX.setintparam(self.env, CPX_PARAM_QPMETHOD, 2)
 
       # create CPLEX problem, add objective and constraints to it
       self.lp = CPX.createprob(self.env, 'test1')
 
       # create CPLEX problem, add objective and constraints to it
       self.lp = CPX.createprob(self.env, 'test1')
@@ -87,6 +88,23 @@ class SIQPSolver(SIQP):
       self.ub = FloatMatrix([self.Inf] * self.numVariables).data
       self.lb = FloatMatrix([-self.Inf] * self.numFeatures + [0.0]*self.numExamples).data
 
       self.ub = FloatMatrix([self.Inf] * self.numVariables).data
       self.lb = FloatMatrix([-self.Inf] * self.numFeatures + [0.0]*self.numExamples).data
 
+      # if we disable to intron length model we set the first boundaries of the
+      # intron support points to 0.0
+      #
+      #import pdb
+      #pdb.set_trace()
+
+      if not run['enable_intron_length']:
+         self.ub[0] = 0.0
+         self.ub[1] = 0.0
+
+         self.lb[0] = 0.0
+         self.lb[1] = 0.0
+
+      #pdb.set_trace()
+
+      ############################
+
       self.matbeg = IntMatrix([1]*self.numVariables).data
       self.matbeg[0] = 0
 
       self.matbeg = IntMatrix([1]*self.numVariables).data
       self.matbeg[0] = 0
 
@@ -106,9 +124,66 @@ class SIQPSolver(SIQP):
                self.matbeg, self.matcnt, self.matind, self.matval, 
                self.lb, self.ub) 
 
                self.matbeg, self.matcnt, self.matind, self.matval, 
                self.lb, self.ub) 
 
+      import pdb
+      #pdb.set_trace()
+
+      assert sum(self.P[self.numFeatures:,self.numFeatures:]) == 0.0, 'slack variables are regularized'
+
       self.qmatbeg, self.qmatcnt, self.qmatind, self.qmatval = self.cpx_matrix(self.P)
       CPX.copyquad(self.env, self.lp, self.qmatbeg, self.qmatcnt, self.qmatind, self.qmatval)
 
       self.qmatbeg, self.qmatcnt, self.qmatind, self.qmatval = self.cpx_matrix(self.P)
       CPX.copyquad(self.env, self.lp, self.qmatbeg, self.qmatcnt, self.qmatind, self.qmatval)
 
+   def enforceMonotonicity(self, begin, end):
+      """
+      This method enforces monotonicity on the parameters indexed by begin to
+      end.
+      """
+      assert -1 < begin < end < self.numFeatures
+
+      for idx in xrange(begin,end):
+         self.enforcePairwiseMonotonicity(idx,idx+1)
+
+   def enforcePairwiseMonotonicity(self,pos1,pos2):
+      energy_deltas = IntMatrix([0.0]*self.numVariables).data 
+
+      energy_deltas[pos1] = -1.0
+      energy_deltas[pos2] = 1.0
+
+      numNewRows = 1
+      numNewCols = 0
+   
+      row_rhs = FloatMatrix([0.0]).data
+      row_sense = CharMatrix(['G']).data
+
+      row_matbeg = IntMatrix([0]).data
+
+      row_entries = [0.0]*self.numVariables
+      indices = [0]*self.numVariables
+
+      currentVal = 0.0
+      numNonZeroElems = 0
+      for idx in range(self.numFeatures):
+         currentVal = energy_deltas[idx]
+         if math.fabs(currentVal) > self.EPSILON:
+            row_entries[numNonZeroElems] = currentVal
+            indices[numNonZeroElems] = idx
+            numNonZeroElems += 1
+
+      row_matind = IntMatrix([0]*(numNonZeroElems+1)).data
+      row_matval = FloatMatrix([0.0]*(numNonZeroElems+1)).data
+
+      for pos in range(numNonZeroElems):
+         row_matind[pos] = indices[pos]
+         row_matval[pos] = row_entries[pos]
+
+      status = CPX.addrows (self.env, self.lp, numNewCols, numNewRows, numNonZeroElems+1, row_rhs,
+                                  row_sense, row_matbeg, row_matind, row_matval)
+      
+      assert status == 0, 'Error ocurred while adding constraint.'
+
+      self.addedConstraints += 1
+
+      return True
+
    def addConstraint(self, energy_deltas, example_idx):
    #def addConstraint(self, energy_deltas, loss, example_idx, useMarginRescaling):
       """
    def addConstraint(self, energy_deltas, example_idx):
    #def addConstraint(self, energy_deltas, loss, example_idx, useMarginRescaling):
       """
@@ -175,8 +250,8 @@ class SIQPSolver(SIQP):
          print >> self.protocol, 'SIQP_CPX: Objective is: %f'%objval
 
          # Check that objective value is monotonically increasing
          print >> self.protocol, 'SIQP_CPX: Objective is: %f'%objval
 
          # Check that objective value is monotonically increasing
-         if math.fabs(objval - self.old_objective_value) >= 10e-5:
-            assert objval >= self.old_objective_value
+         #if math.fabs(objval - self.old_objective_value) >= 1e-4:
+         #   assert objval >= self.old_objective_value
 
          self.old_objective_value = objval
 
 
          self.old_objective_value = objval