Harmony Search (pyHarmonySearch)

Music-Inspired Metaheuristic Algorithm

Harmony Search (Geem, 2001) maintains a "harmony memory" (HM) of the HMS best solutions found so far and improvises a new candidate each iteration. For each coordinate: with probability HMCR, copy from a randomly selected memory entry, optionally adjusting the value with probability PAR; with probability 1 − HMCR, draw a uniform random value. Then evaluate and replace the worst harmony if the new one is better. HumpDay's HarmonySearch is the canonical formulation.

HumpDay's HarmonySearch at a glance

  • Memory size: HMS, typically 5-15.
  • Harmony memory considering rate: HMCR ≈ 0.9 — high memory bias.
  • Pitch adjusting rate: PAR ≈ 0.3 — chance of locally perturbing a memory-copied value.
  • Per iteration: one new harmony evaluated; if better than the current worst, the worst is replaced and the memory is re-sorted.

Interactive 3D Visualization

See Harmony Search in action on 3D optimization surfaces:

Loading 3D visualization...

Requires WebGL support

Instructions: Choose a test function and algorithm, then click Start to watch the step-by-step optimization process. Each step shows where the algorithm decides to move next.

Implementation Details

Component Details Links
Original Algorithm Zong Woo Geem
Music-inspired optimization metaheuristic
Based on musical improvisation and harmony composition
Published: 2001
Original Paper
Reference Implementation mealpy HS
Tested against mealpy's Original Harmony Search.
Reference: mealpy.music_based.HS.OriginalHS
mealpy
HumpDay Python HumpDay HarmonySearch
Canonical Harmony Search: HM + HMCR + PAR + random selection. One new harmony per iteration.
Pure Python; no required dependencies.
File: humpday/optimizers/evolutionary_algorithms.py
Source
HumpDay JavaScript Browser HarmonySearch
Mirrors the Python port.
Class: HarmonySearch
JS Implementation

Performance Characteristics

  • Best for: Mid-range black-box objectives where memory-based exploitation balances against occasional random exploration.
  • Worst for: Smooth basins where directional methods (CMA-ES, PRIMA) converge much faster, and high dimensions where the memory bias struggles.
  • Per iteration: one new harmony evaluated; memory update is O(HMS).
  • Relative to mealpy HS at n_trials=200, n_dim=2: HumpDay wins across sphere, Rosenbrock, and Ackley. See SOTA status for the live numbers.

Educational Resources

Core Algorithm Components

  • Harmony Memory (HM): Population of HMS best solutions found
  • Harmony Memory Size (HMS): Number of solutions stored
  • Memory Consideration Rate (HMCR): Probability of choosing from memory
  • Pitch Adjusting Rate (PAR): Probability of local adjustment
  • Pitch Adjustment: Local search around selected value
  • Random Selection: Generate completely new value

Mathematical Formulation

  • Memory Selection: x'ᵢ = HMⱼ(i) with probability HMCR
  • Pitch Adjustment: x'ᵢ = x'ᵢ ± rand() × bw with probability PAR
  • Random Selection: x'ᵢ ~ Uniform(LBᵢ, UBᵢ) with probability (1-HMCR)
  • Memory Update: Replace worst harmony if new harmony is better

️ Key Parameters

  • HMS: Harmony Memory Size, typically 10-50
  • HMCR: Memory Consideration Rate, usually 0.7-0.95
  • PAR: Pitch Adjusting Rate, usually 0.1-0.5
  • bw: Bandwidth for pitch adjustment
  • Termination: Maximum iterations or convergence criteria

Harmony Search Algorithm Pseudocode

function harmonySearch(objectiveFunction, HMS, HMCR, PAR, maxIter):
    // Step 1: Initialize Harmony Memory
    HM = []
    for i in 1 to HMS:
        harmony = generateRandomSolution()
        HM.add(harmony, objectiveFunction(harmony))

    sortByFitness(HM)

    // Step 2: Improvise New Harmony
    for iteration in 1 to maxIter:
        newHarmony = []

        for j in 1 to problemDimension:
            if random() < HMCR:  // Memory consideration
                // Choose value from memory
                randomHarmonyIndex = randomInt(0, HMS-1)
                newValue = HM[randomHarmonyIndex][j]

                if random() < PAR:  // Pitch adjustment
                    newValue += (random() - 0.5) * 2 * bandwidth
                    newValue = clip(newValue, lowerBound, upperBound)

            else:  // Random selection
                newValue = random() * (upperBound - lowerBound) + lowerBound

            newHarmony[j] = newValue

        // Step 3: Update Harmony Memory
        newFitness = objectiveFunction(newHarmony)

        if newFitness < HM[HMS-1].fitness:  // Better than worst
            HM[HMS-1] = (newHarmony, newFitness)
            sortByFitness(HM)

    return HM[0]  // Best harmony
                    

Harmony Search Variants

  • Classical HS: Original algorithm with fixed parameters
  • Improved HS (IHS): Dynamic PAR and bandwidth adjustment
  • Self-Adaptive HS: Automatic parameter tuning
  • Global-Best HS: Use best harmony for pitch adjustment
  • Differential HS: Incorporate differential evolution operators
  • Multi-Objective HS: Pareto-based harmony search

Musical Analogy Details

  • Musician: Decision variable (instrument)
  • Musical Note: Value of decision variable
  • Harmony: Complete solution vector
  • Harmony Memory: Collection of good musical pieces
  • Improvisation: Generation of new solution
  • Aesthetic Quality: Objective function value

Applications

  • Engineering Design: Structural optimization, water distribution
  • Energy Systems: Power system optimization, renewable energy
  • Manufacturing: Production scheduling, resource allocation
  • Transportation: Vehicle routing, traffic optimization
  • Machine Learning: Feature selection, neural network training
  • Economics: Portfolio optimization, supply chain management

Advantages of Harmony Search

  • Simplicity: Easy to understand and implement
  • Few Parameters: Only HMS, HMCR, PAR to tune
  • Memory-Based: Maintains population of good solutions
  • Flexibility: Adapts to continuous and discrete problems
  • Balance: Good trade-off between exploration and exploitation
  • No Gradient Needed: Works with black-box functions

️ Limitations

  • Parameter Sensitivity: Performance depends on HMCR and PAR
  • Premature Convergence: May converge too quickly with wrong parameters
  • Slow Convergence: Can be slow on some problem types
  • Local Search Ability: Limited fine-tuning capability
  • Memory Management: Fixed memory size may be suboptimal

Parameter Guidelines

  • HMCR = 0.9: High memory consideration for exploitation
  • HMCR = 0.1: Low memory consideration for exploration
  • PAR = 0.3: Moderate pitch adjustment
  • HMS = 10-30: Small memory for fast convergence
  • HMS = 50-100: Large memory for diverse population
  • Dynamic Parameters: Adapt HMCR, PAR during search

Creative Aspects

  • Artistic Inspiration: Based on human creativity and musical composition
  • Improvisation Process: Mimics how musicians create new melodies
  • Harmony Aesthetics: Better solutions are like more beautiful music
  • Cultural Bridge: Connects optimization with arts and music
  • Intuitive Understanding: Easy to explain through musical metaphor

Comparison with Other Metaheuristics

  • vs GA: Memory-based vs generation-based evolution
  • vs PSO: Discrete memory vs continuous population movement
  • vs SA: Population-based vs single solution approach
  • vs ACO: Direct memory vs pheromone-based communication
  • vs DE: Musical improvisation vs biological evolution