Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
dev_max_right = reftrack[:, 2] - w_veh / 2
dev_max_left = reftrack[:, 3] - w_veh / 2
# set minimum deviation to zero
dev_max_right[dev_max_right < 0.001] = 0.001
dev_max_left[dev_max_left < 0.001] = 0.001
# consider value boundaries (-dev_max <= alpha <= dev_max)
G = np.vstack((np.eye(no_points), -np.eye(no_points)))
h = np.ones(2 * no_points) * np.append(dev_max_right, dev_max_left)
# save start time
t_start = time.perf_counter()
# solve problem
alpha_shpath = quadprog.solve_qp(H, -f, -G.T, -h, 0)[0]
# print runtime into console window
if print_debug:
print("Solver runtime opt_shortest_path: " + "{:.3f}".format(time.perf_counter() - t_start) + " seconds")
return alpha_shpath
Delta = np.zeros((M - 1, M))
for i in range(M - 1):
Delta[i, i] = 1
Delta[i, i + 1] = - 1
for k in range(dof):
Xd = np.vstack((q[1:, k], qd[1:, k])).T.flatten() # numpy magic
x0 = np.r_[q[0, k], qd[0, k]]
xM = np.r_[q[-1, k], qd[-1, k]]
G = np.dot(Phi.T, Phi) + np.dot(Delta.T, Delta) * smooth_eps
a = - np.dot(Phi.T, Beta.dot(x0) - Xd)
C = Phi[2 * M - 2:].T
b = xM - Beta[2 * M - 2:].dot(x0)
sol = quadprog.solve_qp(G, a, C, b, meq=2)[0]
Xsol = np.dot(Phi, sol) + np.dot(Beta, x0)
Xsol = Xsol.reshape(-1, 2)
q[1:, k] = Xsol[:, 0]
qd[1:, k] = Xsol[:, 1]
qdd[:-1, k] = sol
qdd[-1, k] = sol[-1]
return tsample, q, qd, qdd
print("quadprog: note that warm-start values ignored by wrapper")
qp_G = P
qp_a = -q
if A is not None:
if G is None:
qp_C = -A.T
qp_b = -b
else:
qp_C = -vstack([A, G]).T
qp_b = -hstack([b, h])
meq = A.shape[0]
else: # no equality constraint
qp_C = -G.T if G is not None else None
qp_b = -h if h is not None else None
meq = 0
return solve_qp(qp_G, qp_a, qp_C, qp_b, meq)[0]
h = np.append(h, con_stack)
# save start time
t_start = time.perf_counter()
# solve problem (CVXOPT) -------------------------------------------------------------------------------------------
# args = [cvxopt.matrix(H), cvxopt.matrix(f), cvxopt.matrix(G), cvxopt.matrix(h)]
# sol = cvxopt.solvers.qp(*args)
#
# if 'optimal' not in sol['status']:
# print("WARNING: Optimal solution not found!")
#
# alpha_mincurv = np.array(sol['x']).reshape((H.shape[1],))
# solve problem (quadprog) -----------------------------------------------------------------------------------------
alpha_mincurv = quadprog.solve_qp(H, -f, -G.T, -h, 0)[0]
# print runtime into console window
if print_debug:
print("Solver runtime opt_min_curv: " + "{:.3f}".format(time.perf_counter() - t_start) + " seconds")
# ------------------------------------------------------------------------------------------------------------------
# CALCULATE CURVATURE ERROR ----------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
# calculate curvature once based on original linearization and once based on a new linearization around the solution
q_x_tmp = q_x + np.matmul(M_x, np.expand_dims(alpha_mincurv, 1))
q_y_tmp = q_y + np.matmul(M_y, np.expand_dims(alpha_mincurv, 1))
x_prime_tmp = np.eye(no_points, no_points) * np.matmul(np.matmul(A_ex_b, A_inv), q_x_tmp)
y_prime_tmp = np.eye(no_points, no_points) * np.matmul(np.matmul(A_ex_b, A_inv), q_y_tmp)
def quadprog_solve_qp(P, q, G=None, h=None, A=None, b=None):
qp_G = .5 * (P + P.T) # make sure P is symmetric
qp_a = -q
if A is not None:
qp_C = -np.vstack([A, G]).T
qp_b = -np.hstack([b, h])
meq = A.shape[0]
else: # no equality constraint
qp_C = -G.T
qp_b = -h
meq = 0
print("qp_G", qp_G)
print("qp_a", qp_a)
print("qp_C", qp_C)
print("qp_b", qp_b)
x, f, _, iterations, _, _ = quadprog.solve_qp(qp_G, qp_a, qp_C, qp_b, meq)
print(iterations, f)
return x
def iterate(self):
wk = self.portfolio.weights
g = self.portfolio.risk_concentration.risk_concentration_vector()
A = self.portfolio.risk_concentration.jacobian_risk_concentration_vector()
At = tf.transpose(A)
Q = 2 * At @ A + self._tauI
q = 2 * tf.linalg.matvec(At, g) - tf.linalg.matvec(Q, wk)
if self.portfolio.has_variance:
Q += self.portfolio.lmd * self.portfolio.covariance
if self.portfolio.has_mean_return:
q -= self.portfolio.alpha * self.portfolio.mean
w_hat = quadprog.solve_qp(Q.numpy(), -q.numpy(), C=self.CCmat, b=self.bvec, meq=self.meq)[0]
#w_hat = qp.solve(Qmat=Q.numpy(), qvec=q.numpy(), Cmat=self.Cmat, cvec=self.cvec,
# Dmat=self.Dmat, dvec=self.dvec, w0=wk, maxiter=self.maxiter,
# tol=self.wtol)
self.portfolio.weights = wk + self.gamma * (w_hat - wk)
fun_next = self.get_objective_function_value()
self.objective_function.append(fun_next.numpy())
has_w_converged = (tf.abs(self.portfolio.weights - wk) <=
.5 * self.wtol * (tf.abs(self.portfolio.weights) +
tf.abs(wk))).numpy().all()
has_fun_converged = (tf.abs(self._funk - fun_next) <=
.5 * self.funtol * (tf.abs(self._funk) +
tf.abs(fun_next))).numpy().all()
if has_w_converged or has_fun_converged:
return False
self.gamma = self.gamma * (1 - self.zeta * self.gamma)
self._funk = fun_next
def quadprog_solve_qp(P, q, G=None, h=None, A=None, b=None, initvals=None):
"""
Solve a Quadratic Program defined as:
minimize
(1/2) * x.T * P * x + q.T * x
subject to
G * x <= h
A * x == b
using quadprog .
"""
P = .5 * (P + P.T) # quadprog assumes that P is symmetric
return solve_qp(P, -q, -G.T, -h)[0]
gradient "gradient", and a memory of task gradients "memories".
Overwrites "gradient" with the final projected update.
input: gradient, p-vector
input: memories, (t * p)-vector
output: x, p-vector
"""
memories_np = memories.cpu().t().double().numpy()
gradient_np = gradient.cpu().contiguous().view(-1).double().numpy()
t = memories_np.shape[0]
P = np.dot(memories_np, memories_np.transpose())
P = 0.5 * (P + P.transpose()) + np.eye(t) * eps
q = np.dot(memories_np, gradient_np) * -1
G = np.eye(t)
h = np.zeros(t) + margin
v = quadprog.solve_qp(P, q, G, h)[0]
x = np.dot(v, memories_np) + gradient_np
gradient.copy_(torch.Tensor(x).view(-1, 1))