本文整理汇总了Python中cvxpy.square方法的典型用法代码示例。如果您正苦于以下问题:Python cvxpy.square方法的具体用法?Python cvxpy.square怎么用?Python cvxpy.square使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cvxpy
的用法示例。
在下文中一共展示了cvxpy.square方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_objective
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import square [as 别名]
def get_objective(self, X_v, U_v, X_last_p, U_last_p):
"""
Get model specific objective to be minimized.
:param X_v: cvx variable for current states
:param U_v: cvx variable for current inputs
:param X_last_p: cvx parameter for last states
:param U_last_p: cvx parameter for last inputs
:return: A cvx objective function.
"""
slack = 0
for j in range(len(self.obstacles)):
slack += cvx.sum(self.s_prime[j])
objective = cvx.Minimize(1e5 * slack)
# objective += cvx.Minimize(cvx.sum(cvx.square(U_v)))
return objective
示例2: newsvendor_opt
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import square [as 别名]
def newsvendor_opt(params, py):
z = cp.Variable(1)
d = params['d']
f = (params['c_lin'] * z + 0.5 * params['c_quad'] * cp.square(z) +
py.T * (params['b_lin'] * cp.pos(d-z) +
0.5 * params['b_quad'] * cp.square(cp.pos(d-z)) +
params['h_lin'] * cp.pos(z-d) +
0.5 * params['h_quad'] * cp.square(cp.pos(z-d)) ))
fval = cp.Problem(cp.Minimize(f), [z >= 0]).solve()
return z.value, fval
# Inventory ordering cost given demand realization
示例3: block_solve_cvx
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import square [as 别名]
def block_solve_cvx(r_j, A_j, a_1_j, a_2_j, m, b_j_init, verbose=False):
# pylint: disable=unused-argument
"""Solve the gelcd optimization problem for a single block with cvx.
b_j_init and verbose are ignored. b_j_init because cvx doesn't support it.
verbose because it doesn't go together with tqdm.
"""
# Convert everything to numpy.
device = A_j.device
dtype = A_j.dtype
r_j = r_j.cpu().numpy()
A_j = A_j.cpu().numpy()
# Create the b_j variable.
b_j = cvx.Variable(A_j.shape[1])
# Form the objective.
q_j = r_j - A_j * b_j
obj_fun = cvx.square(cvx.norm(q_j)) / (2.0 * m)
obj_fun += a_1_j * cvx.norm(b_j) + (a_2_j / 2.0) * cvx.square(cvx.norm(b_j))
# Build the optimization problem.
obj = cvx.Minimize(obj_fun)
problem = cvx.Problem(obj, constraints=None)
problem.solve(solver="CVXOPT", verbose=False)
b_j = np.asarray(b_j.value)
return torch.from_numpy(b_j).to(device, dtype)
示例4: cost
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import square [as 别名]
def cost(self):
p = self.terminals[0].power_var
return self.alpha * cvx.square(p) - self.beta * p + self.gamma
示例5: constraints
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import square [as 别名]
def constraints(self):
p1 = self.terminals[0].power_var
p2 = self.terminals[1].power_var
constrs = []
if self.alpha > 0:
constrs += [p1 + p2 >= self.alpha * cvx.square((p1 - p2) / 2)]
if self.power_max is not None:
constrs += [2 * self.alpha * self.power_max**2 >= p1 + p2]
else:
constrs += [p1 + p2 == 0]
if self.power_max is not None:
constrs += [cvx.abs((p1 - p2) / 2) <= self.power_max]
return constrs
示例6: gel_solve_cvx
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import square [as 别名]
def gel_solve_cvx(As, y, l_1, l_2, ns):
"""Solve a group elastic net problem with cvx.
Arguments:
As: list of tensors.
y: tensor.
l_1, l_2: floats.
ns: iterable.
"""
# Convert everything to numpy
dtype = As[0].dtype
As = [A_j.cpu().numpy() for A_j in As]
y = y.cpu().numpy()
ns = np.array([int(n) for n in ns])
# Create the b variables.
b_0 = cvx.Variable()
bs = []
for _, n_j in zip(As, ns):
bs.append(cvx.Variable(n_j))
# Form g(b).
Ab = sum(A_j * b_j for A_j, b_j in zip(As, bs))
m = As[0].shape[0]
g_b = cvx.square(cvx.norm(y - b_0 - Ab)) / (2 * m)
# Form h(b).
h_b = sum(
np.sqrt(n_j) * (l_1 * cvx.norm(b_j) + l_2 * cvx.square(cvx.norm(b_j)))
for n_j, b_j in zip(ns, bs)
)
# Build the optimization problem.
obj = cvx.Minimize(g_b + h_b)
problem = cvx.Problem(obj, constraints=None)
problem.solve(solver="CVXOPT")
b_0 = b_0.value.item()
# Form B as returned by gel_solve.
p = len(As)
B = torch.zeros(p, int(max(ns)), dtype=dtype)
for j in range(p):
b_j = np.asarray(bs[j].value)
B[j, : ns[j]] = torch.from_numpy(b_j)
return b_0, B