Skip to content

Commit

Permalink
Method to set the objective coeff vector
Browse files Browse the repository at this point in the history
  • Loading branch information
mmghannam committed Jul 20, 2024
1 parent 3aabc14 commit d07c678
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,18 @@ impl Model {
ffi::SoPlex_setIntParam(*self.inner, crate::SCALAR_PARAM_ID, scalar_type.into());
}
}

/// Sets the objective function vector.
///
/// # Arguments
/// * `objvals` - The objective function vector.
pub fn set_obj_vals(&mut self, objvals: &mut [f64]) {
let num_cols = self.num_cols();
assert_eq!(objvals.len(), num_cols, "objvals must have the same length as the number of columns");
unsafe {
ffi::SoPlex_changeObjReal(*self.inner, objvals.as_mut_ptr(), objvals.len() as i32);
}
}
}

/// A solved linear programming model.
Expand Down Expand Up @@ -690,4 +702,16 @@ mod tests {
assert_eq!(result, Status::Optimal);
assert!((lp.obj_val() - 5.0).abs() < 1e-6);
}

#[test]
fn set_objective() {
let mut lp = Model::new();
lp.add_col([], 1.0, 1.0, 1.0);
lp.add_col([], 1.0, 1.0, 1.0);
lp.set_obj_vals(&mut [2.0, 3.0]);
let lp = lp.optimize();
let result = lp.status();
assert_eq!(result, Status::Optimal);
assert!((lp.obj_val() - 5.0).abs() < 1e-6);
}
}

0 comments on commit d07c678

Please sign in to comment.