Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/nsga2
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomangano authored Apr 24, 2024
2 parents 6f7a181 + 69c2a6f commit cec9046
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pyoptsparse/pyCONMIN/pyCONMIN.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# import the compiled module
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
conmin = try_import_compiled_module_from_path("conmin", THIS_DIR)
conmin = try_import_compiled_module_from_path("conmin", THIS_DIR, raise_warning=True)


class CONMIN(Optimizer):
Expand Down
2 changes: 1 addition & 1 deletion pyoptsparse/pyNSGA2/pyNSGA2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

# import the compiled module
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
nsga2 = try_import_compiled_module_from_path("nsga2", THIS_DIR)
nsga2 = try_import_compiled_module_from_path("nsga2", THIS_DIR, raise_warning=True)


class NSGA2(Optimizer):
Expand Down
2 changes: 1 addition & 1 deletion pyoptsparse/pyOpt_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

# Local modules
from .pyOpt_error import Error, pyOptSparseWarning
from .pyOpt_types import Dict1DType
from .pyOpt_utils import INFINITY, _broadcast_to_array, convertToCOO
from .types import Dict1DType


class Constraint:
Expand Down
2 changes: 1 addition & 1 deletion pyoptsparse/pyOpt_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Local modules
from .pyOpt_MPI import MPI
from .pyOpt_optimization import Optimization
from .types import Dict1DType, Dict2DType
from .pyOpt_types import Dict1DType, Dict2DType


class Gradient:
Expand Down
2 changes: 1 addition & 1 deletion pyoptsparse/pyOpt_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .pyOpt_constraint import Constraint
from .pyOpt_error import Error
from .pyOpt_objective import Objective
from .pyOpt_types import Dict1DType, Dict2DType, NumpyType
from .pyOpt_utils import (
ICOL,
IDATA,
Expand All @@ -28,7 +29,6 @@
scaleRows,
)
from .pyOpt_variable import Variable
from .types import Dict1DType, Dict2DType, NumpyType


class Optimization:
Expand Down
File renamed without changes.
11 changes: 8 additions & 3 deletions pyoptsparse/pyOpt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mat = {'csr':[rowp, colind, data], 'shape':[nrow, ncols]} # A csr matrix
mat = {'csc':[colp, rowind, data], 'shape':[nrow, ncols]} # A csc matrix
"""

# Standard Python modules
import importlib
import os
Expand All @@ -24,7 +25,7 @@

# Local modules
from .pyOpt_error import Error
from .types import ArrayType
from .pyOpt_types import ArrayType

# Define index mnemonics
IROW = 0
Expand Down Expand Up @@ -576,7 +577,9 @@ def _broadcast_to_array(name: str, value: ArrayType, n_values: int, allow_none:
return value


def try_import_compiled_module_from_path(module_name: str, path: Optional[str] = None) -> Union[types.ModuleType, str]:
def try_import_compiled_module_from_path(
module_name: str, path: Optional[str] = None, raise_warning: bool = False
) -> Union[types.ModuleType, str]:
"""
Attempt to import a module from a given path.
Expand All @@ -586,6 +589,8 @@ def try_import_compiled_module_from_path(module_name: str, path: Optional[str] =
The name of the module
path : Optional[str]
The path to import from. If None, the default ``sys.path`` is used.
raise_warning : bool
If true, raise an import warning. By default false.
Returns
-------
Expand All @@ -600,7 +605,7 @@ def try_import_compiled_module_from_path(module_name: str, path: Optional[str] =
try:
module = importlib.import_module(module_name)
except ImportError as e:
if path is not None:
if raise_warning:
warnings.warn(
f"{module_name} module could not be imported from {path}.",
stacklevel=2,
Expand Down
2 changes: 1 addition & 1 deletion pyoptsparse/pySLSQP/pySLSQP.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

# import the compiled module
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
slsqp = try_import_compiled_module_from_path("slsqp", THIS_DIR)
slsqp = try_import_compiled_module_from_path("slsqp", THIS_DIR, raise_warning=True)


class SLSQP(Optimizer):
Expand Down
12 changes: 4 additions & 8 deletions pyoptsparse/pySNOPT/pySNOPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def __call__(
self.setOption("Total real workspace", lenrw)

cw = np.empty((lencw, 8), dtype="|S1")
cw[:] = " "
iw = np.zeros(leniw, np.intc)
rw = np.zeros(lenrw, float)
snopt.sninit(iPrint, iSumm, cw, iw, rw)
Expand Down Expand Up @@ -444,11 +445,6 @@ def __call__(
start = np.array(self.getOption("Start"))
ObjAdd = np.array(0.0, float)
ProbNm = np.array(self.optProb.name, "c")
cdummy = -1111111 # this is a magic variable defined in SNOPT for undefined strings
cw[51, :] = cdummy # we set these to cdummy so that a placeholder is used in printout
cw[52, :] = cdummy
cw[53, :] = cdummy
cw[54, :] = cdummy
xs = np.concatenate((xs, np.zeros(ncon, float)))
bl = np.concatenate((blx, blc))
bu = np.concatenate((bux, buc))
Expand Down Expand Up @@ -701,11 +697,11 @@ def _set_snopt_options(self, iPrint: int, iSumm: int, cw: ndarray, iw: ndarray,
if name == "Problem Type":
snopt.snset(value, iPrint, iSumm, inform, cw, iw, rw)
elif name == "Print file":
snopt.snset(name + " " + f"{iPrint}", iPrint, iSumm, inform, cw, iw, rw)
snopt.snset(f"{name} {iPrint}", iPrint, iSumm, inform, cw, iw, rw)
elif name == "Summary file":
snopt.snset(name + " " + f"{iSumm}", iPrint, iSumm, inform, cw, iw, rw)
snopt.snset(f"{name} {iSumm}", iPrint, iSumm, inform, cw, iw, rw)
else:
snopt.snset(name + " " + value, iPrint, iSumm, inform, cw, iw, rw)
snopt.snset(f"{name} {value}", iPrint, iSumm, inform, cw, iw, rw)
elif isinstance(value, float):
snopt.snsetr(name, value, iPrint, iSumm, inform, cw, iw, rw)
elif isinstance(value, int):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_nonexistent_path(self):
if "snopt" in key:
sys.modules.pop(key)
with self.assertWarns(UserWarning):
module = try_import_compiled_module_from_path("snopt", "/a/nonexistent/path")
module = try_import_compiled_module_from_path("snopt", "/a/nonexistent/path", raise_warning=True)
self.assertTrue(isinstance(module, str))

def test_sys_path_unchanged(self):
Expand Down

0 comments on commit cec9046

Please sign in to comment.