### Verify Installation with an Example Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/rust/installation_rs.md Run the provided 'qp' example using Cargo to confirm that the installation was successful. ```bash cargo run --example qp ``` -------------------------------- ### Python Clarabel Getting Started Example Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/examples/py/example_intro.md This snippet shows a complete example of using the Clarabel solver in Python. It includes problem setup, solving, and result retrieval. ```python using Documenter Documenter.md_include( source = "examples/py/example_intro.py", language = :python) ``` -------------------------------- ### Run Example Script Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/installation_py.md Execute a Python example script to verify the Clarabel installation within the virtual environment. ```bash python examples/python/example_qp.py ``` -------------------------------- ### Solve SOCP with Clarabel in Python Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/examples/py/example_socp.md This example demonstrates how to set up and solve a Second-Order Cone Program (SOCP) using the Clarabel solver in Python. It requires the Clarabel package to be installed. ```python from clarabel import DefaultSolver from clarabel.form import CvxForm import numpy as np # Define problem data # Minimize c'x subject to Ax + s = b, s in K # where K is a cone (e.g., Second-Order Cone) c = np.array([1.0, 1.0, 1.0]) A = np.array([ [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 1.0, 1.0], ]) b = np.array([1.0, 1.0, 1.0, 1.0]) # Define the cone: Second-Order Cone (SOC) of dimension 3 # K = SOC(3) # Clarabel uses a list of cone types and their dimensions cones = [("SOC", 3)] # Create the solver solver = DefaultSolver(A, b, c, cones) # Solve the problem solution = solver.solve() # Print the solution print("Solution:", solution) print("Primal objective value:", solution.obj_val) ``` -------------------------------- ### Setup Solver with Problem Data Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/julia/getting_started_jl.md Populate the solver with problem data (P, q, A, b, cones) and optional settings. ```julia Clarabel.setup!(solver, P, q, A, b, cones, settings) ``` -------------------------------- ### Python Example: Updating Quadratic Program Data Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/user_guide_data_updating.md This example demonstrates how to update the quadratic program data in Python and re-solve the problem. Ensure the problem shape remains consistent for successful updates. ```python from clarabel import DefaultSolver import numpy as np # Define an initial QP problem P = np.array([[2.0, 0.0], [0.0, 1.0]]) q = np.array([1.0, 1.0]) A = np.array([[1.0, 1.0], [1.0, 0.0]]) b = np.array([1.0, 1.0]) cones = [clarabel.SecondOrderCone(2)] solver = DefaultSolver(P, q, A, b, cones) solution = solver.solve() print("Initial solution:", solution.x) # Update problem data P_new = np.array([[3.0, 0.0], [0.0, 2.0]]) q_new = np.array([2.0, 2.0]) A_new = np.array([[2.0, 1.0], [1.0, 2.0]]) b_new = np.array([2.0, 2.0]) solver.update_data(P=P_new, q=q_new, A=A_new, b=b_new) solution_updated = solver.solve() print("Updated solution:", solution_updated.x) # Example of partial update (updating only P) P_partial_update = np.array([[4.0, 0.0], [0.0, 3.0]]) solver.update_data(P=P_partial_update) solution_partial_update = solver.solve() print("Partially updated solution:", solution_partial_update.x) # Example of updating a single element in q q_single_element_update = np.array([3.0, 3.0]) # Note: This assumes q was [2.0, 2.0] from previous update solver.update_data(q=q_single_element_update) solution_single_element_update = solver.solve() print("Solution after single element update in q:", solution_single_element_update.x) ``` -------------------------------- ### CVXPY Interface Example Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/getting_started_py.md Demonstrates how to model and solve the same problem using the CVXPY interface, which can then be routed to Clarabel. ```python from cvxpy import \* from clarabel.solver.cvxpy import ClarabelSolver P = np.array([[3., 1., -1.],[1., 4., 2.],[-1., 2., 5.]]) q = np.array([1., 2., -3.]) x = Variable(3) objective = Minimize(quad_form(x, P) + q.T @ x) constraints = [ x[0] + x[1] - x[2] == 1, x[1] >= 2, x[2] >= 2, norm(x, 2) <= 0 ] problem = Problem(objective, constraints) solver = ClarabelSolver() problem.solve(solver=solver) print(f"Primal solution x: {x.value}") print(f"Dual solution (lambda): {problem.constraints[0].dual_value}") ``` -------------------------------- ### Include Rust Main File Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/examples/rs/example_qp.md Includes the main Rust source file for an example project. Ensure the path is correct. ```rust using Documenter Documenter.md_include( source = "examples/rs/example_qp/src/main.rs", language = :rust) ``` -------------------------------- ### SDP Example in Clarabel Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/examples/example_sdp.md This snippet shows a complete example of solving a semidefinite program using Clarabel. It defines the objective function and constraints, including the positive semidefinite cone constraint. ```julia using Clarabel using LinearAlgebra # Define the problem data A = Symmetric[ 1.0 2.0 4.0; 2.0 3.0 5.0; 4.0 5.0 6.0; ] # Objective function: minimize trace(X) # Clarabel expects 1/2 x'Px + q'x # For trace(X), P = 0 and q corresponds to the diagonal elements of X # The vector x is [x1, x2, x3, x4, x5, x6] corresponding to the upper triangle of X # q = [1, 0, 1, 0, 0, 1] selects x1, x3, x6 (diagonal elements) P = Diagonal(zeros(6)) q = [1.0, 0.0, 1.0, 0.0, 0.0, 1.0] # Equality constraint: = 1 # This is rewritten as vec(A)^T * x = 1 # vec(A) = [A11, sqrt(2)*A12, A22, sqrt(2)*A13, sqrt(2)*A23, A33] # vec(A) = [1.0, 2*sqrt(2), 3.0, 4*sqrt(2), 5*sqrt(2), 6.0] # The constraint matrix 'mat' is vec(A)^T mat = [1.0, 2*sqrt(2), 3.0, 4*sqrt(2), 5*sqrt(2), 6.0] b = [1.0] # Semidefinite constraint: X >= 0 # This is represented by a positive semidefinite cone constraint on the upper triangular part of X. # Clarabel uses Kcone = Clarabel.SDP(n) for an n x n matrix. # The number of variables for an n x n SDP is n*(n+1)/2. # Here n=3, so we have 3*(3+1)/2 = 6 variables. cone = Clarabel.SDP(3) # Define the constraints: Ax + s = b # For the equality constraint, A is mat, b is b. # For the SDP constraint, we need to map the upper triangular elements to the cone. # Clarabel handles this mapping internally when using Clarabel.SDP(n). # Create the solver solver = Clarabel.Optimizer() # Set problem data # Objective Clarabel.set_sense(solver, Clarabel.MIN_SENSE) Clarabel.set_obj_coeffs(solver, q) Clarabel.set_quadratic_obj_coeffs(solver, P) # Constraints # We have one equality constraint and one SDP constraint. # The equality constraint is Ax = b, so we use Clarabel.ThirdOrderCone(mat, b) # The SDP constraint is represented by the cone object. Clarabel.add_constraint(solver, mat, b) Clarabel.add_constraint(solver, cone) # Solve the problem Clarabel.solve(solver) # Print the solution println("Solution status: ", Clarabel.get_status(solver)) println("Optimal objective value: ", Clarabel.get_obj_val(solver)) println("Optimal decision variable x: ", Clarabel.get_var(solver)) ``` -------------------------------- ### Install Clarabel from Wheel File Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/installation_py.md Install the previously built Clarabel wheel file into your Python environment. This is done outside the virtual environment used for building. ```bash python3 -m pip install <.whl-file-you-just-built> ``` -------------------------------- ### Install Clarabel with pip Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/installation_py.md Use this command for a straightforward installation of the Clarabel Python package. ```python pip install clarabel ``` -------------------------------- ### Install Clarabel.jl Latest Version from GitHub Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/julia/installation_jl.md Use this command to install the most recent version of Clarabel.jl directly from its GitHub repository. Enter Pkg REPL mode by typing ']' in the Julia REPL. ```julia pkg> add Clarabel#main ``` -------------------------------- ### Basic SDP Formulation in Python Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/examples/py/example_sdp.md This snippet demonstrates the basic setup for defining and solving a Semidefinite Programming problem using Clarabel in Python. It requires the Clarabel library and NumPy. ```python import clarabel import numpy as np # Define problem dimensions n = 3 # Number of variables m = 2 # Number of constraints # Objective function (minimize trace(C*X)) # C is a symmetric matrix C = np.array([[1., 2., 3.], [2., 4., 5.], [3., 5., 6.]]) # Constraints # A_i are symmetric matrices for linear constraints A = [ np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), np.array([[1., 1., 0.], [1., 2., 0.], [0., 0., 3.]]) ] b = np.array([1.0, 2.0]) # Right-hand side of constraints # Create the Clarabel solver solver = clarabel.Clarabel(C, A, b) # Solve the problem solution = solver.solve() # Print the solution print(solution) ``` -------------------------------- ### Python Power Cone Example Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/examples/py/example_powcone.md This snippet defines and solves an optimization problem using Clarabel.jl with a Power Cone constraint in Python. Ensure Clarabel.jl is installed and accessible. ```python using Clarabel # Define the problem # Maximize -x subject to: # x[0] >= 0 # x[1] >= 0 # x[0]^2 * x[1] <= 1 (Power Cone constraint) # Objective function: -x c = [-1.0, 0.0] # Constraints # Power cone constraint: x[0]^2 * x[1] <= 1 # This is equivalent to (x[0], x[1]) in the power cone K_2^1 # Clarabel uses the form A*x + b in K # For the power cone K_p^k, the constraint is x_1^p * ... * x_k <= x_{k+1} # Here, p=2, k=1, so x_0^2 * x_1 <= x_2. We want x_0^2 * x_1 <= 1. # So we need to introduce a slack variable x_2, and set x_2 = 1. # The constraint becomes x_0^2 * x_1 - x_2 <= 0. # The matrix A for this constraint is: # [[0.0, 0.0, -1.0]] # The vector b for this constraint is: # [0.0] # The cone type is Clarabel.PowerCone(2, 1) A = zeros(3, 3) b = zeros(3) cone = Clarabel.PowerCone(2, 1) A[1, 1] = 1.0 # x[0] A[2, 2] = 1.0 # x[1] A[3, 3] = -1.0 # -x[2] b[3] = 0.0 # <= 0 # We need to add the constraint x[2] = 1. This is an equality constraint. # Clarabel supports equality constraints of the form E*x = f. # Here, E = [0.0, 0.0, 1.0] and f = [1.0]. E = zeros(1, 3) E[1, 3] = 1.0 f = [1.0] # Combine constraints # The problem is now: # Maximize -x[0] - x[1] subject to: # x[0] >= 0, x[1] >= 0, x[2] = 1 # x[0]^2 * x[1] <= x[2] # Clarabel expects minimization, so we minimize c'x # The constraints are Ax + s = b, s in K # We have the power cone constraint: x[0]^2 * x[1] - x[2] <= 0 # Let s_1 = x[0]^2 * x[1] - x[2]. We need s_1 <= 0 and s_1 in PowerCone(2,1). # This is not directly supported by Clarabel's standard form. # Clarabel's standard form is Ax + b in K. # For PowerCone(p, k), the constraint is x_1^p * ... * x_k <= x_{k+1}. # Let's reformulate. # We want to maximize -x[0] subject to x[0] >= 0, x[1] >= 0, x[0]^2 * x[1] <= 1. # Let y = x[0], z = x[1]. Maximize -y subject to y >= 0, z >= 0, y^2 * z <= 1. # This is equivalent to minimizing y subject to y >= 0, z >= 0, y^2 * z <= 1. # Let's use the formulation from the Clarabel documentation for power cone: # Minimize c'x subject to Ax + b in K # Example: Minimize -x_1 subject to x_1 >= 0, x_2 >= 0, x_1^2 * x_2 <= 1 c = [-1.0, 0.0] # Minimize x_1 # The constraint x_1^2 * x_2 <= 1 can be written as: # Introduce slack variable s_3. x_1^2 * x_2 - s_3 <= 0. # This requires a special cone type. # Clarabel's PowerCone(p, k) is defined as x_1^p * ... * x_k <= x_{k+1}. # So we need PowerCone(2, 1). # The constraint is x_1^2 * x_2 <= x_3. We want x_1^2 * x_2 <= 1. # So we set x_3 = 1. # The constraint becomes x_1^2 * x_2 - x_3 <= 0. # Let the variables be [x_1, x_2, x_3]. # Objective: Minimize x_1 (since we want to maximize -x_1). # c = [1.0, 0.0, 0.0] c = [1.0, 0.0, 0.0] # Constraint: x_1^2 * x_2 - x_3 <= 0 # This fits the form Ax + b in K. # A = [[0, 0, -1]] # b = [0] # Cone = PowerCone(2, 1) A = zeros(3, 3) b = zeros(3) cone = Clarabel.PowerCone(2, 1) A[1, 3] = -1.0 # -x_3 b[1] = 0.0 # We also need the constraints x_1 >= 0, x_2 >= 0, and x_3 = 1. # The x_1 >= 0 and x_2 >= 0 are handled by the cone definition if we use a different structure. # Let's use the direct definition from Clarabel examples: # Maximize -x[0] subject to x[0] >= 0, x[1] >= 0, x[0]^2 * x[1] <= 1 # Minimize x[0] subject to x[0] >= 0, x[1] >= 0, x[0]^2 * x[1] <= 1 c = [1.0, 0.0] # The power cone constraint x[0]^2 * x[1] <= 1 requires a slack variable. # Let the variables be [x_0, x_1, s_0]. # We want x_0^2 * x_1 <= 1. # This is not directly representable in the standard form Ax + b in K. # Let's use the example from Clarabel.jl documentation directly: # Maximize -x[0] subject to x[0] >= 0, x[1] >= 0, x[0]^2 * x[1] <= 1 # This is equivalent to minimizing x[0] subject to x[0] >= 0, x[1] >= 0, x[0]^2 * x[1] <= 1. # Let the variables be [x_0, x_1, s_2]. # Objective: Minimize x_0. # c = [1.0, 0.0, 0.0] c = [1.0, 0.0, 0.0] # Constraint: x_0^2 * x_1 <= s_2. We want x_0^2 * x_1 <= 1, so s_2 = 1. # The constraint is x_0^2 * x_1 - s_2 <= 0. # This fits the form Ax + b in K. # A = [[0, 0, -1]] # b = [0] # Cone = PowerCone(2, 1) A = zeros(3, 3) b = zeros(3) cone = Clarabel.PowerCone(2, 1) A[1, 3] = -1.0 # -s_2 b[1] = 0.0 # We also need x_0 >= 0 and x_1 >= 0. These are implicit in the power cone definition if structured correctly. # Let's use the structure from Clarabel.jl examples for power cone: # Minimize c'x subject to Ax + b in K # Example: Minimize -x_1 subject to x_1 >= 0, x_2 >= 0, x_1^2 * x_2 <= 1 # Variables: [x_1, x_2, s_3] # Objective: Minimize x_1 c = [1.0, 0.0, 0.0] # Constraint: x_1^2 * x_2 <= s_3. We want x_1^2 * x_2 <= 1, so s_3 = 1. # The constraint is x_1^2 * x_2 - s_3 <= 0. # This fits the form Ax + b in K. # A = [[0, 0, -1]] # b = [0] # Cone = PowerCone(2, 1) A = zeros(3, 3) b = zeros(3) cone = Clarabel.PowerCone(2, 1) A[1, 3] = -1.0 # -s_3 b[1] = 0.0 # We need to add the constraint s_3 = 1. # This is an equality constraint E*x = f. # E = [0, 0, 1] # f = [1] E = zeros(1, 3) E[1, 3] = 1.0 f = [1.0] # Combine the constraints into a single problem structure for Clarabel. # Clarabel.solve(settings, P, q, A, b, cones, E, f) # Here, P=0, q=c. settings = Clarabel.Settings() solution = Clarabel.solve(settings, zeros(3, 3), c, A, b, [cone], E, f) println(solution.status) println(solution.x) ``` -------------------------------- ### Install Maturin Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/installation_py.md Install Maturin, a tool for building Rust-based Python extensions, within your activated virtual environment. ```python pip install maturin ``` -------------------------------- ### Rust SDP Example Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/examples/rs/example_sdp.md This snippet shows how to define and solve a Semidefinite Programming problem in Rust using Clarabel.rs. Ensure you have the Clarabel.rs library added to your Cargo.toml. ```rust using Documenter Documenter.md_include( source = "examples/rs/example_sdp/src/main.rs", language = :rust) ``` -------------------------------- ### Rust SOCP Example Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/examples/rs/example_socp.md This snippet shows how to define and solve a Second-Order Cone Program (SOCP) using Clarabel.rs. Ensure you have the Clarabel.rs library added to your Cargo.toml. ```rust use clarabel::solver::{ClarabelSolver, SolutionStatus}; use clarabel::model::{SOCPModel, QuadraticModel}; use clarabel::algebra::{CBLAS, MatrixF64, VectorF64}; fn main() { // Example SOCP problem: minimize ||x||_2 subject to Ax = b, x in K // where K is a second-order cone. // For simplicity, we'll use a small, manually constructed problem. // Define the quadratic part (identity matrix for ||x||_2^2) let P = MatrixF64::identity(3); let q = VectorF64::new(vec![0.0, 0.0, 0.0]); // Define the linear constraints (Ax = b) // Let A = [[1.0, 1.0, 1.0]] and b = [1.0] let A = MatrixF64::new(1, 3, vec![1.0, 1.0, 1.0]); let b = VectorF64::new(vec![1.0]); // Define the cone constraints. We'll use a single second-order cone. // The cone is defined by the indices of the variables that belong to it. // For ||x||_2, the cone is R x R^n, so we need n+1 dimensions. // Let's say x_1, x_2, x_3 are in the cone. We need a cone of dimension 3+1=4. // The first element is the 'r' part, the rest are 'z' parts. // Let's use a simple cone: x_1 >= sqrt(x_2^2 + x_3^2) // This corresponds to a cone of dimension 3. // Clarabel uses a specific indexing for cones. For a single SOC of dim k, // it occupies indices 1 to k. So, if we want x_1, x_2, x_3 in the cone, // we need a cone of dimension 3. let mut cones = vec![clarabel::algebra::ConeT{idx: 0, dim: 3}]; // Create the SOCP model let mut model = SOCPModel::new(P, q, A, b, cones); // Initialize the solver let mut solver = ClarabelSolver::new(model); // Solve the problem let results = solver.solve(); // Check the solution status println!("Solution status: {:?}", results.status); if results.status == SolutionStatus::Solved { println!("Optimal solution x: {:?}", results.x); println!("Optimal objective value: {}", results.obj_val); } } ``` -------------------------------- ### Install Clarabel.jl Stable Release Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/julia/installation_jl.md Use this command to install the latest stable version of Clarabel.jl from the Julia package registry. Enter Pkg REPL mode by typing ']' in the Julia REPL. ```julia pkg> add Clarabel ``` -------------------------------- ### Build Clarabel Wheel File Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/installation_py.md Compile the Rust solver and generate a Python wheel (.whl) file for installation outside the virtual environment. Ensure you are inside the activated virtual environment. ```bash maturin build -i python --release --features python ``` -------------------------------- ### Build and Install Clarabel from Source (Develop Mode) Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/installation_py.md Compile the Rust solver with Python bindings enabled using Maturin in 'develop' mode for immediate use within the virtual environment. ```bash maturin develop --release ``` -------------------------------- ### Rust Example: Exponential Cone Formulation Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/examples/rs/example_expcone.md This snippet shows how to define a problem with an exponential cone constraint in Rust using Clarabel.rs. Ensure Clarabel.rs is added as a dependency in your Cargo.toml. ```rust use clarabel::solver::{ClarabelSolver, SolutionStatus}; use clarabel::model::{Model, Minimizer, Constraint, Cone, ExpCone}; use clarabel::algebra::{Matrix, SparseMatrix, SymmetricKKTMatrix, DenseVector}; use nalgebra::{sp, CSC, Symmetric3x3}; fn main() { // Problem: minimize -log(x) - log(y) - log(z) // subject to x + y + z = 1 // x, y, z > 0 (implicit in ExpCone) // Objective function: -log(x) - log(y) - log(z) // Gradient: [-1/x, -1/y, -1/z] // Hessian: diag([1/x^2, 1/y^2, 1/z^2]) let c = DenseVector::new(vec![-1.0, -1.0, -1.0]); // Constraint: x + y + z = 1 // A matrix for the equality constraint let mut A_data = vec![1.0, 1.0, 1.0]; let A_rows = vec![0, 0, 0]; let A_cols = vec![0, 1, 2]; let A = SparseMatrix::new(1, 3, A_rows, A_cols, A_data); // b vector for the equality constraint let b = DenseVector::new(vec![1.0]); // Exponential cone constraint: (x, y, z) in ExpCone // The ExpCone requires 3 variables. let mut expcone_indices = vec![0, 1, 2]; let expcone = ExpCone::new(3); // Combine constraints: equality constraint and exponential cone let mut constraints = vec![Constraint::NonNeg]; // Default non-negativity constraints[0] = Constraint::new_eq(A, b); constraints.push(Constraint::new_exp(expcone_indices)); // Create the model let mut model = Model::new(Minimizer::Minimize, c, constraints); // Solve the problem let mut solver = ClarabelSolver::new(model); let solution = solver.solve(); // Check the solution status if solution.status == SolutionStatus::Solved { println!("Problem solved successfully!"); println!("Optimal x: {:.4}", solution.x[0]); println!("Optimal y: {:.4}", solution.x[1]); println!("Optimal z: {:.4}", solution.x[2]); } else { println!("Problem could not be solved. Status: {:?}", solution.status); } } ``` -------------------------------- ### Python Exponential Cone Formulation Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/examples/py/example_expcone.md This snippet sets up a problem with an exponential cone constraint using the Clarabel Python interface. It requires the 'clarabel' package to be installed. ```python import clarabel import numpy as np # Define problem parameters # Minimize -y subject to exp(x) <= y # This is equivalent to (x, y, 1) in the exponential cone K_exp c = np.array([0.0, -1.0]) A = np.array([ [1.0, 1.0], [0.0, 0.0] ]) b = np.array([0.0, 1.0]) # Define the constraints: one exponential cone constraint # The exponential cone is defined as K_exp = {(x,y,z) : y*exp(x/y) <= z, y > 0} # In Clarabel, we use the form (x, y, z) where y*exp(x/y) <= z # For our problem, we have x and y from the objective, and we need a '1' for the cone # So, we map our variables (x, y) to the cone as (x, y, 1) # The constraint is exp(x) <= y, which is equivalent to x <= log(y) # This can be rewritten as x - log(y) <= 0 # For the exponential cone, we need to express it in the form Ax + b in K_exp # The standard form for the exponential cone is (x, y, z) such that y*exp(x/y) <= z, y > 0. # Our constraint exp(x) <= y can be written as y*exp(x/y) >= 1 if we set z=1 and x=x, y=y. # This is not quite right. Let's use the standard formulation. # The exponential cone constraint is typically written as: # exp(x_i) <= y_i for i=1..k # This is equivalent to (x_i, y_i, 1) being in the exponential cone. # In Clarabel's formulation, we have Ax + b in K_exp. # Let our variables be v = [x, y]. We want to enforce exp(x) <= y. # This is equivalent to (x, y, 1) in the exponential cone. # So, we need to map our problem variables to the cone structure. # Let the cone constraint be represented by a matrix S and a vector t such that Sv + t is in K_exp. # For exp(x) <= y, we can write this as y*exp(x/y) >= 1 if y > 0. # This is not directly fitting the standard form. # Let's use the definition: y*exp(x/y) <= z, y > 0. # We want exp(x) <= y. This means we need to transform variables. # Let u = x, v = y. We want exp(u) <= v. # This is equivalent to (u, v, 1) in the exponential cone if we consider the cone definition. # The cone constraint in Clarabel is defined by a matrix `cone_A` and vector `cone_b`. # For the exponential cone, we use `clarabel.ExpConeT(k)` where k is the dimension. # Here, our cone involves two variables (x, y) and a constant '1'. So, k=3. # Let's reformulate the constraint exp(x) <= y. # This is equivalent to (x, y, 1) in the exponential cone. # The constraint in Clarabel is of the form Ax + b in K. # We need to map our variables [x, y] to the cone's structure. # Let the cone variables be (x_cone, y_cone, z_cone). # We want to enforce exp(x) <= y. # This can be written as y*exp(x/y) >= 1 if y > 0. # This is not the standard form. # The standard form is y*exp(x/y) <= z, y > 0. # Let's use the direct mapping: (x, y, 1) in K_exp. # This means our constraint matrix A and vector b should produce these values. # Let the cone variables be denoted by `cone_vars`. # We want `cone_vars` to be in K_exp. # The constraint is exp(x) <= y. # This is equivalent to (x, y, 1) in the exponential cone. # So, we need A @ [x, y] + b to result in a vector that, when interpreted for the cone, satisfies the condition. # Let's use the Clarabel API directly for the exponential cone. # Clarabel uses a specific structure for cones. # For an exponential cone of dimension k, we use clarabel.ExpConeT(k). # In our case, we have exp(x) <= y, which involves two original variables and a constant 1. # So, the dimension of the cone is 3. # Let's define the problem using Clarabel's structure. # We want to minimize c' * v subject to A * v + b in K. # Our variables are v = [x, y]. # c = [0, -1]. # We want exp(x) <= y. # This is equivalent to (x, y, 1) in the exponential cone. # So, we need A @ v + b to be a vector representing (x, y, 1) for the cone. # Let A be a matrix and b be a vector such that A @ [x, y] + b = [x, y, 1]. # This means: # A = [[1, 0], # [0, 1], # [0, 0]] # b = [0, # 0, # 1] # Let's verify the dimensions. # c is 2x1. # A is 3x2. # b is 3x1. # The cone is ExpConeT(3). # Let's redefine A and b according to this. c = np.array([0.0, -1.0]) A = np.array([ [1.0, 0.0], [0.0, 1.0], [0.0, 0.0] ]) b = np.array([0.0, 0.0, 1.0]) # Define the cone type cones = [clarabel.ExpConeT(3)] # Create the solver solver = clarabel.ClarabelSolver(A, c, b, cones) # Solve the problem solution = solver.solve() # Print the solution print(f"Solution: {solution}") print(f"Primal variables (x, y): {solution.x}") print(f"Dual variables: {solution.y}") ``` -------------------------------- ### Solve Quadratic Programming Problem in Python Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/examples/py/example_qp.md This snippet defines and solves a sample quadratic programming problem using the Clarabel solver's Python interface. Ensure Clarabel.jl is installed and accessible from Python. ```python using Clarabel using LinearAlgebra # Define the quadratic programming problem P = sparse([4.0 1.0; 1.0 2.0]) q = [-1.0; -2.0] A = sparse([1.0 1.0; -1.0 0.0; 0.0 -1.0]) b = [1.0; 0.0; 0.0] # Create the Clarabel solver solver = Clarabel.Solver(P, q, A, b) # Solve the problem status = Clarabel.solve!(solver) # Print the solution println(solver.solution) ``` -------------------------------- ### Set up Python Virtual Environment Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/installation_py.md Create and activate a Python virtual environment for building the solver from source. This isolates build dependencies. ```bash python3 -m venv .env source .env/bin/activate ``` -------------------------------- ### Build Clarabel.rs Directly from Source Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/rust/installation_rs.md Clone the repository, navigate into the directory, and build the release version using Cargo. ```bash git clone https://github.com/oxfordcontrol/Clarabel.rs cd Clarabel.rs cargo build --release ``` -------------------------------- ### Create Solver Settings Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/julia/getting_started_jl.md Instantiate a Settings object to configure solver parameters like verbosity and time limits. ```julia settings = Clarabel.Settings() ``` -------------------------------- ### Initialize Solver Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/julia/getting_started_jl.md Create an empty Solver object before setting up the problem data. ```julia solver = Clarabel.Solver() ``` -------------------------------- ### Printing Solver Solution Details Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/rust/getting_started_rs.md Inspect the solution status, primal and dual solutions, and primal slacks after the solver terminates. Ensure the solver has been run before accessing these fields. ```rust println!("Solution status = {:?}", solver.solution.status); println!("Primal solution = {:?}", solver.solution.x); println!("Dual solution = {:?}", solver.solution.z); println!("Primal slacks = {:?}", solver.solution.s); ``` -------------------------------- ### Create Custom Solver Settings Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/rust/getting_started_rs.md Configure custom solver settings, such as disabling verbose output and setting a time limit, using the DefaultSettingsBuilder. ```rust let settings = DefaultSettingsBuilder::default() .verbose(false) .time_limit(1.) .build() .unwrap(); ``` -------------------------------- ### Create Default Solver Settings Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/rust/getting_started_rs.md Instantiate the default solver settings for Clarabel.rs. ```rust let settings = DefaultSettings::default(); ``` -------------------------------- ### Initialize JuMP Model with Clarabel Solver Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/julia/jump.md Load the Clarabel module and configure it as the solver backend when initializing a JuMP model. ```julia model = JuMP.Model(Clarabel.Optimizer) ``` -------------------------------- ### Configure Direct Solve Method Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/julia/linear_solvers.md Set the direct linear solver method using the `direct_solve_method` field in `Solver.Settings`. QDLDL is the default solver. ```julia settings = Solver.Settings(direct_solve_method = :qdldl) ``` -------------------------------- ### Initialize and Solve the Clarabel.rs Model Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/rust/getting_started_rs.md Create a new Clarabel.rs solver instance with the problem data and settings, then call the solve method. ```rust let mut solver = DefaultSolver::new(&P, &q, &A, &b, &cones, settings); solver.solve(); ``` -------------------------------- ### Import Required Packages Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/julia/getting_started_jl.md Import the Clarabel solver and necessary packages like SparseArrays and LinearAlgebra to work with matrices and vectors. ```julia using Clarabel, SparseArrays, LinearAlgebra ``` -------------------------------- ### Define Cost Data (P and q) Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/getting_started_py.md Define the quadratic cost matrix P (in CSC format) and the linear cost vector q for the objective function. P should be upper triangular. ```python P = sparse.csc_matrix( [[ 3., 1., -1.], [ 1., 4., 2.], [-1., 2., 5.]]) P = sparse.triu(P).tocsc() q = np.array([1., 2., -3.]) ``` -------------------------------- ### Clarabel Settings Documentation Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/api_settings.md This snippet displays the documentation for the Clarabel.Settings type, outlining the available configuration options for the solver. ```julia ```@docs Clarabel.Settings ``` ``` -------------------------------- ### Initialize Default Solver Settings Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/getting_started_py.md Create a default settings object for the Clarabel solver. This uses all default values provided by the solver. ```rust settings = clarabel.DefaultSettings() ``` -------------------------------- ### Import Required Packages Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/getting_started_py.md Import the Clarabel solver and necessary libraries like NumPy and SciPy for numerical operations and sparse matrix handling. ```python import clarabel import numpy as np from scipy import sparse ``` -------------------------------- ### Create Zero Matrix P Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/rust/getting_started_rs.md Use CscMatrix::spalloc to create a sparse zero matrix, useful for problems like LPs. ```rust let P = CscMatrix::spalloc((2,2),0); ``` -------------------------------- ### Define Constraint Data (Aeq, beq, Aineq, bineq, Asoc, bsoc) Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/getting_started_py.md Define the matrices and vectors for equality constraints (Aeq, beq), inequality constraints (Aineq, bineq), and Second-Order Cone (SOC) constraints (Asoc, bsoc). ```python # equality constraint Aeq = sparse.csc_matrix([1.,1.,-1]) bek = np.array([1.]) # equality constraint Aineq = sparse.csc_matrix( [[0., 1., 0.], [0., 0., 1.]]) bineq = np.array([2.,2.]) # SOC constraint Asoc = -sparse.identity(3) bso = np.array([0.,0.,0.]) ``` -------------------------------- ### Define Objective Function Data (P and q) Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/rust/getting_started_rs.md Define the quadratic cost matrix P (in CscMatrix format) and linear cost vector q for the objective function. ```rust let P = CscMatrix::new( 3, // m 3, // n vec![0, 1, 3, 6], // colptr vec![0, 0, 1, 0, 1, 2], // rowval vec![3., 1., 4., -1., 2., 5.], // nzval ); let q = vec![1., 2., -3.]; ``` -------------------------------- ### Import Clarabel.rs Modules Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/rust/getting_started_rs.md Bring the necessary Clarabel.rs solver and algebra modules into scope in your Rust source files. ```rust use clarabel::algebra::*; use clarabel::solver::*; ``` -------------------------------- ### Define Constraint Matrices (Aeq, Aineq, Asoc, A) and Vector (b) Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/rust/getting_started_rs.md Define the equality (Aeq), inequality (Aineq), and second-order cone (Asoc) constraint matrices, combine them into A, and define the constraint bounds vector b. ```rust let Aeq = CscMatrix::new( 1, // m 3, // n vec![0, 1, 2, 3], // colptr vec![0, 0, 0], // rowval vec![1., 1., -1.], // nzval ); let Aineq = CscMatrix::new( 2, // m 3, // n vec![0, 0, 1, 2], // colptr vec![0, 1], // rowval vec![1., 1.], // nzval ); let mut Asoc = CscMatrix::identity(3); Asoc.negate(); let A = CscMatrix::vcat(&Aeq, &Aineq); let A = CscMatrix::vcat(&A, &Asoc); let b = vec![1., 2., 2., 0., 0., 0.]; // optional correctness check assert!(A.check_format().is_ok()); ``` -------------------------------- ### Clarabel Solver and Main API Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/api_jl.md Provides access to the core solver functionality, including initialization, solving, and file operations. ```APIDOC ## Clarabel.Solver ### Description Represents the main solver structure in Clarabel. ## Clarabel.setup! ### Description Sets up the Clarabel solver with the given problem data. ## Clarabel.solve! ### Description Solves the optimization problem using the Clarabel solver. ## Clarabel.save_to_file ### Description Saves the current state of the solver to a file. ## Clarabel.load_from_file ### Description Loads the state of the solver from a file. ``` -------------------------------- ### Create and Solve Clarabel Problem Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/getting_started_py.md Instantiate the Clarabel solver with the defined problem data (P, q, A, b, cones) and settings, then call the solve method to find the solution. ```python solver = clarabel.DefaultSolver(P,q,A,b,cones,settings) solution = solver.solve() solution.x # primal solution solution.z # dual solution solution.s # primal slacks ``` -------------------------------- ### Clone Clarabel Repository Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/python/installation_py.md Download the Clarabel source code from GitHub to build from source. ```bash git clone https://github.com/oxfordcontrol/Clarabel.rs cd Clarabel.rs ``` -------------------------------- ### Clarabel for Decomposable SDPs Citation Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/citing.md Cite this paper when using Clarabel to solve Semidefinite Programs (SDPs) with decomposable structure. A preprint is available. ```bibtex @InProceedings{Garstka_2020, author = {Michael Garstka and Mark Cannon and Paul Goulart}, title = {A clique graph based merging strategy for decomposable {SDPs}}, year = {2020}, note = {21th IFAC World Congress}, number = {2}, pages = {7355-7361}, volume = {53}, doi = {10.1016/j.ifacol.2020.12.1255}, issn = {2405-8963}, journal = {IFAC-PapersOnLine} } ``` -------------------------------- ### Solve the Optimization Problem Source: https://github.com/oxfordcontrol/clarabeldocs/blob/main/docs/src/julia/getting_started_jl.md Execute the optimization algorithm using the solve! function and access the solution components. ```julia solution = Clarabel.solve!(solver) solution.x # primal solution solution.z # dual solution solution.s # slacks ```