Skip to content

Commit

Permalink
test: harder test of convex hull
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Nov 9, 2024
1 parent 22c4657 commit 5ed8de3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ext/SymbolicRegressionRecipesBaseExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using SymbolicRegression.LoggingModule: convex_hull

import SymbolicRegression.LoggingModule: make_plot

function make_plot(;
function make_plot(
hall_of_fame::HallOfFame, @nospecialize(options::Options), variable_names
)
plot_result = plot(hall_of_fame, options; variable_names=variable_names)
Expand Down
9 changes: 2 additions & 7 deletions src/Logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function LG.with_logger(f::Function, logger::AbstractSRLogger)
end

# Will get method created by RecipesBase extension
function make_plot(; kws...)
function make_plot(args...)
return error("Please load `Plots` or another plotting package.")
end

Expand Down Expand Up @@ -120,12 +120,7 @@ function log_payload(
end
if should_log_plots
out = merge(
out,
make_plot(;
hall_of_fame=state.halls_of_fame[i],
options,
variable_names=datasets[i].variable_names,
),
out, make_plot(state.halls_of_fame[i], options, datasets[i].variable_names)
)
end
if length(datasets) == 1
Expand Down
34 changes: 22 additions & 12 deletions test/test_logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,29 @@ end
using SymbolicRegression.LoggingModule: convex_hull, convex_hull_area

# Create a Pareto front with an interior point that should be ignored
# Points: (0,0), (0,2), (2,0), and (1,1) which is inside the triangle
points = [
0.0 0.0 # vertex 1
0.0 2.0 # vertex 2
2.0 0.0 # vertex 3
1.0 1.0 # interior point that should be ignored
]
hull = convex_hull(points)

@test length(hull) == 3
@test hull == [[0.0, 0.0], [0.0, 2.0], [2.0, 0.0]]
log_complexities = [1.0, 2.0, 3.0, 4.0]
log_losses = [4.0, 3.0, 3.0, 2.5]

# Add a point to connect things at lower right corner
push!(log_complexities, 5.0)
push!(log_losses, 2.5)

# Add a point to connect things at upper right corner
push!(log_losses, 4.0)
push!(log_complexities, 5.0)

xy = cat(log_complexities, log_losses; dims=2)
hull = convex_hull(xy)
@test length(hull) == 5
@test hull == [[1.0, 4.0], [5.0, 4.0], [5.0, 2.5], [4.0, 2.5], [2.0, 3.0]]

# Expected area = 1/2 * base * height = 1/2 * 2 * 2 = 2
area = convex_hull_area(hull)
@test isapprox(area, 2.0, atol=1e-10)
true_area = (
1 * (4.0 - 2.5) # lower right rectangle
+ 2.0 * (4.0 - 3.0) # block to the slight left and update
+ 1.0 * (4.0 - 3.0) / 2 # top left triangle
+ 2.0 * (3.0 - 2.5) / 2 # bottom triangle
)
@test isapprox(area, true_area, atol=1e-10)
end

0 comments on commit 5ed8de3

Please sign in to comment.