This Julia code: using BenchmarkTools, Distributed addprocs() @everywhere using…
- User
This Julia code: using BenchmarkTools, Distributed addprocs() @everywhere using DataFrames, CSV, DataFrames, Random, StatsBase, LinearAlgebra @everywhere Data = CSV.read("C:/Users/Użytkownik/Desktop/player22.csv", DataFrame)
Indices of players for each position
@everywhere RW_idx = findall(x -> occursin("RW", x), Data[!, :Positions]) @everywhere ST_idx = findall(x -> occursin("ST", x), Data[!, :Positions]) @everywhere GK_idx = findall(x -> occursin("GK", x), Data[!, :Positions]) @everywhere CM_idx = findall(x -> occursin("CM", x), Data[!, :Positions]) @everywhere LW_idx = findall(x -> occursin("LW", x), Data[!, :Positions]) @everywhere CDM_idx = findall(x -> occursin("CDM", x), Data[!, :Positions]) @everywhere LM_idx = findall(x -> occursin("LM", x), Data[!, :Positions]) @everywhere CF_idx = findall(x -> occursin("CF", x), Data[!, :Positions]) @everywhere CB_idx = findall(x -> occursin("CB", x), Data[!, :Positions]) @everywhere CAM_idx = findall(x -> occursin("CAM", x), Data[!, :Positions]) @everywhere LB_idx = findall(x -> occursin("LB", x), Data[!, :Positions]) @everywhere RB_idx = findall(x -> occursin("RB", x), Data[!, :Positions]) @everywhere RM_idx = findall(x -> occursin("RM", x), Data[!, :Positions]) @everywhere LWB_idx = findall(x -> occursin("LWB", x), Data[!, :Positions]) @everywhere RWB_idx = findall(x -> occursin("RWB", x), Data[!, :Positions]) @everywhere position_vectors = [RW_idx, ST_idx, GK_idx, CM_idx, LW_idx, CDM_idx, LM_idx, CF_idx, CB_idx, CAM_idx, LB_idx, RB_idx, RM_idx, LWB_idx, RWB_idx]
@everywhere using DataFrames
@everywhere function create_ones_dataframe(n_rows, length_position_vectors) position_vectors = fill(1, length_position_vectors) df = DataFrame() for i in 1:length_position_vectors col_name = Symbol("Position_$i") df[!, col_name] = position_vectors end return repeat(df, n_rows) end
@everywhere n_rows = 20
@everywhere pop_init = create_ones_dataframe(n_rows, length(position_vectors)) @everywhere n_rows=100 @everywhere pop_init= pop_init[1:(n_rows), :]
@everywhere function mutate(selected_players, position_vectors_list, probability) selected_players_matrix = copy(selected_players)
function select_random_player(idx_list, selected_players) while true random_idx = rand(idx_list) if ismissing(selected_players) || !(random_idx in selected_players) return random_idx end end end for i in 1:size(selected_players_matrix)[1] for pos_idx in 1:length(position_vectors_list) if rand() <= probability selected_players_matrix[i, pos_idx] = select_random_player(position_vectors_list[pos_idx], selected_players_matrix[i, :]) end end end return convert(DataFrame, selected_players_matrix)end
@everywhere n_rows = 100 @everywhere pop_init = mutate(pop_init, position_vectors,1.0)
@everywhere function crossover(parent1, parent2, crossover_point=6)
- Assistant
To optimize the code, you can take the following steps:
- Reduce the search space:
You can try reducing the range of the weight values or use a greater step. This will reduce the number of cases you need to check, thereby making the code faster. For example, you can change the ranges as follows:
weight1_range = 0.05:0.1:0.2 weight2_range = 0:0.2:1.0 weight3_range = 0.1:0.2:0.9 - User
- Assistant
I understand you have already parallelized the code with
pmap()function. However, there are still other ways to optimize the code further. Here's an updated version of your code that incorporates the suggested improvements:using BenchmarkTools, Distributed addprocs() @everywhere using DataFrames, CSV, Random, StatsBase, LinearAlgebra @everywhere Data = CSV.read("C:/Users/Użytkownik/Desktop/player22.csv", DataFrame) # Reduce search space by changing range of the weight values or use a greater step # which will reduce the number of cases you need to check weight1_range = 0.05:0.1:0.2 weight2_range = 0:0.2:1.0 weight3_range = 0.1:0.2:0.9 params = [(weight1, weight2, weight3) for weight1 in weight1_range, weight2 in weight2_range, weight3 in weight3_range] params = vec(params) function process_weights(weights) weight1, weight2, weight3 = weights global_best, global_best_value = GA2(7, 100, 5000, 2, 0.09, weight1, weight2, weight3, 250000000, 250000, 1.2) global_best_df = DataFrame(global_best) return (weight1=weight1, weight2=weight2, weight3=weight3, global_best=global_best_df, global_best_value=global_best_value) end # Evaluate the solutions in parallel using pmap() results = pmap(process_weights, params) df_results = DataFrame(weight1 = Float64[], weight2 = Float64[], weight3 = Float64[], global_best = Any[], global_best_value = Any[]) for r in results push!(df_results, r) end println(df_results)