function GA2(crossoverpoint=7, populationsize=100, numgenerations=10, tournamentsize=2,…
- User
function GA2(crossover_point=7, population_size=100, num_generations=10, tournament_size=2, probability=0.09, weight1=0.15, weight2=0.6, weight3=0.3, contraint_1=250000000, constraint2=250000, contraint_3=1.2)
nrows=population_size parents = mutate(pop_init, position_vectors, 1.0) global_best = pop_init[1, :] global_best_value = target2(global_best,6,weight1,weight2,weight3,contraint_1,constraint2,contraint_3) penalty=1 # Main loop for gen in 1:num_generations # Parent population parent_pop = create_ones_dataframe(n_rows, length(position_vectors)) parent_pop = parent_pop[1:n_rows, :] for c in 1:population_size parent_pop[c, :] = tournament_selection2(parents, tournament_size, penalty,weight1,weight2,weight3,contraint_1,constraint2,contraint_3) end # Generate offspring offspring_temp = create_ones_dataframe(n_rows, length(position_vectors)) offspring_temp = offspring_temp[1:n_rows, :] for c in 1:2:population_size offsprings = crossover(parent_pop[c, :], parent_pop[c + 1, :], crossover_point) offspring_temp = vcat(offspring_temp, offsprings) end offspring_temp = offspring_temp[nrows+1:end, :] parents = mutate(offspring_temp, position_vectors, 0.09) # Evaluate solutions solutions = [target2(parent,6,weight1,weight2,weight3,contraint_1,constraint2,contraint_3) for parent in eachrow(parents)] idx_sol = argmin(solutions) temp_best = parents[idx_sol, :] temp_target_value = solutions[idx_sol] if penalty==4 penalty=0 else penalty+0.5 end if temp_target_value <= global_best_value global_best = temp_best global_best_value = temp_target_value end end return global_best, global_best_valueend
I want to iterate over diffrent weight1=0.05:0.05:0.2, weight2=0:1:0:1:0:9 and weight3=0.1:0.1:0.9
For each of those combinations I need DataFrame of global_best and global_best_value. I also need to know exactly for which combination of weight1,weigh2 and weight3 the output of GA2 are.
- Assistant
You can achieve this by iterating over the different combinations of weights and storing the results in a dictionary or a DataFrame. Here's an example using a dictionary:
# Initialize a results dictionary results = Dict{Tuple{Float64, Float64, Float64}, Tuple{Array{Float64,1}, Float64}}() # Iterate over the different weight combinations for w1 in 0.05:0.05:0.2 for w2 in 0:0.1:0.9 for w3 in 0.1:0.1:0.9 # Run the GA2 function with the current weight combination global_best, global_best_value = GA2(weight1=w1, weight2=w2, weight3=w3) # Store the results in the dictionary with the weight combination as the key results[(w1, w2, w3)] = (global_best, global_best_value) end end end # Now you can access the results dictionary to find the global_best and global_best_value for any weight combinationFurthermore, if you want to store the results in a DataFrame, you can do the following:
using DataFrames # Initialize an empty DataFrame with the desired columns results_df = DataFrame(W1=Float64[], W2=Float64[], W3=Float64[], Best_Solution=Vector{Float64}[], Best_Value=Float64[]) # Iterate over the different weight combinations for w1 in 0.05:0.05:0.2 for w2 in 0:0.1:0.9 for w3 in 0.1:0.1:0.9 # Run the GA2 function with the current weight combination global_best, global_best_value = GA2(weight1=w1, weight2=w2, weight3=w3) # Append a row to the dataframe with the results push!(results_df, (w1, w2, w3, global_best, global_best_value)) end end end # Now the results_df DataFrame contains the results for each weight combination