eispy2d.evoalglib package#

Submodules#

eispy2d.evoalglib.boundary module#

class eispy2d.evoalglib.boundary.BoundaryCondition#

Bases: ABC

Abstract base class for boundary conditions in evolutionary algorithms.

Boundary conditions handle how variables that go outside the allowed range are treated during evolution. This class defines the interface for all boundary handling strategies.

run(x)#

Apply boundary condition to the solution vector.

abstractmethod run(x)#

Apply boundary condition to the solution vector.

Parameters:

x (numpy.ndarray) – Solution vector (1D or 2D) to be processed.

class eispy2d.evoalglib.boundary.Reflection#

Bases: BoundaryCondition

Reflection boundary condition.

Reflects values that fall outside [0, 1] back into the range using a reflection strategy. Values are reflected multiple times if needed.

Notes

This boundary handling strategy preserves the interval [0, 1] by reflecting out-of-bounds values back into the valid range using a mirroring technique.

run(x)#

Apply reflection to the solution vector.

Parameters:

x (numpy.ndarray) – Solution vector to be reflected.

class eispy2d.evoalglib.boundary.Truncation#

Bases: BoundaryCondition

Truncation boundary condition.

Clips values to the [0, 1] range. Values below 0 are set to 0, values above 1 are set to 1.

Notes

This is a simple boundary handling strategy that preserves the interval [0, 1] by clipping out-of-bounds values.

run(x)#

Apply truncation to the solution vector.

Parameters:

x (numpy.ndarray) – Solution vector to be truncated.

eispy2d.evoalglib.crossover module#

class eispy2d.evoalglib.crossover.Binomial(crossover_rate)#

Bases: Crossover

Binomial crossover operator.

Creates offspring by mixing variables from two parents based on a crossover rate. Commonly used in Differential Evolution.

Parameters:

crossover_rate (float) – Probability of taking variable from the second parent.

Notes

Binomial crossover is the standard crossover operator in DE. Each variable has crossover_rate probability of coming from the second parent, otherwise from the first.

copy(new=None)#

Create a copy of the crossover operator.

Parameters:

new (Crossover, optional) – If provided, copies data into this object. If None, creates a new copy.

Return type:

Crossover or None

run(x1, x2, fx1, fx2, probability=None)#

Perform crossover operation.

Parameters:
  • x1 (numpy.ndarray) – Parent solutions.

  • x2 (numpy.ndarray) – Parent solutions.

  • fx1 (numpy.ndarray or float) – Fitness values of parents.

  • fx2 (numpy.ndarray or float) – Fitness values of parents.

  • probability (float, optional) – Crossover probability.

Returns:

(offspring, fitness) where fitness may be NaN for new offspring.

Return type:

tuple

class eispy2d.evoalglib.crossover.Convex(extrapolation=0)#

Bases: Crossover

Convex crossover operator.

Creates offspring as a convex combination of parents: x = u*x1 + (1-u)*x2 where u is random in [-xi, 1+xi].

Parameters:

extrapolation (float, default=0) – Extrapolation factor. Values > 0 allow exploration outside the convex hull of parents.

Notes

When extrapolation > 0, the operator can produce solutions outside the parent range, enabling better exploration.

copy(new=None)#

Create a copy of the crossover operator.

Parameters:

new (Crossover, optional) – If provided, copies data into this object. If None, creates a new copy.

Return type:

Crossover or None

run(x1, x2, fx1, fx2, probability=None)#

Perform crossover operation.

Parameters:
  • x1 (numpy.ndarray) – Parent solutions.

  • x2 (numpy.ndarray) – Parent solutions.

  • fx1 (numpy.ndarray or float) – Fitness values of parents.

  • fx2 (numpy.ndarray or float) – Fitness values of parents.

  • probability (float, optional) – Crossover probability.

Returns:

(offspring, fitness) where fitness may be NaN for new offspring.

Return type:

tuple

class eispy2d.evoalglib.crossover.Crossover#

Bases: ABC

Abstract base class for crossover operators in evolutionary algorithms.

Crossover operators combine two parent solutions to produce offspring.

run(x1, x2, fx1, fx2, probability=None)#

Perform crossover operation.

copy(new=None)#

Create a copy of the crossover operator.

copy(new=None)#

Create a copy of the crossover operator.

Parameters:

new (Crossover, optional) – If provided, copies data into this object. If None, creates a new copy.

Return type:

Crossover or None

abstractmethod run(x1, x2, fx1, fx2, probability=None)#

Perform crossover operation.

Parameters:
  • x1 (numpy.ndarray) – Parent solutions.

  • x2 (numpy.ndarray) – Parent solutions.

  • fx1 (numpy.ndarray or float) – Fitness values of parents.

  • fx2 (numpy.ndarray or float) – Fitness values of parents.

  • probability (float, optional) – Crossover probability.

Returns:

(offspring, fitness) where fitness may be NaN for new offspring.

Return type:

tuple

class eispy2d.evoalglib.crossover.Discrete#

Bases: Crossover

Discrete crossover operator.

Creates offspring by taking each variable from either parent with equal probability. Also known as uniform crossover.

Notes

For 1D inputs, each variable is independently chosen from either parent. For 2D inputs, the operation is applied row-wise.

copy(new=None)#

Create a copy of the crossover operator.

Parameters:

new (Crossover, optional) – If provided, copies data into this object. If None, creates a new copy.

Return type:

Crossover or None

run(x1, x2, fx1, fx2, probability=None)#

Perform crossover operation.

Parameters:
  • x1 (numpy.ndarray) – Parent solutions.

  • x2 (numpy.ndarray) – Parent solutions.

  • fx1 (numpy.ndarray or float) – Fitness values of parents.

  • fx2 (numpy.ndarray or float) – Fitness values of parents.

  • probability (float, optional) – Crossover probability.

Returns:

(offspring, fitness) where fitness may be NaN for new offspring.

Return type:

tuple

class eispy2d.evoalglib.crossover.SimulatedBinary(eta)#

Bases: Crossover

Simulated Binary Crossover (SBX) operator.

Creates offspring by simulating the behavior of binary crossover in real-valued spaces. The distribution index eta controls the spread of offspring around parents.

Parameters:

eta (float) – Distribution index. Higher values produce offspring closer to parents.

Notes

SBX is commonly used in real-coded genetic algorithms and produces offspring with a probability distribution that mimics binary crossover.

copy(new=None)#

Create a copy of the crossover operator.

Parameters:

new (Crossover, optional) – If provided, copies data into this object. If None, creates a new copy.

Return type:

Crossover or None

run(x1, x2, fx1, fx2, probability=None)#

Perform crossover operation.

Parameters:
  • x1 (numpy.ndarray) – Parent solutions.

  • x2 (numpy.ndarray) – Parent solutions.

  • fx1 (numpy.ndarray or float) – Fitness values of parents.

  • fx2 (numpy.ndarray or float) – Fitness values of parents.

  • probability (float, optional) – Crossover probability.

Returns:

(offspring, fitness) where fitness may be NaN for new offspring.

Return type:

tuple

eispy2d.evoalglib.de module#

class eispy2d.evoalglib.de.DifferentialEvolution(boundary_condition, selection, mutation, scaling_factor, crossover, pcross=1.0, index_selection='random')#

Bases: Mechanism

Differential Evolution (DE) evolutionary mechanism.

Implements various DE mutation strategies including: - Rand: DE/rand/1 - Best: DE/best/1 - Current-to-best: DE/current-to-best/1 - Rand-to-best: DE/rand-to-best/1

Parameters:
  • boundary_condition (BoundaryCondition) – Boundary handling strategy.

  • selection (Selection) – Selection operator.

  • mutation ({'rand', 'best', 'current-to-best', 'rand-to-best'}) – DE mutation strategy.

  • scaling_factor (float) – Mutation scaling factor (F).

  • crossover (Crossover) – Crossover operator.

  • pcross (float, default=1.0) – Crossover probability.

  • index_selection ({'random', 'permutation'}, default='random') – How to select indices for mutation.

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of DE.

copy(new=None)#
reset_variables(population_size, representation)#

Reset internal variables for a new run.

Parameters:
  • population_size (int) – Size of the population.

  • representation (Representation) – Solution representation.

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of Differential Evolution.

Parameters:
  • population (numpy.ndarray) – Current population matrix (POP × NVAR).

  • population_fitness (numpy.ndarray) – Fitness values of current population.

  • objective_function (ObjectiveFunction) – Objective function to evaluate.

  • current_nevals (int) – Current number of evaluations.

Returns:

(population, population_fitness, new_evaluation_count)

Return type:

tuple

eispy2d.evoalglib.ga module#

class eispy2d.evoalglib.ga.GeneticAlgorithm(boundary_condition, crossover, pcross, mutation, pmut, selection, pair_selection='random')#

Bases: Mechanism

Genetic Algorithm (GA) evolutionary mechanism.

Implements a standard genetic algorithm with crossover and mutation operators.

Parameters:
  • boundary_condition (BoundaryCondition) – Boundary handling strategy.

  • crossover (Crossover) – Crossover operator.

  • pcross (float) – Crossover probability.

  • mutation (Mutation) – Mutation operator.

  • pmut (float) – Mutation probability.

  • selection (Selection) – Selection operator.

  • pair_selection ({'random', 'permutation'}, default='random') – How to select pairs for crossover.

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of GA.

copy(new=None)#
reset_variables(population_size, representation)#

Reset internal variables for a new run.

Parameters:
  • population_size (int) – Size of the population.

  • representation (Representation) – Solution representation.

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of Genetic Algorithm.

Parameters:
  • population (numpy.ndarray) – Current population matrix (POP × NVAR).

  • population_fitness (numpy.ndarray) – Fitness values of current population.

  • objective_function (ObjectiveFunction) – Objective function to evaluate.

  • current_nevals (int) – Current number of evaluations.

Returns:

(population, population_fitness, new_evaluation_count)

Return type:

tuple

eispy2d.evoalglib.initialization module#

class eispy2d.evoalglib.initialization.BornApproximation#

Bases: Initialization

Born approximation initialization.

Initializes population using the Born approximation to estimate initial fields. Requires DiscretizationElementBased representation.

Notes

This initialization uses the first-order Born approximation to initialize the total field, providing a better starting point than random initialization for electromagnetic inverse scattering problems.

run(population_size, representation, incident_field, inputdata)#

Initialize population.

Parameters:
  • population_size (int) – Number of individuals in the population.

  • representation (Representation) – Solution representation.

  • incident_field (numpy.ndarray) – Incident field data.

  • inputdata (InputData) – Input data object containing problem parameters.

Returns:

Initial population matrix (POP × NVAR).

Return type:

numpy.ndarray

class eispy2d.evoalglib.initialization.Initialization#

Bases: ABC

Abstract base class for population initialization strategies.

run(population_size, representation, incident_field, inputdata)#

Initialize population.

abstractmethod run(population_size, representation, incident_field, inputdata)#

Initialize population.

Parameters:
  • population_size (int) – Number of individuals in the population.

  • representation (Representation) – Solution representation.

  • incident_field (numpy.ndarray) – Incident field data.

  • inputdata (InputData) – Input data object containing problem parameters.

Returns:

Initial population matrix (POP × NVAR).

Return type:

numpy.ndarray

class eispy2d.evoalglib.initialization.UniformRandomDistribution#

Bases: Initialization

Uniform random distribution initialization.

Initializes population with random values uniformly distributed in [0, 1] for all variables.

run(population_size, representation, incident_field, inputdata)#

Initialize population.

Parameters:
  • population_size (int) – Number of individuals in the population.

  • representation (Representation) – Solution representation.

  • incident_field (numpy.ndarray) – Incident field data.

  • inputdata (InputData) – Input data object containing problem parameters.

Returns:

Initial population matrix (POP × NVAR).

Return type:

numpy.ndarray

eispy2d.evoalglib.mechanism module#

class eispy2d.evoalglib.mechanism.Mechanism(boundary_condition)#

Bases: ABC

Abstract base class for evolutionary mechanisms.

Evolutionary mechanisms define how a population evolves from one generation to the next.

Parameters:

boundary_condition (BoundaryCondition) – Boundary handling strategy.

bc#

Boundary handling strategy.

Type:

BoundaryCondition

xopt#

Best solution found.

Type:

numpy.ndarray or None

fopt#

Best fitness value found.

Type:

float or None

reset_variables(population_size, representation)#

Reset internal variables for a new run.

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of evolution.

best()#

Return the best solution found.

best()#

Return the best solution found.

Returns:

(xopt, fopt) where xopt is the best solution and fopt its fitness.

Return type:

tuple

copy(new=None)#
abstractmethod reset_variables(population_size, representation)#

Reset internal variables for a new run.

Parameters:
  • population_size (int) – Size of the population.

  • representation (Representation) – Solution representation.

abstractmethod run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of evolution.

Parameters:
  • population (numpy.ndarray) – Current population matrix (POP × NVAR).

  • population_fitness (numpy.ndarray) – Fitness values of current population.

  • objective_function (ObjectiveFunction) – Objective function to evaluate.

  • current_nevals (int) – Current number of evaluations.

Returns:

(population, population_fitness, new_evaluation_count)

Return type:

tuple

eispy2d.evoalglib.mechanism.get_indexes(NMAX, size, selection)#

Generate random indexes for evolutionary operations.

Parameters:
  • NMAX (int) – Maximum index value (exclusive).

  • size (int) – Number of indexes to generate.

  • selection ({'random', 'permutation'}) – Selection strategy: - ‘random’: Random choice with replacement. - ‘permutation’: Permutation without replacement.

Returns:

Array of indexes of length size.

Return type:

numpy.ndarray

eispy2d.evoalglib.mutation module#

class eispy2d.evoalglib.mutation.Gaussian(std=0.5)#

Bases: Mutation

Gaussian mutation operator.

Adds Gaussian noise to solution vector.

Parameters:

std (float, default=0.5) – Standard deviation of Gaussian noise.

copy(new=None)#
run(x, fx, probability=None)#

Apply mutation to solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector to mutate.

  • fx (numpy.ndarray or float) – Fitness values of solutions.

  • probability (float, optional) – Mutation probability.

Returns:

(mutated_x, mutated_fx) where mutated_fx may be NaN for new mutations.

Return type:

tuple

class eispy2d.evoalglib.mutation.Mutation#

Bases: ABC

Abstract base class for mutation operators.

Mutation operators introduce random variations to solutions.

run(x, fx, probability=None)#

Apply mutation to solution vector.

copy(new=None)#

Create a copy of the mutation operator.

copy(new=None)#
abstractmethod run(x, fx, probability=None)#

Apply mutation to solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector to mutate.

  • fx (numpy.ndarray or float) – Fitness values of solutions.

  • probability (float, optional) – Mutation probability.

Returns:

(mutated_x, mutated_fx) where mutated_fx may be NaN for new mutations.

Return type:

tuple

class eispy2d.evoalglib.mutation.Polynomial(eta)#

Bases: Mutation

Polynomial mutation operator.

Implements polynomial mutation commonly used in real-coded GAs.

Parameters:

eta (float) – Distribution index. Higher values produce mutations closer to parent.

copy(new=None)#
run(x, fx, probability)#

Apply mutation to solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector to mutate.

  • fx (numpy.ndarray or float) – Fitness values of solutions.

  • probability (float, optional) – Mutation probability.

Returns:

(mutated_x, mutated_fx) where mutated_fx may be NaN for new mutations.

Return type:

tuple

eispy2d.evoalglib.objectivefunction module#

class eispy2d.evoalglib.objectivefunction.Ackley#

Bases: ObjectiveFunction

Ackley benchmark function (canonical, nonlinear, multimodal).

eval(x)#

Evaluate objective function for a solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Objective function value.

Return type:

float

class eispy2d.evoalglib.objectivefunction.ObjectiveFunction#

Bases: ABC

Abstract base class for objective functions.

Defines the interface for evaluating solution quality in evolutionary algorithms.

set_parameters(representation, scattered_field, incident_field)#

Set parameters for objective function evaluation.

eval(x)#

Evaluate objective function for a solution.

abstractmethod eval(x)#

Evaluate objective function for a solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Objective function value.

Return type:

float

set_parameters(representation, scattered_field, incident_field)#

Set parameters for objective function evaluation.

Parameters:
  • representation (Representation) – Solution representation.

  • scattered_field (numpy.ndarray) – Scattered field data.

  • incident_field (numpy.ndarray) – Incident field data.

class eispy2d.evoalglib.objectivefunction.Rastrigin(amplitude=10)#

Bases: ObjectiveFunction

Rastrigin benchmark function (canonical, nonlinear, multimodal).

Parameters:

amplitude (float, default=10) – Amplitude parameter controlling the function’s shape.

eval(x)#

Evaluate objective function for a solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Objective function value.

Return type:

float

class eispy2d.evoalglib.objectivefunction.Rosenbrock#

Bases: ObjectiveFunction

Rosenbrock benchmark function (canonical, nonlinear, multimodal).

eval(x)#

Evaluate objective function for a solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Objective function value.

Return type:

float

class eispy2d.evoalglib.objectivefunction.WeightedSum#

Bases: ObjectiveFunction

Weighted sum of data and state equation residuals.

Objective function for electromagnetic inverse scattering problems. Combines data misfit and state equation misfit.

eval(x)#

Evaluate objective function for a solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Objective function value.

Return type:

float

eispy2d.evoalglib.pso module#

class eispy2d.evoalglib.pso.ParticleSwarmOptimization(boundary_condition, acceleration=2.0, inertia=0.4)#

Bases: Mechanism

Particle Swarm Optimization (PSO) evolutionary mechanism.

Implements standard PSO with inertia weight and acceleration coefficients.

Parameters:
  • boundary_condition (BoundaryCondition) – Boundary handling strategy.

  • acceleration (float or tuple, default=2.0) – Acceleration coefficients (c1, c2). If float, both are equal.

  • inertia (float, default=0.4) – Inertia weight (w).

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of PSO.

copy(new=None)#
reset_variables(population_size, representation)#

Reset internal variables for a new run.

Parameters:
  • population_size (int) – Size of the population.

  • representation (Representation) – Solution representation.

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of evolution.

Parameters:
  • population (numpy.ndarray) – Current population matrix (POP × NVAR).

  • population_fitness (numpy.ndarray) – Fitness values of current population.

  • objective_function (ObjectiveFunction) – Objective function to evaluate.

  • current_nevals (int) – Current number of evaluations.

Returns:

(population, population_fitness, new_evaluation_count)

Return type:

tuple

eispy2d.evoalglib.representation module#

class eispy2d.evoalglib.representation.CanonicalProblems(number_variables, lb, ub)#

Bases: Representation

Representation for canonical optimization problems.

Used for testing evolutionary algorithms on standard benchmark functions.

Parameters:
  • number_variables (int) – Number of decision variables.

  • lb (float or array-like) – Lower bound(s) for variables.

  • ub (float or array-like) – Upper bound(s) for variables.

contrast(x, mode='array')#

Extract contrast function from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Contrast function.

Return type:

numpy.ndarray

current(x)#

Extract current from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Current distribution.

Return type:

numpy.ndarray

scattered_field(x)#

Compute scattered field from solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Scattered field.

Return type:

numpy.ndarray

total_field(x, mode='array')#

Extract total field from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Total field.

Return type:

numpy.ndarray

class eispy2d.evoalglib.representation.DiscretizationElementBased(discretization, contrast_bounds, total_bounds)#

Bases: Representation

Representation for electromagnetic inverse scattering problems.

Represents the contrast function and total field using discretization elements. Supports perfect dielectric and good conductor assumptions.

Parameters:
  • discretization (Discretization) – Discretization method.

  • contrast_bounds (float, tuple, or list) – Bounds for contrast variables.

  • total_bounds (float, complex, tuple, or list) – Bounds for total field variables.

contrast(x, mode='array')#

Extract contrast function from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Contrast function.

Return type:

numpy.ndarray

current(x, mode='array')#

Extract current from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Current distribution.

Return type:

numpy.ndarray

scattered_field(x)#

Compute scattered field from solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Scattered field.

Return type:

numpy.ndarray

total_field(x, mode='array')#

Extract total field from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Total field.

Return type:

numpy.ndarray

class eispy2d.evoalglib.representation.Representation#

Bases: ABC

Abstract base class for solution representation.

Defines how solutions are encoded and decoded in evolutionary algorithms.

nvar#

Number of decision variables.

Type:

int

lb#

Lower bounds for variables.

Type:

numpy.ndarray

ub#

Upper bounds for variables.

Type:

numpy.ndarray

dtype#

Data type of variables.

Type:

type

abstractmethod contrast(x, mode='array')#

Extract contrast function from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Contrast function.

Return type:

numpy.ndarray

abstractmethod current(x, mode='array')#

Extract current from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Current distribution.

Return type:

numpy.ndarray

real2unit(x)#

Convert real-space values to unit-space values.

Parameters:

x (numpy.ndarray) – Values in real space.

Returns:

Values in [0, 1] range.

Return type:

numpy.ndarray

abstractmethod scattered_field(x)#

Compute scattered field from solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Scattered field.

Return type:

numpy.ndarray

abstractmethod total_field(x, mode='array')#

Extract total field from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Total field.

Return type:

numpy.ndarray

unit2real(x)#

Convert unit-space values to real-space values.

Parameters:

x (numpy.ndarray) – Values in [0, 1] range.

Returns:

Values in real space.

Return type:

numpy.ndarray

eispy2d.evoalglib.selection module#

class eispy2d.evoalglib.selection.BinaryTournament(elitism=True, pair_selection='random')#

Bases: Selection

Binary tournament selection operator.

Selects individuals by running tournaments between randomly chosen pairs.

Parameters:
  • elitism (bool, default=True) – Whether to preserve the best individual.

  • pair_selection ({'random', 'permutation'}, default='random') – How to select pairs for tournaments.

copy(new=None)#
run(P1, fx1, P2=None, fx2=None, NPOP=None)#

Perform selection.

Parameters:
  • P1 (numpy.ndarray) – First population.

  • fx1 (numpy.ndarray) – Fitness values of first population.

  • P2 (numpy.ndarray, optional) – Second population (for elitism).

  • fx2 (numpy.ndarray, optional) – Fitness values of second population.

  • NPOP (int, optional) – Number of individuals to select.

Returns:

(selected_population, selected_fitness)

Return type:

tuple

class eispy2d.evoalglib.selection.Roullete(elitism=True)#

Bases: Selection

Roulette wheel selection operator.

Selects individuals proportionally to their fitness (probability proportional to fitness). Also known as fitness proportional selection.

Parameters:

elitism (bool, default=True) – Whether to preserve the best individual.

copy(new=None)#
run(P1, fx1, P2=None, fx2=None, NPOP=None)#

Perform selection.

Parameters:
  • P1 (numpy.ndarray) – First population.

  • fx1 (numpy.ndarray) – Fitness values of first population.

  • P2 (numpy.ndarray, optional) – Second population (for elitism).

  • fx2 (numpy.ndarray, optional) – Fitness values of second population.

  • NPOP (int, optional) – Number of individuals to select.

Returns:

(selected_population, selected_fitness)

Return type:

tuple

class eispy2d.evoalglib.selection.Selection#

Bases: ABC

Abstract base class for selection operators.

Selection operators choose individuals for reproduction based on fitness.

run(P1, fx1, P2, fx2, NPOP)#

Perform selection.

copy(new=None)#

Create a copy of the selection operator.

copy(new=None)#
abstractmethod run(P1, fx1, P2=None, fx2=None, NPOP=None)#

Perform selection.

Parameters:
  • P1 (numpy.ndarray) – First population.

  • fx1 (numpy.ndarray) – Fitness values of first population.

  • P2 (numpy.ndarray, optional) – Second population (for elitism).

  • fx2 (numpy.ndarray, optional) – Fitness values of second population.

  • NPOP (int, optional) – Number of individuals to select.

Returns:

(selected_population, selected_fitness)

Return type:

tuple

eispy2d.evoalglib.selection.find_edge(u, cumsum)#

Find index where cumulative sum exceeds u.

Parameters:
  • u (float) – Value to find in cumulative distribution.

  • cumsum (numpy.ndarray) – Cumulative sum array.

Returns:

Index where cumsum[i] >= u.

Return type:

int

Module contents#

Evolutionary Algorithms Library for eispy2d.

Comprehensive framework for evolutionary computation applied to electromagnetic inverse scattering problems.

Components: - representation: Solution encoding schemes - objectivefunction: Fitness evaluation functions - initialization: Population initialization strategies - selection: Parent selection operators - crossover: Recombination operators - mutation: Variation operators - boundary: Constraint handling methods - de: Differential Evolution - pso: Particle Swarm Optimization - ga: Genetic Algorithm

class eispy2d.evoalglib.Ackley#

Bases: ObjectiveFunction

Ackley benchmark function (canonical, nonlinear, multimodal).

eval(x)#

Evaluate objective function for a solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Objective function value.

Return type:

float

class eispy2d.evoalglib.BinaryTournament(elitism=True, pair_selection='random')#

Bases: Selection

Binary tournament selection operator.

Selects individuals by running tournaments between randomly chosen pairs.

Parameters:
  • elitism (bool, default=True) – Whether to preserve the best individual.

  • pair_selection ({'random', 'permutation'}, default='random') – How to select pairs for tournaments.

copy(new=None)#
run(P1, fx1, P2=None, fx2=None, NPOP=None)#

Perform selection.

Parameters:
  • P1 (numpy.ndarray) – First population.

  • fx1 (numpy.ndarray) – Fitness values of first population.

  • P2 (numpy.ndarray, optional) – Second population (for elitism).

  • fx2 (numpy.ndarray, optional) – Fitness values of second population.

  • NPOP (int, optional) – Number of individuals to select.

Returns:

(selected_population, selected_fitness)

Return type:

tuple

class eispy2d.evoalglib.Binomial(crossover_rate)#

Bases: Crossover

Binomial crossover operator.

Creates offspring by mixing variables from two parents based on a crossover rate. Commonly used in Differential Evolution.

Parameters:

crossover_rate (float) – Probability of taking variable from the second parent.

Notes

Binomial crossover is the standard crossover operator in DE. Each variable has crossover_rate probability of coming from the second parent, otherwise from the first.

copy(new=None)#

Create a copy of the crossover operator.

Parameters:

new (Crossover, optional) – If provided, copies data into this object. If None, creates a new copy.

Return type:

Crossover or None

run(x1, x2, fx1, fx2, probability=None)#

Perform crossover operation.

Parameters:
  • x1 (numpy.ndarray) – Parent solutions.

  • x2 (numpy.ndarray) – Parent solutions.

  • fx1 (numpy.ndarray or float) – Fitness values of parents.

  • fx2 (numpy.ndarray or float) – Fitness values of parents.

  • probability (float, optional) – Crossover probability.

Returns:

(offspring, fitness) where fitness may be NaN for new offspring.

Return type:

tuple

class eispy2d.evoalglib.BornApproximation#

Bases: Initialization

Born approximation initialization.

Initializes population using the Born approximation to estimate initial fields. Requires DiscretizationElementBased representation.

Notes

This initialization uses the first-order Born approximation to initialize the total field, providing a better starting point than random initialization for electromagnetic inverse scattering problems.

run(population_size, representation, incident_field, inputdata)#

Initialize population.

Parameters:
  • population_size (int) – Number of individuals in the population.

  • representation (Representation) – Solution representation.

  • incident_field (numpy.ndarray) – Incident field data.

  • inputdata (InputData) – Input data object containing problem parameters.

Returns:

Initial population matrix (POP × NVAR).

Return type:

numpy.ndarray

class eispy2d.evoalglib.CanonicalProblems(number_variables, lb, ub)#

Bases: Representation

Representation for canonical optimization problems.

Used for testing evolutionary algorithms on standard benchmark functions.

Parameters:
  • number_variables (int) – Number of decision variables.

  • lb (float or array-like) – Lower bound(s) for variables.

  • ub (float or array-like) – Upper bound(s) for variables.

contrast(x, mode='array')#

Extract contrast function from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Contrast function.

Return type:

numpy.ndarray

current(x)#

Extract current from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Current distribution.

Return type:

numpy.ndarray

scattered_field(x)#

Compute scattered field from solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Scattered field.

Return type:

numpy.ndarray

total_field(x, mode='array')#

Extract total field from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Total field.

Return type:

numpy.ndarray

class eispy2d.evoalglib.Crossover#

Bases: ABC

Abstract base class for crossover operators in evolutionary algorithms.

Crossover operators combine two parent solutions to produce offspring.

run(x1, x2, fx1, fx2, probability=None)#

Perform crossover operation.

copy(new=None)#

Create a copy of the crossover operator.

copy(new=None)#

Create a copy of the crossover operator.

Parameters:

new (Crossover, optional) – If provided, copies data into this object. If None, creates a new copy.

Return type:

Crossover or None

abstractmethod run(x1, x2, fx1, fx2, probability=None)#

Perform crossover operation.

Parameters:
  • x1 (numpy.ndarray) – Parent solutions.

  • x2 (numpy.ndarray) – Parent solutions.

  • fx1 (numpy.ndarray or float) – Fitness values of parents.

  • fx2 (numpy.ndarray or float) – Fitness values of parents.

  • probability (float, optional) – Crossover probability.

Returns:

(offspring, fitness) where fitness may be NaN for new offspring.

Return type:

tuple

class eispy2d.evoalglib.DifferentialEvolution(boundary_condition, selection, mutation, scaling_factor, crossover, pcross=1.0, index_selection='random')#

Bases: Mechanism

Differential Evolution (DE) evolutionary mechanism.

Implements various DE mutation strategies including: - Rand: DE/rand/1 - Best: DE/best/1 - Current-to-best: DE/current-to-best/1 - Rand-to-best: DE/rand-to-best/1

Parameters:
  • boundary_condition (BoundaryCondition) – Boundary handling strategy.

  • selection (Selection) – Selection operator.

  • mutation ({'rand', 'best', 'current-to-best', 'rand-to-best'}) – DE mutation strategy.

  • scaling_factor (float) – Mutation scaling factor (F).

  • crossover (Crossover) – Crossover operator.

  • pcross (float, default=1.0) – Crossover probability.

  • index_selection ({'random', 'permutation'}, default='random') – How to select indices for mutation.

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of DE.

copy(new=None)#
reset_variables(population_size, representation)#

Reset internal variables for a new run.

Parameters:
  • population_size (int) – Size of the population.

  • representation (Representation) – Solution representation.

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of Differential Evolution.

Parameters:
  • population (numpy.ndarray) – Current population matrix (POP × NVAR).

  • population_fitness (numpy.ndarray) – Fitness values of current population.

  • objective_function (ObjectiveFunction) – Objective function to evaluate.

  • current_nevals (int) – Current number of evaluations.

Returns:

(population, population_fitness, new_evaluation_count)

Return type:

tuple

class eispy2d.evoalglib.DiscretizationElementBased(discretization, contrast_bounds, total_bounds)#

Bases: Representation

Representation for electromagnetic inverse scattering problems.

Represents the contrast function and total field using discretization elements. Supports perfect dielectric and good conductor assumptions.

Parameters:
  • discretization (Discretization) – Discretization method.

  • contrast_bounds (float, tuple, or list) – Bounds for contrast variables.

  • total_bounds (float, complex, tuple, or list) – Bounds for total field variables.

contrast(x, mode='array')#

Extract contrast function from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Contrast function.

Return type:

numpy.ndarray

current(x, mode='array')#

Extract current from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Current distribution.

Return type:

numpy.ndarray

scattered_field(x)#

Compute scattered field from solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Scattered field.

Return type:

numpy.ndarray

total_field(x, mode='array')#

Extract total field from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Total field.

Return type:

numpy.ndarray

class eispy2d.evoalglib.Gaussian(std=0.5)#

Bases: Mutation

Gaussian mutation operator.

Adds Gaussian noise to solution vector.

Parameters:

std (float, default=0.5) – Standard deviation of Gaussian noise.

copy(new=None)#
run(x, fx, probability=None)#

Apply mutation to solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector to mutate.

  • fx (numpy.ndarray or float) – Fitness values of solutions.

  • probability (float, optional) – Mutation probability.

Returns:

(mutated_x, mutated_fx) where mutated_fx may be NaN for new mutations.

Return type:

tuple

class eispy2d.evoalglib.GeneticAlgorithm(boundary_condition, crossover, pcross, mutation, pmut, selection, pair_selection='random')#

Bases: Mechanism

Genetic Algorithm (GA) evolutionary mechanism.

Implements a standard genetic algorithm with crossover and mutation operators.

Parameters:
  • boundary_condition (BoundaryCondition) – Boundary handling strategy.

  • crossover (Crossover) – Crossover operator.

  • pcross (float) – Crossover probability.

  • mutation (Mutation) – Mutation operator.

  • pmut (float) – Mutation probability.

  • selection (Selection) – Selection operator.

  • pair_selection ({'random', 'permutation'}, default='random') – How to select pairs for crossover.

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of GA.

copy(new=None)#
reset_variables(population_size, representation)#

Reset internal variables for a new run.

Parameters:
  • population_size (int) – Size of the population.

  • representation (Representation) – Solution representation.

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of Genetic Algorithm.

Parameters:
  • population (numpy.ndarray) – Current population matrix (POP × NVAR).

  • population_fitness (numpy.ndarray) – Fitness values of current population.

  • objective_function (ObjectiveFunction) – Objective function to evaluate.

  • current_nevals (int) – Current number of evaluations.

Returns:

(population, population_fitness, new_evaluation_count)

Return type:

tuple

class eispy2d.evoalglib.Initialization#

Bases: ABC

Abstract base class for population initialization strategies.

run(population_size, representation, incident_field, inputdata)#

Initialize population.

abstractmethod run(population_size, representation, incident_field, inputdata)#

Initialize population.

Parameters:
  • population_size (int) – Number of individuals in the population.

  • representation (Representation) – Solution representation.

  • incident_field (numpy.ndarray) – Incident field data.

  • inputdata (InputData) – Input data object containing problem parameters.

Returns:

Initial population matrix (POP × NVAR).

Return type:

numpy.ndarray

class eispy2d.evoalglib.Mutation#

Bases: ABC

Abstract base class for mutation operators.

Mutation operators introduce random variations to solutions.

run(x, fx, probability=None)#

Apply mutation to solution vector.

copy(new=None)#

Create a copy of the mutation operator.

copy(new=None)#
abstractmethod run(x, fx, probability=None)#

Apply mutation to solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector to mutate.

  • fx (numpy.ndarray or float) – Fitness values of solutions.

  • probability (float, optional) – Mutation probability.

Returns:

(mutated_x, mutated_fx) where mutated_fx may be NaN for new mutations.

Return type:

tuple

class eispy2d.evoalglib.ObjectiveFunction#

Bases: ABC

Abstract base class for objective functions.

Defines the interface for evaluating solution quality in evolutionary algorithms.

set_parameters(representation, scattered_field, incident_field)#

Set parameters for objective function evaluation.

eval(x)#

Evaluate objective function for a solution.

abstractmethod eval(x)#

Evaluate objective function for a solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Objective function value.

Return type:

float

set_parameters(representation, scattered_field, incident_field)#

Set parameters for objective function evaluation.

Parameters:
  • representation (Representation) – Solution representation.

  • scattered_field (numpy.ndarray) – Scattered field data.

  • incident_field (numpy.ndarray) – Incident field data.

class eispy2d.evoalglib.ParticleSwarmOptimization(boundary_condition, acceleration=2.0, inertia=0.4)#

Bases: Mechanism

Particle Swarm Optimization (PSO) evolutionary mechanism.

Implements standard PSO with inertia weight and acceleration coefficients.

Parameters:
  • boundary_condition (BoundaryCondition) – Boundary handling strategy.

  • acceleration (float or tuple, default=2.0) – Acceleration coefficients (c1, c2). If float, both are equal.

  • inertia (float, default=0.4) – Inertia weight (w).

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of PSO.

copy(new=None)#
reset_variables(population_size, representation)#

Reset internal variables for a new run.

Parameters:
  • population_size (int) – Size of the population.

  • representation (Representation) – Solution representation.

run(population, population_fitness, objective_function, current_nevals)#

Execute one generation of evolution.

Parameters:
  • population (numpy.ndarray) – Current population matrix (POP × NVAR).

  • population_fitness (numpy.ndarray) – Fitness values of current population.

  • objective_function (ObjectiveFunction) – Objective function to evaluate.

  • current_nevals (int) – Current number of evaluations.

Returns:

(population, population_fitness, new_evaluation_count)

Return type:

tuple

class eispy2d.evoalglib.Polynomial(eta)#

Bases: Mutation

Polynomial mutation operator.

Implements polynomial mutation commonly used in real-coded GAs.

Parameters:

eta (float) – Distribution index. Higher values produce mutations closer to parent.

copy(new=None)#
run(x, fx, probability)#

Apply mutation to solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector to mutate.

  • fx (numpy.ndarray or float) – Fitness values of solutions.

  • probability (float, optional) – Mutation probability.

Returns:

(mutated_x, mutated_fx) where mutated_fx may be NaN for new mutations.

Return type:

tuple

class eispy2d.evoalglib.Reflection#

Bases: BoundaryCondition

Reflection boundary condition.

Reflects values that fall outside [0, 1] back into the range using a reflection strategy. Values are reflected multiple times if needed.

Notes

This boundary handling strategy preserves the interval [0, 1] by reflecting out-of-bounds values back into the valid range using a mirroring technique.

run(x)#

Apply reflection to the solution vector.

Parameters:

x (numpy.ndarray) – Solution vector to be reflected.

class eispy2d.evoalglib.Representation#

Bases: ABC

Abstract base class for solution representation.

Defines how solutions are encoded and decoded in evolutionary algorithms.

nvar#

Number of decision variables.

Type:

int

lb#

Lower bounds for variables.

Type:

numpy.ndarray

ub#

Upper bounds for variables.

Type:

numpy.ndarray

dtype#

Data type of variables.

Type:

type

abstractmethod contrast(x, mode='array')#

Extract contrast function from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Contrast function.

Return type:

numpy.ndarray

abstractmethod current(x, mode='array')#

Extract current from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Current distribution.

Return type:

numpy.ndarray

real2unit(x)#

Convert real-space values to unit-space values.

Parameters:

x (numpy.ndarray) – Values in real space.

Returns:

Values in [0, 1] range.

Return type:

numpy.ndarray

abstractmethod scattered_field(x)#

Compute scattered field from solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Scattered field.

Return type:

numpy.ndarray

abstractmethod total_field(x, mode='array')#

Extract total field from solution vector.

Parameters:
  • x (numpy.ndarray) – Solution vector.

  • mode ({'array', 'image'}, default='array') – Output format.

Returns:

Total field.

Return type:

numpy.ndarray

unit2real(x)#

Convert unit-space values to real-space values.

Parameters:

x (numpy.ndarray) – Values in [0, 1] range.

Returns:

Values in real space.

Return type:

numpy.ndarray

class eispy2d.evoalglib.Rosenbrock#

Bases: ObjectiveFunction

Rosenbrock benchmark function (canonical, nonlinear, multimodal).

eval(x)#

Evaluate objective function for a solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Objective function value.

Return type:

float

class eispy2d.evoalglib.Selection#

Bases: ABC

Abstract base class for selection operators.

Selection operators choose individuals for reproduction based on fitness.

run(P1, fx1, P2, fx2, NPOP)#

Perform selection.

copy(new=None)#

Create a copy of the selection operator.

copy(new=None)#
abstractmethod run(P1, fx1, P2=None, fx2=None, NPOP=None)#

Perform selection.

Parameters:
  • P1 (numpy.ndarray) – First population.

  • fx1 (numpy.ndarray) – Fitness values of first population.

  • P2 (numpy.ndarray, optional) – Second population (for elitism).

  • fx2 (numpy.ndarray, optional) – Fitness values of second population.

  • NPOP (int, optional) – Number of individuals to select.

Returns:

(selected_population, selected_fitness)

Return type:

tuple

class eispy2d.evoalglib.SimulatedBinary(eta)#

Bases: Crossover

Simulated Binary Crossover (SBX) operator.

Creates offspring by simulating the behavior of binary crossover in real-valued spaces. The distribution index eta controls the spread of offspring around parents.

Parameters:

eta (float) – Distribution index. Higher values produce offspring closer to parents.

Notes

SBX is commonly used in real-coded genetic algorithms and produces offspring with a probability distribution that mimics binary crossover.

copy(new=None)#

Create a copy of the crossover operator.

Parameters:

new (Crossover, optional) – If provided, copies data into this object. If None, creates a new copy.

Return type:

Crossover or None

run(x1, x2, fx1, fx2, probability=None)#

Perform crossover operation.

Parameters:
  • x1 (numpy.ndarray) – Parent solutions.

  • x2 (numpy.ndarray) – Parent solutions.

  • fx1 (numpy.ndarray or float) – Fitness values of parents.

  • fx2 (numpy.ndarray or float) – Fitness values of parents.

  • probability (float, optional) – Crossover probability.

Returns:

(offspring, fitness) where fitness may be NaN for new offspring.

Return type:

tuple

class eispy2d.evoalglib.UniformRandomDistribution#

Bases: Initialization

Uniform random distribution initialization.

Initializes population with random values uniformly distributed in [0, 1] for all variables.

run(population_size, representation, incident_field, inputdata)#

Initialize population.

Parameters:
  • population_size (int) – Number of individuals in the population.

  • representation (Representation) – Solution representation.

  • incident_field (numpy.ndarray) – Incident field data.

  • inputdata (InputData) – Input data object containing problem parameters.

Returns:

Initial population matrix (POP × NVAR).

Return type:

numpy.ndarray

class eispy2d.evoalglib.WeightedSum#

Bases: ObjectiveFunction

Weighted sum of data and state equation residuals.

Objective function for electromagnetic inverse scattering problems. Combines data misfit and state equation misfit.

eval(x)#

Evaluate objective function for a solution.

Parameters:

x (numpy.ndarray) – Solution vector.

Returns:

Objective function value.

Return type:

float