Рассмотрите этот код для оптимизации методом наименьших квадратов для вектора с тремя переменными и тремя измерениями :
Код: Выделить всё
import scipy
import numpy as np
import cvxpy as cvx
import clarabel
A = np.array([[-43.83695965, 0.34990409, -1.32935518],
[-48.86811152, 0.57411847, -1.38219424],
[-49.25621142, 0.75738835, -1.2979702 ]])
b = np.array([ 53897.91898074, -159390.11128713, -62013.59835614])
bounds_arr = np.array([[-1., -1., -1.],
[ 1., 1., 1.]])
#------- Using CLARABEL as cvxpy backend---------------
x = cvx.Variable(A.shape[1])
cost = cvx.sum_squares(A @ x - b)
constraints = []
constraints += [xv >= bnd for (xv, bnd) in zip(x, bounds_arr[0])]
constraints += [xv = -np.sqrt(3)]
# constraints += [cvx.sum(x) >= -np.sqrt(3)] # This form doesn't work either
prob = cvx.Problem(cvx.Minimize(cost), constraints)
prob.solve(solver="CLARABEL", verbose=True) # Says 'PrimalInfeasible'
print("x: ", x.value)
#------- Using CLARABEL directly---------------
P = A.T @ A
P = scipy.sparse.triu(P).tocsc()
q = -b.T @ A
# Note constant term is ignored in this case because it is irrelevant to the solution of the optimisation problem.
Ac = scipy.sparse.vstack([-scipy.sparse.identity(A.shape[1]),
scipy.sparse.identity(A.shape[1]),
np.array([[-1.0, -1.0, -1.0]])]).tocsc()
bc = np.concat([-bounds_arr[0], bounds_arr[1], [np.sqrt(3)]], axis=0)
cones = [clarabel.NonnegativeConeT(A.shape[1]), clarabel.NonnegativeConeT(A.shape[1]), clarabel.NonnegativeConeT(1)]
settings = clarabel.DefaultSettings()
solver = clarabel.DefaultSolver(P, q, Ac, bc, cones, settings) #, A, b, cones, settings
solution = solver.solve() # WORKS
print("x: ", solution.x)
Если нет, то это ошибка в cvxpy?
Подробнее здесь: https://stackoverflow.com/questions/790 ... d-of-cvxpy