Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nonlinear expressions #26

Closed
hfossli opened this issue Nov 26, 2014 · 2 comments
Closed

Nonlinear expressions #26

hfossli opened this issue Nov 26, 2014 · 2 comments
Labels

Comments

@hfossli
Copy link
Collaborator

hfossli commented Nov 26, 2014

It seems like nonlinear expressions is handled automagically. I don't know if this is by design since the java version throws a "nonlinear expression exception".

// This is a nonlinear expression and should not be supported
BOOST_AUTO_TEST_CASE(variable_is_denominator)
{
    variable x(0), y(0);
    simplex_solver solver;

    BOOST_CHECK_EQUAL(x.value(), 0);
    BOOST_CHECK_EQUAL(y.value(), 0);

    solver.add_constraint(y == 10);

    BOOST_CHECK_EQUAL(x.value(), 0);
    BOOST_CHECK_EQUAL(y.value(), 10);

    solver.add_constraint(x == 5 / y);

    BOOST_CHECK_EQUAL(x.value(), 0.5);
    BOOST_CHECK_EQUAL(y.value(), 10);
}

// This is a linear expression and should be supported
BOOST_AUTO_TEST_CASE(variable_is_numerator)
{
    variable x(0), y(0);
    simplex_solver solver;

    BOOST_CHECK_EQUAL(x.value(), 0);
    BOOST_CHECK_EQUAL(y.value(), 0);

    solver.add_constraint(y == 10);

    BOOST_CHECK_EQUAL(x.value(), 0);
    BOOST_CHECK_EQUAL(y.value(), 10);

    solver.add_constraint(x == y / 5);

    BOOST_CHECK_EQUAL(x.value(), 2);
    BOOST_CHECK_EQUAL(y.value(), 10);
}

Result

Running 41 test cases...
/unit_tests.cpp:777: error in "variable_is_denominator": check x.value() == 0.5 failed [2 != 0.5]

*** 1 failure detected in test suite "rhea"

This is the Rhea implementation

linear_expression& linear_expression::operator/=(const linear_expression& x)
{
    if (is_constant())
        return *this = x / constant();

    if (!x.is_constant())
        throw nonlinear_expression();

    return operator/=(x.constant());
}

This is the java implementation

public final Expression divide(double x)
           throws NonlinearExpression
   {
       if (Util.approx(x, 0.0))
       {
           throw new NonlinearExpression();
       }
       return times(1.0 / x);
   }
@Nocte-
Copy link
Owner

Nocte- commented Nov 27, 2014

Nope, that's definitely not supposed to happen. Good catch.

@Nocte- Nocte- added the bug label Nov 27, 2014
@Nocte- Nocte- closed this as completed in 10645ce Nov 27, 2014
@hfossli
Copy link
Collaborator Author

hfossli commented Nov 27, 2014

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants