Skip to content

BinaryOp sometimes returns nan, -inf, and inf for min() and max() #630

Description

@fastbodin

There are many possible instances of this.
Below is an example where the Multiplies node has predecessors with min and max values of [0,1] and [-inf, inf].
This results in min and max values of nan.

#include <catch2/catch_test_macros.hpp>

#include "dwave-optimization/graph.hpp"
#include "dwave-optimization/nodes/binaryop.hpp"
#include "dwave-optimization/nodes/numbers.hpp"
#include "dwave-optimization/nodes/unaryop.hpp"

namespace dwave::optimization {

TEST_CASE("BinaryOpNode - Example Test") {
    auto graph = Graph();
    constexpr double inf = std::numeric_limits<double>::infinity();

    GIVEN("A very contrived example") {
        auto* b_ptr = graph.emplace_node<BinaryNode>(1);
        CHECK(b_ptr->min() == 0);
        CHECK(b_ptr->max() == 1);

        auto* x_ptr = graph.emplace_node<IntegerNode>(1, -1000000, 1000000);
        CHECK(x_ptr->min() == -1000000);
        CHECK(x_ptr->max() == 1000000);

        auto* exp_x_ptr = graph.emplace_node<ExpNode>(x_ptr);
        CHECK(exp_x_ptr->min() == 0);
        CHECK(exp_x_ptr->max() == inf);

        auto* neg_exp_x_ptr = graph.emplace_node<NegativeNode>(exp_x_ptr);
        CHECK(neg_exp_x_ptr->min() == -inf);
        CHECK(neg_exp_x_ptr->max() == 0);

        auto* inf_ptr = graph.emplace_node<AddNode>(exp_x_ptr, neg_exp_x_ptr);
        CHECK(inf_ptr->min() == -inf);
        CHECK(inf_ptr->max() == inf);

        auto* multiplies_ptr = graph.emplace_node<MultiplyNode>(b_ptr, inf_ptr);
        CHECK(std::isnan(multiplies_ptr->min()));
        CHECK(std::isnan(multiplies_ptr->max()));

        THEN("Min and max of `multiplies_ptr` should be ordered but are not") {
            CHECK(multiplies_ptr->min() <= multiplies_ptr->max()); // <- fails
        }
    }
}

}  // namespace dwave::optimization

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions