To this Julia code: addprocs() @everywhere using DataFrames Load Data data =…
- User
To this Julia code: addprocs() @everywhere using DataFrames
Load Data
data = CSV.read("C:/Users/Użytkownik/Desktop/player22.csv", DataFrame)
(position -> global _position; @eval begin (_position)_idx = findall(occursin.( (_position), data.Positions)) end)(position) for position in ["RW", "ST", "GK", "CM", "LW", "CDM", "LM", "CF", "CB", "CAM", "LB", "RB", "RM", "LWB", "RWB"])
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]
Mutation 2
@everywhere function mutate(selected_players_df, position_vectors, probability) n_rows, _ = size(selected_players_df) selected_players_matrix = selected_players_df
codefunction select_random_player(idx_list, selected_players) while true random_idx = rand(idx_list) (in(random_idx, selected_players)) || return(random_idx) end end for i in 1:n_rows for (pos_idx, index_vector) in enumerate(position_vectors) if rand() <= probability idx = select_random_player(index_vector, Set(selected_players_matrix[i, :])) selected_players_matrix[i, pos_idx] = idx end end end selected_players_df = DataFrame(selected_players_matrix) return selected_players_dfend
n_rows = 100 pop_init = DataFrame(Matrix{Union{Missing,Int}}(missing, n_rows, length(position_vectors))) pop_init = mutate(pop_init, position_vectors, probability=1)
Target
row1 = pop_init[1, :]
@everywhere using DataFrames @everywhere target(row1; penalty=5) = begin position_ratings = ["RWRating", "STRating", "GKRating", "CMRating", "LWRating", "CDMRating", "LMRating", "CFRating", "CBRating", "CAMRating", "LBRating", "RBRating", "RMRating", "LWBRating", "RWBRating"]
code - Assistant
It looks like the error is due to a misplaced parenthesis in the list comprehension at line 12. The correct code should be:
code[position -> (global _position; @eval begin (_position)_idx = findall(occursin.( (_position), data.Positions)) end)(_position) for position in ["RW", "ST", "GK", "CM", "LW", "CDM", "LM", "CF", "CB", "CAM", "LB", "RB", "RM", "LWB", "RWB"]] - User
- Assistant
The error "UndefVarError: RW_idx not defined" occurs because the
@evalblock that defines the variables like "RW_idx", "ST_idx", etc., is not evaluated correctly in all workers. You can resolve this issue by adding abroadcaststatement immediately after the list comprehension that creates variables for positions.Here's the corrected code:
juliaaddprocs() @everywhere using DataFrames # Load Data data = CSV.read("C:/Users/Użytkownik/Desktop/player22.csv", DataFrame) (position -> (global _position; @eval begin (_position)_idx = findall(occursin.( (_position), data.Positions)) end)(_position) for position in ["RW", "ST", "GK", "CM", "LW", "CDM", "LM", "CF", "CB", "CAM", "LB", "RB", "RM", "LWB", "RWB"]) # Add this line to define the variables in workers broadcast(x -> (@eval(Main, x)), keys(position_vectors)) 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] # ... rest of the code