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.convert(op) admm_params = ADMMParameters() qubo_optimizer = MinimumEigenOptimizer(NumPyMinimumEigensolver()) continuous_optimizer = CplexOptimizer() solver = ADMMOptimizer( qubo_optimizer=qubo_optimizer, continuous_optimizer=continuous_optimizer, params=admm_params, ) result = solver.solve(op) result = converter.interpret(result) self.assertEqual(result.x[0], 10.9) self.assertListEqual(result.variable_names, ['c', 'x']) self.assertDictEqual(result.variables_dict, {'c': 10.9, 'x': 0}) except NameError as ex: self.skipTest(str(ex))
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))