Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(in1,v1,c.dot(x,x),(2*x).T),
(in1,v1,c.dot(x**2,x),(3*x**2).T),
#(in1,v1,c.det(horzcat(*[x,DM([1,2])])),DM([-1,2])), not implemented
(in1,v1,f1.call(in1)[1],y),
(in1,v1,f1.call([x**2,y])[1],y*2*vertcat(*[x.T,x.T])),
(in1,v1,f2.call(in1)[0],DM.zeros(0,2)),
(in1,v1,f2(x**2,y),DM.zeros(0,2)),
(in1,v1,f3.call(in1)[0],DM.zeros(0,2)),
(in1,v1,f3.call([x**2,y])[0],DM.zeros(0,2)),
(in1,v1,f4.call(in1)[0],DM.zeros(0,2)),
(in1,v1,f4.call([x**2,y])[0],DM.zeros(0,2)),
#(in1,v1,f1([x**2,[]])[1],DM.zeros(2,2)),
#(in1,v1,f1([[],y])[1],DM.zeros(2,2)),
(in1,v1,vertcat(*[x,DM(0,1)]),DM.eye(2)),
(in1,v1,project(x**2, sparsify(DM([0,1])).sparsity()),blockcat([[MX(1,1),MX(1,1)],[MX(1,1),2*x[1]]])),
(in1,v1,c.dot(x,y[:,0]),y[:,0].T),
(in1,v1,x.nz[IM([[1,0]])].T*y.nz[IM([[0,2]])],blockcat([[MX(1,1),y.nz[0]],[y.nz[2],MX(1,1)]])),
(in1,v1,x.nz[c.diag([1,0])]*y.nz[c.diag([0,2])],blockcat([[MX(1,1),y.nz[0]],[MX(1,1),MX(1,1)],[MX(1,1),MX(1,1)],[y.nz[2],MX(1,1)]])),
]:
print(out)
fun = Function("fun", inputs,[out,jac])
funsx = fun.expand("expand_fun")
fun_ad = [Function("fun", inputs,[out,jac], {'ad_weight':w, 'ad_weight_sp':w}) for w in [0,1]]
funsx_ad = [f.expand('expand_'+f.name()) for f in fun_ad]
fun_out = fun.call(values)
funsx_out = funsx.call(values)
self.checkarray(fun_out[0],funsx_out[0])
self.checkarray(fun_out[1],funsx_out[1])
self.check_codegen(fun,inputs=values)
def norm_2(mx):
return c.dot(mx,mx)
N=10
def fun(self,N,setup):
c.dot(setup['A'],setup['B'])
(in1,v1,yyy[:,0],sparsify(DM([[0,1],[1,0]]))),
(in1,v1,mtimes(y,x),y),
(in1,v1,mtimes(x.T,y.T),y),
(in1,v1,mac(y,x,DM.zeros(Sparsity.triplet(2,1,[1],[0]))),y[Sparsity.triplet(2,2,[1,1],[0,1])]),
(in1,v1,mac(x.T,y.T,DM.zeros(Sparsity.triplet(2,1,[1],[0]).T)),y[Sparsity.triplet(2,2,[1,1],[0,1])]),
(in1,v1,mtimes(y[Sparsity.triplet(2,2,[0,1,1],[0,0,1])],x),y[Sparsity.triplet(2,2,[0,1,1],[0,0,1])]),
(in1,v1,mtimes(x.T,y[Sparsity.triplet(2,2,[0,1,1],[0,0,1])].T),y[Sparsity.triplet(2,2,[0,1,1],[0,0,1])]),
(in1,v1,mtimes(y,x**2),y*2*vertcat(*[x.T,x.T])),
(in1,v1,sin(x),c.diag(cos(x))),
(in1,v1,sin(x**2),c.diag(cos(x**2)*2*x)),
(in1,v1,x*y[:,0],c.diag(y[:,0])),
(in1,v1,x*y.nz[[0,1]],c.diag(y.nz[[0,1]])),
(in1,v1,x*y.nz[[1,0]],c.diag(y.nz[[1,0]])),
(in1,v1,x*y[[0,1],0],c.diag(y[[0,1],0])),
(in1,v1,x*y[[1,0],0],c.diag(y[[1,0],0])),
(in1,v1,c.dot(x,x),(2*x).T),
(in1,v1,c.dot(x**2,x),(3*x**2).T),
#(in1,v1,c.det(horzcat(*[x,DM([1,2])])),DM([-1,2])), not implemented
(in1,v1,f1.call(in1)[1],y),
(in1,v1,f1.call([x**2,y])[1],y*2*vertcat(*[x.T,x.T])),
(in1,v1,f2.call(in1)[0],DM.zeros(0,2)),
(in1,v1,f2(x**2,y),DM.zeros(0,2)),
(in1,v1,f3.call(in1)[0],DM.zeros(0,2)),
(in1,v1,f3.call([x**2,y])[0],DM.zeros(0,2)),
(in1,v1,f4.call(in1)[0],DM.zeros(0,2)),
(in1,v1,f4.call([x**2,y])[0],DM.zeros(0,2)),
#(in1,v1,f1([x**2,[]])[1],DM.zeros(2,2)),
#(in1,v1,f1([[],y])[1],DM.zeros(2,2)),
(in1,v1,vertcat(*[x,DM(0,1)]),DM.eye(2)),
(in1,v1,project(x**2, sparsify(DM([0,1])).sparsity()),blockcat([[MX(1,1),MX(1,1)],[MX(1,1),2*x[1]]])),
(in1,v1,c.dot(x,y[:,0]),y[:,0].T),
(in1,v1,x.nz[IM([[1,0]])].T*y.nz[IM([[0,2]])],blockcat([[MX(1,1),y.nz[0]],[y.nz[2],MX(1,1)]])),
def setupfun(self,N):
A = SX.sym("A",Sparsity.diag(N))
A[-1,0]=SX("off") # Have one of-diagonal element
B = SX.sym("B",N,1)
f = Function('f', [A,B],[c.dot(A,B)])
return {'f':f}
def fun(self,N,setup):
def add_cop_constraint(self, contact, p, z, scaling=0.95):
X = scaling * contact.X
Y = scaling * contact.Y
CZ, ZG = z - contact.p, p - z
CZxZG = casadi.cross(CZ, ZG)
Dx = casadi.dot(contact.b, CZxZG) / X
Dy = casadi.dot(contact.t, CZxZG) / Y
ZGn = casadi.dot(contact.n, ZG)
slackness = Dx ** 2 + Dy ** 2 - ZGn ** 2
self.nlp.add_constraint(slackness, lb=[-self.nlp.infty], ub=[-0.005])
def add_linear_friction_constraints(self, contact, p, z):
mu_inner = contact.friction / casadi.sqrt(2)
ZG = p - z
ZGt = casadi.dot(ZG, contact.t)
ZGb = casadi.dot(ZG, contact.b)
ZGn = casadi.dot(ZG, contact.n)
c0 = ZGt - mu_inner * ZGn
c1 = -ZGt - mu_inner * ZGn
c2 = ZGb - mu_inner * ZGn
c3 = -ZGb - mu_inner * ZGn
self.nlp.add_constraint(c0, lb=[-self.nlp.infty], ub=[0])
self.nlp.add_constraint(c1, lb=[-self.nlp.infty], ub=[0])
self.nlp.add_constraint(c2, lb=[-self.nlp.infty], ub=[0])
self.nlp.add_constraint(c3, lb=[-self.nlp.infty], ub=[0])
def add_cop_constraint(self, contact, p, z, scaling=0.95):
X = scaling * contact.X
Y = scaling * contact.Y
CZ, ZG = z - contact.p, p - z
CZxZG = casadi.cross(CZ, ZG)
Dx = casadi.dot(contact.b, CZxZG) / X
Dy = casadi.dot(contact.t, CZxZG) / Y
ZGn = casadi.dot(contact.n, ZG)
slackness = Dx ** 2 + Dy ** 2 - ZGn ** 2
self.nlp.add_constraint(slackness, lb=[-self.nlp.infty], ub=[-0.005])
'p_%d' % (k + 1), 3, init=start_com, lb=self.p_min,
ub=self.p_max)
v_k = self.nlp.new_variable(
'v_%d' % (k + 1), 3, init=start_comd, lb=self.v_min,
ub=self.v_max)
self.nlp.add_equality_constraint(p_next, p_k)
self.nlp.add_equality_constraint(v_next, v_k)
self.nlp.add_constraint(
T_swing, lb=[swing_duration], ub=[100],
name='T_swing')
p_last, v_last = p_k, v_k
p_diff = p_last - end_com
v_diff = v_last - end_comd
self.nlp.extend_cost(self.weights['p'] * casadi.dot(p_diff, p_diff))
self.nlp.extend_cost(self.weights['v'] * casadi.dot(v_diff, v_diff))
self.nlp.extend_cost(self.weights['t'] * T_total)
self.nlp.create_solver()
#
self.com_target = com_target
self.end_com = array(end_com)
self.end_comd = array(end_comd)
self.nb_steps = nb_steps
self.preview = ZMPPreviewBuffer(contact_sequence)
def evaluate_coordinate_state_cost(self,coordinates_state):
""" evaluate the function h(x) """
value=1.
for i in range(0,self._number_of_constraints):
value *= Obstacle.trim_and_square(\
cd.dot(self._a[:,i], coordinates_state) + self._b[i]\
)
return value