Пример #1
0
def test_operations_poly(model):
    m, x, y, z = model
    expr = x * x * x + 2 * y * y
    assert isinstance(expr, Expr)
    assert expr[x] == 0.0
    assert expr[y] == 0.0
    assert expr[CONST] == 0.0
    assert expr[Term(x, x, x)] == 1.0
    assert expr[Term(y, y)] == 2.0
    assert expr.terms == (x**3 + 2 * y**2).terms
Пример #2
0
    def _import_objective(self):
        picosSense, picosObjective = self.ext.objective

        # Retrieve objective sense.
        if picosSense in ("find", "min"):
            minimize = True
            scipSense = "minimize"
        elif picosSense == "max":
            minimize = False
            scipSense = "maximize"
        else:
            raise NotImplementedError(
                "Objective sense '{0}' not supported by SCIP.".format(
                    picosSense))

        # Import objective function.
        if isinstance(picosObjective, AffinExp):
            scipObjective = self._scalar_affinexp_pic2scip(picosObjective)
        elif isinstance(picosObjective, QuadExp):
            scipObjective = self._convert_quadratic_objective(
                picosObjective, minimize)
        else:
            raise NotImplementedError(
                "Objective of type '{0}' not supported by SCIP.".format(
                    type(picosObjective)))

        # HACK: Remove a constant term from the objective as this is not allowed
        #       by SCIP under Python 2. (Not sure if the term is stored or
        #       dropped under Python 3, but no exception is thrown there.)
        from pyscipopt.scip import Term
        constantTerm = Term()
        if constantTerm in scipObjective.terms:
            scipObjective.terms.pop(constantTerm)

        self.int.setObjective(scipObjective, scipSense)
Пример #3
0
def test_power_for_quadratic(model):
    m, x, y, z = model
    expr = x**2 + x + 1
    assert isinstance(expr, Expr)
    assert expr[Term(x, x)] == 1.0
    assert expr[x] == 1.0
    assert expr[CONST] == 1.0
    assert len(expr.terms) == 3

    assert (x**2).terms == (x * x).terms
    assert ((x + 3)**2).terms == (x**2 + 6 * x + 9).terms
Пример #4
0
def test_operations_quadratic(model):
    m, x, y, z = model
    expr = x * x
    assert isinstance(expr, Expr)
    assert expr[x] == 0.0
    assert expr[y] == 0.0
    assert expr[CONST] == 0.0
    assert expr[Term(x, x)] == 1.0

    expr = x * y
    assert isinstance(expr, Expr)
    assert expr[x] == 0.0
    assert expr[y] == 0.0
    assert expr[CONST] == 0.0
    assert expr[Term(x, y)] == 1.0

    expr = (x - 1) * (y + 1)
    assert isinstance(expr, Expr)
    assert expr[x] == 1.0
    assert expr[y] == -1.0
    assert expr[CONST] == -1.0
    assert expr[Term(x, y)] == 1.0
Пример #5
0
import pytest

from pyscipopt import Model, sqrt, log, exp
from pyscipopt.scip import Expr, GenExpr, ExprCons, Term, quicksum


@pytest.fixture(scope="module")
def model():
    m = Model()
    x = m.addVar("x")
    y = m.addVar("y")
    z = m.addVar("z")
    return m, x, y, z


CONST = Term()


def test_upgrade(model):
    m, x, y, z = model
    expr = x + y
    assert isinstance(expr, Expr)
    expr += exp(z)
    assert isinstance(expr, GenExpr)

    expr = x + y
    assert isinstance(expr, Expr)
    expr -= exp(z)
    assert isinstance(expr, GenExpr)

    expr = x + y