Skip to content

Commit

Permalink
started plumbing for SOC
Browse files Browse the repository at this point in the history
  • Loading branch information
enzbus committed Sep 10, 2024
1 parent 4a3a008 commit 6a8e15b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions project_euromir/cvxpy_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from cvxpy.reductions.solution import Solution
from cvxpy.reductions.solvers import utilities
from cvxpy.reductions.solvers.conic_solvers.conic_solver import (
ConicSolver, NonNeg, Zero)
SOC, ConicSolver, NonNeg, Zero)
except ImportError as exc: # pragma: no cover
raise ImportError(
"Can't use CVXPY interface if CVXPY is not installed!") from exc
Expand Down Expand Up @@ -73,7 +73,7 @@ class Solver(ConicSolver):
"""

MIP_CAPABLE = False
SUPPORTED_CONSTRAINTS = [Zero, NonNeg]
SUPPORTED_CONSTRAINTS = [Zero, NonNeg, SOC]
REQUIRES_CONSTR = False

def name(self):
Expand All @@ -86,7 +86,7 @@ def solve_via_data(

x_orig, y_orig, s_orig = solve(
matrix=data['A'], b=data['b'], c=data['c'], zero=data['dims'].zero,
nonneg=data['dims'].nonneg)
nonneg=data['dims'].nonneg, soc=data['dims'].soc)

# breakpoint()

Expand Down
10 changes: 5 additions & 5 deletions project_euromir/loss_no_hsde.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# The loss function used in the main loop
###

def create_workspace(m, n, zero):
def create_workspace(m, n, zero, soc=()):
workspace = {}

# preallocate some variables
Expand All @@ -36,7 +36,7 @@ def create_workspace(m, n, zero):
return workspace

# variable is xy
def loss_gradient(xy, m, n, zero, matrix, b, c, workspace):
def loss_gradient(xy, m, n, zero, matrix, b, c, workspace, soc=()):
"""Function for LBFGS loop, used in line search as well."""

x = xy[:n]
Expand Down Expand Up @@ -81,7 +81,7 @@ def loss_gradient(xy, m, n, zero, matrix, b, c, workspace):

return loss, workspace['gradient']

def hessian(xy, m, n, zero, matrix, b, c, workspace, regularizer = 0.):
def hessian(xy, m, n, zero, matrix, b, c, workspace, soc=(), regularizer = 0.):
"""Hessian to use inside LBFGS loop."""

x = xy[:n]
Expand Down Expand Up @@ -129,7 +129,7 @@ def _matvec(dxdy):
matvec=_matvec
)

def residual(xy, m, n, zero, matrix, b, c):
def residual(xy, m, n, zero, matrix, b, c, soc=()):
"""Residual function to use L-M approach instead."""

x = xy[:n]
Expand Down Expand Up @@ -160,7 +160,7 @@ def residual(xy, m, n, zero, matrix, b, c):

return res

def Dresidual(xy, m, n, zero, matrix, b, c):
def Dresidual(xy, m, n, zero, matrix, b, c, soc=()):
"""Linear operator to matrix multiply the residual derivative."""

x = xy[:n]
Expand Down
2 changes: 1 addition & 1 deletion project_euromir/refinement.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import scipy as sp


def refine(z, matrix, b, c, zero, nonneg, max_iters=None):
def refine(z, matrix, b, c, zero, nonneg, soc=(), max_iters=None):
"""All Python for now, will call all the rest.
Equilibration is not done here, data must be already transformed.
Expand Down
16 changes: 8 additions & 8 deletions project_euromir/solver_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

QR_PRESOLVE = True

def solve(matrix, b, c, zero, nonneg,
def solve(matrix, b, c, zero, nonneg, soc=(),
# xy = None, # need to import logic for equilibration
):
"Main function."
Expand All @@ -65,7 +65,7 @@ def solve(matrix, b, c, zero, nonneg,
d, e, sigma, rho, matrix_transf, b_transf, c_transf = \
equilibrate.hsde_ruiz_equilibration(
matrix, b, c, dimensions={
'zero': zero, 'nonneg': nonneg, 'second_order': ()},
'zero': zero, 'nonneg': nonneg, 'second_order': soc},
max_iters=25)

if QR_PRESOLVE:
Expand All @@ -82,18 +82,18 @@ def solve(matrix, b, c, zero, nonneg,
def _local_loss(xy):
return loss_gradient(
xy, m=m, n=n, zero=zero, matrix=matrix_transf, b=b_transf,
c=c_transf, workspace=workspace)[0]
c=c_transf, workspace=workspace, soc=soc)[0]

def _local_grad(xy):
# very important, need to copy the output, to redesign
return np.copy(loss_gradient(
xy, m=m, n=n, zero=zero, matrix=matrix_transf, b=b_transf,
c=c_transf, workspace=workspace)[1])
c=c_transf, workspace=workspace, soc=soc)[1])

def _local_hessian(xy):
return hessian(
xy, m=m, n=n, zero=zero, matrix=matrix_transf, b=b_transf,
c=c_transf, workspace=workspace)
c=c_transf, workspace=workspace, soc=soc)

def _local_hessian_x_nogap(x):
return hessian_x_nogap(
Expand All @@ -106,12 +106,12 @@ def _local_hessian_y_nogap(x):
def _local_residual(xy):
return residual(
xy, m=m, n=n, zero=zero, matrix=matrix_transf, b=b_transf,
c=c_transf)
c=c_transf, soc=soc)

def _local_derivative_residual(xy):
return Dresidual(
xy, m=m, n=n, zero=zero, matrix=matrix_transf, b=b_transf,
c=c_transf)
c=c_transf, soc=soc)

xy = np.zeros(n+m)
loss_xy = _local_loss(xy)
Expand Down Expand Up @@ -254,7 +254,7 @@ def _local_derivative_residual(xy):
for _ in range(3):
u, v = refine(
z=u-v, matrix=matrix_transf, b=b_transf, c=c_transf, zero=zero,
nonneg=nonneg)
nonneg=nonneg, soc=soc)

if u[-1] < 1e-8:
raise FloatingPointError(
Expand Down

0 comments on commit 6a8e15b

Please sign in to comment.