Skip to main content

Using a Grobner basis to calculate common roots of a system of polynomial equations


We are trying to solve the inverse kinematics of a robot with 3 revolute joints and one prismatic arm, link 4, able to take lengths between 1 and 2. We assume all other lengths are 1. Image of robot Image courtesy of Cox, Little, and O'Shea [page 300, 1]. We define a point (a,b,θ) in configuration space to be a position and orientation of the end "hand", with(a,b)∈R2 the position of the hand, and θ the angle the hand makes with the standard x axis. As the hand is rigidly attached to length 4 it follows that any orientation θ is precisely the sum θ1+θ2+θ3. For any such position in configuration space (a,b,θ) we want to find whether there exists a point (θ1,θ2,θ3,l4) in joint space mapping to (a,b,θ) under the forward kinematic equation f(θ1,θ2,θ3,l4)=(l4cosθ+l3cos(θ2+θ1)+l2cosθ1l4sinθ+l3sin(θ1+θ2)+l2sinθ1θ),

Where θ=θ1+θ2+θ3. As θ is fully determined by s:=sinθ and c:=cosθ, this inverse kinematic problem boils down to solving, with ci=cosθi and si=sinθi: a=l4c+l3(c1c2−s1s2)+l2c1
b=l4s+l3(s1c2+s2c1)+l2s1


We want to solve this equation by first computing a Grobner basis to make calculations easier. To this end we add extra information in terms of trig identities, and end up wanting to calculate a Grobner basis for the following system of polynomials in terms of the variables c1,s1,c2,s2,l4. We do this using Mathematica with the following code:


pola = {l4*c + c1*c2 - s1*s2 + c1 - a, l4*s + s1*c2 + s2*c1 + s1-b, 
c1^2 + s1^2 - 1, c2^2 + s2^2 - 1, c^2 + s^2 - 1};
Gb = GroebnerBasis[polys, {c1, s1, c2, s2, c3, s3, l4},
MonomialOrder -> DegreeReverseLexicographic]

This almost instantly outputs a nice enough Groebner basis. Not nice to work with by hand easily, but something that a modern pc should easily be able to find the common roots of. However, trying to compute these roots using:


Solve[Gb == 0 , {l4, c2, c1, s1, s2}]


gives no solution. This is worrying. If we try


Solve[Gb == 0 , {l4, c2, c1, s1, s2},MaxExtraConditions->all]

we get an infeasible run time. If anyone can explain why this is the case it would be much appreciated. Obviously it cannot be true that there are no solutions, because the robot arm has to be able to reach at least one point. To see whether the Groebner basis is the problem, we try a different approach. There are particular points in configuration space we know that the robot arm should be able to reach. For example the robot should definitely be able to reach the point (3,0,0), corresponding to a=3, b=0, and c=1, s=0. So instead of trying to solve the equation in terms of parameters, we do the following:


Gb1 = Gb /. {a -> 4, b -> 0, s -> 0, c -> 1};
Sol=Solve[Gb1 == 0 , {l4, c2, c1, s1, s2}] /. {a -> 3, b -> 0, s ->
0,c -> 1};
ReSol = Select[Sol, Im@(l4 /. #) == 0 &];
PhysSol=Select[Sol, (l4 /. #) >= 1 && (l4 /. #) <= 2 &]


The physical solution PhysSol is the solution


{{l4 -> 1, c2 -> 1, c1 -> 1, s1 -> 0, s2 -> 0}}

as would be expected, which contradicts the empty list returned after trying to find a general solution. Using this method to solve for a=0, b=4, and c=0, s=1 also results in the correct point in joint space. However, trying for the point a=2, b=2, c=1/√2 and s=1/√2 leads to no solution (well the only solutions are with l4∉[1,2]). Trying various other points which the robot should be able to reach also gives no allowable solutions.


I have to conclude that either the robot is exceptionally useless, or there is something wrong with my code, or perhaps it is in fact impossible to get a nice exact solution to this problem (I am looking at attempting to solve it via Newton-Rhapson next). The middle one seems to be the most likely, as all mathematical programming I did in my Bachelor's was in Maple, and this is the first project I am attempting to do in Mathematica. Any advice on where I am going wrong, or where I can improve my approach would be much appreciated. I should note that this problem is largely based on exercises 16 and 17 in chapter 6.3 of Cox, Little, O'Shea.


[1] D. O’Shea D. Cox, J. Little. Ideals, Varieties, and Algorithms: An Introduction to Computational Algebraic Geometry and Commutative Algebra. Springer Internation Publishing, 2010.



Answer



I will suggest setting this up in a way that is, to my view, slightly simpler. have each angle be measured from the positive horizontal axis. Place the origin at juncture of the vertical bar and the second bar (you already did that). The arm orientation angle of interest is simply the third angle.


With this the trig polynomial system becomes:



polys = {c1 + c2 + l4*c3 - a, s1 + s2 + l4*s3 - b, c1^2 + s1^2 - 1, 
c2^2 + s2^2 - 1}

For given configuration values one can do simple replacement to get the specific system of interest.


sys[x_, y_, theta_] := 
polys /. {a -> x, b -> y, c3 -> Cos[theta], s3 -> Sin[theta]}

A solver that retains only real solutions satisfying the required inequalities can be done as below. Bearing in mind that this is underdetermined, we also specify a value for the cosine of the first angle.


realsolns[x_, y_, theta_, c1val_] := Module[
{solns, realsolns, viablesolns},

solns = {c2, s1, s2, l4} /. NSolve[sys[x, y, theta] /. c1 -> c1val];
realsolns = Select[solns, FreeQ[#, Complex] &];
viablesolns = Select[realsolns, 1 <= Last[#] <= 2 &]
]

Here is an example with the first cosine set to be small.


In[64]:= realsolns[2, 2, Pi/4, 1/25]

(* Out[64]= {{0.9992, 0.9992, 0.04, 1.35878}} *)


We can get two viable solutions if we set it to be fairly large.


In[65]:= realsolns[2, 2, Pi/4, 1 - 1/25]

(* Out[65]= {{-0.28, -0.28, 0.96, 1.86676}, {0.28, 0.28, 0.96, 1.0748}} *)

One can get parametrized solutions using Solve.


solns[x_, y_, theta_] := 
{c2, s1, s2, l} /. Solve[sys[x, y, theta] == 0]



In[72]:= solns22pi4 = solns[2, 2, Pi/4]

(* Out[72]= {{-c1, -Sqrt[1 - c1^2], Sqrt[1 - c1^2], 2 Sqrt[2]}, {-c1,
Sqrt[1 - c1^2], -Sqrt[1 - c1^2],
2 Sqrt[2]}, {-Sqrt[1 - c1^2], -Sqrt[1 - c1^2], c1, (
4 - 2 c1 + 2 Sqrt[1 - c1^2])/Sqrt[2]}, {Sqrt[1 - c1^2], Sqrt[
1 - c1^2], c1, (4 - 2 c1 - 2 Sqrt[1 - c1^2])/Sqrt[2]}} *)

For any viable c1 cosine value (that is, between -1 and 1 inclusive), the first two solutions are not viable, the fourth is viable, while the third can have an arm length greater or less than 2 depending on c1 (the viable range is actually fairly small, between approximately .9365 and 1).


In[85]:= NSolve[(4 - 2 c1 + 2 Sqrt[1 - c1^2])/Sqrt[2] == 2, c1]


(* Out[85]= {{c1 -> 0.936487}} *)

For final angle of -pi/4 there are no solutions that work. AN analysis of the result will show that all l4 values will be either negtaive or have imaginary parts.


Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

How to remap graph properties?

Graph objects support both custom properties, which do not have special meanings, and standard properties, which may be used by some functions. When importing from formats such as GraphML, we usually get a result with custom properties. What is the simplest way to remap one property to another, e.g. to remap a custom property to a standard one so it can be used with various functions? Example: Let's get Zachary's karate club network with edge weights and vertex names from here: http://nexus.igraph.org/api/dataset_info?id=1&format=html g = Import[ "http://nexus.igraph.org/api/dataset?id=1&format=GraphML", {"ZIP", "karate.GraphML"}] I can remap "name" to VertexLabels and "weights" to EdgeWeight like this: sp[prop_][g_] := SetProperty[g, prop] g2 = g // sp[EdgeWeight -> (PropertyValue[{g, #}, "weight"] & /@ EdgeList[g])] // sp[VertexLabels -> (# -> PropertyValue[{g, #}, "name"]...