MAGIKS  1.1
Manipulator General Inverse Kinematic Solver
 All Classes Namespaces Files Functions Variables Pages
optimization.py
Go to the documentation of this file.
1 ## @file optimization.py
2 # @brief This module provides fast solution to some typical optimization problems
3 # @author Nima Ramezani Taghiabadi
4 #
5 # PhD Researcher
6 # Faculty of Engineering and Information Technology
7 # University of Technology Sydney (UTS)
8 # Broadway, Ultimo, NSW 2007, Australia
9 # Phone No. : 04 5027 4611
10 # Email(1) : nima.ramezani@gmail.com
11 # Email(2) : Nima.RamezaniTaghiabadi@uts.edu.au
12 # @version 1.0
13 #
14 # start date: June 2015
15 # Last Revision: 19 June 2015
16 
17 import numpy as np, math
18 
19 from math_tools import general_math as gen
20 
21 max_iter = 20
22 
23 def distance_from_ellipsoid(a, b, c, x0, y0, z0, t0 = None, silent = True, maximum = False):
24  global max_iter
25  '''
26  Find theta and phi that minimize function:
27  f(theta, phi) = ( a*cos(theta)*cos(phi) - x0 )^2 + ( b*sin(theta)*cos(phi) - y0 )^2 + ( c*sin(phi) - z0 )^2
28 
29  The method is numerical using the Hessian matrix.
30  The iterations start from theta_0 and phi_0
31  '''
32  a2 = a*a
33  b2 = b*b
34  c2 = c*c
35 
36  if t0 == None:
37  if gen.equal(x0, 0.0) and gen.equal(y0, 0.0) and gen.equal(z0, 0.0):
38  if maximum:
39  if max(a,b,c) == a: # To maximize the function, min must be replaced by max
40  t0 = np.zeros(2)
41  elif max(a,b,c) == b:
42  t0 = np.array([math.pi/2, 0.0])
43  else:
44  t0 = np.array([0.0, math.pi/2])
45  else:
46  if min(a,b,c) == a: # To maximize the function, min must be replaced by max
47  t0 = np.zeros(2)
48  elif min(a,b,c) == b:
49  t0 = np.array([math.pi/2, 0.0])
50  else:
51  t0 = np.array([0.0, math.pi/2])
52  else:
53  if maximum:
54  ssign = - 1.0
55  else:
56  ssign = 1.0
57  s = ssign/math.sqrt(x0*x0/a2 + y0*y0/b2 + z0*z0/c2) # To find the max distance s must be negative s = -1.0/math.sqrt(...)
58  the = math.atan2(a*y0, b*x0)
59  phi = math.atan2(z0*s/c, s*math.sqrt(x0*x0/a2 + y0*y0/b2))
60  assert gen.equal(a*math.cos(the)*math.cos(phi), x0*s)
61  assert gen.equal(b*math.sin(the)*math.cos(phi), y0*s)
62  assert gen.equal(c*math.sin(phi), z0*s)
63  t0 = np.array([the,phi])
64 
65  H = np.zeros((2,2))
66  df = np.zeros(2)
67 
68  t = t0
69  stay = True
70  cnt = 0
71 
72  while stay and (cnt < max_iter):
73 
74  ct = math.cos(t[0])
75  cf = math.cos(t[1])
76  st = math.sin(t[0])
77  sf = math.sin(t[1])
78  ct2 = ct*ct
79  st2 = st*st
80  cf2 = cf*cf
81  sf2 = sf*sf
82  c2t = ct2 - st2
83  c2f = cf2 - sf2
84  s2t = 2*st*ct
85  s2f = 2*sf*cf
86 
87  df[0] = 0.5*(b2 - a2)*s2t*cf2 + cf*(a*x0*st - b*y0*ct)
88  df[1] = - 0.5*s2f*(a2*ct2 + b2*st2 - c2) + sf*(a*x0*ct + b*y0*st) - c*z0*cf
89 
90  stay = not gen.equal(np.linalg.norm(df), 0.0)
91  if stay:
92  H[0,0] = (b2 - a2)*c2t*cf2 + cf*(a*x0*ct + b*y0*st)
93  H[0,1] = 0.5*(a2 - b2)*s2t*s2f - sf*(a*x0*st - b*y0*ct)
94  H[1,0] = H[0,1]
95  H[1,1] = - c2f*(a2*ct2 + b2*st2 - c2) + cf*(a*x0*ct + b*y0*st) + c*z0*sf
96 
97  t = t - np.dot(np.linalg.inv(H), df)
98 
99  cnt += 1
100  if not silent:
101  print
102  print 'iteration:', cnt, ' cost function value:', (a*ct*cf - x0)**2 + (b*st*cf - y0)**2 + (c*sf - z0)**2, ' norm(div_f):', np.linalg.norm(df)
103 
104  return (t[0], t[1])