I would like to find the position [X,Y,Z] of several elements in order to have the global center of gravity at a desired point.
To do so, I defined 2 classes:
- elements which store the element's mass and position
- plane which takes a list of element and can compute its center of gravity according to the set of element.
I also defined an error function which compute the difference between the actual global center of gravity from the plane's set of elements and the desired center of gravity.
To minimize this function, I wanted to use the scypi.minimize
function with the Nelder-Mead Simplex algorithm.
I put each element's coordinate into x0
and then pass x0
and the error function as parameter to minimize.
I received this error which I don't understand.:
ValueError: setting an array element with a sequence.
Moreover according to what I want to do, you may have a better idea to solve/fix my problem?
here is the code :
import numpy as np
from scipy.optimize import minimize
class plane(object):
def __init__(self, elts):
self.elements=elts
self.TotalMasse=self.calc_masse(self.elements)
self.cdg = self.calc_CDG()
def __getitem__(self):
return self.elements,self.TotalMasse
def calc_masse(self,elements):
Lm=[]
for el in elements:
Lm.append(el.masse)
return sum(Lm)
def calc_CDG(self):
Xcdg=0
Ycdg=0
Zcdg=0
for el in self.elements:
Xcdg+=el.masse*el.position[0]/self.TotalMasse
Ycdg+=el.masse*el.position[1]/self.TotalMasse
Zcdg+=el.masse*el.position[2]/self.TotalMasse
return [Xcdg,Ycdg,Zcdg]
class element(object):
def __init__(self, mass, pos):
self.masse=mass
self.position=pos
def __getitem__(self):
return self.masse, self.position
def calculErreurPosCDG(cdg):
global positionCDGconsigne
return [positionCDGconsigne[0]-cdg[0], positionCDGconsigne[1]-cdg[1],positionCDGconsigne[2]-cdg[2]]
battery = element(0.5,[0.5,1,1])
motor = element(0.2,[1,1,0])
servoL = element(0.01,[-0.7,1,0])
servoR = element(0.01,[0.7,1,0])
reciever = element(0.01,[0.1,1,1])
elements=[battery, motor, servoL, servoR, reciever]
positionCDGconsigne=[1,1,1]
plane1=plane(elements)
x0=np.array([])
for el in elements:
x0= np.append(x0,[el.position])
res=minimize(calculErreurPosCDG,x0,method='nelder-mead', options={'xtol':1e-8,'disp':True})