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 returnreplace (
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)
- local_search(swap_count=10)
Do a local search for each individual of the population.
- Parameters
swap_count (
int) – number of swap for each of the 2-opt optimization
- post_local_search(count=10, swap_count=2000)
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 executedswap_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:
objectThis class is a Facade for the different Sankey optimizers.
- Parameters
graph (
EvolvingCommunitiesGraph) – community flow graphalgo (
Literal[‘SA’, ‘GA’, ‘HA’]) – chosen optimizer typetime_max (
float) – maximum time allocated to optimization processoptimizer_kwargs – keyworded arguments passed to optimizer constructor
- init_optimizer(algo, **kwargs)
Create and initialize actual sankey optimizer.
- Parameters
algo (
Literal[‘SA’, ‘GA’, ‘HA’]) – chosen algorithmkwargs – 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