Test Problems¶
This script loops through all test problems, displays their signature, cost function, and – if robotics problem – komo scene.
[1]:
import optsam as op
import numpy as np
import matplotlib.pylab as plt
import time
The following is a plotting helper problem: It creates a 2D grid; if the problem is 2D it evaluates on that grid; otherwise on a random hyperplane (determined by x0). Then plots.
[2]:
def display_2d_unconstrained(nlp: op.NLP, resolution=30):
B = nlp.bounds
x0 = nlp.getInitializationSample()
dim = 2
X = [None] * dim
for i in range(dim):
X[i] = np.linspace(B[0][i], B[1][i], resolution)
X = np.stack(np.meshgrid(*X, indexing='ij'), axis=-1)
if nlp.dimension==2:
fX = np.array([nlp.eval_scalar(x)[0] for x in X.reshape(-1, dim)])
else:
f = lambda x: nlp.eval_scalar(np.concatenate((x, x0[2:])))[0]
fX = np.array([f(x) for x in X.reshape(-1, dim)])
fX = fX.reshape(X.shape[:-1])
fig, ax = plt.subplots()
ax.contour(X[:,:,0], X[:,:,1], fX, 200)
plt.show()
The following displays a problem more generically: If it has constraints, it is converted to unconstraint (using the Augmented Lagrangian). If it is a robotics problem, it also displays the komo scene.
[3]:
def display_any(nlp: op.NLP):
ty = nlp.types
komo = nlp.as_KOMO()
if komo is not None:
for _ in range(5):
x = nlp.getUniformSample()
# x = nlp.getInitializationSample()
nlp.evaluate(x)
komo.view(False, 'random init')
time.sleep(.2)
if (op.eq in ty) or (op.ineq in ty):
nlp_org = nlp
nlp = nlp.aug_lag(1e1, -1.)
display_2d_unconstrained(nlp)
We can now loop through all test problems, print their signatur, and display.
[4]:
def main():
problems = op.get_NLP_Problem_names()
print(problems)
# p = problems[5]
for p in problems:
nlp = op.make_NLP_Problem(p)
print('===\n', p, nlp.report(1), '===')
display_any(nlp)
[5]:
if __name__ == "__main__":
main()
['square', 'Rugged', 'Rastrigin', 'Rosenbrock', 'Ackley', 'Himmelblau', 'Box', 'Modes', 'Wedge', 'HalfCircle', 'LinearProgram', 'IK', 'IKobstacle', 'IKtorus', 'PushToReach', 'StableSphere', 'SpherePacking', 'MinimalConvexCore']
===
square NLP of type 'NLP_Squared' dimension:2 #objectives: 2
===
===
Rugged NLP of type 'NLP_Rugged' dimension:2 #objectives: 1
===
===
Rastrigin NLP of type 'NLP_Rastrigin' dimension:2 #objectives: 1
===
===
Rosenbrock NLP of type 'NLP_Rosenbrock' dimension:2 #objectives: 1
===
===
Ackley NLP of type 'NLP_Ackley' dimension:2 #objectives: 1
===
===
Himmelblau NLP of type 'NLP_Himmelblau' dimension:2 #objectives: 1
===
===
Box NLP of type 'BoxNLP' dimension:2 #objectives: 4
===
===
Modes NLP of type 'ModesNLP' dimension:2 #objectives: 1
===
===
Wedge NLP of type 'NLP_Wedge' dimension:2 #objectives: 3
===
===
HalfCircle NLP of type 'NLP_HalfCircle' dimension:2 #objectives: 3
===
===
LinearProgram NLP of type 'NLP_RandomLP' dimension:2 #objectives: 16
===
===
IK specs: { x_dim: 7, dual_dim: 0, T: 1, k: 0, phases: 1, slicesPerPhase: 1, enableCollisions!, tau: 1, #slices: 1, #totalDOF: 7, #frames: 43, #pathQueries: 0 } ===
===
IKobstacle specs: { x_dim: 7, dual_dim: 0, T: 1, k: 0, phases: 1, slicesPerPhase: 1, enableCollisions!, tau: 1, #slices: 1, #totalDOF: 7, #frames: 44, #pathQueries: 906 } ===
===
IKtorus specs: { x_dim: 7, dual_dim: 0, T: 1, k: 0, phases: 1, slicesPerPhase: 1, enableCollisions!, tau: 1, #slices: 1, #totalDOF: 7, #frames: 44, #pathQueries: 1812 } ===
===
PushToReach specs: { x_dim: 28, dual_dim: 0, T: 4, k: 1, phases: 4, slicesPerPhase: 1, enableCollisions!, tau: 1, #slices: 5, #totalDOF: 46, #frames: 240, #pathQueries: 2718 }
switches: { sw: "SWITCH timeOfApplication=0 symbol=joint jointType=rigid fromId=45'obj_trans' toId=42'box'", sw: "SWITCH timeOfApplication=2 symbol=joint jointType=free fromId=35'l_gripper' toId=44'stick'" } ===
-- testProblems_KOMO.cpp:problem_StableSphere:193(0)
======================
supports: box wall l_finger r_finger
===
StableSphere specs: { x_dim: 9, dual_dim: 0, T: 1, k: 0, phases: 1, slicesPerPhase: 1, enableCollisions, tau: 1, #slices: 1, #totalDOF: 33, #frames: 15, #pathQueries: 3624 }
switches: { sw: "SWITCH timeOfApplication=-1 symbol=addContact jointType=none fromId=14'obj' toId=13'box'", sw: "SWITCH timeOfApplication=-1 symbol=addContact jointType=none fromId=14'obj' toId=12'wall'", sw: "SWITCH timeOfApplication=-1 symbol=addContact jointType=none fromId=14'obj' toId=4'l_finger'", sw: "SWITCH timeOfApplication=-1 symbol=addContact jointType=none fromId=14'obj' toId=9'r_finger'" } ===
===
SpherePacking SpherePacking problem
===
===
MinimalConvexCore MinimalConvexCore problem
===