コード例 #1
0
 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))
コード例 #2
0
 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 = {'x0': 1, 'x1': 2, '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()
     op2 = conv.convert(op)
     result = OptimizationResult(x=[0, 1, 1, 1, 1],
                                 fval=17,
                                 variables=op2.variables)
     new_result = conv.interpret(result)
     np.testing.assert_array_almost_equal(new_result.x, [0, 1, 5])
     self.assertEqual(new_result.fval, 17)
     self.assertListEqual(new_result.variable_names, ['x0', 'x1', 'x2'])
     self.assertDictEqual(new_result.variables_dict, {
         'x0': 0,
         'x1': 1,
         'x2': 5
     })
コード例 #3
0
 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 = {'x0': 1, 'x1': 2, '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.convert(op)
     new_x = conv.interpret([0, 1, 1, 1, 1])
     np.testing.assert_array_almost_equal(new_x, [0, 1, 5])
コード例 #4
0
 def test_integer_to_binary(self):
     """ Test integer to binary """
     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 = {}
     for i, x in enumerate(op.variables):
         linear[x.name] = i + 1
     op.maximize(0, linear, {})
     conv = IntegerToBinary()
     op2 = conv.convert(op)
     for x in op2.variables:
         self.assertEqual(x.vartype, Variable.Type.BINARY)
     dct = op2.objective.linear.to_dict()
     self.assertEqual(dct[2], 3)
     self.assertEqual(dct[3], 6)
     self.assertEqual(dct[4], 6)
コード例 #5
0
 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.convert(op)
     result = OptimizationResult(x=[0, 1, 1, 1, 1], fval=17)
     new_result = conv.interpret(result)
     self.assertListEqual(new_result.x, [0, 1, 5])
     self.assertEqual(new_result.fval, 17)