xxxxxxxxxxDemo for the PlutoCon 201
Author: Daniel Molina Cabrera
Talk: Teaching Computer Sciences and Metaheuristics Algorithms using Pluto
xxxxxxxxxxMaximize Diversity Problem (MDP)
In this problem we have a set of data n and the target is to choose m of them (n < m) to maximize the distance between the selected. In other words, it is wanted
xxxxxxxxxxshowvalues (generic function with 1 method)xxxxxxxxxxInitial Set
Then, from the following data:
xxxxxxxxxxscene (generic function with 1 method)xxxxxxxxxxn:
xxxxxxxxxxxxxxxxxxxxDistance of the problem
We are not going to work with the points, we are going to work using the distances between them.
xxxxxxxxxxArgumentError: `DataFrame` constructor from a `Matrix` requires passing :auto as a second argument to automatically generate column names: `DataFrame(matrix, :auto)`
- DataFrames.DataFrame(::Matrix{Float64})@dataframe.jl:363
- top-level scope@Local: 3[inlined]
begin dists = pairwise(Euclidean(), problem, dims=1) df = DataFrame(dists) rename!(df, string.(1:n))endProblem: We choose the m value and the distance measure
xxxxxxxxxxm value:
xxxxxxxxxxmetrica (generic function with 2 methods)xxxxxxxxxxfunction metrica(solucion, dists=dists) distancias = Float64[] for i in 1:m for j in (i+1):m push!(distancias, dists[solucion[i],solucion[j]]) end end return sum(distancias)endSolution for that problem
A random solution could be create easily choosing randomly m variable without repetition.
xxxxxxxxxxxxxxxxxxxxrandom solution: {2, 4, 8}
xxxxxxxxxxThe interpretation of the solution will be:
xxxxxxxxxxplot_sol (generic function with 1 method)xxxxxxxxxxIn which the selected instances are in red, and the connexions with dashed lines. The fitness is obtained with the measure function.
xxxxxxxxxxxxxxxxxxxxplot_sol(solucion)Local Search algorithm
The local search starts from an initial solution, and it is continuously improved. It can be described as:
Initial current solution is a random solution S.
If it has not yet finished (less than 100_000 evaluations or local optimum) continue steps 3-7.
Choose randomly a position i from S.
It create a new solution, in which the value of position i is changed by a new value j, where
.Compare the new solution S' with the previous one.
If S' fitness is worse than the S fitness, go to step 2.
If it is better
and go to step 2.
xxxxxxxxxxBL (generic function with 1 method)xxxxxxxxxxWe apply the LS during
xxxxxxxxxxxxxxxxxxxxbegin best, best_fit = BL(solucion, maxevals=maxevals) best_sol = join(string.(best), ", ") total = maxevals md""" The best found solution is **\{$(best_sol)\}** with fitness: **$(round(best_fit,digits=3))** """endUndefVarError: best not defined
- top-level scope@Local: 1
xxxxxxxxxxplot_sol(best)We can see dynamically how it improves
xxxxxxxxxxIteration:
xxxxxxxxxxxxxxxxxxxxbegin best_dyn, best_fit_dyn = BL(solucion, maxevals=slider) plot_sol(best_dyn)endshowdf (generic function with 1 method)xxxxxxxxxxplot_greedy (generic function with 1 method)xxxxxxxxxxGreedy Algorithm
This algorithm tackle the previous problem. It is a variant of classic algorithm because we cannot create clusters due to the fact that we only know the distances between instances, so we cannot create intermediate positions.
xxxxxxxxxxAlgorithm
Define
, y ={todos los elementos}.Calculate for each element i i its accumulated distance
xxxxxxxxxxUndefVarError: dists not defined
- top-level scope@Local: 1
xxxxxxxxxxaccumulated_distance = rename!(sum(dists, dims=2) |> showdf, ["Distance"])Set to
the element which maximize , .
xxxxxxxxxxxxxxxxxxxxUndefVarError: dists not defined
- top-level scope@Local: 4
xxxxxxxxxxbegin inicia_greedy local best best = argmax(sum(dists, dims=1)) Sel = [best[2]] others = setdiff(1:n, Sel) |> collect first = true tam_others = n-1 md"Sel: **$(showvalues(Sel))**"endfor each element c from S, to calculate the minimum distance to each element of Set
xxxxxxxxxxUndefVarError: others not defined
- top-level scope@Local: 1155
xxxxxxxxxx candidate_greedy Slider(1:size(others, 1))UndefVarError: others not defined
- top-level scope@Local: 1
xxxxxxxxxxif candidate_greedy <= size(others, 1) plot_greedy(Sel, others[candidate_greedy])else plot_greedy(Sel, others[end])endChoose the element c' with maximum dist(C, Sel).
xxxxxxxxxxselect_best (generic function with 1 method)xxxxxxxxxxNext step:
xxxxxxxxxxUndefVarError: Sel not defined
- top-level scope@Local: 3
xxxxxxxxxxbegin if !doit best_next = select_best(Sel, others) md"Next better: **$(others[best_next])**" endendUndefVarError: Sel not defined
- top-level scope@Local: 9
xxxxxxxxxxbegin local best if doit && size(Sel, 1) < m best = select_best(Sel, others) push!(Sel, others[best]) deleteat!(others, best) end md"Sel: **$(showvalues(Sel))**, Others: **$(showvalues(others))**"end