Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeoli committed May 31, 2020
1 parent 0ba811b commit 9efedae
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 36 deletions.
2 changes: 1 addition & 1 deletion notebooks/ml/SupportVectorMachines.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@
"\n",
"<img src=\"./img/svr.png\" width=600/>\n",
"\n",
"Referring to this figure, the region bound by $y'_{i}\\pm\\epsilon \\ \\forall_{i}$ is called an $\\epsilon$-insensitive tube. The other modification to the penalty function is that output variables which are outside the tube are given one of two slack variable penalties depending on whether they lie above $(\\xi^{+})$ or below $(\\xi^{-})$ the tube (where $\\xi^{+}>0, \\xi^{-}>0 \\ \\forall_{i}$):\n",
"Referring to this figure, the region bound by $y'_{i}\\pm\\epsilon \\ \\forall_{i}$ is called an $\\epsilon$-insensitive tube. The other modification to the penalty function is that output variables which are outside the tube are given one of two slack variable penalties depending on whether they lie above $(\\xi^{+})$ or below $(\\xi^{-})$ the tube (where $\\xi^{+} \\geq 0, \\xi^{-} \\geq 0 \\ \\forall_{i}$):\n",
"\n",
"$$\n",
"\\begin{equation}\n",
Expand Down
11 changes: 4 additions & 7 deletions optiml/ml/svm/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .smo import SMO, SMOClassifier, SMORegression
from ...opti import Optimizer
from ...opti import Quadratic
from ...opti.qp import LagrangianEqualityConstrainedQuadratic, LagrangianConstrainedQuadratic
from ...opti.qp import LagrangianConstrainedQuadratic
from ...opti.qp.bcqp import BoxConstrainedQuadraticOptimizer, LagrangianBoxConstrainedQuadratic
from ...opti.unconstrained import ProximalBundle
from ...opti.unconstrained.line_search import LineSearchOptimizer
Expand Down Expand Up @@ -357,12 +357,9 @@ def fit(self, X, y):

if issubclass(self.optimizer, BoxConstrainedQuadraticOptimizer):

self.obj = LagrangianEqualityConstrainedQuadratic(Q, q, A)
self.optimizer = self.optimizer(f=self.obj, ub=np.append(ub, 0), max_iter=self.max_iter,
verbose=self.verbose)
self.optimizer.x = np.zeros(self.obj.ndim)
self.optimizer.minimize()
alphas = self.optimizer.x[:-1]
self.optimizer = self.optimizer(f=self.obj, ub=ub, max_iter=self.max_iter,
verbose=self.verbose).minimize()
alphas = self.optimizer.x

elif issubclass(self.optimizer, Optimizer):

Expand Down
4 changes: 2 additions & 2 deletions optiml/opti/qp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__all__ = ['LagrangianDual', 'LagrangianEqualityConstrainedQuadratic', 'LagrangianConstrainedQuadratic']
__all__ = ['LagrangianDual', 'LagrangianConstrainedQuadratic']

from .lagrangian_dual import LagrangianDual, LagrangianEqualityConstrainedQuadratic, LagrangianConstrainedQuadratic
from .lagrangian_dual import LagrangianDual, LagrangianConstrainedQuadratic
27 changes: 1 addition & 26 deletions optiml/opti/qp/lagrangian_dual.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,6 @@ def callback(self, args=()):
self._callback(self, *args, *self.callback_args)


class LagrangianEqualityConstrainedQuadratic(Quadratic):

def __init__(self, Q, q, A):
"""
Construct the lagrangian relaxation of an equality constrained quadratic function defined as:
1/2 x^T Q x + q^T x : A x = 0
By using Lagrange multipliers and seeking the extremum of the Lagrangian, it may be readily
shown that the solution to the equality constrained problem is given by the linear system:
| Q A^T | | x | = | -q |
| A 0 | | lambda | | 0 |
where lambda is a set of Lagrange multipliers which come out of the solution alongside x.
:param A: equality constraints matrix to be relaxed
"""
A = np.atleast_2d(np.asarray(A, dtype=np.float))
Q = np.vstack((np.hstack((Q, A.T)),
np.hstack((A, np.zeros((A.shape[0], A.shape[0]))))))
q = np.hstack((-q, np.zeros(A.shape[0])))
super().__init__(Q, q)


class LagrangianConstrainedQuadratic(Quadratic):

def __init__(self, quad, A, ub):
Expand All @@ -127,7 +102,7 @@ def __init__(self, quad, A, ub):
# of Q because it is symmetric but could be not positive definite.
# This will be used at each iteration to solve the Lagrangian relaxation.
self.L, self.D, self.P = ldl(self.Q)
self.A = np.atleast_2d(np.asarray(A, dtype=np.float))
self.A = np.asarray(A, dtype=np.float)
if any(u < 0 for u in ub):
raise ValueError('the lower bound must be > 0')
self.ub = np.asarray(ub, dtype=np.float)
Expand Down

0 comments on commit 9efedae

Please sign in to comment.