Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. #%%%%%%%% NEUROMECHANICS %%%%%%%%%%%%%
  3. # (c) Francisco Valero-Cuevas
  4. # August 2016, version 2.0
  5. # Filename: zonotope_muti_N.py
  6.  
  7. # This script shows how to map N-dimensional N-cubes into 2D and 3D via a
  8. # random H matrix of dimensions 2 x N and 3 x N, respectively.
  9.  
  10. # It then plots the convex hulls of the zonotopes when considering
  11. # the input to be between 3 and 8 dimensions. That is, a system having 3 to
  12. # 8 muscles
  13.  
  14. import numpy as np
  15. import itertools
  16. from scipy.spatial import ConvexHull
  17. import matplotlib.pyplot as plt
  18. import random
  19.  
  20. #%%%%%%%%%%%%%%%%%% User Input %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  21. # Enter the dimensions of the input space here. Note it must be >=3
  22. LargestDimension = 8 # this is the largest dimension
  23.  
  24. # Iterate to calculate the zonotopes for inout dimensions from 3 to 8
  25.  
  26. def GenerateActivationSpaceVertices(dim):
  27. """
  28. This function takes the place of the Matlab function ncube.m.
  29. Takes in a dimension (dim) and gives all of the vertices needed
  30. for a Minkowski sum.
  31. """
  32. output = np.matrix(list(itertools.product([0, 1], repeat=dim))).T
  33. return(output)
  34.  
  35. X = [GenerateActivationSpaceVertices(i) for i in range(3,LargestDimension+1)]
  36. H = np.random.rand(3,LargestDimension)*2-1 # this is the random full 3x8 H matrix.
  37. Vertices = [np.array(H[:2,:i]*X[i-3]) for i in range(3,LargestDimension+1)]
  38. Hull = [ConvexHull(Vertices[i-3].T) for i in range(3,LargestDimension+1)]
  39.  
  40. def Plot2DConvHull(ax,Vertices,Hull,Matrix):
  41. #ax.plot(Vertices[:,0],Vertices[:,1],'ko')
  42. for simplex in Hull.simplices:
  43. ax.plot(Vertices[simplex,0],Vertices[simplex,1], color = 'grey')
  44.  
  45. plt.figure()
  46. ax = plt.gca()
  47. [Plot2DConvHull(ax,Vertices[i-3].T,Hull[i-3],H[:2,:i]) for i in range(3,LargestDimension+1)]
  48. UsedVertices = np.array([0.,0.])
  49. for i in range(3,LargestDimension+1):
  50. RandomVertex = Vertices[i-3].T[random.choice(random.choice(Hull[i-3].simplices))]
  51. while np.any(RandomVertex == UsedVertices):
  52. RandomVertex = Vertices[i-3].T[random.choice(random.choice(Hull[i-3].simplices))]
  53. plt.text(RandomVertex[0], RandomVertex[1], str(i)+" Muscles")
  54. UsedVertices = np.concatenate((UsedVertices,RandomVertex),axis=0)
  55. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement