-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds optional JSON file read/write support * adds optional faer-rs support
- Loading branch information
1 parent
d8adc52
commit c3a7b1e
Showing
21 changed files
with
402 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule Clarabel.rs
updated
41 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
project(clarabel_examples VERSION ${CLARABEL_PROJECT_VERSION}) | ||
add_compile_definitions("EXAMPLES_ROOT_DIR=${CMAKE_CURRENT_SOURCE_DIR}") | ||
|
||
add_subdirectory(c) | ||
add_subdirectory(cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ set(C_EXAMPLES | |
example_qp_f64 | ||
example_socp | ||
example_sdp | ||
example_json | ||
) | ||
|
||
# Compile utils.c as a library | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// #define FEATURE_SDP | ||
#include "utils.h" | ||
#include <string.h> | ||
#include <Clarabel.h> | ||
#include <math.h> | ||
#include <stdio.h> | ||
|
||
#define STRING(string) #string | ||
#define TO_STRING(string) STRING(string) | ||
|
||
int main(void) | ||
{ | ||
#ifndef FEATURE_SERDE | ||
printf("This example requires JSON serde support.\n"); | ||
return 1; | ||
#else | ||
|
||
char filename[1024]; | ||
strcpy(filename, TO_STRING(EXAMPLES_ROOT_DIR)); | ||
strcat(filename, "/data/hs35.json"); | ||
|
||
ClarabelDefaultSolver* solver = clarabel_DefaultSolver_read_from_file(filename); | ||
clarabel_DefaultSolver_solve(solver); | ||
|
||
// write it back to a file | ||
// char filename_out[1024]; | ||
// strcpy(filename_out, TO_STRING(EXAMPLES_ROOT_DIR)); | ||
// strcat(filename_out, "/data/output_c.json"); | ||
// clarabel_DefaultSolver_write_to_file(solver, filename_out); | ||
|
||
clarabel_DefaultSolver_free(solver); | ||
return 0; | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "utils.h" | ||
|
||
#include <Clarabel> | ||
#include <Eigen/Eigen> | ||
#include <vector> | ||
|
||
using namespace clarabel; | ||
using namespace std; | ||
using namespace Eigen; | ||
|
||
// NB: this example requires that the solver be built with -DCLARABEL_FEATURE_FAER_SPARSE | ||
|
||
int main(void) | ||
{ | ||
|
||
#ifndef FEATURE_FAER_SPARSE | ||
|
||
printf("This example requires faer-rs support.\n"); | ||
return 1; | ||
|
||
#else | ||
|
||
|
||
/* From dense matrix: | ||
* [[6., 0.], | ||
* [0., 4.]] | ||
*/ | ||
MatrixXd P_dense(2, 2); | ||
P_dense << | ||
6., 0., | ||
0., 4.; | ||
|
||
SparseMatrix<double> P = P_dense.sparseView(); | ||
P.makeCompressed(); | ||
|
||
Vector<double, 2> q = { -1., -4. }; | ||
|
||
MatrixXd A_dense(5, 2); | ||
A_dense << | ||
1., -2., // <-- LHS of equality constraint (lower bound) | ||
1., 0., // <-- LHS of inequality constraint (upper bound) | ||
0., 1., // <-- LHS of inequality constraint (upper bound) | ||
-1., 0., // <-- LHS of inequality constraint (lower bound) | ||
0., -1.; // <-- LHS of inequality constraint (lower bound) | ||
|
||
SparseMatrix<double> A = A_dense.sparseView(); | ||
A.makeCompressed(); | ||
|
||
Vector<double, 5> b = { 0., 1., 1., 1., 1. }; | ||
|
||
vector<SupportedConeT<double>> cones | ||
{ | ||
ZeroConeT<double>(1), | ||
NonnegativeConeT<double>(4), | ||
}; | ||
|
||
// Settings | ||
DefaultSettings<double> settings = DefaultSettings<double>::default_settings(); | ||
settings.direct_solve_method = ClarabelDirectSolveMethods::FAER; | ||
|
||
// Build solver | ||
DefaultSolver<double> solver(P, q, A, b, cones, settings); | ||
|
||
// Solve | ||
solver.solve(); | ||
|
||
// Get solution | ||
DefaultSolution<double> solution = solver.solution(); | ||
utils::print_solution(solution); | ||
|
||
return 0; | ||
|
||
#endif // FEATURE_FAER_SPARSE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// #define FEATURE_SDP | ||
#include "utils.h" | ||
#include <cstdio> | ||
#include <Clarabel> | ||
|
||
using namespace clarabel; | ||
using namespace std; | ||
|
||
#define STRING(string) #string | ||
#define TO_STRING(string) STRING(string) | ||
|
||
int main(void) | ||
{ | ||
#ifndef FEATURE_SERDE | ||
|
||
printf("This example requires JSON serde support.\n"); | ||
return 1; | ||
|
||
#else | ||
|
||
std::string rootdir = TO_STRING(EXAMPLES_ROOT_DIR); | ||
std::string filepath = "/data/hs35.json"; | ||
std::string filename = rootdir + filepath; | ||
cout << "Read from file: " << filename << endl; | ||
|
||
DefaultSolver<double> solver = DefaultSolver<double>::read_from_file(filename); | ||
solver.solve(); | ||
|
||
// write it back to a file | ||
// std::string outpath = "/data/output.json"; | ||
// std::string filename_out = rootdir + outpath; | ||
// solver.write_to_file(filename_out); | ||
|
||
return 0; | ||
|
||
#endif // FEATURE_SERDE | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"P":{"m":3,"n":3,"colptr":[0,1,3,5],"rowval":[0,0,1,0,2],"nzval":[4.000000000000001,2.0000000000000004,4.000000000000001,2.0,2.0]},"q":[-8.0,-6.0,-4.0],"A":{"m":4,"n":3,"colptr":[0,2,4,6],"rowval":[0,1,0,2,0,3],"nzval":[1.0,-1.0,1.0,-1.0,2.0,-1.0]},"b":[3.0,0.0,0.0,0.0],"cones":[{"NonnegativeConeT":4}],"settings":{"max_iter":200,"time_limit":1.7976931348623157e308,"verbose":true,"max_step_fraction":0.99,"tol_gap_abs":1e-8,"tol_gap_rel":1e-8,"tol_feas":1e-8,"tol_infeas_abs":1e-8,"tol_infeas_rel":1e-8,"tol_ktratio":1e-6,"reduced_tol_gap_abs":0.00005,"reduced_tol_gap_rel":0.00005,"reduced_tol_feas":0.0001,"reduced_tol_infeas_abs":0.00005,"reduced_tol_infeas_rel":0.00005,"reduced_tol_ktratio":0.0001,"equilibrate_enable":true,"equilibrate_max_iter":10,"equilibrate_min_scaling":0.0001,"equilibrate_max_scaling":10000.0,"linesearch_backtrack_step":0.8,"min_switch_step_length":0.1,"min_terminate_step_length":0.0001,"direct_kkt_solver":true,"direct_solve_method":"qdldl","static_regularization_enable":true,"static_regularization_constant":1e-8,"static_regularization_proportional":4.930380657631324e-32,"dynamic_regularization_enable":true,"dynamic_regularization_eps":1e-13,"dynamic_regularization_delta":2e-7,"iterative_refinement_enable":true,"iterative_refinement_reltol":1e-13,"iterative_refinement_abstol":1e-12,"iterative_refinement_max_iter":10,"iterative_refinement_stop_ratio":5.0,"presolve_enable":true,"chordal_decomposition_enable":true,"chordal_decomposition_merge_method":"clique_graph","chordal_decomposition_compact":true,"chordal_decomposition_complete_dual":true}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.