From daa1835e80cd2b874c88c7c17bd4ab0200e51d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Rei=C3=9Fmann?= Date: Wed, 21 May 2025 21:27:27 +1000 Subject: [PATCH] fix_concurrency issue --- README.md | 4 +--- examples/Main_min_example_mo.jl | 6 ++---- paper/ConstraintViaSBP.jl | 10 +++++----- src/Gep.jl | 17 +++++++---------- tutorial/JGEP_demo.ipynb | 2 +- 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index b353ebc..8711425 100644 --- a/README.md +++ b/README.md @@ -149,9 +149,7 @@ inputs = (x1,x2,u1,u2,u3) size(a_pred[1]) != size(a[1]) && return (typemax(Float64),) loss = norm(a_pred .- a) - return (loss,) - else - return (elem.fitness,) + elem.fitness = (loss,) end end fit!(regressor, epochs, population_size, loss_new) diff --git a/examples/Main_min_example_mo.jl b/examples/Main_min_example_mo.jl index 9481004..a83d8c6 100644 --- a/examples/Main_min_example_mo.jl +++ b/examples/Main_min_example_mo.jl @@ -26,12 +26,10 @@ regressor = GepRegressor(number_features; number_of_objectives=2) try if isnan(mean(elem.fitness)) || validate y_pred = elem.compiled_function(x_data', regressor.operators_) - return (get_loss_function("mse")(y_data, y_pred), length(elem.expression_raw)*0.01) - else - return elem.fitness + elem.fitness = (get_loss_function("mse")(y_data, y_pred), length(elem.expression_raw)*0.01) end catch e - return (typemax(Float64),typemax(Float64)) + elem.fitness = (typemax(Float64),typemax(Float64)) end end diff --git a/paper/ConstraintViaSBP.jl b/paper/ConstraintViaSBP.jl index 8376a18..c0f2577 100644 --- a/paper/ConstraintViaSBP.jl +++ b/paper/ConstraintViaSBP.jl @@ -89,7 +89,7 @@ function main() @show ("Current case: ", case_name) #gep_params epochs = 1000 - population_size = 200 + population_size = 500 results = DataFrame(Seed=[], Name=String[], NoiseLeve=String[], Fitness=Float64[], Equation=String[], R2_test=Float64[], @@ -129,13 +129,13 @@ function main() try if isnan(mean(elem.fitness)) || validate y_pred = elem.compiled_function(x_train', regressor.operators_) - return (get_loss_function("mse")(y_train, y_pred),) + elem.fitness = (get_loss_function("mse")(y_train, y_pred),) else - return (elem.fitness, length(elem.expression_raw) * elem.fitness) + elem.fitness = (elem.fitness, length(elem.expression_raw) * elem.fitness) #return (elem.fitness,) end catch e - return (typemax(Float64),typemax(Float64)) + elem.fitness = (typemax(Float64),typemax(Float64)) end end @@ -143,7 +143,7 @@ function main() #perform the regression by entering epochs, population_size, the feature cols, the target col and the loss function fit!(regressor, epochs, population_size, x_train', y_train; x_test=x_test', y_test=y_test', target_dimension=target_dim, - loss_fun="mse", break_condition=break_condition) + loss_fun="mse", break_condition=break_condition, correction_amount=0.5) end_time = (time_ns() - start_time) / 1e9 elem = regressor.best_models_[1] diff --git a/src/Gep.jl b/src/Gep.jl index 4ad26f2..7d40074 100644 --- a/src/Gep.jl +++ b/src/Gep.jl @@ -118,12 +118,10 @@ Returns the computed fitness value (loss) or crash_value if computation fails try if isnan(mean(elem.fitness)) || validate y_pred = elem.compiled_function(evalArgs.x_data, evalArgs.operators) - return (evalArgs.loss_function(evalArgs.y_data, y_pred),) - else - return elem.fitness + elem.fitness = (evalArgs.loss_function(evalArgs.y_data, y_pred),) end catch e - return (evalArgs.crash_value,) + elem.fitness = (evalArgs.crash_value,) end end @@ -342,16 +340,15 @@ The evolution process stops when either: Threads.@threads for i in eachindex(population[1:population_size]) if isnan(mean(population[i].fitness)) key = join(population[i].expression_raw, ",") - cache_value = get(fit_cache, key, nothing) - - if isnothing(cache_value) - population[i].fitness = compute_fitness(population[i], evalStrategy) + cache_value = key in keys(fit_cache) + if !(cache_value) + compute_fitness(population[i], evalStrategy) lock(cache_lock) - fit_cache[key] = population[i].fitness + fit_cache[key] = population[i].fitness unlock(cache_lock) else atomic_add!(same, 1) - population[i].fitness = cache_value + population[i].fitness = toolbox.fitness_reset[1] end end end diff --git a/tutorial/JGEP_demo.ipynb b/tutorial/JGEP_demo.ipynb index 3bf385c..1e84d3a 100644 --- a/tutorial/JGEP_demo.ipynb +++ b/tutorial/JGEP_demo.ipynb @@ -294,7 +294,7 @@ "#define the \n", "regressor = GepRegressor(number_features)\n", "\n", - "fit!(regressor, epochs, population_size, x_data, y_data; loss_fun=\"mse\")\n", + "fit!(regressor, epochs, population_size, x_data', y_data; loss_fun=\"mse\")\n", "\n", "pred = regressor(x_data')\n", "\n",