Skip to content

Commit

Permalink
Merge pull request #330 from alphaville/fix/ball2
Browse files Browse the repository at this point in the history
Important bug fix in ball2
  • Loading branch information
alphaville authored Oct 27, 2023
2 parents 0961e47 + a1273e9 commit 8d25f34
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
Note: This is the main Changelog file for the Rust solver. The Changelog file for the Python interface (`opengen`) can be found in [/open-codegen/CHANGELOG.md](open-codegen/CHANGELOG.md)


<!-- ---------------------
v0.8.1
--------------------- -->
## [v0.8.1] - 2023-10-27

### Fixed

- Fix bug in implementation of `ball2.rs` (radius was being ignored for balls centered not at the origin)



<!-- ---------------------
v0.8.0
--------------------- -->
Expand Down Expand Up @@ -256,7 +267,8 @@ This is a breaking API change.
--------------------- -->

<!-- Releases -->
[v0.7.8]: https://github.com/alphaville/optimization-engine/compare/v0.7.7...v0.8.0
[v0.8.1]: https://github.com/alphaville/optimization-engine/compare/v0.8.0...v0.8.1
[v0.8.0]: https://github.com/alphaville/optimization-engine/compare/v0.7.7...v0.8.0
[v0.7.7]: https://github.com/alphaville/optimization-engine/compare/v0.7.6...v0.7.7
[v0.7.6]: https://github.com/alphaville/optimization-engine/compare/v0.7.5...v0.7.6
[v0.7.5]: https://github.com/alphaville/optimization-engine/compare/v0.7.4...v0.7.5
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ homepage = "https://alphaville.github.io/optimization-engine/"
repository = "https://github.com/alphaville/optimization-engine"

# Version of this crate (SemVer)
version = "0.8.0"
version = "0.8.1"

edition = "2018"

Expand Down
2 changes: 1 addition & 1 deletion src/constraints/ball2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'a> Constraint for Ball2<'a> {

if norm_difference > self.radius {
x.iter_mut().zip(center.iter()).for_each(|(x, c)| {
*x = *c + (*x - *c) / norm_difference;
*x = *c + self.radius * (*x - *c) / norm_difference;
});
}
} else {
Expand Down
46 changes: 43 additions & 3 deletions src/constraints/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ fn t_rectangle_only_xmax() {
}

#[test]
fn t_ball_at_origin() {
fn t_ball2_at_origin() {
let radius = 1.0;
let mut x = [1.0, 1.0];
let ball = Ball2::new(None, radius);
Expand All @@ -246,7 +246,47 @@ fn t_ball_at_origin() {
}

#[test]
fn t_ball_elsewhere() {
fn t_ball2_at_origin_different_radius_outside() {
let radius = 0.8;
let mut x = [1.0, 1.0];
let ball = Ball2::new(None, radius);
ball.project(&mut x);
let norm_proj_x = crate::matrix_operations::norm2(&x);
unit_test_utils::assert_nearly_equal(radius, norm_proj_x, 1e-10, 1e-12, "wrong norm");
}

#[test]
fn t_ball2_at_origin_different_radius_inside() {
let radius = 0.8;
let mut x = [-0.2, 0.15];
let ball = Ball2::new(None, radius);
ball.project(&mut x);
unit_test_utils::assert_nearly_equal_array(&x, &[-0.2, 0.15], 1e-10, 1e-12, "wrong");
}

#[test]
fn t_ball2_at_center_different_radius_outside() {
let radius = 1.2;
let mut x = [1.0, 1.0];
let center = [-0.8, -1.1];
let ball = Ball2::new(Some(&center), radius);
ball.project(&mut x);
let norm_x_minus_c = crate::matrix_operations::norm2_squared_diff(&x, &center).sqrt();
unit_test_utils::assert_nearly_equal(radius, norm_x_minus_c, 1e-10, 1e-12, "wrong norm");
}

#[test]
fn t_ball2_at_center_different_radius_inside() {
let radius = 1.2;
let mut x = [-0.9, -0.85];
let center = [-0.8, -1.1];
let ball = Ball2::new(Some(&center), radius);
ball.project(&mut x);
unit_test_utils::assert_nearly_equal_array(&[-0.9, -0.85], &x, 1e-10, 1e-12, "wrong result");
}

#[test]
fn t_ball2_elsewhere() {
let radius = 1.0;
let center = [1.0, 1.0];
let mut x = [2.0, 2.0];
Expand Down Expand Up @@ -465,7 +505,7 @@ fn t_cartesian_product_dimension() {
#[test]
fn t_cartesian_ball_no_constraint() {
let xc = [1., 0., 0.];
let radius = 0.5;
let radius = 1.0;
let ball2 = Ball2::new(Some(&xc), radius);
let no_constraints = NoConstraints::new();
let cartesian = CartesianProduct::new_with_capacity(4)
Expand Down

0 comments on commit 8d25f34

Please sign in to comment.