Edit or run this notebook
5.7 s

Demo for the PlutoCon 201

Author: Daniel Molina Cabrera

Talk: Teaching Computer Sciences and Metaheuristics Algorithms using Pluto

8.4 μs

Maximize 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 MN that maximize iNjNDij.

10.9 μs
showvalues (generic function with 1 method)
21.7 μs

Initial Set

Then, from the following data:

4.1 μs
scene (generic function with 1 method)
69.4 μs

n:

47.1 ms
4.2 s

Distance of the problem

We are not going to work with the points, we are going to work using the distances between them.

3.6 μs

ArgumentError: `DataFrame` constructor from a `Matrix` requires passing :auto as a second argument to automatically generate column names: `DataFrame(matrix, :auto)`

  1. DataFrames.DataFrame(::Matrix{Float64})@dataframe.jl:363
  2. top-level scope@Local: 3[inlined]
---

Problem: We choose the m value and the distance measure

2.8 μs

m value:

6.5 ms
metrica (generic function with 2 methods)
48.5 μs

Solution for that problem

A random solution could be create easily choosing randomly m variable without repetition.

3.7 μs
30.2 μs

random solution: {2, 4, 8}

148 ms

The interpretation of the solution will be:

3.5 μs
plot_sol (generic function with 1 method)
78.9 μs

In which the selected instances are in red, and the connexions with dashed lines. The fitness is obtained with the measure function.

3.1 μs

UndefVarError: dists not defined

  1. metrica(::Vector{Int64})@Other: 2
  2. plot_sol(::Vector{Int64})@Other: 10
  3. top-level scope@Local: 1
---

Local Search algorithm

The local search starts from an initial solution, and it is continuously improved. It can be described as:

  1. Initial current solution is a random solution S.

  2. If it has not yet finished (less than 100_000 evaluations or local optimum) continue steps 3-7.

  3. Choose randomly a position i from S.

  4. It create a new solution, in which the value of position i is changed by a new value j, where j{Nsolucion[i]}.

  5. Compare the new solution S' with the previous one.

  6. If S' fitness is worse than the S fitness, go to step 2.

  7. If it is better SS and go to step 2.

16.4 μs
BL (generic function with 1 method)
93.5 μs

We apply the LS during iterations

60.1 μs

UndefVarError: dists not defined

  1. metrica(::Vector{Int64})@Other: 2
  2. var"#BL#4"(::Int64, ::Nothing, ::typeof(Main.workspace75.BL), ::Vector{Int64})@Other: 3
  3. top-level scope@Local: 2
---

UndefVarError: best not defined

  1. top-level scope@Local: 1
---

We can see dynamically how it improves

3.1 μs

Iteration:

23.9 ms

UndefVarError: dists not defined

  1. metrica(::Vector{Int64})@Other: 2
  2. var"#BL#4"(::Int64, ::Nothing, ::typeof(Main.workspace75.BL), ::Vector{Int64})@Other: 3
  3. top-level scope@Local: 2
---
showdf (generic function with 1 method)
20.8 μs
plot_greedy (generic function with 1 method)
85.6 μs

Greedy 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.

4.1 μs

Algorithm

  1. Define Sel=, y S={todos los elementos}.

  2. Calculate for each element i i its accumulated distance di=jSDij

11.8 μs
accumulated_distance

UndefVarError: dists not defined

  1. top-level scope@Local: 1
---
  1. Set to Sel the element sol1 which maximize di, S={Ssoli}.

6.3 μs
44.0 μs

UndefVarError: dists not defined

  1. top-level scope@Local: 4
---
  1. for each element c from S, to calculate the minimum distance to each element of Set CS,dist(C,Sel)=min(DCsol),solSel

5.6 μs

UndefVarError: others not defined

  1. top-level scope@Local: 1155
---

UndefVarError: others not defined

  1. top-level scope@Local: 1
---
  1. Choose the element c' with maximum dist(C, Sel).

4.5 μs
select_best (generic function with 1 method)
54.6 μs

Next step:

25.4 ms

UndefVarError: Sel not defined

  1. top-level scope@Local: 3
---

UndefVarError: Sel not defined

  1. top-level scope@Local: 9
---
Loading...i