-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbasic_expcone.cpp
More file actions
107 lines (90 loc) · 2.9 KB
/
basic_expcone.cpp
File metadata and controls
107 lines (90 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <clarabel.hpp>
#include <Eigen/Eigen>
#include <cmath>
#include <gtest/gtest.h>
#include <iostream>
#include <limits>
#include <vector>
using namespace std;
using namespace clarabel;
using namespace Eigen;
class BasicExpConeTest : public testing::Test
{
// produces data for the following exponential cone problem
// max x
// s.t. y * exp(x / y) <= z
// y == 1, z == exp(5)
protected:
SparseMatrix<double> P, A;
Vector<double, 3> c = {-1., 0., 0.};
Vector<double, 5> b = {0., 0., 0., 1.0, exp(5.)};
vector<SupportedConeT<double>> cones = {
ExponentialConeT<double>(),
ZeroConeT<double>(2),
};
DefaultSettings<double> settings = DefaultSettings<double>::default_settings();
BasicExpConeTest()
{
P = MatrixXd::Zero(3, 3).sparseView();
P.makeCompressed();
MatrixXd A1 = -MatrixXd::Identity(3,3);
MatrixXd A2(2,3);
A2 << 0, 1, 0, // y = 1
0, 0, 1; // z = exp(5)
MatrixXd A_dense = MatrixXd::Zero(5,3);
A_dense << A1, A2;
A = A_dense.sparseView();
A.makeCompressed();
}
};
TEST_F(BasicExpConeTest, Feasible)
{
// solve the following exponential cone problem
// max x
// s.t. y * exp(x / y) <= z
// y == 1, z == exp(5)
//
// This is just the default problem data above
DefaultSolver<double> solver(P, c, A, b, cones, settings);
solver.solve();
DefaultSolution<double> solution = solver.solution();
ASSERT_EQ(solution.status, SolverStatus::Solved);
// Check the solution
Vector3d ref_solution{ 5.0, 1.0, exp(5.0) };
ASSERT_EQ(solution.x.size(), 3);
ASSERT_TRUE(solution.x.isApprox(ref_solution, 1e-6));
double ref_obj = -5.0;
ASSERT_NEAR(solution.obj_val, ref_obj, 1e-6);
ASSERT_NEAR(solution.obj_val_dual, ref_obj, 1e-6);
}
TEST_F(BasicExpConeTest, PrimalInfeasible)
{
// solve the following exponential cone problem
// max x
// s.t. y * exp(x / y) <= z
// y == 1, z == -1
//
// Same as default, but last element of b is different
b[4] = -1.;
DefaultSolver<double> solver(P, c, A, b, cones, settings);
solver.solve();
DefaultSolution<double> solution = solver.solution();
ASSERT_EQ(solution.status, SolverStatus::PrimalInfeasible);
}
TEST_F(BasicExpConeTest, DualInfeasible)
{
// solve the following exponential cone problem
// max x
// s.t. y * exp(x / y) <= z
//
// Same as default, but no equality constraint
Vector<double, 3> b = {0., 0., 0.};
vector<SupportedConeT<double>> cones = {ExponentialConeT<double>()};
MatrixXd A1 = -MatrixXd::Identity(3,3);
SparseMatrix<double> A = A1.sparseView();
A.makeCompressed();
DefaultSolver<double> solver(P, c, A, b, cones, settings);
solver.solve();
DefaultSolution<double> solution = solver.solution();
ASSERT_EQ(solution.status, SolverStatus::DualInfeasible);
}