Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rmitsuboshi committed Nov 28, 2024
1 parent 4872a27 commit ac12080
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 54 deletions.
67 changes: 38 additions & 29 deletions src/booster/erlpboost/qp_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::hypothesis::Classifier;

use std::iter;

const QP_TOLERANCE: f64 = 1e-300;
const QP_TOLERANCE: f64 = 1e-9;

/// A quadratic programming model for edge minimization.
/// `QPModel` solves the entropy regularized edge minimization problem:
Expand Down Expand Up @@ -45,33 +45,33 @@ const QP_TOLERANCE: f64 = 1e-300;
/// ```txt
/// # of
/// rows γ d1 ... dm
/// ┏ ┃ ┓ ┏ ┓
/// 1 ┃ 0 ┃ 1 ... 1 ┃ = ┃ 1
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ 0 ┃ ┃ ≤ ┃ 0
/// ┃ 0 ┃ ┃ ≤ ┃ 0
/// ┃ . ┃ (-1) * Identity matrix ┃ . ┃ .
/// m ┃ . ┃ m x m ┃ . ┃ .
/// ┃ . ┃ ┃ . ┃ .
/// ┃ 0 ┃ ┃ ≤ ┃ 0
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ 0 ┃ ┃ ≤ ┃ 0
/// ┃ 0 ┃ ┃ ≤ ┃ 0
/// ┃ . ┃ ┃ . ┃ .
/// m ┃ . ┃ Identity matrix ┃ . ┃ .
/// ┃ . ┃ m x m ┃ . ┃ .
/// ┃ 0 ┃ ┃ ≤ ┃ 0
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ -1 ┃ y_1 h_1(x_1) ... y_m h_1(x_m) ┃ ≤ ┃ 0
/// ┃ -1 ┃ y_1 h_2(x_1) ... y_m h_2(x_m) ┃ ≤ ┃ 0
/// ┃ . ┃ . ... . ┃ . ┃ .
/// H ┃ . ┃ . ... . ┃ . ┃ .
/// ┃ . ┃ . ... . ┃ . ┃ .
/// ┃ -1 ┃ y_1 h_T(x_1) ... y_m h_T(x_m) ┃ ≤ ┃ 0
/// ┗ ┃ ┛ ┗ ┛
/// ┏ ┃ ┓ ┏
/// 1 ┃ 0 ┃ 1 ... 1 ┃ = ┃ 1
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ 0 ┃ ┃ ≤ ┃ 0
/// ┃ 0 ┃ ┃ ≤ ┃ 0
/// ┃ . ┃ (-1) * Identity matrix ┃ . ┃ .
/// m ┃ . ┃ m x m ┃ . ┃ .
/// ┃ . ┃ ┃ . ┃ .
/// ┃ 0 ┃ ┃ ≤ ┃ 0
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ 0 ┃ ┃ ≤ ┃ 1/ν
/// ┃ 0 ┃ ┃ ≤ ┃ 1/ν
/// m ┃ . ┃ Identity matrix ┃ . ┃ .
/// ┃ . ┃ m x m ┃ . ┃ .
/// ┃ . ┃ ┃ . ┃ .
/// ┃ 0 ┃ ┃ ≤ ┃ 1/ν
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ -1 ┃ y_1 h_1(x_1) ... y_m h_1(x_m) ┃ ≤ ┃ 0
/// ┃ -1 ┃ y_1 h_2(x_1) ... y_m h_2(x_m) ┃ ≤ ┃ 0
/// ┃ . ┃ . ... . ┃ . ┃ .
/// H ┃ . ┃ . ... . ┃ . ┃ .
/// ┃ . ┃ . ... . ┃ . ┃ .
/// ┃ -1 ┃ y_1 h_T(x_1) ... y_m h_T(x_m) ┃ ≤ ┃ 0
/// ┗ ┃ ┛ ┗
///
/// # of
/// cols 1 ┃ m
/// cols 1 ┃ m
/// ```
pub(super) struct QPModel {
pub(self) n_examples: usize, // number of columns
Expand Down Expand Up @@ -124,7 +124,13 @@ impl QPModel {
let rhs = self.build_rhs();


let mut old_objval = 1e9;
let mut old_objval = 1e3;

// Initialize `dist` as the uniform distribution.
dist.iter_mut()
.for_each(|di| {
*di = 1f64 / self.n_examples as f64;
});
loop {
let settings = DefaultSettingsBuilder::default()
.equilibrate_enable(true)
Expand Down Expand Up @@ -172,7 +178,7 @@ impl QPModel {

pub(self) fn build_linear_part_objective(&self, dist: &[f64]) -> Vec<f64> {
let mut linear = Vec::with_capacity(1 + self.n_examples);
linear.push(0f64);
linear.push(1f64);
let iter = dist.into_iter()
.copied()
.map(|di| (1f64 / self.eta) * di.ln());
Expand All @@ -194,6 +200,9 @@ impl QPModel {
col_ptr.push(0usize);
row_val.push(0usize);
nonzero.push(1f64);
// NOTE:
// we do not need to multiply 0.5f64
// since clarabel add it automatically.
for (i, &di) in (1..).zip(dist) {
col_ptr.push(i);
row_val.push(i);
Expand Down Expand Up @@ -238,7 +247,7 @@ impl QPModel {

// capping constraint: `d_i ≤ 1/ν`
row_val.push(self.n_examples + j);
nonzero.push(1f64);
nonzero.push(self.cap_inv);

// margin constraints of `i`-th column
for (i, &yh) in (0..).zip(margins) {
Expand Down
50 changes: 25 additions & 25 deletions src/booster/softboost/qp_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,30 @@ const QP_TOLERANCE: f64 = 1e-9;
/// ```txt
/// # of
/// rows d1 ... dm
/// ┏ ┓ ┏ ┓
/// 1 ┃ 1 ... 1 ┃ = ┃ 1
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ ┃ ≤ ┃ 0
/// ┃ ┃ ≤ ┃ 0
/// ┃ (-1) * Identity matrix ┃ . ┃ .
/// m ┃ m x m ┃ . ┃ .
/// ┃ ┃ . ┃ .
/// ┃ ┃ ≤ ┃ 0
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ ┃ ≤ ┃ 0
/// ┃ ┃ ≤ ┃ 0
/// ┃ . ┃ .
/// mIdentity matrix ┃ . ┃ .
/// ┃ m x m ┃ . ┃ .
/// ┃ ┃ ≤ ┃ 0
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ y_1 h_1(x_1) ... y_m h_1(x_m) ┃ ≤ ┃ 0
/// ┃ y_1 h_2(x_1) ... y_m h_2(x_m) ┃ ≤ ┃ 0
/// ┃ . ... . ┃ . ┃ .
/// H ┃ . ... . ┃ . ┃ .
/// ┃ . ... . ┃ . ┃ .
/// ┃ y_1 h_T(x_1) ... y_m h_T(x_m) ┃ ≤ ┃ 0
/// ┗ ┛ ┗ ┛
/// ┏ ┓ ┏
/// 1 ┃ 1 ... 1 ┃ = ┃ 1
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ ┃ ≤ ┃ 0
/// ┃ ┃ ≤ ┃ 0
/// ┃ (-1) * Identity matrix ┃ . ┃ .
/// m ┃ m x m ┃ . ┃ .
/// ┃ ┃ . ┃ .
/// ┃ ┃ ≤ ┃ 0
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ ┃ ≤ ┃ 1/ν
/// ┃ ┃ ≤ ┃ 1/ν
/// mIdentity matrix ┃ . ┃ .
/// m x m ┃ . ┃ .
/// ┃ ┃ . ┃ .
/// ┃ ┃ ≤ ┃ 1/ν
/// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/// ┃ y_1 h_1(x_1) ... y_m h_1(x_m) ┃ ≤ ┃ 0
/// ┃ y_1 h_2(x_1) ... y_m h_2(x_m) ┃ ≤ ┃ 0
/// ┃ . ... . ┃ . ┃ .
/// H ┃ . ... . ┃ . ┃ .
/// ┃ . ... . ┃ . ┃ .
/// ┃ y_1 h_T(x_1) ... y_m h_T(x_m) ┃ ≤ ┃ 0
/// ┗ ┛ ┗
///
/// # of
/// cols m
Expand Down Expand Up @@ -236,7 +236,7 @@ impl QPModel {

// capping constraint: `d_i ≤ 1/ν`
row_val.push(self.n_examples + j + 1);
nonzero.push(1f64);
nonzero.push(self.cap_inv);

// margin constraints of `i`-th column
for (i, &yh) in (0..).zip(margins) {
Expand Down

0 comments on commit ac12080

Please sign in to comment.