sankey_optimization

This module is used to optimize sankey diagram for better readability.

Todo

  • document rest of code (only new Facade currently is)

  • reorganize classes for better integration (useful to remove Facade next)

Optimize a sankey diagram.

python sankey_optimization.py [-h] [--stats STATS]
    [--stop STOP] [-o OUT] [-a {GA,HA,SA}] sankey

Positional arguments:
* sankey                sankey file

Options:
* -h, --help            show this help message and exit
* --stats STATS         name of a csv file which will be used to save the
                        algorithm statistics
* --stop STOP           stop at this iteration
* -o OUT, --out OUT     output directory
* -a {GA,HA,SA}, --algorithm {GA,HA,SA}
                        optimization algorithm
class dyn.drawing.sankey_optimization.GeneticAlgorithm(environment, population=None, crossover_rate=0.7, mutation_rate=0.2, crossover_p=None, mutation_p=None)

Bases: object

create_random_population(size)
crossover_cycle(indiv1, indiv2)

Return two children using a cycle crossover.

crossover_layer(indiv1, indiv2)

Randomly choose layers from the first or the second individual.

crossover_ordered(indiv1, indiv2)

Return two children using an ordered crossover.

crossover_random(indiv1, indiv2, p=None)

Randomly apply a crossover method for the two individuals.

get_best_individual()

Return the best individual in the population.

init_random_population(size)
iterate()

Execute an iteration of the algorithm.

iterator(stop=None)
replace_duplicate_individuals()

Replace duplicate individuals by random individuals.

select_individuals(count=2)
select_rank_roulette(count=None, replace=True)

Select individuals using a rank roulette.

Parameters
  • count (Optional[int]) – number of individuals to return

  • replace (bool) – can return several times the same individual

select_tournament(size=2)

Organize a tournament selection to obtain an individual in the population.

Parameters

size (int) – size of the random subpopulation amongst which the most fit individual will be selected

class dyn.drawing.sankey_optimization.HybridGeneticAlgorithm(environment, population=None, crossover_rate=0.7, mutation_rate=0.2, crossover_p=None, mutation_p=None)

Bases: GeneticAlgorithm

init_random_population(size)

Create the population and do local search on half of the population.

iterator(stop=None)

Do a local search for each individual of the population.

Parameters

swap_count (int) – number of swap for each of the 2-opt optimization

Do a local search to try to optimize the final result.

Optimize the best individual using a 2-opt swap.

Parameters
  • count (int) – number of times the local search will be executed

  • swap_count (int) – number of swap for each of the 2-opt optimization

class dyn.drawing.sankey_optimization.SankeyOptimizer(graph, algo='SA', time_max=30, **optimizer_kwargs)

Bases: object

This class is a Facade for the different Sankey optimizers.

Parameters
  • graph (EvolvingCommunitiesGraph) – community flow graph

  • algo (Literal[‘SA’, ‘GA’, ‘HA’]) – chosen optimizer type

  • time_max (float) – maximum time allocated to optimization process

  • optimizer_kwargs – keyworded arguments passed to optimizer constructor

init_optimizer(algo, **kwargs)

Create and initialize actual sankey optimizer.

Parameters
  • algo (Literal[‘SA’, ‘GA’, ‘HA’]) – chosen algorithm

  • kwargs – parameters passed to actual sankey optimizer

run()

Run sankey optimization.

Return type

EvolvingCommunitiesGraph

Returns

optimized community flow graph

property state_string: str

Optimizer state expressed for logger/print

Return type

str

class dyn.drawing.sankey_optimization.SimulatedAnnealing(solution, temp_0, temp_end=1, cooling='exponential')

Bases: object

iterate(alpha, steps=1)

Iterate one step of the simulated annealing.

iterator(alpha, steps=1, iterations=None)

Start the simulated annealing algorithm.