end_pose_solver.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 from __future__ import print_function, division
3 
4 from scipy.optimize import (
5  minimize,
6  Bounds,
7  # LinearConstraint,
8  NonlinearConstraint,
9  # BFGS,
10  SR1,
11 )
12 import numpy as np
13 from time import time
14 
15 
16 class SciPyEndPoseSolver(object):
17  """
18  Uses SciPy to solve a constrained EndPoseProblem. Options for SciPy minimize
19  can be found here:
20  https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
21  """
22 
23  def __init__(self, problem=None, method=None, debug=False):
24  print("Initialising SciPy Solver")
25  self.problem = problem
26  self.debug = debug
27  self.method = method
29  if self.method != "SLSQP":
30  self.hessian_update_strategy = SR1()
31  self.max_iterations = 100
32 
33  def specifyProblem(self, problem):
34  self.problem = problem
35 
36  def eq_constraint_fun(self, x):
37  self.problem.update(x)
38  return self.problem.get_equality()
39 
40  def eq_constraint_jac(self, x):
41  self.problem.update(x)
42  return self.problem.get_equality_jacobian()
43 
44  def neq_constraint_fun(self, x):
45  self.problem.update(x)
46  # print("NEQ", -1. * self.problem.get_inequality())
47  return -1.0 * self.problem.get_inequality()
48 
49  def neq_constraint_jac(self, x):
50  self.problem.update(x)
51  return -1.0 * self.problem.get_inequality_jacobian()
52 
53  def cost_fun(self, x):
54  self.problem.update(x)
55  return self.problem.get_scalar_cost(), self.problem.get_scalar_jacobian()
56 
57  def cost_jac(self, x):
58  self.problem.update(x)
59  return self.problem.get_scalar_jacobian()
60 
61  def solve(self):
62  # Extract start state
63  x0 = self.problem.start_state.copy()
64 
65  self.problem.pre_update()
66 
67  # Add constraints
68  cons = []
69  if self.method != "trust-constr":
70  if self.neq_constraint_fun(np.zeros((self.problem.N,))).shape[0] > 0:
71  cons.append(
72  {
73  "type": "ineq",
74  "fun": self.neq_constraint_fun,
75  "jac": self.neq_constraint_jac,
76  }
77  )
78 
79  if self.eq_constraint_fun(np.zeros((self.problem.N,))).shape[0] > 0:
80  cons.append(
81  {
82  "type": "eq",
83  "fun": self.eq_constraint_fun,
84  "jac": self.eq_constraint_jac,
85  }
86  )
87  else:
88  if self.neq_constraint_fun(np.zeros((self.problem.N,))).shape[0] > 0:
89  cons.append(
90  NonlinearConstraint(
91  self.neq_constraint_fun,
92  0.0,
93  np.inf,
94  jac=self.neq_constraint_jac,
95  hess=self.hessian_update_strategy,
96  )
97  )
98 
99  if self.eq_constraint_fun(np.zeros((self.problem.N,))).shape[0] > 0:
100  cons.append(
101  NonlinearConstraint(
102  self.eq_constraint_fun,
103  0.0,
104  0.0,
105  jac=self.eq_constraint_jac,
106  hess=self.hessian_update_strategy,
107  )
108  )
109 
110  # Bounds
111  bounds = None
112  if self.problem.use_bounds:
113  bounds = Bounds(
114  self.problem.get_bounds()[:, 0], self.problem.get_bounds()[:, 1]
115  )
116 
117  options = {"disp": self.debug, "maxiter": self.max_iterations}
118  if self.method == "trust-constr":
119  options["initial_tr_radius"] = 1000.0
120 
121  s = time()
122  res = minimize(
123  self.cost_fun,
124  x0,
125  method=self.method,
126  bounds=bounds,
127  jac=True,
128  hess=self.hessian_update_strategy,
129  constraints=cons,
130  options=options,
131  )
132  e = time()
133  if self.debug:
134  print(e - s, res.x)
135 
136  return [res.x]
def __init__(self, problem=None, method=None, debug=False)


exotica_scipy_solver
Author(s): Wolfgang Merkt
autogenerated on Sat Apr 10 2021 02:36:55