Beispiel #1
0
 def test_iadd_number(self):
     qm = QuadraticModel()
     original = qm
     qm += 1
     self.assertIs(qm, original)
     self.assertEqual(qm.offset, 1)
     self.assertEqual(qm.num_variables, 0)
Beispiel #2
0
 def test_radd_number(self):
     qm = QuadraticModel()
     new = 1 + qm
     self.assertIsNot(qm, new)
     self.assertEqual(qm.offset, 0)
     self.assertEqual(new.offset, 1)
     self.assertEqual(qm.num_variables, 0)
     self.assertEqual(new.num_variables, 0)
Beispiel #3
0
    def test(self, _, BQM):
        for vartype in ['SPIN', 'BINARY']:
            with self.subTest(vartype):
                bqm = BQM({'a': 1}, {'ab': 2, 'bc': 3}, 4, vartype)
                qm = QuadraticModel.from_bqm(bqm)

                self.assertEqual(bqm.linear, qm.linear)
                self.assertEqual(bqm.quadratic, qm.quadratic)
                self.assertEqual(bqm.offset, qm.offset)

                for v in bqm.variables:
                    self.assertIs(qm.vartype(v), bqm.vartype)
Beispiel #4
0
def build_knapsack_cqm(costs, weights, max_weight):
    """Construct a CQM for the knapsack problem.

    Args:
        costs (array-like):
            Array of costs for the items.
        weights (array-like):
            Array of weights for the items.
        max_weight (int):
            Maximum allowable weight for the knapsack.

    Returns:
        Constrained quadratic model instance that represents the knapsack problem.
    """
    num_items = len(costs)
    print("\nBuilding a CQM for {} items.".format(str(num_items)))

    cqm = ConstrainedQuadraticModel()
    obj = BinaryQuadraticModel(vartype='BINARY')
    constraint = QuadraticModel()

    for i in range(num_items):
        # Objective is to maximize the total costs
        obj.add_variable(i)
        obj.set_linear(i, -costs[i])
        # Constraint is to keep the sum of items' weights under or equal capacity
        constraint.add_variable('BINARY', i)
        constraint.set_linear(i, weights[i])

    cqm.set_objective(obj)
    cqm.add_constraint(constraint, sense="<=", rhs=max_weight, label='capacity')

    return cqm
Beispiel #5
0
    def test_set_upper_bound(self, dtype):
        qm = QuadraticModel(dtype=dtype)
        qm.add_variable('INTEGER', 'i')
        qm.add_variable('SPIN', 's')
        qm.add_variable('BINARY', 'x')

        # cannot set less than max_integer
        with self.assertRaises(ValueError):
            qm.set_upper_bound('i', np.finfo(dtype).max)

        # cannot set for non-integer
        with self.assertRaises(ValueError):
            qm.set_upper_bound('s', -2)
        with self.assertRaises(ValueError):
            qm.set_upper_bound('x', 10)

        # cannot set one less than the current lower bound
        with self.assertRaises(ValueError):
            qm.set_upper_bound('i', qm.lower_bound('i') - 1)

        qm.set_upper_bound('i', 10)
        self.assertEqual(qm.upper_bound('i'), 10)

        qm.set_upper_bound('i', 11.3)
        self.assertEqual(qm.upper_bound('i'), 11)