def test_binary_to_integer(self): """ Test binary to integer """ op = QuadraticProgram() for i in range(0, 2): op.binary_var(name='x{}'.format(i)) op.integer_var(name='x2', lowerbound=0, upperbound=5) linear = {} linear['x0'] = 1 linear['x1'] = 2 linear['x2'] = 1 op.maximize(0, linear, {}) linear = {} for x in op.variables: linear[x.name] = 1 op.linear_constraint(linear, Constraint.Sense.EQ, 6, 'x0x1x2') conv = IntegerToBinary() _ = conv.encode(op) result = OptimizationResult(x=[0, 1, 1, 1, 1], fval=17) new_result = conv.decode(result) self.assertListEqual(new_result.x, [0, 1, 5]) self.assertEqual(new_result.fval, 17)
def test_continuous_variable_decode(self): """ Test decode func of IntegerToBinaryConverter for continuous variables""" try: mdl = Model('test_continuous_varable_decode') c = mdl.continuous_var(lb=0, ub=10.9, name='c') x = mdl.binary_var(name='x') mdl.maximize(c + x * x) op = QuadraticProgram() op.from_docplex(mdl) converter = IntegerToBinary() op = converter.encode(op) admm_params = ADMMParameters() qubo_optimizer = MinimumEigenOptimizer(NumPyMinimumEigensolver()) continuous_optimizer = CplexOptimizer() solver = ADMMOptimizer( qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer, params=admm_params, ) solution = solver.solve(op) solution = converter.decode(solution) self.assertEqual(solution.x[0], 10.9) except NameError as ex: self.skipTest(str(ex))