Skip to content

BarelangFC/IK-Differential-Evolution-Python

Repository files navigation

IK-Differential-Evolution

Python 3.10+ NumPy License CI

4-DOF arm Inverse Kinematics via Differential Evolution. Pure NumPy, no ROS.

Demo GIF

Overview

Solves IK for a 4-DOF articulated arm using the Differential Evolution global optimisation algorithm. No analytical solution needed — works with arbitrary joint configurations and target poses.

Arm geometry

base → joint1 (yaw) → joint2 (pitch) → joint3 (pitch) → joint4 (pitch) → EE
Link Length Description
L1 0.15 m Base → shoulder (vertical)
L2 0.25 m Upper arm
L3 0.25 m Forearm
L4 0.10 m Wrist → end-effector

Dependencies

  • Python ≥ 3.10
  • NumPy ≥ 1.21
  • (optional) matplotlib + pillow for GIF generation

Install

pip install git+https://github.com/BarelangFC/IK-Differential-Evolution-Python.git

Or for development (with visualisation):

git clone git@github.com:BarelangFC/IK-Differential-Evolution-Python.git
cd IK-Differential-Evolution-Python
pip install -e ".[demo]"

Usage

CLI

# Solve IK for a target position (x y z metres)
ik-arm solve 0.3 0.2 0.4

# Random test (generate random FK target, then solve)
ik-arm random --seed 42

# Customise solver parameters
ik-arm solve 0.3 0.2 0.4 --pop 100 --gen 300 --tol-pos 0.0005

Python API

from ik_de_arm import solve, fk, ee_position
import numpy as np

# Single IK solve
target = np.array([0.3, 0.2, 0.4])
result = solve(target, seed=42)

if result["success"]:
    print(f"Joints (deg): {[round(np.degrees(j), 2) for j in result['joints']]}")
    print(f"Position error: {result['position_error']:.6f} m")

# FK verification
pos = ee_position(result["joints"])
print(f"EE at: {pos}")

# Convergence history
for h in result["history"]:
    print(f"Gen {h.gen:4d}: best_fit={h.best_fit:.4f}")

Solver parameters

Parameter Default Description
pop_size 50 Population size
max_generations 200 Max DE iterations
F 0.8 Mutation factor
CR 0.9 Crossover rate
tol 1e-4 Cost convergence
tol_pos 1e-3 Position error (m) convergence

Generate demo GIF

pip install -e ".[demo]"
python scripts/generate_demo.py

How it works

Differential Evolution is a population-based stochastic optimiser:

  1. Initialisation: random population of pop_size joint configurations
  2. Mutation: v = a + F × (b − c) (DE/rand/1)
  3. Crossover: binomial — each gene from mutant with probability CR
  4. Selection: greedy — keep trial if it reduces EE-target distance
  5. Repeat until convergence or max generations

Cost function = weighted sum of position error + optional orientation error.

Development

pip install -e ".[dev]"

License

MIT — see LICENSE.

About

inverse kinematics 4 DOF using the differential Evolution optimization method

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages