Exemplo n.º 1
0
    def _test_expr(self, expr, printing=False):
        # Get Callback param.
        expr_canon = expr.canonicalize()[0]
        cb_param = expr_canon.data
        true_expr_value = sp.csc_matrix(cb_param.value)

        # Set up param handler.
        param_handler = ParamHandler(expr_canon, [], [])
        template_vars = param_handler.get_template_vars()
        make_target_dir(target_dir)
        param_handler.render(target_dir)

        # Set up test handler.
        render(target_dir, template_vars, HARNESS_C, 'harness.c')
        render(target_dir, template_vars, CODEGEN_H, 'codegen.h')
        tested_cb_param_values = self._run_test(target_dir)
        mat = list(tested_cb_param_values.values())[0]
        test_expr_value = sp.csc_matrix((mat['nzval'],
                                         mat['rowidx'],
                                         mat['colptr']))
        if printing:
            print('\nTrue value:\n', true_expr_value)
            print('\nTest value:\n', test_expr_value)
            print('\nDifference:\n', test_expr_value - true_expr_value)
        self.assertAlmostEqualMatrices(true_expr_value, test_expr_value)
Exemplo n.º 2
0
    def render(self, target_dir):
        solver_dir = os.path.join(target_dir, self.name)

        # make target directory
        if os.path.exists(solver_dir):
            sh.rmtree(solver_dir)

        # Copy ecos source files
        sh.copytree(self.SOURCE_DIR, solver_dir, ignore=self.IGNORES)

        # Render interface template:
        render(target_dir, self.template_vars, 'solvers/ecos_intf.c.jinja',
               'solver_intf.c')
Exemplo n.º 3
0
    def _test_expr(self, expr, printing=False):
        # Get Callback param.
        prob = cg.Problem(cvxpy.Minimize(expr))
        obj, constrs = expr.canonicalize()
        data = ECOS.get_problem_data(obj, constrs, prob._cached_data)
        true_obj_coeff  = data[s.C]
        true_obj_offset = data[s.OFFSET]
        true_eq_coeff   = data[s.A]
        true_eq_offset  = data[s.B]
        true_leq_coeff  = data[s.G]
        true_leq_offset = data[s.H]

        # Do code generation
        vars = prob.variables()
        params = prob.parameters()
        obj, constraints = prob.canonical_form
        code_generator = CodeGenerator(obj, constraints, vars, params)
        code_generator.codegen(target_dir)
        template_vars = code_generator.template_vars

        # Set up test harness.
        render(target_dir, template_vars, HARNESS_C, 'harness.c')
        render(target_dir, template_vars, CODEGEN_H, 'codegen.h')
        test_data = self._run_test(target_dir)
        test_obj_coeff  = np.array(test_data['obj_coeff'])
        test_obj_offset = np.array(test_data['obj_offset'])
        test_eq_coeff  = sp.csc_matrix((test_data['eq_nzval'],
                                        test_data['eq_rowidx'],
                                        test_data['eq_colptr']),
                                        shape = (test_data['eq_size0'],
                                                 test_data['eq_size1']))
        test_eq_offset = np.array(test_data['eq_offset'])
        test_leq_coeff = sp.csc_matrix((test_data['eq_nzval'],
                                        test_data['eq_rowidx'],
                                        test_data['eq_colptr']),
                                        shape = (test_data['eq_size0'],
                                                 test_data['eq_size1']))
        test_leq_offset = np.array(test_data['leq_offset'])

        if printing:
            print('\nTest objective coeff  :\n',   test_obj_coeff)
            print('\nTrue objective coeff  :\n',   true_obj_coeff)

            print('\nTest objective offset :\n',   test_obj_offset)
            print('\nTrue objective offset :\n',   true_obj_offset)

            print('\nTest equality offset :\n',    test_eq_coeff)
            print('\nTrue equality offset :\n',    true_eq_coeff)

            print('\nTest equality offset :\n',    test_eq_offset)
            print('\nTrue equality offset :\n',    true_eq_offset)

            print('\nTest inequality offset :\n',  test_leq_coeff)
            print('\nTrue inequality offset :\n',  true_leq_coeff)

            print('\nTest inequality offset :\n',  test_leq_offset)
            print('\nTrue inequality offset :\n',  true_leq_offset)

        self.assertAlmostEqualMatrices(true_obj_coeff,  test_obj_coeff)
        self.assertAlmostEqualMatrices(true_obj_offset, test_obj_offset)
        self.assertAlmostEqualMatrices(true_eq_coeff,   test_eq_coeff)
        self.assertAlmostEqualMatrices(true_eq_offset,  test_eq_offset)
        self.assertAlmostEqualMatrices(true_leq_coeff,  test_leq_coeff)
        self.assertAlmostEqualMatrices(true_leq_offset, test_leq_offset)
Exemplo n.º 4
0
 def render(self, target_dir):
     render(target_dir, self.template_vars, 'linop_sym/linop_sym.c.jinja',
            'linop.c')
Exemplo n.º 5
0
 def render(self, target_dir, template_vars):
     template_vars.update(self.template_vars)
     render(target_dir, template_vars, 'templates/codegen.c.jinja', 'codegen.c')
     render(target_dir, template_vars, 'templates/codegen.h.jinja', 'codegen.h')
     render(target_dir, template_vars, 'templates/codegenmodule.c.jinja', 'codegenmodule.c')
     render(target_dir, template_vars, 'templates/cvxpy_codegen_solver.py.jinja', 'cvxpy_codegen_solver.py')
     render(target_dir, template_vars, 'templates/setup.py.jinja', 'setup.py')
     render(target_dir, template_vars, 'templates/Makefile.jinja', 'Makefile')
     render(target_dir, template_vars, 'templates/example_problem.c.jinja', 'example_problem.c')
Exemplo n.º 6
0
 def render(self, target_dir):
     render(target_dir, self.template_vars, 'param/param.c.jinja', 'param.c')