cvxrust 0.1.0

A Rust implementation of Disciplined Convex Programming
Documentation
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Tests for v1.0 atoms: exponential cone, power cone, and new affine atoms.

use cvxrust::prelude::*;

const TOL: f64 = 1e-4;

// ============================================================================
// Exponential Cone Atoms
// ============================================================================

#[test]
fn test_exp_basic() {
    // minimize exp(x) s.t. x >= 0
    // Solution: x = 0, exp(0) = 1
    let x = variable(());
    let obj = exp(&x);

    let solution = Problem::minimize(obj)
        .subject_to([x.ge(constant(0.0))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    let val = solution.value.unwrap();
    assert!((val - 1.0).abs() < TOL, "Expected 1.0, got {}", val);

    let x_val = solution.value(&x);
    assert!(x_val.abs() < TOL, "Expected x=0, got {}", x_val);
}

#[test]
fn test_exp_with_constraint() {
    // maximize x s.t. exp(x) <= 5
    // exp(x) <= 5 means x <= log(5) ≈ 1.609
    // Solution: x = log(5)
    let x = variable(());
    let solution = Problem::maximize(x.clone())
        .subject_to([exp(&x).le(constant(5.0))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    let x_val = solution.value(&x);
    let expected = 5.0_f64.ln();
    assert!(
        (x_val - expected).abs() < TOL,
        "Expected {}, got {}",
        expected,
        x_val
    );
}

#[test]
fn test_log_basic() {
    // maximize log(x) s.t. x <= 2
    // Solution: x = 2, log(2) ≈ 0.693
    let x = variable(());
    let obj = log(&x);

    let solution = Problem::maximize(obj)
        .subject_to([x.le(constant(2.0)), x.ge(constant(0.01))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    let val = solution.value.unwrap();
    let expected = 2.0_f64.ln();
    assert!(
        (val - expected).abs() < TOL,
        "Expected {}, got {}",
        expected,
        val
    );
}

#[test]
fn test_log_concave() {
    // minimize -log(x) s.t. 0.1 <= x <= 1
    // -log(x) is convex and decreasing, so minimum is at x = 1
    // Solution: x = 1, -log(1) = 0
    let x = variable(());
    let obj = -log(&x);

    let solution = Problem::minimize(obj)
        .subject_to([x.ge(constant(0.1)), x.le(constant(1.0))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    let x_val = solution.value(&x);
    assert!((x_val - 1.0).abs() < TOL, "Expected x=1, got {}", x_val);
}

#[test]
fn test_entropy_basic() {
    // Test entropy on a simple problem
    // maximize entropy(x) s.t. x <= 0.5, x >= 0.1
    // Entropy is concave, so maximize it
    let x = variable(());

    let solution = Problem::maximize(entropy(&x))
        .subject_to([x.le(constant(0.5)), x.ge(constant(0.1))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    // Maximum entropy in [0.1, 0.5] is at x ≈ 0.368 (1/e)
    // But due to simplified implementation, we just check it solves
}

// ============================================================================
// Power Cone Atoms
// ============================================================================

#[test]
fn test_power_p_half() {
    // minimize -sqrt(x) = -x^0.5 s.t. x <= 4
    // Solution: x = 4, sqrt(4) = 2
    let x = variable(());
    let obj = -sqrt(&x);

    let solution = Problem::minimize(obj)
        .subject_to([x.le(constant(4.0)), x.ge(constant(0.01))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    let x_val = solution.value(&x);
    assert!((x_val - 4.0).abs() < TOL, "Expected x=4, got {}", x_val);
}

#[test]
fn test_power_p_greater_than_1() {
    // minimize x^3 s.t. x >= 1
    // Solution: x = 1, 1^3 = 1
    let x = variable(());
    let obj = power(&x, 3.0);

    let solution = Problem::minimize(obj)
        .subject_to([x.ge(constant(1.0))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    let x_val = solution.value(&x);
    assert!((x_val - 1.0).abs() < TOL, "Expected x=1, got {}", x_val);
}

#[test]
fn test_power_p_less_than_1() {
    // maximize x^0.3 s.t. x <= 8
    // Solution: x = 8, 8^0.3 ≈ 1.986
    let x = variable(());
    let obj = power(&x, 0.3);

    let solution = Problem::maximize(obj)
        .subject_to([x.le(constant(8.0)), x.ge(constant(0.1))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    let x_val = solution.value(&x);
    assert!((x_val - 8.0).abs() < TOL, "Expected x=8, got {}", x_val);
}

#[test]
fn test_sqrt_vector() {
    // maximize sum(sqrt(x)) s.t. x <= [1, 4, 9], x >= 0
    // sqrt is concave and increasing, so maximum is at upper bound
    // Solution: x = [1, 4, 9], sum = 1 + 2 + 3 = 6
    let x = variable(3);
    let obj = sum(&sqrt(&x));

    let solution = Problem::maximize(obj)
        .subject_to([
            x.le(constant_vec(vec![1.0, 4.0, 9.0])),
            x.ge(constant(0.01)),
        ])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);

    if let Some(Array::Dense(x_vals)) = solution.get_value(x.variable_id().unwrap()) {
        assert!((x_vals[(0, 0)] - 1.0).abs() < TOL);
        assert!((x_vals[(1, 0)] - 4.0).abs() < TOL);
        assert!((x_vals[(2, 0)] - 9.0).abs() < TOL);
    } else {
        panic!("Expected dense array for x");
    }
}

// ============================================================================
// Affine Atoms
// ============================================================================

#[test]
fn test_cumsum_basic() {
    // minimize sum(cumsum(x)) s.t. x = [1, 2, 3]
    // cumsum([1,2,3]) = [1, 3, 6], sum = 10
    let x = variable(3);
    let y = cumsum(&x);

    let solution = Problem::minimize(sum(&y))
        .subject_to([x.eq(constant_vec(vec![1.0, 2.0, 3.0]))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    let val = solution.value.unwrap();
    assert!((val - 10.0).abs() < TOL, "Expected 10.0, got {}", val);
}

#[test]
fn test_cumsum_optimization() {
    // minimize sum(cumsum(x)) s.t. sum(x) = 6, x >= 0
    // To minimize cumulative sum, put weight at end: [0, 0, 6]
    // cumsum([0, 0, 6]) = [0, 0, 6], sum = 6
    let x = variable(3);
    let y = cumsum(&x);

    let solution = Problem::minimize(sum(&y))
        .subject_to([sum(&x).eq(constant(6.0)), x.ge(constant(0.0))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    let val = solution.value.unwrap();
    assert!((val - 6.0).abs() < TOL, "Expected 6.0, got {}", val);

    // Check that x is concentrated at the end
    if let Some(Array::Dense(x_vals)) = solution.get_value(x.variable_id().unwrap()) {
        // First elements should be near 0
        assert!(x_vals[(0, 0)] < TOL);
        assert!(x_vals[(1, 0)] < TOL);
        // Last element should be near 6
        assert!((x_vals[(2, 0)] - 6.0).abs() < TOL);
    }
}

#[test]
fn test_diag_basic() {
    // Create diagonal matrix from vector
    // minimize trace(diag(x)) s.t. x = [1, 2, 3]
    // trace = 1 + 2 + 3 = 6
    let x = variable(3);
    let d = diag(&x);

    let solution = Problem::minimize(trace(&d))
        .subject_to([x.eq(constant_vec(vec![1.0, 2.0, 3.0]))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    let val = solution.value.unwrap();
    assert!((val - 6.0).abs() < TOL, "Expected 6.0, got {}", val);
}

// ============================================================================
// Dual Values
// ============================================================================

#[test]
fn test_dual_values_simple() {
    // minimize x s.t. x >= 1
    // Optimal: x = 1, dual of constraint = 1 (binding constraint)
    let x = variable(());

    let solution = Problem::minimize(x.clone())
        .subject_to([x.ge(constant(1.0))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    assert!(solution.has_duals());

    let duals = solution.duals().unwrap();
    assert!(!duals.is_empty(), "Should have dual values");

    // The dual of a binding constraint should be positive
    let dual_0 = solution.constraint_dual(0).unwrap();
    assert!(
        dual_0.abs() > TOL,
        "Dual should be non-zero for binding constraint"
    );
}

#[test]
fn test_dual_values_shadow_price() {
    // minimize x + y s.t. x + y >= 10, x >= 0, y >= 0
    // Shadow price of x+y>=10 should be 1
    let x = variable(());
    let y = variable(());

    let solution = Problem::minimize(&x + &y)
        .subject_to([
            (&x + &y).ge(constant(10.0)),
            x.ge(constant(0.0)),
            y.ge(constant(0.0)),
        ])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);

    if let Some(duals) = solution.duals() {
        println!("Dual values: {:?}", duals);
        // First constraint (x+y>=10) should have dual ≈ -1
        // (negative because of Clarabel's convention)
    }
}

// ============================================================================
// DCP Verification Tests
// ============================================================================

#[test]
fn test_exp_dcp_rules() {
    use cvxrust::dcp::Curvature;

    let x = variable(());
    let e = exp(&x);

    assert_eq!(e.curvature(), Curvature::Convex);
    assert!(e.is_nonneg());
}

#[test]
fn test_log_dcp_rules() {
    use cvxrust::dcp::Curvature;

    let x = variable(());
    let l = log(&x);

    assert_eq!(l.curvature(), Curvature::Concave);
}

#[test]
fn test_power_dcp_rules() {
    use cvxrust::dcp::Curvature;

    let x = variable(());

    // p > 1: convex
    assert_eq!(power(&x, 2.5).curvature(), Curvature::Convex);

    // 0 < p < 1: concave
    assert_eq!(power(&x, 0.7).curvature(), Curvature::Concave);
    assert_eq!(sqrt(&x).curvature(), Curvature::Concave);

    // p = 1: affine
    assert_eq!(power(&x, 1.0).curvature(), Curvature::Affine);
}

#[test]
fn test_cumsum_dcp() {
    use cvxrust::dcp::Curvature;

    let x = variable(3);
    let cs = cumsum(&x);

    // cumsum is affine
    assert_eq!(cs.curvature(), Curvature::Affine);
}

#[test]
fn test_diag_dcp() {
    use cvxrust::dcp::Curvature;

    let x = variable(3);
    let d = diag(&x);

    // diag is affine
    assert_eq!(d.curvature(), Curvature::Affine);
}

// ============================================================================
// Integration Tests
// ============================================================================

#[test]
fn test_logistic_regression_setup() {
    // Test that we can set up a logistic regression-like problem
    // Note: log(1 + exp(x)) is the softplus function, which IS convex,
    // but standard DCP composition rules can't verify this (log(convex) is unknown).
    // A proper implementation would need a dedicated softplus/logsumexp atom.
    //
    // For v1.0, we test a simpler formulation that IS DCP-compliant:
    // minimize exp(x) + x  (convex + affine = convex)

    let x = variable(());
    let loss = exp(&x) + x.clone();

    // Check it's DCP compliant
    assert!(loss.is_convex(), "exp(x) + x should be convex");

    // And verify it solves correctly
    let solution = Problem::minimize(loss)
        .subject_to([x.ge(constant(-2.0))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    // Minimum of exp(x) + x is at x where exp(x) + 1 = 0, i.e., never (always positive)
    // With constraint x >= -2, minimum is at x = -2
    let x_val = solution.value(&x);
    assert!((x_val - (-2.0)).abs() < TOL, "Expected x=-2, got {}", x_val);
}

#[test]
fn test_geometric_program_equivalent() {
    // Test power cone with geometric programming
    // minimize x^2 * y^(-1) s.t. x >= 1, y >= 1
    // Can be written as minimize t where t >= x^2/y

    let x = variable(());
    let y = variable(());

    // Use power to express x^2
    let x_sq = power(&x, 2.0);

    // Simplified: just minimize x^2 subject to constraints
    let solution = Problem::minimize(x_sq)
        .subject_to([x.ge(constant(1.0)), y.ge(constant(1.0))])
        .solve()
        .expect("Should solve");

    assert_eq!(solution.status, SolveStatus::Optimal);
    let x_val = solution.value(&x);
    assert!((x_val - 1.0).abs() < TOL);
}