walking_objectives

Walking-specific objective factories for pylinkage’s optimizer infrastructure.

Pre-built objective functions for multi-objective walking optimization.

These objectives wrap the standard pylinkage eval_func contract:

objective(linkage, dimensions, init_positions) -> float

They can be passed directly to multi_objective_optimization or composed with chain_optimizers.

Example:

from leggedsnake import (
    multi_objective_optimization,
    stride_length_objective,
    energy_efficiency_objective,
)

front = multi_objective_optimization(
    objectives=[
        stride_length_objective(duration=40, n_legs=4),
        energy_efficiency_objective(duration=40, n_legs=4),
    ],
    linkage=my_walker,
    bounds=my_bounds,
    objective_names=["stride length (maximize)", "energy per meter"],
)
leggedsnake.walking_objectives.energy_efficiency_objective(*, duration: float = 40.0, n_legs: int = 4, param_expander: Callable[[...], Any] | None = None, min_distance: float = 5.0, config: WorldConfig | None = None) Callable[[...], float]

Create a dynamic energy-efficiency objective (to maximize).

Returns total_efficiency / total_energy from physics simulation. Returns 0 if the walker doesn’t travel at least min_distance.

Parameters:
  • duration (float) – Simulation duration in seconds.

  • n_legs (int) – Number of leg pairs for the walker.

  • param_expander (callable, optional) – Function to expand compact parameters to full dimensions.

  • min_distance (float) – Minimum distance the walker must cover to get a non-zero score.

  • config (WorldConfig, optional) – Simulation parameters (gravity, terrain, etc.).

Returns:

objective(linkage, dims, pos) -> float

Return type:

callable

leggedsnake.walking_objectives.multi_objective_walking_optimization(linkage: Any, objectives: Sequence[Callable[[...], float]], bounds: tuple[Sequence[float], Sequence[float]], objective_names: Sequence[str] | None = None, algorithm: Literal['nsga2', 'nsga3'] = 'nsga2', n_generations: int = 100, pop_size: int = 100, seed: int | None = None, verbose: bool = True, **kwargs: Any) ParetoFront

Multi-objective optimization for walking linkages.

Convenience wrapper around pylinkage.optimization.multi_objective_optimization that accepts walking-specific objective factories.

Parameters:
  • linkage (Walker or Linkage) – The linkage to optimize.

  • objectives (sequence of callables) – Objective functions, each with signature (linkage, dims, pos) -> float. Use the factory functions in this module (stride_length_objective, etc.) to create them.

  • bounds (tuple of (lower, upper)) – Parameter bounds.

  • objective_names (sequence of str, optional) – Names for plotting and identification.

  • algorithm ({"nsga2", "nsga3"}) – Multi-objective algorithm. Default “nsga2”.

  • n_generations (int) – Number of generations. Default 100.

  • pop_size (int) – Population size. Default 100.

  • seed (int, optional) – Random seed for reproducibility.

  • verbose (bool) – Show progress. Default True.

Returns:

Collection of non-dominated solutions with .best_compromise(), .plot(), and .filter() methods.

Return type:

ParetoFront

leggedsnake.walking_objectives.stride_length_objective(*, lap_points: int = 12, step_height: float = 0.5, step_width: float = 0.2, stride_height: float = 0.2, foot_index: int = -2, param_expander: Callable[[...], Any] | None = None) Callable[[...], float]

Create a kinematic stride-length objective (to maximize).

This is a fast, physics-free evaluation that measures horizontal travel of the foot locus. Suitable for initial exploration stages.

Parameters:
  • lap_points (int) – Points per crank revolution for simulation.

  • step_height (float) – Obstacle clearance requirements.

  • step_width (float) – Obstacle clearance requirements.

  • stride_height (float) – Height threshold for stride extraction.

  • foot_index (int) – Index of the foot joint in the locus output.

  • param_expander (callable, optional) – Function to expand compact parameters to full dimensions (e.g., param2dimensions for symmetric linkages).

Returns:

objective(linkage, dims, pos) -> float

Return type:

callable

leggedsnake.walking_objectives.total_distance_objective(*, duration: float = 40.0, n_legs: int = 4, param_expander: Callable[[...], Any] | None = None, config: WorldConfig | None = None) Callable[[...], float]

Create a dynamic total-distance objective (to maximize).

Returns the horizontal position of the walker body after simulation.

Parameters:
  • duration (float) – Simulation duration in seconds.

  • n_legs (int) – Number of leg pairs for the walker.

  • param_expander (callable, optional) – Function to expand compact parameters to full dimensions.

  • config (WorldConfig, optional) – Simulation parameters (gravity, terrain, etc.).

Returns:

objective(linkage, dims, pos) -> float

Return type:

callable