Skip to content

Commit

Permalink
Merge pull request #93 from chrhansk/hotfix-nonlin-reliability
Browse files Browse the repository at this point in the history
Discard unreliable nonlinearity measures
  • Loading branch information
chrhansk authored May 24, 2024
2 parents f42c4a8 + 60000c0 commit cfafc46
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
project = "pygradflow"
copyright = "2023, Christoph Hansknecht"
author = "Christoph Hansknecht"
release = "0.5.0"
release = "0.5.1"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
5 changes: 1 addition & 4 deletions pygradflow/cons_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
import scipy as sp

from pygradflow.problem import Problem


def sparse_zero(shape):
return sp.sparse.coo_matrix(([], ([], [])), shape)
from pygradflow.util import sparse_zero


class ConstrainedProblem(Problem):
Expand Down
11 changes: 9 additions & 2 deletions pygradflow/iterate.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,16 @@ def total_res(self) -> float:
def obj_nonlin(self, other: "Iterate") -> float:
dx = other.x - self.x
next_obj = self.obj + np.dot(dx, self.obj_grad)
return abs(other.obj - next_obj) / np.dot(dx, dx)
dx_dot = np.dot(dx, dx)
if np.isclose(dx_dot, 0.0):
return 0.0
return abs(other.obj - next_obj) / dx_dot

def cons_nonlin(self, other: "Iterate") -> np.ndarray:
dx = other.x - self.x
next_cons = self.cons + self.cons_jac.dot(dx)
return (other.cons - next_cons) / np.dot(dx, dx)
dx_dot = np.dot(dx, dx)
if np.isclose(dx_dot, 0.0):
problem = self.problem
return np.zeros((problem.num_cons,))
return (other.cons - next_cons) / dx_dot
21 changes: 2 additions & 19 deletions pygradflow/runners/cutest_runner.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import logging
from functools import cached_property

import numpy as np
import pycutest

from pygradflow.solver import Problem, Solver
from pygradflow.problem import Problem

from .instance import Instance
from .runner import Runner

formatter = logging.Formatter("%(asctime)s:%(name)s:%(levelname)s:%(message)s")


def cutest_is_ne_prob(name):
return name.endswith("NE")
Expand Down Expand Up @@ -48,14 +45,9 @@ def cons_jac(self, x):
def lag_hess(self, x, y):
return self.instance.sphess(x, v=None)

@property
def x0(self):
return self.instance.x0

@property
def y0(self):
return np.zeros((self.num_cons,))


class ConstrainedCUTEstProblem(Problem):
def __init__(self, instance):
Expand Down Expand Up @@ -86,11 +78,7 @@ def lag_hess(self, x, y):

@property
def x0(self):
return self.instance.x0

@property
def y0(self):
return self.instance.v0
return self.instance.x0()


# Nonlinear equations: Goal is to minimize the violation
Expand Down Expand Up @@ -162,11 +150,6 @@ def problem(self):
else:
return ConstrainedCUTEstProblem(cutest_problem)

def solve(self, params):
problem = self.problem()
solver = Solver(problem, params)
return solver.solve(problem.x0, problem.y0)


class CUTestRunner(Runner):
def __init__(self):
Expand Down
12 changes: 10 additions & 2 deletions pygradflow/runners/instance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from abc import ABC, abstractmethod

import numpy as np

from pygradflow.solver import Solver


class Instance(ABC):
def __init__(self, name, num_vars, num_cons):
Expand All @@ -11,9 +15,10 @@ def __init__(self, name, num_vars, num_cons):
def size(self):
return self.num_vars + self.num_cons

@abstractmethod
def solve(self, params):
raise NotImplementedError()
problem = self.problem()
solver = Solver(problem, params)
return solver.solve(self.x0(), self.y0())

@abstractmethod
def problem(self):
Expand All @@ -22,3 +27,6 @@ def problem(self):
@abstractmethod
def x0(self):
raise NotImplementedError()

def y0(self):
return np.zeros((self.num_cons,))
18 changes: 7 additions & 11 deletions pygradflow/runners/qplib_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pyqplib

from pygradflow.solver import Problem, Solver
from pygradflow.solver import Problem

from .instance import Instance
from .runner import Runner
Expand Down Expand Up @@ -34,13 +34,11 @@ def cons_jac(self, x):
def lag_hess(self, x, y):
return self.problem.lag_hess(x, y)

@property
def x0(self):
return self.problem.x0
return self.problem.x0()

@property
def y0(self):
return self.problem.y0
return self.problem.y0()


class QPLIBInstance(Instance):
Expand All @@ -53,17 +51,15 @@ def __init__(self, description):
def filename(self):
return self.description.filename

def solve(self, params):
problem = self.problem()
solver = Solver(problem, params)
return solver.solve(problem.x0, problem.y0)

def problem(self):
qproblem = pyqplib.read_problem(self.filename)
return QPLIBProblem(qproblem)

def x0(self):
return self.problem().x0
return self.problem().x0()

def y0(self):
return self.problem().y0()


class QPLIBRunner(Runner):
Expand Down
9 changes: 9 additions & 0 deletions pygradflow/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
from numpy import ndarray


def sparse_zero(shape, format=None):
zero_mat = sp.sparse.coo_matrix(([], ([], [])), shape)

if format not in [None, "coo"]:
return zero_mat.asformat(format)

return zero_mat


def norm_sq(x: ndarray) -> float:
return np.dot(x, x)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pygradflow"
version = "0.5.0"
version = "0.5.1"
description = "PyGradFlow is a simple implementation of the sequential homotopy method to be used to solve general nonlinear programs."
authors = ["Christoph Hansknecht <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit cfafc46

Please sign in to comment.