def test_qp_maximization_reduction_path_qp_solver(self): qp_maximization = Problem(Maximize(QuadForm(self.x, -self.Q)), [self.x <= -1]) path = PathFinder().reduction_path(ProblemType(qp_maximization), [QpSolver]) self.assertEquals(3, len(path)) self.assertEquals(path[1], QpMatrixStuffing) self.assertEquals(path[2], FlipObjective)
def test_qp_maximization_reduction_path_ecos(self): qp_maximization = Problem(Maximize(-sum_squares(self.x)), [self.x <= -1]) self.assertTrue(qp_maximization.is_dcp()) path = PathFinder().reduction_path(ProblemType(qp_maximization), [ECOS]) self.assertEquals(4, len(path)) self.assertEquals(path[1], ConeMatrixStuffing) self.assertEquals(path[2], Dcp2Cone) self.assertEquals(path[3], FlipObjective)
def reduction_path(self, current_type, current_path): """A reduction path is like a stack: the first element (0) is the end of the reduction path (e.g. solver), the first reduction to apply to the problem is path.pop(). """ if self.is_valid_reduction_path(current_type, current_path): return current_path for reduction in self.applicable_reductions(current_type): self.reductions.remove(reduction) current_path.insert(1, reduction) old_type = current_type if not hasattr(reduction, 'postconditions'): current_path.pop(1) current_type = old_type continue current_type = ProblemType(reduction.postconditions(current_type)) candidate_path = self.reduction_path(current_type, current_path) if candidate_path: return candidate_path else: self.reductions.add(reduction) current_path.pop(1) current_type = old_type
def test_cone_reduction_path_valid_as_is(self): path = PathFinder().reduction_path(ProblemType(self.cp), [ECOS]) self.assertEquals(1, len(path))
def test_qp_reduction_path(self): path = PathFinder().reduction_path(ProblemType(self.qp), [QpSolver]) self.assertEquals(2, len(path)) self.assertEquals(path[1], QpMatrixStuffing)
def test_QPcanon_postconditions(self): pa = ProblemType(self.qp) pc = Qp2SymbolicQp.postconditions(pa.type) self.assertEquals(True, (Minimize, is_quadratic, True) in pc) self.assertEquals(True, (NonPos, are_arguments_affine, True) in pc) self.assertEquals(False, any(type(c[0]) == Zero for c in pc))
def test_QPcanon_type(self): pa = ProblemType(self.qp) self.assertEquals(True, (Minimize, is_quadratic, True) in pa.type) self.assertEquals(True, (NonPos, is_affine, True) in pa.type)