How to use the diffcp.SolverError function in diffcp

To help you get started, we’ve selected a few diffcp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cvxgrp / cvxpylayers / cvxpylayers / torch / test_cvxpylayer.py View on Github external
def test_infeasible(self):
        x = cp.Variable(1)
        param = cp.Parameter(1)
        prob = cp.Problem(cp.Minimize(param), [x >= 1, x <= -1])
        layer = CvxpyLayer(prob, [param], [x])
        param_tch = torch.ones(1)
        with self.assertRaises(diffcp.SolverError):
            layer(param_tch)
github cvxgrp / cvxpylayers / cvxpylayers / tensorflow / test_cvxpylayer.py View on Github external
def test_infeasible(self):
        x = cp.Variable(1)
        param = cp.Parameter(1)
        prob = cp.Problem(cp.Minimize(param), [x >= 1, x <= -1])
        layer = CvxpyLayer(prob, [param], [x])
        param_tf = tf.ones(1)
        with self.assertRaises(diffcp.SolverError):
            layer(param_tf)
github cvxgrp / cvxpylayers / cvxpylayers / torch / test_cvxpylayer.py View on Github external
def test_unbounded(self):
        x = cp.Variable(1)
        param = cp.Parameter(1)
        prob = cp.Problem(cp.Minimize(x), [x <= param])
        layer = CvxpyLayer(prob, [param], [x])
        param_tch = torch.ones(1)
        with self.assertRaises(diffcp.SolverError):
            layer(param_tch)
github cvxgrp / cvxpylayers / cvxpylayers / torch / cvxpylayer.py View on Github external
dict(zip(param_ids, params_numpy_i)),
                    keep_zeros=True)
                A = -neg_A  # cvxpy canonicalizes -A
                As.append(A)
                bs.append(b)
                cs.append(c)
                cone_dicts.append(cone_dims)
                ctx.shapes.append(A.shape)
            info['canon_time'] = time.time() - start

            # compute solution and derivative function
            start = time.time()
            try:
                xs, _, _, _, ctx.DT_batch = diffcp.solve_and_derivative_batch(
                    As, bs, cs, cone_dicts, **solver_args)
            except diffcp.SolverError as e:
                print(
                    "Please consider re-formulating your problem so that "
                    "it is always solvable or increasing the number of "
                    "solver iterations.")
                raise e
            info['solve_time'] = time.time() - start

            # extract solutions and append along batch dimension
            sol = [[] for _ in range(len(variables))]
            for i in range(ctx.batch_size):
                sltn_dict = compiler.split_solution(
                    xs[i], active_vars=var_dict)
                for j, v in enumerate(variables):
                    sol[j].append(to_torch(
                        sltn_dict[v.id], ctx.dtype, ctx.device).unsqueeze(0))
            sol = [torch.cat(s, 0) for s in sol]
github cvxgrp / cvxpylayers / cvxpylayers / tensorflow / cvxpylayer.py View on Github external
params = [param_map[pid] for pid in self.param_ids]

        As, bs, cs = [], [], []
        for i in range(batch_size):
            params_i = [
                p if sz == 0 else p[i] for p, sz in zip(params, batch_sizes)]
            A, b, c = self._problem_data_from_params(params_i)
            As.append(A)
            bs.append(b)
            cs.append(c)

        try:
            xs, _, ss, _, DT = diffcp.solve_and_derivative_batch(
                As=As, bs=bs, cs=cs, cone_dicts=[self.cones] * batch_size,
                **solver_args)
        except diffcp.SolverError as e:
            print(
                "Please consider re-formulating your problem so that "
                "it is always solvable or increasing the number of "
                "solver iterations.")
            raise e

        DT = self._restrict_DT_to_dx(DT, batch_size, ss[0].shape)
        solns = [self._split_solution(x) for x in xs]
        # soln[i] is a tensor with first dimension equal to batch_size, holding
        # the optimal values for variable i
        solution = [
            tf.stack([s[i] for s in solns]) for i in range(len(self.vars))]
        if not any_batched:
            solution = [tf.squeeze(s, 0) for s in solution]

        if self.gp: