示例#1
0
import os.path
from pyomo.common.fileutils import this_file_dir
import pyutilib.th as unittest
from pyutilib.misc import import_file
import pyomo.environ as pyo

from pyomo.contrib.pynumero.dependencies import (numpy as np, numpy_available,
                                                 scipy_sparse as spa,
                                                 scipy_available)
if not (numpy_available and scipy_available):
    raise unittest.SkipTest(
        "Pynumero needs scipy and numpy to run CyIpopt tests")

from pyomo.contrib.pynumero.asl import AmplInterface
if not AmplInterface.available():
    raise unittest.SkipTest(
        "Pynumero needs the ASL extension to run CyIpopt tests")

import pyomo.contrib.pynumero.algorithms.solvers.cyipopt_solver as cyipopt_solver
if not cyipopt_solver.ipopt_available:
    raise unittest.SkipTest(
        "PyNumero needs CyIpopt installed to run CyIpopt tests")
import cyipopt as cyipopt_core

example_dir = os.path.join(this_file_dir(), '..', 'examples')


class TestPyomoCyIpoptSolver(unittest.TestCase):
    def test_status_maps(self):
        self.assertEqual(len(cyipopt_core.STATUS_MESSAGES),
                         len(cyipopt_solver._cyipopt_status_enum))
示例#2
0
#  ___________________________________________________________________________
#
#  Pyomo: Python Optimization Modeling Objects
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________
import sys
import pyutilib.th as unittest
try:
    import numpy as np
    import scipy
except ImportError:
    raise unittest.SkipTest("Pynumero needs scipy and numpy to run NLP tests")

from pyomo.contrib.pynumero.sparse import BlockVector
import pyomo.contrib.pynumero as pn


class TestSparseIntrinsics(unittest.TestCase):

    def setUp(self):
        self.v1 = np.array([1.1, 2.2, 3.3])
        self.v2 = np.array([4.4, 5.5, 6.6, 7.7])
        self.v3 = np.array([1.1, 2.2, 3.3])*2
        self.v4 = np.array([4.4, 5.5, 6.6, 7.7])*2
        self.bv = BlockVector([self.v1, self.v2])
        self.bv2 = BlockVector([self.v3, self.v4])
示例#3
0
#  ___________________________________________________________________________
#
#  Pyomo: Python Optimization Modeling Objects
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________
import pyutilib.th as unittest
try:
    import numpy as np
    from scipy.sparse import coo_matrix, tril
except ImportError:
    raise unittest.SkipTest("Pynumero needs scipy and numpy to run linear solver tests")

try:
    from pyomo.contrib.pynumero.linalg.mumps_interface import MumpsCentralizedAssembledLinearSolver
except ImportError:
    raise unittest.SkipTest("Pynumero needs pymumps to run linear solver tests")

from pyomo.contrib.pynumero.sparse import BlockMatrix, BlockVector


class TestMumpsLinearSolver(unittest.TestCase):
    def test_mumps_linear_solver(self):
        A = np.array([[ 1,  7,  3],
                      [ 7,  4, -5],
                      [ 3, -5,  6]], dtype=np.double)
        A = coo_matrix(A)
        A_lower = tril(A)
示例#4
0
#  Pyomo: Python Optimization Modeling Objects
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________
import pyutilib.th as unittest
import pyomo.environ as aml
import os

try:
    import scipy.sparse as spa
    import numpy as np
except ImportError:
    raise unittest.SkipTest("Pynumero needs scipy and numpy to run NLP tests")

from pyomo.contrib.pynumero.extensions.asl import AmplInterface
if not AmplInterface.available():
    raise unittest.SkipTest(
        "Pynumero needs the ASL extension to run NLP tests")

from pyomo.contrib.pynumero.interfaces.nlp import PyomoNLP
from pyomo.contrib.pynumero.interfaces.nlp_transformations import AdmmNLP


def create_basic_dense_qp(G, A, b, c):

    nx = G.shape[0]
    nl = A.shape[0]
示例#5
0
#  Pyomo: Python Optimization Modeling Objects
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________

import pyutilib.th as unittest
import pyomo.environ as pyo

from pyomo.contrib.pynumero.dependencies import (
    numpy as np, numpy_available, scipy_sparse as spa, scipy_available
)
if not (numpy_available and scipy_available):
    raise unittest.SkipTest("Pynumero needs scipy and numpy to run NLP tests")

from pyomo.contrib.pynumero.extensions.asl import AmplInterface
if not AmplInterface.available():
    raise unittest.SkipTest(
        "Pynumero needs the ASL extension to run CyIpoptSolver tests")

try:
    import ipopt
except ImportError:
    raise unittest.SkipTest("Pynumero needs cyipopt to run CyIpoptSolver tests")

from pyomo.contrib.pynumero.algorithms.solvers.pyomo_ext_cyipopt import ExternalInputOutputModel, PyomoExternalCyIpoptProblem
from pyomo.contrib.pynumero.algorithms.solvers.cyipopt_solver import CyIpoptSolver

class PressureDropModel(ExternalInputOutputModel):
示例#6
0
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________

import pyutilib.th as unittest
import pyomo.environ as pyo
from pyomo.common.dependencies import attempt_import

np, numpy_available = attempt_import('numpy',
                                     'Interior point requires numpy',
                                     minimum_version='1.13.0')
scipy, scipy_available = attempt_import('scipy',
                                        'Interior point requires scipy')
mumps, mumps_available = attempt_import('mumps',
                                        'Interior point requires mumps')
if not (numpy_available and scipy_available):
    raise unittest.SkipTest('Interior point tests require numpy and scipy')

if scipy_available:
    from pyomo.contrib.interior_point.linalg.scipy_interface import ScipyInterface
if mumps_available:
    from pyomo.contrib.interior_point.linalg.mumps_interface import MumpsInterface

import numpy as np

from pyomo.contrib.pynumero.asl import AmplInterface
asl_available = AmplInterface.available()
from pyomo.contrib.interior_point.interior_point import (
    process_init, process_init_duals_lb, process_init_duals_ub,
    _fraction_to_the_boundary_helper_lb, _fraction_to_the_boundary_helper_ub,
    InteriorPointStatus, InteriorPointSolver)
示例#7
0
 def setUpClass(cls):
     if not solver['glpk','lp']:
         raise unittest.SkipTest(
             "The glpk solver is not available")
示例#8
0
#  ___________________________________________________________________________
#
#  Pyomo: Python Optimization Modeling Objects
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________
import sys
import pyutilib.th as unittest

from pyomo.contrib.pynumero.dependencies import (numpy as np, numpy_available,
                                                 scipy_available)
if not (numpy_available and scipy_available):
    raise unittest.SkipTest(
        "Pynumero needs scipy and numpy to run Sparse intrinsict tests")

from pyomo.contrib.pynumero.sparse import BlockVector
import pyomo.contrib.pynumero as pn


class TestSparseIntrinsics(unittest.TestCase):
    def setUp(self):
        self.v1 = np.array([1.1, 2.2, 3.3])
        self.v2 = np.array([4.4, 5.5, 6.6, 7.7])
        self.v3 = np.array([1.1, 2.2, 3.3]) * 2
        self.v4 = np.array([4.4, 5.5, 6.6, 7.7]) * 2
        self.bv = BlockVector(2)
        self.bv2 = BlockVector(2)
        self.bv.set_blocks([self.v1, self.v2])
        self.bv2.set_blocks([self.v3, self.v4])
示例#9
0
#  ___________________________________________________________________________
#
#  Pyomo: Python Optimization Modeling Objects
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________
import ctypes
import pyutilib.th as unittest
from pyomo.contrib.pynumero.dependencies import numpy as np, numpy_available
if not numpy_available:
    raise unittest.SkipTest('pynumero MA27 tests require numpy')
from pyomo.contrib.pynumero.linalg.ma57 import *


@unittest.skipIf(not MA57Interface.available(), reason='MA57 not available')
class TestMA57Interface(unittest.TestCase):

    def test_get_cntl(self):
        ma57 = MA57Interface()
        self.assertEqual(ma57.get_icntl(1), 6)
        self.assertEqual(ma57.get_icntl(7), 1)

        self.assertAlmostEqual(ma57.get_cntl(1), 1e-2) # Numerical pivot threshold
        self.assertAlmostEqual(ma57.get_cntl(2), 1e-20) # Null pivot threshold

    def test_set_icntl(self):
        ma57 = MA57Interface()
        ma57.set_icntl(5, 4) # Set output printing to max verbosity
示例#10
0
#  ___________________________________________________________________________
#
#  Pyomo: Python Optimization Modeling Objects
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________
import pyutilib.th as unittest

import pyomo.contrib.pynumero as pn
if not (pn.sparse.numpy_available and pn.sparse.scipy_available):
    raise unittest.SkipTest(
        "Pynumero needs scipy and numpy to run BlockVector tests")

from scipy.sparse import coo_matrix, bmat
import numpy as np

try:
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    if comm.Get_size() < 3:
        raise unittest.SkipTest("These tests need at least 3 processors")
except ImportError:
    raise unittest.SkipTest(
        "Pynumero needs mpi4py to run mpi block matrix tests")

try:
    from pyomo.contrib.pynumero.sparse.mpi_block_vector import MPIBlockVector
except ImportError:
示例#11
0
    def setUpClass(cls):
        # test problem 1

        if comm.Get_size() < 3:
            raise unittest.SkipTest("Need at least 3 processors to run tests")

        row = np.array([0, 3, 1, 2, 3, 0])
        col = np.array([0, 0, 1, 2, 3, 3])
        data = np.array([2., 1, 3, 4, 5, 1])
        m = coo_matrix((data, (row, col)), shape=(4, 4))

        rank = comm.Get_rank()
        # create mpi matrix
        rank_ownership = [[0, -1], [-1, 1]]
        bm = MPIBlockMatrix(2, 2, rank_ownership, comm)
        if rank == 0:
            bm.set_block(0, 0, m)
        if rank == 1:
            bm.set_block(1, 1, m)

        # create serial matrix image
        serial_bm = BlockMatrix(2, 2)
        serial_bm.set_block(0, 0, m)
        serial_bm.set_block(1, 1, m)
        cls.square_serial_mat = serial_bm

        bm.broadcast_block_sizes()
        cls.square_mpi_mat = bm

        # create mpi matrix
        rank_ownership = [[0, -1], [-1, 1]]
        bm = MPIBlockMatrix(2, 2, rank_ownership, comm)
        if rank == 0:
            bm.set_block(0, 0, m)
        if rank == 1:
            bm.set_block(1, 1, m)

        cls.square_mpi_mat_no_broadcast = bm

        # create matrix with shared blocks
        rank_ownership = [[0, -1], [-1, 1]]
        bm = MPIBlockMatrix(2, 2, rank_ownership, comm)
        if rank == 0:
            bm.set_block(0, 0, m)
        if rank == 1:
            bm.set_block(1, 1, m)
        bm.set_block(0, 1, m)

        bm.broadcast_block_sizes()
        cls.square_mpi_mat2 = bm

        # create serial matrix image
        serial_bm = BlockMatrix(2, 2)
        serial_bm.set_block(0, 0, m)
        serial_bm.set_block(1, 1, m)
        serial_bm.set_block(0, 1, m)
        cls.square_serial_mat2 = serial_bm

        row = np.array([0, 1, 2, 3])
        col = np.array([0, 1, 0, 1])
        data = np.array([1., 1., 1., 1.])
        m2 = coo_matrix((data, (row, col)), shape=(4, 2))

        rank_ownership = [[0, -1, 0], [-1, 1, -1]]
        bm = MPIBlockMatrix(2, 3, rank_ownership, comm)
        if rank == 0:
            bm.set_block(0, 0, m)
            bm.set_block(0, 2, m2)
        if rank == 1:
            bm.set_block(1, 1, m)
        bm.broadcast_block_sizes()
        cls.rectangular_mpi_mat = bm

        bm = BlockMatrix(2, 3)
        bm.set_block(0, 0, m)
        bm.set_block(0, 2, m2)
        bm.set_block(1, 1, m)
        cls.rectangular_serial_mat = bm
示例#12
0
import pyutilib.th as unittest
from pyomo.common.dependencies import attempt_import

np, np_available = attempt_import('numpy', minimum_version='1.13.0')
scipy, scipy_available = attempt_import('scipy.sparse')
mumps, mumps_available = attempt_import('mumps')
if not np_available or not scipy_available:
    raise unittest.SkipTest(
        'numpy and scipy are needed for interior point tests')
import numpy as np
from scipy.sparse import coo_matrix, tril
from pyomo.contrib import interior_point as ip
from pyomo.contrib.pynumero.linalg.ma27 import MA27Interface

ma27_available = MA27Interface.available()


def get_base_matrix(use_tril):
    if use_tril:
        row = [0, 1, 1, 2, 2]
        col = [0, 0, 1, 0, 2]
        data = [1, 7, 4, 3, 6]
    else:
        row = [0, 0, 0, 1, 1, 2, 2]
        col = [0, 1, 2, 0, 1, 0, 2]
        data = [1, 7, 3, 7, 4, 3, 6]
    mat = coo_matrix((data, (row, col)), shape=(3, 3), dtype=np.double)
    return mat


def get_base_matrix_wrong_order(use_tril):
示例#13
0
    def test_linear_scaling(self):
        model = pe.ConcreteModel()
        model.x = pe.Var([1, 2, 3], bounds=(-10, 10), initialize=5.0)
        model.z = pe.Var(bounds=(10, 20))
        model.obj = pe.Objective(expr=model.z + model.x[1])

        # test scaling of duals as well
        model.dual = pe.Suffix(direction=pe.Suffix.IMPORT)
        model.rc = pe.Suffix(direction=pe.Suffix.IMPORT)

        def con_rule(m, i):
            if i == 1:
                return m.x[1] + 2 * m.x[2] + 1 * m.x[3] == 4.0
            if i == 2:
                return m.x[1] + 2 * m.x[2] + 2 * m.x[3] == 5.0
            if i == 3:
                return m.x[1] + 3.0 * m.x[2] + 1 * m.x[3] == 5.0

        model.con = pe.Constraint([1, 2, 3], rule=con_rule)
        model.zcon = pe.Constraint(expr=model.z >= model.x[2])

        x_scale = 0.5
        obj_scale = 2.0
        z_scale = -10.0
        con_scale1 = 0.5
        con_scale2 = 2.0
        con_scale3 = -5.0
        zcon_scale = -3.0

        unscaled_model = model.clone()
        unscaled_model.scaling_factor = pe.Suffix(direction=pe.Suffix.EXPORT)
        unscaled_model.scaling_factor[unscaled_model.obj] = obj_scale
        unscaled_model.scaling_factor[unscaled_model.x] = x_scale
        unscaled_model.scaling_factor[unscaled_model.z] = z_scale
        unscaled_model.scaling_factor[unscaled_model.con[1]] = con_scale1
        unscaled_model.scaling_factor[unscaled_model.con[2]] = con_scale2
        unscaled_model.scaling_factor[unscaled_model.con[3]] = con_scale3
        unscaled_model.scaling_factor[unscaled_model.zcon] = zcon_scale

        scaled_model = pe.TransformationFactory(
            'core.scale_model').create_using(unscaled_model)

        # print('*** unscaled ***')
        # unscaled_model.pprint()
        # print('*** scaled ***')
        # scaled_model.pprint()

        glpk_solver = pe.SolverFactory('glpk')
        if isinstance(glpk_solver, UnknownSolver) or \
           (not glpk_solver.available()):
            raise unittest.SkipTest("glpk solver not available")

        glpk_solver.solve(unscaled_model)
        glpk_solver.solve(scaled_model)

        # check vars
        self.assertAlmostEqual(pe.value(unscaled_model.x[1]),
                               pe.value(scaled_model.scaled_x[1]) / x_scale, 4)
        self.assertAlmostEqual(pe.value(unscaled_model.x[2]),
                               pe.value(scaled_model.scaled_x[2]) / x_scale, 4)
        self.assertAlmostEqual(pe.value(unscaled_model.x[3]),
                               pe.value(scaled_model.scaled_x[3]) / x_scale, 4)
        self.assertAlmostEqual(pe.value(unscaled_model.z),
                               pe.value(scaled_model.scaled_z) / z_scale, 4)
        # check var lb
        self.assertAlmostEqual(pe.value(unscaled_model.x[1].lb),
                               pe.value(scaled_model.scaled_x[1].lb) / x_scale,
                               4)
        self.assertAlmostEqual(pe.value(unscaled_model.x[2].lb),
                               pe.value(scaled_model.scaled_x[2].lb) / x_scale,
                               4)
        self.assertAlmostEqual(pe.value(unscaled_model.x[3].lb),
                               pe.value(scaled_model.scaled_x[3].lb) / x_scale,
                               4)
        # note: z_scale is negative, therefore, the inequality directions swap
        self.assertAlmostEqual(pe.value(unscaled_model.z.lb),
                               pe.value(scaled_model.scaled_z.ub) / z_scale, 4)
        # check var ub
        self.assertAlmostEqual(pe.value(unscaled_model.x[1].ub),
                               pe.value(scaled_model.scaled_x[1].ub) / x_scale,
                               4)
        self.assertAlmostEqual(pe.value(unscaled_model.x[2].ub),
                               pe.value(scaled_model.scaled_x[2].ub) / x_scale,
                               4)
        self.assertAlmostEqual(pe.value(unscaled_model.x[3].ub),
                               pe.value(scaled_model.scaled_x[3].ub) / x_scale,
                               4)
        # note: z_scale is negative, therefore, the inequality directions swap
        self.assertAlmostEqual(pe.value(unscaled_model.z.ub),
                               pe.value(scaled_model.scaled_z.lb) / z_scale, 4)
        # check var multipliers (rc)
        self.assertAlmostEqual(
            pe.value(unscaled_model.rc[unscaled_model.x[1]]),
            pe.value(scaled_model.rc[scaled_model.scaled_x[1]]) * x_scale /
            obj_scale, 4)
        self.assertAlmostEqual(
            pe.value(unscaled_model.rc[unscaled_model.x[2]]),
            pe.value(scaled_model.rc[scaled_model.scaled_x[2]]) * x_scale /
            obj_scale, 4)
        self.assertAlmostEqual(
            pe.value(unscaled_model.rc[unscaled_model.x[3]]),
            pe.value(scaled_model.rc[scaled_model.scaled_x[3]]) * x_scale /
            obj_scale, 4)
        self.assertAlmostEqual(
            pe.value(unscaled_model.rc[unscaled_model.z]),
            pe.value(scaled_model.rc[scaled_model.scaled_z]) * z_scale /
            obj_scale, 4)
        # check constraint multipliers
        self.assertAlmostEqual(
            pe.value(unscaled_model.dual[unscaled_model.con[1]]),
            pe.value(scaled_model.dual[scaled_model.scaled_con[1]]) *
            con_scale1 / obj_scale, 4)
        self.assertAlmostEqual(
            pe.value(unscaled_model.dual[unscaled_model.con[2]]),
            pe.value(scaled_model.dual[scaled_model.scaled_con[2]]) *
            con_scale2 / obj_scale, 4)
        self.assertAlmostEqual(
            pe.value(unscaled_model.dual[unscaled_model.con[3]]),
            pe.value(scaled_model.dual[scaled_model.scaled_con[3]]) *
            con_scale3 / obj_scale, 4)

        # put the solution from the scaled back into the original
        pe.TransformationFactory('core.scale_model').propagate_solution(
            scaled_model, model)

        # compare var values and rc with the unscaled soln
        for vm in model.component_objects(ctype=pe.Var, descend_into=True):
            cuid = pe.ComponentUID(vm)
            vum = cuid.find_component_on(unscaled_model)
            self.assertEqual((vm in model.rc), (vum in unscaled_model.rc))
            if vm in model.rc:
                self.assertAlmostEqual(pe.value(model.rc[vm]),
                                       pe.value(unscaled_model.rc[vum]), 4)
            for k in vm:
                vmk = vm[k]
                vumk = vum[k]
                self.assertAlmostEqual(pe.value(vmk), pe.value(vumk), 4)
                self.assertEqual((vmk in model.rc),
                                 (vumk in unscaled_model.rc))
                if vmk in model.rc:
                    self.assertAlmostEqual(pe.value(model.rc[vmk]),
                                           pe.value(unscaled_model.rc[vumk]),
                                           4)

        # compare constraint duals and value
        for model_con in model.component_objects(ctype=pe.Constraint,
                                                 descend_into=True):
            cuid = pe.ComponentUID(model_con)
            unscaled_model_con = cuid.find_component_on(unscaled_model)
            self.assertEqual((model_con in model.rc),
                             (unscaled_model_con in unscaled_model.rc))
            if model_con in model.dual:
                self.assertAlmostEqual(
                    pe.value(model.dual[model_con]),
                    pe.value(unscaled_model.dual[unscaled_model_con]), 4)
            for k in model_con:
                mk = model_con[k]
                umk = unscaled_model_con[k]
                self.assertEqual((mk in model.dual),
                                 (umk in unscaled_model.dual))
                if mk in model.dual:
                    self.assertAlmostEqual(pe.value(model.dual[mk]),
                                           pe.value(unscaled_model.dual[umk]),
                                           4)
示例#14
0
import pyutilib.th as unittest
import pyomo.environ as pe
from pyomo.core.base import ConcreteModel, Var, Constraint, Objective
from pyomo.common.dependencies import attempt_import

np, numpy_available = attempt_import('numpy',
                                     'Interior point requires numpy',
                                     minimum_version='1.13.0')
scipy, scipy_available = attempt_import('scipy',
                                        'Interior point requires scipy')
mumps, mumps_available = attempt_import('mumps',
                                        'Interior point requires mumps')
if not (numpy_available and scipy_available):
    raise unittest.SkipTest('Interior point tests require numpy and scipy')

from pyomo.contrib.pynumero.asl import AmplInterface
asl_available = AmplInterface.available()
if not asl_available:
    raise unittest.SkipTest('Regularization tests require ASL')
import pyomo.contrib.interior_point as ip
from pyomo.contrib.pynumero.linalg.ma27 import MA27Interface
ma27_available = MA27Interface.available()


def make_model():
    m = ConcreteModel()
    m.x = Var([1, 2, 3], initialize=0)
    m.f = Var([1, 2, 3], initialize=0)
    m.F = Var(initialize=0)
    m.f[1].fix(1)
    m.f[2].fix(2)
示例#15
0
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________
import sys
import os
import pyutilib.th as unittest

try:
    from scipy.sparse.csr import csr_matrix
    from scipy.sparse.csc import csc_matrix
    from scipy.sparse.coo import coo_matrix
    import numpy as np
except ImportError:
    raise unittest.SkipTest(
        "Pynumero needs scipy and numpy to run COO matrix tests")

from pyomo.contrib.pynumero.sparse.base import SparseBase
from pyomo.contrib.pynumero.sparse.coo import (COOMatrix, COOSymMatrix,
                                               IdentityMatrix, DiagonalMatrix,
                                               EmptyMatrix)

from pyomo.contrib.pynumero.sparse.csr import CSRMatrix, CSRSymMatrix
from pyomo.contrib.pynumero.sparse.csc import CSCMatrix, CSCSymMatrix
from pyomo.contrib.pynumero.sparse.utils import (_is_symmetric_numerically,
                                                 _convert_matrix_to_symmetric,
                                                 is_symmetric_dense)

from pyomo.contrib.pynumero.extensions.sparseutils import SparseLibInterface
sparselib = SparseLibInterface()
示例#16
0
#
#  Pyomo: Python Optimization Modeling Objects
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________

from __future__ import division
import pyutilib.th as unittest

from pyomo.contrib.pynumero.dependencies import (numpy as np, numpy_available,
                                                 scipy_available)
if not (numpy_available and scipy_available):
    raise unittest.SkipTest(
        "Pynumero needs scipy and numpy to run BlockVector tests")

from pyomo.contrib.pynumero.sparse.block_vector import (
    BlockVector, NotFullyDefinedBlockVectorError)


class TestBlockVector(unittest.TestCase):
    def test_constructor(self):

        v = BlockVector(2)
        self.assertEqual(v.nblocks, 2)
        self.assertEqual(v.bshape, (2, ))
        with self.assertRaises(NotFullyDefinedBlockVectorError):
            v_size = v.size

        v.set_block(0, np.ones(2))
示例#17
0
from pyomo.common.dependencies import attempt_import

np, numpy_available = attempt_import('numpy',
                                     'inverse_reduced_hessian numpy',
                                     minimum_version='1.13.0')
scipy, scipy_available = attempt_import(
    'scipy', 'inverse_reduced_hessian requires scipy')

if numpy_available:
    from pyomo.contrib.pynumero.asl import AmplInterface
    asl_available = AmplInterface.available()
else:
    asl_available = False

if not (numpy_available and scipy_available and asl_available):
    raise unittest.SkipTest(
        'inverse_reduced_hessian tests require numpy, scipy, and asl')
from pyomo.common.dependencies import (pandas as pd, pandas_available)
import pyomo.environ as pe
ipopt_solver = pe.SolverFactory('ipopt')
if not ipopt_solver.available(exception_flag=False):
    raise unittest.SkipTest('ipopt is not available')

numdiff_available = True
try:
    import numdifftools as nd
except:
    numdiff_available = False

from pyomo.contrib.interior_point.inverse_reduced_hessian import inv_reduced_hessian_barrier

示例#18
0
from pyomo.common.dependencies import numpy_available, scipy_available
if not numpy_available or not scipy_available:
    import pyutilib.th as unittest
    raise unittest.SkipTest('numpy and scipy required for interior point')
from .interface import BaseInteriorPointInterface, InteriorPointInterface
from .interior_point import InteriorPointSolver, InteriorPointStatus
from pyomo.contrib.interior_point import linalg
from .inverse_reduced_hessian import inv_reduced_hessian_barrier