4-DOF arm Inverse Kinematics via Differential Evolution. Pure NumPy, no ROS.
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.
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 |
- Python ≥ 3.10
- NumPy ≥ 1.21
- (optional) matplotlib + pillow for GIF generation
pip install git+https://github.com/BarelangFC/IK-Differential-Evolution-Python.gitOr for development (with visualisation):
git clone git@github.com:BarelangFC/IK-Differential-Evolution-Python.git
cd IK-Differential-Evolution-Python
pip install -e ".[demo]"# 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.0005from 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}")| 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 |
pip install -e ".[demo]"
python scripts/generate_demo.pyDifferential Evolution is a population-based stochastic optimiser:
- Initialisation: random population of
pop_sizejoint configurations - Mutation:
v = a + F × (b − c)(DE/rand/1) - Crossover: binomial — each gene from mutant with probability
CR - Selection: greedy — keep trial if it reduces EE-target distance
- Repeat until convergence or max generations
Cost function = weighted sum of position error + optional orientation error.
pip install -e ".[dev]"MIT — see LICENSE.
