Skip to main content

MHSA - Metaheuristics Similarity Analyzer

Project description

MHSA - Metaheuristics Similarity Analyzer

This repository contains the source code of the experiments in the paper Measuring the similarity of metaheuristic search strategies with machine learning models. MHSA provides an alternative way to compare and analyze metaheuristic-search strategies with the help of machine learning.

Usage

To use MHSA for similarity analysis we first have to define the gene spaces which will be used by the genetic algorithm. First key of the gene space dictionary must correspond with the class name of the the algorithm which must be implemented in the NiaPy micro-framework. In this case we chose BatAlgorithm and ParticleSwarmAlgorithm.

PSA_gene_spaces = {
    "ParticleSwarmAlgorithm": {
        "c1": {"low": 0.01, "high": 2.5, "step": 0.01},
        "c2": {"low": 0.01, "high": 2.5, "step": 0.01},
        "w": {"low": 0.0, "high": 1.0, "step": 0.01},
    }
}
BA_gene_spaces = {
    "BatAlgorithm": {
        "loudness": {"low": 0.01, "high": 1.0, "step": 0.01},
        "pulse_rate": {"low": 0.01, "high": 1.0, "step": 0.01},
        "alpha": {"low": 0.9, "high": 1.0, "step": 0.001},
        "gamma": {"low": 0.0, "high": 1.0, "step": 0.01},
    }
}

We also have to chose diversity metrics which will be used as the basis of the analysis and the optimization problem the metaheuristics are going to solve.

from mhsa.diversity_metrics.population_diversity.dpc import DPC
from mhsa.diversity_metrics.population_diversity.fdc import FDC
from mhsa.diversity_metrics.population_diversity.pfsd import PFSD
from mhsa.diversity_metrics.population_diversity.pfm import PFM
from mhsa.diversity_metrics.individual_diversity.idt import IDT
from mhsa.diversity_metrics.individual_diversity.isi import ISI
from mhsa.diversity_metrics.individual_diversity.ifm import IFM
from mhsa.diversity_metrics.individual_diversity.ifiqr import IFIQR
from mhsa.problems.schwefel import Schwefel

OPTIMIZATION_PROBLEM = Schwefel(dimension=20)

POP_DIVERSITY_METRICS = [
    DPC(OPTIMIZATION_PROBLEM),
    FDC(OPTIMIZATION_PROBLEM, [420.968746], True),
    PFSD(),
    PFM(),
]
INDIV_DIVERSITY_METRICS = [
    IDT(),
    ISI(),
    IFM(),
    IFIQR(),
]

In the next step we have to instantiate the MetaGA class which uses the GA class from the PyGAD library. At this point we decide which of the algorithms will be analyzed and which will be the "reference" or "target" algorithm. The gene space of the analyzed algorithm gets assigned to the gene_spaces argument of the MetaGA.

from mhsa.tools.meta_ga import MetaGA, MetaGAFitnessFunction

meta_ga = MetaGA(
    fitness_function_type=MetaGAFitnessFunction.TARGET_PERFORMANCE_SIMILARITY,
    ga_generations=20,
    ga_solutions_per_pop=15,
    ga_percent_parents_mating=60,
    ga_parent_selection_type="tournament",
    ga_k_tournament=2,
    ga_crossover_type="uniform",
    ga_mutation_type="random",
    ga_crossover_probability=0.9,
    ga_mutation_num_genes=1,
    ga_keep_elitism=1,
    gene_spaces=BA_gene_spaces,
    pop_size=30,
    max_evals=10000,
    num_runs=30,
    problem=OPTIMIZATION_PROBLEM,
    pop_diversity_metrics=POP_DIVERSITY_METRICS,
    indiv_diversity_metrics=INDIV_DIVERSITY_METRICS,
)

In the last step we have to instantiate the MetaheuristicSimilarityAnalyzer class and pass it the configured MetaGA instance and the gene space of the target algorithm. Then we simply call the run_similarity_analysis method to start the analysis.

from mhsa.tools.metaheuristics_similarity_analyzer import MetaheuristicsSimilarityAnalyzer

mhsa = MetaheuristicsSimilarityAnalyzer(meta_ga=meta_ga, target_gene_space=PSA_gene_spaces)

mhsa.run_similarity_analysis(
    num_comparisons=10
    get_info=True,
    generate_dataset=True,
    calculate_similarity_metrics=True,
    export=True,
)

After the analysis we can choose to export results of the analysis as a .pdf and/or .tex file with export_results_to_latex method or access them trough the MetaheuristicsSimilarityAnalyzer class instance.

mhsa.export_results_to_latex(generate_pdf=True)

For more information check out examples.

This project depends on

NiaPy Python microframework

PyGAD Python library

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mhsa-1.1.0.tar.gz (40.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mhsa-1.1.0-py3-none-any.whl (50.9 kB view details)

Uploaded Python 3

File details

Details for the file mhsa-1.1.0.tar.gz.

File metadata

  • Download URL: mhsa-1.1.0.tar.gz
  • Upload date:
  • Size: 40.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0.dev0 CPython/3.10.18 Linux/6.6.87.2-microsoft-standard-WSL2

File hashes

Hashes for mhsa-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d411b8c0a43dd5b001b17697be3c4b6f0a182616a07e51d6fb7df1834663df50
MD5 38d44741be13115c9a626acaf021e2fb
BLAKE2b-256 979d1a56bc9eeb8de5501ea46b0c2e9887eb0a293c8118db5c7ae07aa2198016

See more details on using hashes here.

File details

Details for the file mhsa-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: mhsa-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 50.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0.dev0 CPython/3.10.18 Linux/6.6.87.2-microsoft-standard-WSL2

File hashes

Hashes for mhsa-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d96d359bc22d38341bcb9e434981493d7ed3d5ef4e06e5c4f71ef4e1943a060
MD5 3ae007df26eebc048be48fb0021f9406
BLAKE2b-256 ddf3f14203559c2a0d1b00bfc5bffead24d7f0649c0963da0ecebd1318acd345

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page