|
| 1 | +""" |
| 2 | +Copyright 2022, the CVXPY Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +
|
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +
|
| 17 | +""" |
| 18 | +import numpy as np |
| 19 | +import scipy.sparse as sp |
| 20 | + |
| 21 | +import cvxpy.settings as s |
| 22 | +from cvxpy.constraints import SOC, ExpCone, PowCone3D |
| 23 | +from cvxpy.reductions.solution import Solution, failure_solution |
| 24 | +from cvxpy.reductions.solvers import utilities |
| 25 | +from cvxpy.reductions.solvers.conic_solvers.conic_solver import ConicSolver |
| 26 | +from cvxpy.utilities.citations import CITATION_DICT |
| 27 | + |
| 28 | + |
| 29 | +def dims_to_solver_cones(jl, cone_dims): |
| 30 | + |
| 31 | + jl.seval("""cones = Clarabel.SupportedCone[]""") |
| 32 | + |
| 33 | + # assume that constraints are presented |
| 34 | + # in the preferred ordering of SCS. |
| 35 | + |
| 36 | + if cone_dims.zero > 0: |
| 37 | + jl.push_b(jl.cones, jl.Clarabel.ZeroConeT(cone_dims.zero)) |
| 38 | + |
| 39 | + if cone_dims.nonneg > 0: |
| 40 | + jl.push_b(jl.cones, jl.Clarabel.NonnegativeConeT(cone_dims.nonneg)) |
| 41 | + |
| 42 | + for dim in cone_dims.soc: |
| 43 | + jl.push_b(jl.cones, jl.Clarabel.SecondOrderConeT(dim)) |
| 44 | + |
| 45 | + for dim in cone_dims.psd: |
| 46 | + jl.push_b(jl.cones, jl.Clarabel.PSDTriangleConeT(dim)) |
| 47 | + |
| 48 | + for _ in range(cone_dims.exp): |
| 49 | + jl.push_b(jl.cones, jl.Clarabel.ExponentialConeT()) |
| 50 | + |
| 51 | + for pow in cone_dims.p3d: |
| 52 | + jl.push_b(jl.cones, jl.Clarabel.PowerConeT(pow)) |
| 53 | + |
| 54 | + |
| 55 | +class CUCLARABEL(ConicSolver): |
| 56 | + """An interface for the Clarabel solver. |
| 57 | + """ |
| 58 | + |
| 59 | + # Solver capabilities. |
| 60 | + MIP_CAPABLE = False |
| 61 | + SUPPORTED_CONSTRAINTS = ConicSolver.SUPPORTED_CONSTRAINTS \ |
| 62 | + + [SOC, ExpCone, PowCone3D] |
| 63 | + |
| 64 | + STATUS_MAP = { |
| 65 | + "SOLVED": s.OPTIMAL, |
| 66 | + "PRIMAL_INFEASIBLE": s.INFEASIBLE, |
| 67 | + "DUAL_INFEASIBLE": s.UNBOUNDED, |
| 68 | + "ALMOST_SOLVED": s.OPTIMAL_INACCURATE, |
| 69 | + "ALMOST_PRIMAL_INFEASIBLE": s.INFEASIBLE_INACCURATE, |
| 70 | + "Almost_DUAL_INFEASIBLE": s.UNBOUNDED_INACCURATE, |
| 71 | + "MAX_ITERATIONS": s.USER_LIMIT, |
| 72 | + "MAX_TIME": s.USER_LIMIT, |
| 73 | + "NUMERICAL_ERROR": s.SOLVER_ERROR, |
| 74 | + "INSUFFICIENT_PROGRESS": s.SOLVER_ERROR |
| 75 | + } |
| 76 | + |
| 77 | + # Order of exponential cone arguments for solver. |
| 78 | + EXP_CONE_ORDER = [0, 1, 2] |
| 79 | + |
| 80 | + def name(self): |
| 81 | + """The name of the solver. |
| 82 | + """ |
| 83 | + return 'CUCLARABEL' |
| 84 | + |
| 85 | + def import_solver(self) -> None: |
| 86 | + """Imports the solver. |
| 87 | + """ |
| 88 | + # Does NOT import juliacall because that starts a Julia interpreter |
| 89 | + # and this method is called on CVXPY importing and that's too heavy. |
| 90 | + import cupy # noqa F401 |
| 91 | + |
| 92 | + def supports_quad_obj(self) -> bool: |
| 93 | + """Clarabel supports quadratic objective with any combination |
| 94 | + of conic constraints. |
| 95 | + """ |
| 96 | + return True |
| 97 | + |
| 98 | + @staticmethod |
| 99 | + def extract_dual_value(result_vec, offset, constraint): |
| 100 | + """Extracts the dual value for constraint starting at offset. |
| 101 | + """ |
| 102 | + return utilities.extract_dual_value(result_vec, offset, constraint) |
| 103 | + |
| 104 | + def invert(self, solution, inverse_data): |
| 105 | + """Returns the solution to the original problem given the inverse_data. |
| 106 | + """ |
| 107 | + |
| 108 | + attr = {} |
| 109 | + status = self.STATUS_MAP[str(solution.status)] |
| 110 | + attr[s.SOLVE_TIME] = solution.solve_time |
| 111 | + attr[s.NUM_ITERS] = solution.iterations |
| 112 | + # more detailed statistics here when available |
| 113 | + # attr[s.EXTRA_STATS] = solution.extra.FOO |
| 114 | + |
| 115 | + if status in s.SOLUTION_PRESENT: |
| 116 | + primal_val = solution.obj_val |
| 117 | + opt_val = primal_val + inverse_data[s.OFFSET] |
| 118 | + primal_vars = { |
| 119 | + inverse_data[CUCLARABEL.VAR_ID]: np.array(solution.x) |
| 120 | + } |
| 121 | + eq_dual_vars = utilities.get_dual_values( |
| 122 | + np.array(solution.z[:inverse_data[ConicSolver.DIMS].zero]), |
| 123 | + self.extract_dual_value, |
| 124 | + inverse_data[CUCLARABEL.EQ_CONSTR] |
| 125 | + ) |
| 126 | + ineq_dual_vars = utilities.get_dual_values( |
| 127 | + np.array(solution.z[inverse_data[ConicSolver.DIMS].zero:]), |
| 128 | + self.extract_dual_value, |
| 129 | + inverse_data[CUCLARABEL.NEQ_CONSTR] |
| 130 | + ) |
| 131 | + dual_vars = {} |
| 132 | + dual_vars.update(eq_dual_vars) |
| 133 | + dual_vars.update(ineq_dual_vars) |
| 134 | + return Solution(status, opt_val, primal_vars, dual_vars, attr) |
| 135 | + else: |
| 136 | + return failure_solution(status, attr) |
| 137 | + |
| 138 | + def solve_via_data(self, data, warm_start: bool, verbose: bool, solver_opts, solver_cache=None): |
| 139 | + """Returns the result of the call to the solver. |
| 140 | +
|
| 141 | + Parameters |
| 142 | + ---------- |
| 143 | + data : dict |
| 144 | + Data generated via an apply call. |
| 145 | + warm_start : Bool |
| 146 | + Whether to warm_start Clarabel. |
| 147 | + verbose : Bool |
| 148 | + Control the verbosity. |
| 149 | + solver_opts : dict |
| 150 | + Clarabel-specific solver options. |
| 151 | +
|
| 152 | + Returns |
| 153 | + ------- |
| 154 | + The result returned by a call to clarabel.solve(). |
| 155 | + """ |
| 156 | + import cupy |
| 157 | + from cupyx.scipy.sparse import csr_matrix as cucsr_matrix |
| 158 | + from juliacall import Main as jl |
| 159 | + jl.seval('using Clarabel, LinearAlgebra, SparseArrays') |
| 160 | + jl.seval('using CUDA, CUDA.CUSPARSE') |
| 161 | + |
| 162 | + A = data[s.A] |
| 163 | + b = data[s.B] |
| 164 | + q = data[s.C] |
| 165 | + |
| 166 | + if s.P in data: |
| 167 | + P = data[s.P] |
| 168 | + else: |
| 169 | + nvars = q.size |
| 170 | + P = sp.csr_array((nvars, nvars)) |
| 171 | + |
| 172 | + P = sp.triu(P).tocsr() |
| 173 | + |
| 174 | + cones = data[ConicSolver.DIMS] |
| 175 | + |
| 176 | + Pgpu = cucsr_matrix(P) |
| 177 | + qgpu = cupy.array(q) |
| 178 | + |
| 179 | + Agpu = cucsr_matrix(A) |
| 180 | + bgpu = cupy.array(b) |
| 181 | + |
| 182 | + if Pgpu.nnz != 0: |
| 183 | + jl.P = jl.Clarabel.cupy_to_cucsrmat( |
| 184 | + jl.Float64, int(Pgpu.data.data.ptr), int(Pgpu.indices.data.ptr), |
| 185 | + int(Pgpu.indptr.data.ptr), *Pgpu.shape, Pgpu.nnz) |
| 186 | + else: |
| 187 | + jl.seval(f""" |
| 188 | + P = CuSparseMatrixCSR(sparse(Float64[], Float64[], Float64[], {nvars}, {nvars})) |
| 189 | + """) |
| 190 | + jl.q = jl.Clarabel.cupy_to_cuvector(jl.Float64, int(qgpu.data.ptr), qgpu.size) |
| 191 | + |
| 192 | + jl.A = jl.Clarabel.cupy_to_cucsrmat( |
| 193 | + jl.Float64, int(Agpu.data.data.ptr), int(Agpu.indices.data.ptr), |
| 194 | + int(Agpu.indptr.data.ptr), *Agpu.shape, Agpu.nnz) |
| 195 | + jl.b = jl.Clarabel.cupy_to_cuvector(jl.Float64, int(bgpu.data.ptr), bgpu.size) |
| 196 | + |
| 197 | + |
| 198 | + dims_to_solver_cones(jl, cones) |
| 199 | + |
| 200 | + results = jl.seval(""" |
| 201 | + settings = Clarabel.Settings(direct_solve_method = :cudss) |
| 202 | + solver = Clarabel.Solver(P,q,A,b,cones, settings) |
| 203 | + Clarabel.solve!(solver) |
| 204 | + """) |
| 205 | + return results |
| 206 | + |
| 207 | + def cite(self, data): |
| 208 | + """Returns bibtex citation for the solver. |
| 209 | +
|
| 210 | + Parameters |
| 211 | + ---------- |
| 212 | + data : dict |
| 213 | + Data generated via an apply call. |
| 214 | + """ |
| 215 | + return CITATION_DICT["CUCLARABEL"] |
0 commit comments